Skip to content

Commit

Permalink
Throw on bad config file (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
janosh authored Oct 27, 2021
1 parent 5ce0392 commit 40a0ee6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
11 changes: 6 additions & 5 deletions markdownlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ function readConfiguration(userConfigFile) {
markdownlint.readConfigSync(userConfigFile, configFileParsers);
config = require('deep-extend')(config, userConfig);
} catch (error) {
console.warn('Cannot read or parse config file ' + userConfigFile + ': ' + error.message);
console.error(`Cannot read or parse config file '${userConfigFile}': ${error.message}`);
process.exitCode = 1;
}
}

Expand Down Expand Up @@ -131,14 +132,14 @@ function prepareFileList(files, fileExtensions, previousResults) {
}

function printResult(lintResult) {

const results = flatten(Object.keys(lintResult).map(file => {
return lintResult[file].map(result => {
if (options.json) {
return {
fileName: file,
...result
}
...result
}
} else {
return {
file: file,
Expand All @@ -152,7 +153,7 @@ function printResult(lintResult) {
}
});
}));

let lintResultString = '';
if (results.length > 0) {
if (options.json) {
Expand Down
2 changes: 2 additions & 0 deletions test/malformed-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- foo
- bar
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,30 @@ test('configuration file can be JavaScript', async t => {
t.is(result.stderr, '');
});

test('error on configuration file not found', async t => {
try {
await execa('../markdownlint.js',
['--config', 'non-existent-file-path.yaml', 'correct.md'],
{stripFinalNewline: false});
} catch (error) {
t.is(error.exitCode, 1);
t.is(error.stdout, '');
t.regex(error.stderr, /Cannot read or parse config file 'non-existent-file-path.yaml': ENOENT: no such file or directory, open 'non-existent-file-path.yaml'/);
}
});

test('error on malformed YAML configuration file', async t => {
try {
await execa('../markdownlint.js',
['--config', 'malformed-config.yaml', 'correct.md'],
{stripFinalNewline: false});
} catch (error) {
t.is(error.exitCode, 1);
t.is(error.stdout, '');
t.regex(error.stderr, /Cannot read or parse config file 'malformed-config.yaml': Unable to parse 'malformed-config.yaml'; Unexpected token/);
}
});

function getCwdConfigFileTest(extension) {
return async t => {
try {
Expand Down

0 comments on commit 40a0ee6

Please sign in to comment.