Skip to content

Commit def6874

Browse files
joaocgreisMyles Borins
authored and
Myles Borins
committed
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 PR-URL: #9079 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Johan Bergström <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]> Reviewed-By: Myles Borins <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent b2a2a57 commit def6874

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

test/parallel/test-npm-install.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ const assert = require('assert');
77
const fs = require('fs');
88

99
common.refreshTmpDir();
10+
const npmSandbox = path.join(common.tmpDir, 'npm-sandbox');
11+
fs.mkdirSync(npmSandbox);
12+
const installDir = path.join(common.tmpDir, 'install-dir');
13+
fs.mkdirSync(installDir);
1014

1115
const npmPath = path.join(
1216
common.testDir,
@@ -28,23 +32,26 @@ const pkgContent = JSON.stringify({
2832
}
2933
});
3034

31-
const pkgPath = path.join(common.tmpDir, 'package.json');
35+
const pkgPath = path.join(installDir, 'package.json');
3236

3337
fs.writeFileSync(pkgPath, pkgContent);
3438

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

3845
const proc = spawn(process.execPath, args, {
39-
cwd: common.tmpDir,
46+
cwd: installDir,
4047
env: env
4148
});
4249

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

0 commit comments

Comments
 (0)