Skip to content

Commit

Permalink
fix: handle npm prefix config environment variables (#7070)
Browse files Browse the repository at this point in the history
* fix: handle npm prefix config environment variables
Fixes #4682

* Updated changelog

* Implemented PR feedback

* Fixed logic error
  • Loading branch information
NicholasBoll authored and arcanis committed Mar 5, 2019
1 parent 82d5891 commit b66549d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,10 @@ Please add one entry in this file for each change in Yarn's behavior. Use the sa

## Master

- Fixes `npm_config_` environment variable parsing to support those prefixed with underscore (ex: `_auth`)

[#7070](https://github.com/yarnpkg/yarn/pull/7070) - [**Nicholas Boll**](https://github.com/NicholasBoll)

- Fixes yarn `upgrade --latest` for dependencies using `>` or `>=` range specifier

[#7080](https://github.com/yarnpkg/yarn/pull/7080) - [**Xukai Wu**](https://github.com/shilcare)
Expand Down
31 changes: 31 additions & 0 deletions __tests__/registries/npm-registry.js
Expand Up @@ -731,6 +731,37 @@ describe('isScopedPackage functional test', () => {
});
});

describe('environment variables functional test', () => {
beforeEach(() => {
process.env.npm_config_always_auth = 'true';
process.env.npm_config__auth = 'auth';
process.env.npm_config__authtoken = 'authToken';
process.env.npm_config__username = 'username';
process.env.npm_config__password = 'password';
});

afterEach(() => {
delete process.env.npm_config_always_auth;
delete process.env.npm_config__auth;
delete process.env.npm_config__authToken;
delete process.env.npm_config__username;
delete process.env.npm_config__password;
});

test('correctly escapes environment config variables', () => {
const testCwd = '.';
const {mockRequestManager, mockRegistries, mockReporter} = createMocks();
const npmRegistry = new NpmRegistry(testCwd, mockRegistries, mockRequestManager, mockReporter, true, []);

npmRegistry.mergeEnv('npm_config_');
expect(npmRegistry.config).toHaveProperty('always-auth', true);
expect(npmRegistry.config).toHaveProperty('_auth', 'auth');
expect(npmRegistry.config).toHaveProperty('_authtoken', 'authToken');
expect(npmRegistry.config).toHaveProperty('_username', 'username');
expect(npmRegistry.config).toHaveProperty('_password', 'password');
});
});

describe('getRequestUrl functional test', () => {
test('returns pathname when it is a full URL', () => {
const testCwd = '.';
Expand Down
4 changes: 2 additions & 2 deletions src/registries/base-registry.js
Expand Up @@ -170,8 +170,8 @@ export default class BaseRegistry {
// replace dunders with dots
key = key.replace(/__/g, '.');

// replace underscores with dashes
key = key.replace(/_/g, '-');
// replace underscores with dashes ignoring keys that start with an underscore
key = key.replace(/([^_])_/g, '$1-');

// set it via a path
objectPath.set(this.config, key, val);
Expand Down

0 comments on commit b66549d

Please sign in to comment.