Skip to content

Commit fa00fd7

Browse files
authored
Merge pull request #774 from DaNish808/cmd-tool-returns-0-761
bring back correct exit code on validation w/ errors
2 parents afb599e + fab7ba4 commit fa00fd7

File tree

265 files changed

+193
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

265 files changed

+193
-0
lines changed

bids-validator/cli.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,30 @@ var colors = require('colors/safe')
55
var fs = require('fs')
66
const remoteFiles = require('./utils/files/remoteFiles')
77

8+
const exitProcess = issues => {
9+
if (
10+
issues === 'Invalid' ||
11+
(issues.errors && issues.errors.length >= 1) ||
12+
(issues.config && issues.config.length >= 1)
13+
) {
14+
process.exit(1)
15+
} else {
16+
process.exit(0)
17+
}
18+
}
19+
820
module.exports = function(dir, options) {
921
if (fs.existsSync(dir)) {
1022
if (options.json) {
1123
validate.BIDS(dir, options, function(issues, summary) {
1224
console.log(JSON.stringify({ issues, summary }))
25+
exitProcess(issues)
1326
})
1427
} else {
1528
validate.BIDS(dir, options, function(issues, summary) {
1629
console.log(validate.consoleFormat.issues(issues, options) + '\n')
1730
console.log(validate.consoleFormat.summary(summary, options))
31+
exitProcess(issues)
1832
})
1933
}
2034
} else {

bids-validator/tests/cli.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const { spawn } = require('child_process')
33
const dir = process.cwd()
44
const data_dir = dir + '/bids-validator/tests/data/'
55
const test_data = data_dir + 'valid_headers/'
6+
const data_with_errors = data_dir + 'empty_files'
7+
const data_without_errors = data_dir + 'valid_dataset'
68

79
const cli_path = './bids-validator/bin/bids-validator'
810

@@ -55,6 +57,40 @@ describe('CLI', () => {
5557
})
5658
})
5759

60+
it('without errors should exit with code 0', done => {
61+
const command = spawn(cli_path, [data_without_errors, '--json'])
62+
command.on('exit', code => {
63+
assert.equal(code, 0)
64+
done()
65+
})
66+
})
67+
68+
it('with errors should not exit with code 0', done => {
69+
const command = spawn(cli_path, [data_with_errors])
70+
command.on('exit', code => {
71+
assert.notEqual(code, 0)
72+
done()
73+
})
74+
})
75+
76+
it('with errors should not exit with code 0 with --json argument', done => {
77+
const command = spawn(cli_path, [data_with_errors, '--json'])
78+
let commandOutput = []
79+
let output = {}
80+
command.stdout.on('data', data => {
81+
const dataLines = data.toString().split('\n')
82+
commandOutput = commandOutput.concat(dataLines)
83+
})
84+
command.stderr.on('end', () => {
85+
output = JSON.parse(commandOutput.join(''))
86+
})
87+
command.on('exit', code => {
88+
assert(output.issues.errors.length > 0)
89+
assert.notEqual(code, 0)
90+
done()
91+
})
92+
})
93+
5894
it('should print valid json when the --json argument is provided', done => {
5995
const command = spawn(cli_path, [test_data, '--json'])
6096
let commandOutput = ''
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"Name": "Synthetic dataset for inclusion in BIDS-examples",
3+
"BIDSVersion": "1.0.2",
4+
"License": "PD",
5+
"Authors": ["Markiewicz, C. J."]
6+
}

0 commit comments

Comments
 (0)