Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
test: use npm sandbox in test-npm-install
npm should run in a sandbox to avoid unwanted interactions. Without
this change, npm would read the userconfig file $HOME/.npmrc which may
contain configs that break this test.

Fixes: #9074
  • Loading branch information
joaocgreis committed Oct 13, 2016
commit 06498b0b3c20b1bbe7fe423d94a930cff48af425
13 changes: 10 additions & 3 deletions test/parallel/test-npm-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const assert = require('assert');
const fs = require('fs');

common.refreshTmpDir();
const npmSandbox = path.join(common.tmpDir, 'npm-sandbox');
fs.mkdirSync(npmSandbox);
const installDir = path.join(common.tmpDir, 'install-dir');
fs.mkdirSync(installDir);

const npmPath = path.join(
common.testDir,
Expand All @@ -32,23 +36,26 @@ const pkgContent = JSON.stringify({
}
});

const pkgPath = path.join(common.tmpDir, 'package.json');
const pkgPath = path.join(installDir, 'package.json');

fs.writeFileSync(pkgPath, pkgContent);

const env = Object.create(process.env);
env['PATH'] = path.dirname(process.execPath);
env['NPM_CONFIG_PREFIX'] = path.join(npmSandbox, 'npm-prefix');
env['NPM_CONFIG_TMP'] = path.join(npmSandbox, 'npm-tmp');
env['HOME'] = path.join(npmSandbox, 'home');

const proc = spawn(process.execPath, args, {
cwd: common.tmpDir,
cwd: installDir,
env: env
});

function handleExit(code, signalCode) {
assert.equal(code, 0, 'npm install should run without an error');
assert.ok(signalCode === null, 'signalCode should be null');
assert.doesNotThrow(function() {
fs.accessSync(common.tmpDir + '/node_modules/package-name');
fs.accessSync(installDir + '/node_modules/package-name');
});
}

Expand Down