-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check for npmrc in home directory for linux root users #4047
Changes from 3 commits
134c88f
7ab2495
8c93001
8dc1ca6
d5ff169
e01a46f
c51a71d
754b215
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,12 +50,17 @@ function createMocks(): Object { | |
getScopedOption: jest.fn(), | ||
}, | ||
}; | ||
const mockReporter = jest.fn(); | ||
const mockLog = []; | ||
const mockReporter = { | ||
lang: (...args) => mockLog.push(...args), | ||
verbose: jest.fn(), | ||
}; | ||
|
||
return { | ||
mockRequestManager, | ||
mockRegistries, | ||
mockReporter, | ||
mockLog, | ||
}; | ||
} | ||
|
||
|
@@ -292,3 +297,16 @@ describe('getScope functional test', () => { | |
}); | ||
}); | ||
}); | ||
|
||
describe('getPossibleConfigLocations', () => { | ||
describe('searches recursively to home directory', async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a |
||
const testCwd = './project/subdirectory'; | ||
const {mockRequestManager, mockRegistries, mockReporter, mockLog} = createMocks(); | ||
const npmRegistry = new NpmRegistry(testCwd, mockRegistries, mockRequestManager, mockReporter); | ||
await npmRegistry.getPossibleConfigLocations('npmrc', mockReporter); | ||
|
||
expect(mockLog.indexOf('project/subdirectory/.npmrc')).toBeGreaterThan(-1); | ||
expect(mockLog.indexOf('project/.npmrc')).toBeGreaterThan(-1); | ||
expect(mockLog.indexOf(`${homeDir}/.npmrc`)).toBeGreaterThan(-1); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,12 +12,11 @@ import envReplace from '../util/env-replace.js'; | |
import Registry from './base-registry.js'; | ||
import {addSuffix} from '../util/misc'; | ||
import {getPosixPath, resolveWithHome} from '../util/path'; | ||
|
||
const normalizeUrl = require('normalize-url'); | ||
const userHome = require('../util/user-home-dir').default; | ||
const path = require('path'); | ||
const url = require('url'); | ||
const ini = require('ini'); | ||
import normalizeUrl from 'normalize-url'; | ||
import {default as userHome, home} from '../util/user-home-dir'; | ||
import path from 'path'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! I think the convention is to list stdlib imports at the top, right? |
||
import url from 'url'; | ||
import ini from 'ini'; | ||
|
||
const DEFAULT_REGISTRY = 'https://registry.npmjs.org/'; | ||
const REGEX_REGISTRY_PREFIX = /^https?:/; | ||
|
@@ -178,6 +177,12 @@ export default class NpmRegistry extends Registry { | |
[false, path.join(getGlobalPrefix(), 'etc', filename)], | ||
]; | ||
|
||
// When home directory for global install is different from where $HOME/npmrc is stored, | ||
// E.g. /usr/local/share vs /root on linux machines, check the additional location | ||
if (home !== userHome) { | ||
possibles.push([true, path.join(home, localfile)]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should change this data structure to be a normal object instead of an array sometime in the future: |
||
} | ||
|
||
// npmrc --> ../.npmrc, ../../.npmrc, etc. | ||
const foldersFromRootToCwd = getPosixPath(this.cwd).split('/'); | ||
while (foldersFromRootToCwd.length > 1) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can also use the
BufferReporter
for tests?