diff --git a/__tests__/registries/npm-registry.js b/__tests__/registries/npm-registry.js index 54ade142a0..da6e0a395f 100644 --- a/__tests__/registries/npm-registry.js +++ b/__tests__/registries/npm-registry.js @@ -1,6 +1,6 @@ /* @flow */ -import {resolve, join as pathJoin} from 'path'; +import {resolve, join as pathJoin, sep as pathSep} from 'path'; import NpmRegistry from '../../src/registries/npm-registry.js'; import {BufferReporter} from '../../src/reporters/index.js'; @@ -836,6 +836,30 @@ describe('getPossibleConfigLocations', () => { ]), ); }); + + test('aware of NPM_CONFIG_GLOBALCONFIG directory when present', async () => { + const customrc = pathSep + pathJoin('tmp', 'customrc'); + try { + process.env.NPM_CONFIG_GLOBALCONFIG = customrc; + const testCwd = './project/subdirectory'; + const {mockRequestManager, mockRegistries} = createMocks(); + const reporter = new BufferReporter({verbose: true}); + const npmRegistry = new NpmRegistry(testCwd, mockRegistries, mockRequestManager, reporter, true, []); + await npmRegistry.getPossibleConfigLocations('npmrc', reporter); + + const logs = reporter.getBuffer().map(logItem => logItem.data); + expect(logs).toEqual( + expect.arrayContaining([ + expect.stringContaining(JSON.stringify(customrc)), + expect.stringContaining(JSON.stringify(pathJoin('project', 'subdirectory', '.npmrc'))), + expect.stringContaining(JSON.stringify(pathJoin('project', '.npmrc'))), + expect.stringContaining(JSON.stringify(pathJoin(homeDir, '.npmrc'))), + ]), + ); + } finally { + delete process.env.NPM_GLOBAL_GLOBALCONFIG; + } + }); }); describe('checkOutdated functional test', () => { diff --git a/src/registries/npm-registry.js b/src/registries/npm-registry.js index 083b8c1bab..8e253e5649 100644 --- a/src/registries/npm-registry.js +++ b/src/registries/npm-registry.js @@ -253,6 +253,10 @@ export default class NpmRegistry extends Registry { async getPossibleConfigLocations(filename: string, reporter: Reporter): Promise> { let possibles = []; + if (process.env.NPM_CONFIG_GLOBALCONFIG) { + possibles.push([false, process.env.NPM_CONFIG_GLOBALCONFIG]); + } + for (const rcFile of this.extraneousRcFiles.slice().reverse()) { possibles.push([false, path.resolve(process.cwd(), rcFile)]); }