From 8d2dc995ef42b7ccefa2160bd3cd99cf209498ca Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 12 Nov 2019 13:33:22 -0800 Subject: [PATCH] Exit with the yaml parse error on bad rc files Fix #613 --- bin/run.js | 23 ++++++++++++++++------- test/run/bad-rcfile.js | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 test/run/bad-rcfile.js diff --git a/bin/run.js b/bin/run.js index 25648e868..9ee999705 100755 --- a/bin/run.js +++ b/bin/run.js @@ -123,7 +123,10 @@ const defaultFiles = options => new Promise((res, rej) => { }) }) -const main = async options => { +const main = options => + mainAsync(options).catch(er => onError(er)) + +const mainAsync = async options => { debug('main', options) if (require.main !== module) @@ -799,13 +802,14 @@ const parsePackageJson = () => { } const parseRcFile = path => { + let contents try { - const contents = fs.readFileSync(path, 'utf8') - return yaml.parse(contents) || {} + contents = fs.readFileSync(path, 'utf8') } catch (er) { - // if no dotfile exists, or invalid yaml, fail gracefully + // if no dotfile exists, just return an empty object return {} } + return yaml.parse(contents) } const strToRegExp = g => { @@ -815,9 +819,7 @@ const strToRegExp = g => { return new RegExp(g, flags) } -try { - require('./jack.js')(main) -} catch (er) { +const onError = er => { /* istanbul ignore else - parse errors are the only ones we ever expect */ if (er.name.match(/^AssertionError/) && !er.generatedMessage) { console.error('Error: ' + er.message) @@ -827,3 +829,10 @@ try { throw er } } + +try { + process.on('unhandledRejection', onError) + require('./jack.js')(main) +} catch (er) { + onError(er) +} diff --git a/test/run/bad-rcfile.js b/test/run/bad-rcfile.js new file mode 100644 index 000000000..45fb4ed7b --- /dev/null +++ b/test/run/bad-rcfile.js @@ -0,0 +1,16 @@ +const { + tmpfile, + run, + tap, + dir, + t, +} = require('./') + +const fs = require('fs') +t.test('bad rc file', t => { + fs.writeFileSync(`${dir}/.taprc`, 'this : is not : valid : yaml') + run(['--dump-config'], { cwd: dir }, (er, o, e) => { + t.match(er, { code: 1 }) + t.end() + }) +})