Skip to content

Commit 031ec8b

Browse files
committed
Improve test coverage
1 parent 597a98c commit 031ec8b

File tree

13 files changed

+69
-35
lines changed

13 files changed

+69
-35
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const runEslint = (paths, options) => {
4848
};
4949

5050
const lintText = (string, options) => {
51-
const {options: foundOptions, prettierOptions} = mergeWithFileConfig(options);
51+
const {options: foundOptions, prettierOptions} = mergeWithFileConfig(normalizeOptions(options));
5252
options = buildConfig(foundOptions, prettierOptions);
5353

5454
if (options.ignores && !isEqual(getIgnores({}), options.ignores) && typeof options.filename !== 'string') {

lib/options-manager.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const mergeFn = (previousValue, value) => {
5151
* Find config for `lintText`.
5252
* The config files are searched starting from `options.filename` if defined or `options.cwd` otherwise.
5353
*/
54-
const mergeWithFileConfig = (options = {}) => {
54+
const mergeWithFileConfig = options => {
5555
options.cwd = path.resolve(options.cwd || process.cwd());
5656
const configExplorer = cosmiconfigSync(MODULE_NAME, {searchPlaces: CONFIG_FILES, loaders: {noExt: defaultLoaders['.json']}, stopDir: options.cwd});
5757
const pkgConfigExplorer = cosmiconfigSync('engines', {searchPlaces: ['package.json'], stopDir: options.cwd});
@@ -80,7 +80,7 @@ const mergeWithFileConfig = (options = {}) => {
8080
* Find config for each files found by `lintFiles`.
8181
* The config files are searched starting from each files.
8282
*/
83-
const mergeWithFileConfigs = async (files, options = {}) => {
83+
const mergeWithFileConfigs = async (files, options) => {
8484
options.cwd = path.resolve(options.cwd || process.cwd());
8585
return [...(await pReduce(files, async (configs, file) => {
8686
const configExplorer = cosmiconfig(MODULE_NAME, {searchPlaces: CONFIG_FILES, loaders: {noExt: defaultLoaders['.json']}, stopDir: options.cwd});
@@ -100,8 +100,8 @@ const mergeWithFileConfigs = async (files, options = {}) => {
100100

101101
const {hash, options: optionsWithOverrides} = applyOverrides(filepath, fileOptions);
102102

103-
const prettierConfigPath = optionsWithOverrides.prettier ? await prettier.resolveConfigFile(filepath) || undefined : undefined;
104-
const prettierOptions = prettierConfigPath ? await prettier.resolveConfig(filepath, {config: prettierConfigPath}) || {} : {};
103+
const prettierConfigPath = optionsWithOverrides.prettier ? await prettier.resolveConfigFile(filepath) : undefined;
104+
const prettierOptions = prettierConfigPath ? await prettier.resolveConfig(filepath, {config: prettierConfigPath}) : {};
105105

106106
const cacheKey = JSON.stringify({xoConfigPath, enginesConfigPath, prettierConfigPath, hash});
107107
const cachedGroup = configs.get(cacheKey);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"xo": {
3+
"overrides": [
4+
{
5+
"files": "semicolon.js",
6+
"prettier": true
7+
}
8+
]
9+
}
10+
}

test/fixtures/nested-configs/child-override/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"xo": {
33
"overrides": [
44
{
5-
"files": "semicolon.js",
6-
"prettier": true
5+
"files": "two-spaces.js",
6+
"space": 4
77
}
88
]
99
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
console.log([
2+
2
3+
]);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('no-semicolon')
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
{
22
"xo": {
3-
"semicolon": true,
4-
"prettier": true
5-
},
6-
"prettier": {
7-
"singleQuote": false
3+
"semicolon": true
84
}
95
}

test/fixtures/nested-configs/single-quote.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/lint-files.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ test('do not lint eslintignored files', async t => {
132132
t.is(results.some(r => r.filePath === negative), false);
133133
});
134134

135-
test.only('find configurations close to linted file', async t => {
135+
test('find configurations close to linted file', async t => {
136136
const {results} = await fn.lintFiles('**/*', {cwd: 'fixtures/nested-configs'});
137137

138138
t.true(
@@ -146,16 +146,24 @@ test.only('find configurations close to linted file', async t => {
146146
t.true(
147147
hasRule(
148148
results,
149-
path.resolve('fixtures/nested-configs/child-override/semicolon.js'),
149+
path.resolve('fixtures/nested-configs/child-override/child-prettier-override/semicolon.js'),
150150
'prettier/prettier'
151151
)
152152
);
153153

154154
t.true(
155155
hasRule(
156156
results,
157-
path.resolve('fixtures/nested-configs/single-quote.js'),
158-
'prettier/prettier'
157+
path.resolve('fixtures/nested-configs/no-semicolon.js'),
158+
'semi'
159+
)
160+
);
161+
162+
t.true(
163+
hasRule(
164+
results,
165+
path.resolve('fixtures/nested-configs/child-override/two-spaces.js'),
166+
'indent'
159167
)
160168
);
161169
});

test/lint-text.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,17 @@ test('enable rules based on nodeVersion in override', async t => {
253253
});
254254

255255
test('find configurations close to linted file', t => {
256-
const {results: childResults} = fn.lintText('console.log(\'semicolon\');\n', {filename: 'fixtures/nested-configs/child/file.js'});
257-
t.true(hasRule(childResults, 'semi'));
256+
let {results} = fn.lintText('console.log(\'semicolon\');\n', {filename: 'fixtures/nested-configs/child/semicolon.js'});
257+
t.true(hasRule(results, 'semi'));
258258

259-
const {results} = fn.lintText('console.log(\'single-quote\');\n', {filename: 'fixtures/nested-configs/file.js'});
259+
({results} = fn.lintText('console.log(\'semicolon\');\n', {filename: 'fixtures/nested-configs/child-override/child-prettier-override/semicolon.js'}));
260260
t.true(hasRule(results, 'prettier/prettier'));
261-
});
262261

263-
test('find configurations close to linted file and use overrides', t => {
264-
const {results} = fn.lintText('console.log(\'semicolon\');\n', {filename: 'fixtures/nested-configs/child-override/semicolon.js'});
265-
t.true(hasRule(results, 'prettier/prettier'));
262+
({results} = fn.lintText('console.log(\'no-semicolon\')\n', {filename: 'fixtures/nested-configs/no-semicolon.js'}));
263+
t.true(hasRule(results, 'semi'));
264+
265+
({results} = fn.lintText(`console.log([
266+
2
267+
]);\n`, {filename: 'fixtures/nested-configs/child-override/two-spaces.js'}));
268+
t.true(hasRule(results, 'indent'));
266269
});

test/options-manager.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -454,27 +454,24 @@ test(mergeWithFileConfigFileType, {type: '.xo-config', dir: 'xo-config'});
454454
test('mergeWithFileConfigs: nested configs with prettier', async t => {
455455
const cwd = path.resolve('fixtures', 'nested-configs');
456456
const paths = [
457-
'single-quote.js',
458-
'package.json',
459-
'child/package.json',
457+
'no-semicolon.js',
460458
'child/semicolon.js',
461-
'child-override/package.json',
462-
'child-override/semicolon.js'
459+
'child-override/two-spaces.js',
460+
'child-override/child-prettier-override/semicolon.js'
463461
];
464462
const result = await manager.mergeWithFileConfigs(paths, {cwd});
465463

466464
t.deepEqual(result, [
467465
{
468-
files: [path.resolve(cwd, 'single-quote.js')],
466+
files: [path.resolve(cwd, 'no-semicolon.js')],
469467
options: {
470468
semicolon: true,
471-
prettier: true,
472469
nodeVersion: undefined,
473470
cwd,
474471
extensions: DEFAULT_EXTENSION,
475472
ignores: DEFAULT_IGNORES
476473
},
477-
prettierOptions: {singleQuote: false}
474+
prettierOptions: {}
478475
},
479476
{
480477
files: [path.resolve(cwd, 'child/semicolon.js')],
@@ -488,8 +485,9 @@ test('mergeWithFileConfigs: nested configs with prettier', async t => {
488485
prettierOptions: {}
489486
},
490487
{
491-
files: [path.resolve(cwd, 'child-override/semicolon.js')],
488+
files: [path.resolve(cwd, 'child-override/two-spaces.js')],
492489
options: {
490+
space: 4,
493491
rules: {},
494492
settings: {},
495493
globals: [],
@@ -499,10 +497,26 @@ test('mergeWithFileConfigs: nested configs with prettier', async t => {
499497
nodeVersion: undefined,
500498
cwd: path.resolve(cwd, 'child-override'),
501499
extensions: DEFAULT_EXTENSION,
500+
ignores: DEFAULT_IGNORES
501+
},
502+
prettierOptions: {}
503+
},
504+
{
505+
files: [path.resolve(cwd, 'child-override/child-prettier-override/semicolon.js')],
506+
options: {
507+
prettier: true,
508+
rules: {},
509+
settings: {},
510+
globals: [],
511+
envs: [],
512+
plugins: [],
513+
extends: [],
514+
nodeVersion: undefined,
515+
cwd: path.resolve(cwd, 'child-override', 'child-prettier-override'),
516+
extensions: DEFAULT_EXTENSION,
502517
ignores: DEFAULT_IGNORES,
503-
prettier: true
504518
},
505-
prettierOptions: {singleQuote: false}
519+
prettierOptions: {semi: false}
506520
}
507521
]);
508522
});

0 commit comments

Comments
 (0)