From ce3841f5538c33dd52bd907bb1d3125bbfc561e6 Mon Sep 17 00:00:00 2001 From: Pierre Vanduynslager Date: Sat, 25 Nov 2017 14:04:36 -0500 Subject: [PATCH] test: Ignore the ~/.npmrc file during tests When the test are run npm read the ~/.npmrc and ./.npmrc files and set some environment variables starting with `npm_`. When the test create temporary folders and created a `.npmrc` the values there are ignored as the environment variables take precedence. This commit remove this environment variable (from the local `process.env`) before starting the integrations test and restore them after. This way the `./.npmrc` files created in temp directory for the test are correctly used. --- .travis.yml | 1 + test/integration.test.js | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index e08cb38b..3d14d274 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,6 +21,7 @@ install: - travis_retry npm install script: + - git config --get user.email - npm run test after_success: diff --git a/test/integration.test.js b/test/integration.test.js index 8e1c5370..89dc76ba 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -34,7 +34,24 @@ test.beforeEach(t => { t.context.env = Object.assign({}, process.env); // Save the current working diretory t.context.cwd = process.cwd(); - + // Delete env paramaters that could have been set on the machine running the tests + delete process.env.NPM_TOKEN; + delete process.env.NPM_USERNAME; + delete process.env.NPM_PASSWORD; + delete process.env.NPM_EMAIL; + delete process.env.GH_TOKEN; + delete process.env.GITHUB_TOKEN; + delete process.env.GH_URL; + delete process.env.GITHUB_URL; + delete process.env.GH_PREFIX; + delete process.env.GITHUB_PREFIX; + // Delete all `npm_config` environment variable set by CI as they take precedence over the `.npmrc` because the process that runs the tests is started before the `.npmrc` is created + for (let i = 0, keys = Object.keys(process.env); i < keys.length; i++) { + if (keys[i].startsWith('npm_config')) { + delete process.env[keys[i]]; + } + } + // Disable logs during tests t.context.log = stub(console, 'log'); t.context.error = stub(console, 'error'); t.context.stdout = stub(process.stdout, 'write'); @@ -46,7 +63,7 @@ test.afterEach.always(t => { process.env = Object.assign({}, t.context.env); // Restore the current working directory process.chdir(t.context.cwd); - + // Restore the logs t.context.log.restore(); t.context.error.restore(); t.context.stdout.restore();