Skip to content

Commit 9d35593

Browse files
Warn when unsupported ava.config.json files are encountered
Co-authored-by: Mark Wubben <mark@novemberborn.net>
1 parent ada1a4f commit 9d35593

File tree

8 files changed

+76
-18
lines changed

8 files changed

+76
-18
lines changed

lib/cli.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,14 @@ export default async function loadCli() { // eslint-disable-line complexity
104104
let confError;
105105
try {
106106
const {argv: {config: configFile}} = yargs(hideBin(process.argv)).help(false).version(false);
107-
conf = await loadConfig({configFile});
107+
const loaded = await loadConfig({configFile});
108+
if (loaded.unsupportedFiles.length > 0) {
109+
console.log(chalk.magenta(
110+
` ${figures.warning} AVA does not support JSON config, ignoring:\n\n ${loaded.unsupportedFiles.join('\n ')}`,
111+
));
112+
}
113+
114+
conf = loaded.config;
108115
if (conf.configFile && path.basename(conf.configFile) !== path.relative(conf.projectDir, conf.configFile)) {
109116
console.log(chalk.magenta(` ${figures.warning} Using configuration from ${conf.configFile}`));
110117
}

lib/eslint-plugin-helper-worker.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const buildGlobs = ({conf, providers, projectDir, overrideExtensions, overrideFi
4141

4242
const resolveGlobs = async (projectDir, overrideExtensions, overrideFiles) => {
4343
if (!configCache.has(projectDir)) {
44-
configCache.set(projectDir, loadConfig({resolveFrom: projectDir}).then(async conf => {
44+
configCache.set(projectDir, loadConfig({resolveFrom: projectDir}).then(async ({config: conf}) => {
4545
const providers = await collectProviders({conf, projectDir});
4646
return {conf, providers};
4747
}));

lib/load-config.js

+27-6
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ const importConfig = async ({configFile, fileForErrorMessage}) => {
2020
};
2121

2222
const loadConfigFile = async ({projectDir, configFile}) => {
23-
if (!fs.existsSync(configFile)) {
24-
return null;
25-
}
26-
2723
const fileForErrorMessage = path.relative(projectDir, configFile);
2824
try {
25+
await fs.promises.access(configFile);
2926
return {config: await importConfig({configFile, fileForErrorMessage}), configFile, fileForErrorMessage};
3027
} catch (error) {
28+
if (error.code === 'ENOENT') {
29+
return null;
30+
}
31+
3132
throw Object.assign(new Error(`Error loading ${fileForErrorMessage}: ${error.message}`), {parent: error});
3233
}
3334
};
@@ -63,6 +64,20 @@ async function findRepoRoot(fromDir) {
6364
return root;
6465
}
6566

67+
async function checkJsonFile(searchDir) {
68+
const file = path.join(searchDir, 'ava.config.json');
69+
try {
70+
await fs.promises.access(file);
71+
return file;
72+
} catch (error) {
73+
if (error.code === 'ENOENT') {
74+
return null;
75+
}
76+
77+
throw error;
78+
}
79+
}
80+
6681
export async function loadConfig({configFile, resolveFrom = process.cwd(), defaults = {}} = {}) {
6782
let packageConf = await packageConfig('ava', {cwd: resolveFrom});
6883
const filepath = packageJsonPath(packageConf);
@@ -74,6 +89,7 @@ export async function loadConfig({configFile, resolveFrom = process.cwd(), defau
7489
const allowConflictWithPackageJson = Boolean(configFile);
7590
configFile = resolveConfigFile(configFile);
7691

92+
const unsupportedFiles = [];
7793
let fileConf = NO_SUCH_FILE;
7894
let fileForErrorMessage;
7995
let conflicting = [];
@@ -86,12 +102,17 @@ export async function loadConfig({configFile, resolveFrom = process.cwd(), defau
86102
let searchDir = projectDir;
87103
const stopAt = path.dirname(repoRoot);
88104
do {
89-
const results = await Promise.all([ // eslint-disable-line no-await-in-loop
105+
const [jsonFile, ...results] = await Promise.all([ // eslint-disable-line no-await-in-loop
106+
checkJsonFile(searchDir),
90107
loadConfigFile({projectDir, configFile: path.join(searchDir, 'ava.config.js')}),
91108
loadConfigFile({projectDir, configFile: path.join(searchDir, 'ava.config.cjs')}),
92109
loadConfigFile({projectDir, configFile: path.join(searchDir, 'ava.config.mjs')}),
93110
]);
94111

112+
if (jsonFile !== null) {
113+
unsupportedFiles.push(jsonFile);
114+
}
115+
95116
[{config: fileConf, fileForErrorMessage, configFile} = {config: NO_SUCH_FILE, fileForErrorMessage: undefined}, ...conflicting] = results.filter(result => result !== null);
96117

97118
searchDir = path.dirname(searchDir);
@@ -139,5 +160,5 @@ export async function loadConfig({configFile, resolveFrom = process.cwd(), defau
139160
}
140161
}
141162

142-
return config;
163+
return {config, unsupportedFiles};
143164
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
failFast: true,
3+
};
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+
"type": "module"
3+
}

test/config/loader.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ const FIXTURE_ROOT = fileURLToPath(new URL('../../test-tap/fixture/load-config',
1010

1111
const resolve = relpath => path.resolve(FIXTURE_ROOT, relpath);
1212

13-
const loadFromSetup = setup => {
13+
const loadFromSetup = async setup => {
1414
if (typeof setup === 'string') {
15-
return loadConfig();
15+
const loaded = await loadConfig();
16+
return loaded.config;
1617
}
1718

1819
const {configFile, defaults, resolveFrom} = setup;
19-
return loadConfig({configFile, defaults, resolveFrom});
20+
const loaded = await loadConfig({configFile, defaults, resolveFrom});
21+
return loaded.config;
2022
};
2123

2224
const ok = setup => async (t, assert = tt => tt.pass()) => {

test/config/next-gen.js

+28-7
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,31 @@ const FIXTURE_ROOT = fileURLToPath(new URL('fixtures', import.meta.url));
1010

1111
const resolve = relpath => path.resolve(FIXTURE_ROOT, relpath);
1212

13-
const loadFromSetup = setup => {
13+
const loadFromSetup = async (setup, t, assertUnsupportedFiles = (tt, files) => tt.is(files.length, 0)) => {
1414
if (typeof setup === 'string') {
15-
return loadConfig();
15+
const loaded = await loadConfig();
16+
return loaded.config;
1617
}
1718

18-
const {configFile, defaults, resolveFrom} = setup;
19-
return loadConfig({configFile, defaults, resolveFrom});
19+
const {
20+
configFile,
21+
defaults,
22+
resolveFrom,
23+
} = setup;
24+
25+
const loaded = await loadConfig({configFile, defaults, resolveFrom});
26+
assertUnsupportedFiles(t, loaded.unsupportedFiles);
27+
return loaded.config;
2028
};
2129

22-
const ok = setup => async (t, assert = tt => tt.pass()) => {
30+
const ok = setup => async (t, assert = tt => tt.pass(), assertUnsupportedFiles = undefined) => {
2331
const fixture = typeof setup === 'string' ? setup : setup.fixture;
2432

2533
const stub = sinon.stub(process, 'cwd');
2634
t.teardown(() => stub.restore());
2735
stub.returns(resolve(fixture));
2836

29-
const conf = loadFromSetup(setup);
37+
const conf = loadFromSetup(setup, t, assertUnsupportedFiles);
3038
await t.notThrowsAsync(conf);
3139
const result = await t.try(assert, await conf, setup);
3240
result.commit();
@@ -39,7 +47,7 @@ const notOk = setup => async (t, assert = (tt, error) => tt.snapshot(error.messa
3947
t.teardown(() => stub.restore());
4048
stub.returns(resolve(fixture));
4149

42-
const conf = loadFromSetup(setup);
50+
const conf = loadFromSetup(setup, t);
4351
const error = await t.throwsAsync(conf);
4452
const result = await t.try(assert, error, setup);
4553
result.commit();
@@ -67,6 +75,19 @@ test.serial('loads .js config as ESM', ok('js-as-esm'), (t, conf) => {
6775
t.true(conf.failFast);
6876
});
6977

78+
test.serial('finds unsupported configs',
79+
ok({
80+
fixture: 'unsupported-configs',
81+
}),
82+
(t, conf) => {
83+
t.true(conf.failFast);
84+
},
85+
(t, unsupportedFiles) => {
86+
t.is(unsupportedFiles.length, 1);
87+
t.regex(unsupportedFiles[0], /ava\.config\.json/);
88+
},
89+
);
90+
7091
test.serial('handles errors when loading .js config as ESM', notOk({
7192
fixture: 'js-as-esm',
7293
configFile: 'error.js',

0 commit comments

Comments
 (0)