Skip to content
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

fix: handle npm prefix config environment variables #7070

Merged
merged 6 commits into from Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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

- Fix `npm_config_` environment variable parsing to support `_auth`, `_authToken`, `_username` and `_password` config options

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

- Adds support for `yarn policies set-version berry`

[#7041](https://github.com/yarnpkg/yarn/pull/7041/files) - [**Maël Nison**](https://twitter.com/arcanis)
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, []);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had trouble figuring out where these tests should go. mergeEnv is an instance method that requires a whole mocked registry to test.

Simplifying this test would involve refactoring the mergeEnv method to use a utility method, but I decided to reduce the changes to the code and instead have a more complicated test


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: 3 additions & 1 deletion src/registries/base-registry.js
Expand Up @@ -171,7 +171,9 @@ export default class BaseRegistry {
key = key.replace(/__/g, '.');

// replace underscores with dashes
key = key.replace(/_/g, '-');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you instead add it in a more generic way that supports __?

For example: key.replace(/([^_])_/g, '$1-')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea. We're probably assuming any key starting with an underscore needs to stay an underscore, but that's probably always true

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arcanis Are the changes satisfactory?

if (!['_auth', '_authtoken', '_username', '_password'].includes(key)) {
key = key.replace(/_/g, '-');
}

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