Skip to content

Commit

Permalink
Merge pull request #4 from afeld/exit-code
Browse files Browse the repository at this point in the history
give a non-zero exit code when validation fails
  • Loading branch information
zrrrzzt committed Sep 21, 2016
2 parents e9ac672 + 7f73576 commit 6dd1f69
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
20 changes: 19 additions & 1 deletion index.js
Expand Up @@ -44,6 +44,24 @@ validator(opts, function (error, data) {
console.error(error)
process.exit(1)
} else {
console.log(opts.format === 'json' ? JSON.stringify(data) : data)
var msg
var validationFailed = false

if (opts.format === 'json') {
msg = JSON.stringify(data)
if (data.messages.length) {
validationFailed = true
}
} else {
msg = data
if (data.includes('There were errors')) {
validationFailed = true
}
}

console.log(msg)
if (validationFailed) {
process.exit(2)
}
}
})
19 changes: 10 additions & 9 deletions test/cli-test.js
Expand Up @@ -48,6 +48,13 @@ tap.test('It returns error on error', function testError (test) {
})
})

tap.test('It returns error on validation failure', function testError (test) {
exec('./index.js', ['--file=test/data/invalid.html'], function versionWithV (error, stdout, stderr) {
test.ok(error, 'Error OK')
test.end()
})
})

tap.test('It returns data if url supplied', function testError (test) {
exec('./index.js', ['https://www.github.com'], function versionWithV (error, stdout, stderr) {
if (error) {
Expand All @@ -59,20 +66,14 @@ tap.test('It returns data if url supplied', function testError (test) {
})

tap.test('It returns data if file supplied', function testError (test) {
exec('./index.js', ['--file=test/data/invalid.html'], function versionWithV (error, stdout, stderr) {
if (error) {
throw error
}
exec('./index.js', ['--file=test/data/invalid.html'], function versionWithV (_, stdout, stderr) {
test.ok(stdout.toString().trim(), 'Data OK')
test.end()
})
})

tap.test('It returns data on supplied supplied format', function testError (test) {
exec('./index.js', ['--file=test/data/invalid.html', '--format=json'], function versionWithV (error, stdout, stderr) {
if (error) {
throw error
}
tap.test('It returns data in supplied format', function testError (test) {
exec('./index.js', ['--file=test/data/invalid.html', '--format=json'], function versionWithV (_, stdout, stderr) {
test.equal(JSON.parse(stdout.toString().trim()).messages.length, 3)
test.end()
})
Expand Down

0 comments on commit 6dd1f69

Please sign in to comment.