From 1847670c88da5a10cdae6380b21af696b329471d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Reis?= Date: Thu, 13 Oct 2016 13:36:26 +0100 Subject: [PATCH] test: use npm sandbox in test-npm-install MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/issues/9074 PR-URL: https://github.com/nodejs/node/pull/9079 Reviewed-By: Jeremiah Senkpiel Reviewed-By: Johan Bergström Reviewed-By: Santiago Gimeno Reviewed-By: Myles Borins Reviewed-By: James M Snell Reviewed-By: Rich Trott Reviewed-By: Colin Ihrig --- test/parallel/test-npm-install.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-npm-install.js b/test/parallel/test-npm-install.js index 6fc5151d38..60c590f40c 100644 --- a/test/parallel/test-npm-install.js +++ b/test/parallel/test-npm-install.js @@ -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, @@ -32,15 +36,18 @@ 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 }); @@ -48,7 +55,7 @@ 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'); }); }