Skip to content

Commit 8637292

Browse files
committed
fix: override arrays when merging user configuration
* add integration tests for getConfiguration * improve test for presence of user config * override arrays when merging user config by using mergeWith fixes #11
1 parent b465ca8 commit 8637292

File tree

9 files changed

+86
-7
lines changed

9 files changed

+86
-7
lines changed

.conventional-changelog-lintrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"type-enum": [ 2, "always", [ "Fix", "New", "Breaking", "Docs", "Build", "Upgrade", "Chore", "Update" ] ]
4+
}
5+
}

fixtures/empty-file/.conventional-changelog-lintrc

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": []
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["____foooooo"]
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"type-enum": [ 2, "always", [ "a", "b", "c", "d" ]]
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": ["angular"],
3+
"rules": {
4+
"type-enum": [ 2, "always", [ "a", "b", "c", "d" ]]
5+
}
6+
}

source/library/get-configuration.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {merge, pick} from 'lodash';
1+
import {omit, merge, mergeWith, pick} from 'lodash';
22
import rc from 'rc';
33

44
import resolveExtends from './resolve-extends';
@@ -16,21 +16,23 @@ const defaultSettings = {
1616

1717
export default async (name = defaultName, settings = defaultSettings, seed = {}) => {
1818
// Obtain config from .rc files
19-
const userConfig = rc(name, settings.defaults);
19+
const userConfig = omit(rc(name, settings.defaults), '_');
2020

21-
// Use the default extends config if there is no userConfig
21+
// Use the default extends config if there is no userConfig file found
2222
// See https://git.io/vwT1C for reference
23-
const applicableDefaults = Object.keys(userConfig) > 0 ?
24-
{} :
25-
defaults;
23+
const applicableDefaults = userConfig.config ? {} : defaults;
2624

2725
// Merge passed config with file based options
2826
const config = merge(userConfig, seed);
2927
const opts = merge({}, applicableDefaults, pick(config, 'extends'));
3028

3129
// Resolve extends key
3230
const extended = resolveExtends(opts, settings.prefix);
33-
const preset = merge({}, extended, config);
31+
const preset = mergeWith({}, extended, config, (a, b) => {
32+
if (Array.isArray(b)) {
33+
return b;
34+
}
35+
});
3436

3537
// Execute rule config functions if needed
3638
const executed = await Promise.all(['rules', 'wildcards']

test/integration/get-configuration.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import path from 'path';
2+
import test from 'ava';
3+
import expect from 'unexpected';
4+
5+
import getConfiguration from '../../source/library/get-configuration';
6+
7+
const cwd = process.cwd();
8+
9+
test('overridden-type-enums should return the exact type-enum', async t => {
10+
const back = chdir('fixtures/overridden-type-enums');
11+
const actual = await getConfiguration();
12+
expect(actual.rules['type-enum'][2], 'to equal', [ "a", "b", "c", "d" ]);
13+
back();
14+
});
15+
16+
test('overridden-extended-type-enums should return the exact type-enum', async t => {
17+
const back = chdir('fixtures/overridden-extended-type-enums');
18+
const actual = await getConfiguration();
19+
expect(actual.rules['type-enum'][2], 'to equal', [ "a", "b", "c", "d" ]);
20+
back();
21+
});
22+
23+
test('extends-empty should have no rules', async t => {
24+
const back = chdir('fixtures/extends-empty');
25+
const actual = await getConfiguration();
26+
expect(actual.rules, 'to equal', {});
27+
back();
28+
});
29+
30+
test('invalid extend should throw', async t => {
31+
const back = chdir('fixtures/extends-invalid');
32+
t.throws(getConfiguration(), Error);
33+
back();
34+
});
35+
36+
test('empty file should have no rules', async t => {
37+
const back = chdir('fixtures/empty-object-file');
38+
const actual = await getConfiguration();
39+
expect(actual.rules, 'to equal', {});
40+
back();
41+
});
42+
43+
test('empty file should extend angular', async t => {
44+
const back = chdir('fixtures/empty-file');
45+
const actual = await getConfiguration();
46+
expect(actual.extends, 'to equal', ['angular']);
47+
back();
48+
});
49+
50+
function chdir(target) {
51+
const to = path.resolve(cwd, target.split('/').join(path.sep));
52+
process.chdir(to);
53+
return () => process.chdir(cwd);
54+
}

0 commit comments

Comments
 (0)