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(config): allow .npmrc config to supersede yarn defaults #5825

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions __tests__/registries/yarn-registry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* @flow */

import YarnRegistry from '../../src/registries/yarn-registry.js';
import {BufferReporter} from '../../src/reporters/index.js';
import {YARN_REGISTRY} from '../../src/constants.js';

function createMocks(): Object {
const mockRequestManager = {
request: jest.fn(),
};

const mockRegistries = {
npm: {
getOption(key): mixed {
return this.config[key];
},
},
yarn: {},
};

const reporter = new BufferReporter({verbose: true});

return {
mockRequestManager,
mockRegistries,
reporter,
};
}

describe('yarn registry getOption', () => {
test('that getOption falls back to npm if the key is defined as a npm config and also is a yarn default', () => {
const {mockRequestManager, mockRegistries, reporter} = createMocks();
const yarnRegistry = new YarnRegistry('.', mockRegistries, mockRequestManager, reporter);

mockRegistries.npm.config = {
registry: 'npm:custom-registry',
};

yarnRegistry.config = {
registry: YARN_REGISTRY,
};

yarnRegistry.configWithoutDefaults = {};

expect(yarnRegistry.getOption('registry')).toEqual('npm:custom-registry');
});

test('that getOption will respect custom yarn options over npm', () => {
const {mockRequestManager, mockRegistries, reporter} = createMocks();
const yarnRegistry = new YarnRegistry('.', mockRegistries, mockRequestManager, reporter);

mockRegistries.npm.config = {
registry: 'npm:custom-registry',
};

yarnRegistry.config = {
registry: 'yarn:custom-registry',
};

yarnRegistry.configWithoutDefaults = {
registry: 'yarn:custom-registry',
};

expect(yarnRegistry.getOption('registry')).toEqual('yarn:custom-registry');
});

test('that if a option is not defined on either yarn or npm we use yarns default', () => {
const {mockRequestManager, mockRegistries, reporter} = createMocks();
const yarnRegistry = new YarnRegistry('.', mockRegistries, mockRequestManager, reporter);

mockRegistries.npm.config = {};

yarnRegistry.config = {
registry: YARN_REGISTRY,
};

yarnRegistry.configWithoutDefaults = {};

expect(yarnRegistry.getOption('registry')).toEqual(YARN_REGISTRY);
});

test('that if the yarn config value is the same as the default it will be used', () => {
const {mockRequestManager, mockRegistries, reporter} = createMocks();
const yarnRegistry = new YarnRegistry('.', mockRegistries, mockRequestManager, reporter);

mockRegistries.npm.config = {
registry: 'npm:registry',
};

yarnRegistry.config = {
registry: YARN_REGISTRY,
};

yarnRegistry.configWithoutDefaults = {
registry: YARN_REGISTRY,
};

expect(yarnRegistry.getOption('registry')).toEqual(YARN_REGISTRY);
});
});
6 changes: 4 additions & 2 deletions src/registries/yarn-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ export default class YarnRegistry extends NpmRegistry {

homeConfigLoc: string;
homeConfig: Object;
configWithoutDefaults: Object;

getOption(key: string): mixed {
let val = this.config[key];
let val = this.configWithoutDefaults[key];

// if this isn't set in a yarn config, then use npm
if (typeof val === 'undefined') {
Expand All @@ -66,7 +67,7 @@ export default class YarnRegistry extends NpmRegistry {

// if this isn't set in a yarn config or npm config, then use the default (or undefined)
if (typeof val === 'undefined') {
val = DEFAULTS[key];
val = this.config[key];
}

return val;
Expand Down Expand Up @@ -102,6 +103,7 @@ export default class YarnRegistry extends NpmRegistry {
}

// default yarn config
this.configWithoutDefaults = Object.assign({}, this.config);
this.config = Object.assign({}, DEFAULTS, this.config);
}

Expand Down