Skip to content

Commit

Permalink
Exit with the yaml parse error on bad rc files
Browse files Browse the repository at this point in the history
Fix #613
  • Loading branch information
isaacs committed Nov 12, 2019
1 parent 57eb83c commit 8d2dc99
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
23 changes: 16 additions & 7 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 => {
Expand All @@ -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)
Expand All @@ -827,3 +829,10 @@ try {
throw er
}
}

try {
process.on('unhandledRejection', onError)
require('./jack.js')(main)
} catch (er) {
onError(er)
}
16 changes: 16 additions & 0 deletions test/run/bad-rcfile.js
Original file line number Diff line number Diff line change
@@ -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()
})
})

0 comments on commit 8d2dc99

Please sign in to comment.