Skip to content

Commit

Permalink
Fail hard when --concurrency is set to invalid values (#1478)
Browse files Browse the repository at this point in the history
* Update warning message when the flag is used without a value
* Detect non-integer or negative integer values
  • Loading branch information
timdeschryver authored and novemberborn committed Aug 12, 2017
1 parent 57f5007 commit b6eef5a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ exports.run = () => {
}

if (cli.flags.concurrency === '') {
throw new Error(colors.error(figures.cross) + ' The --concurrency and -c flags must be provided the maximum number of test files to run at once.');
throw new Error(colors.error(figures.cross) + ' The --concurrency and -c flags must be provided.');
}

if (cli.flags.concurrency &&
(!Number.isInteger(Number.parseFloat(cli.flags.concurrency)) || parseInt(cli.flags.concurrency, 10) < 0)) {
throw new Error(colors.error(figures.cross) + ' The --concurrency and -c flags must be a nonnegative integer.');
}

if (hasFlag('--require') || hasFlag('-r')) {
Expand Down
36 changes: 33 additions & 3 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,17 +414,47 @@ test('bails when config contains `"tap": true` and `"watch": true`', t => {
});

['--concurrency', '-c'].forEach(concurrencyFlag => {
test(`bails when ${concurrencyFlag} provided without value`, t => {
test(`bails when ${concurrencyFlag} is provided without value`, t => {
execCli(['test.js', concurrencyFlag], {dirname: 'fixture/concurrency'}, (err, stdout, stderr) => {
t.is(err.code, 1);
t.match(stderr, 'The --concurrency and -c flags must be provided the maximum number of test files to run at once.');
t.match(stderr, 'The --concurrency and -c flags must be provided.');
t.end();
});
});
});

['--concurrency', '-c'].forEach(concurrencyFlag => {
test(`works when ${concurrencyFlag} provided with value`, t => {
test(`bails when ${concurrencyFlag} is provided with an input that is a string`, t => {
execCli([`${concurrencyFlag}=foo`, 'test.js', concurrencyFlag], {dirname: 'fixture/concurrency'}, (err, stdout, stderr) => {
t.is(err.code, 1);
t.match(stderr, 'The --concurrency and -c flags must be a nonnegative integer.');
t.end();
});
});
});

['--concurrency', '-c'].forEach(concurrencyFlag => {
test(`bails when ${concurrencyFlag} is provided with an input that is a float`, t => {
execCli([`${concurrencyFlag}=4.7`, 'test.js', concurrencyFlag], {dirname: 'fixture/concurrency'}, (err, stdout, stderr) => {
t.is(err.code, 1);
t.match(stderr, 'The --concurrency and -c flags must be a nonnegative integer.');
t.end();
});
});
});

['--concurrency', '-c'].forEach(concurrencyFlag => {
test(`bails when ${concurrencyFlag} is provided with an input that is negative`, t => {
execCli([`${concurrencyFlag}=-1`, 'test.js', concurrencyFlag], {dirname: 'fixture/concurrency'}, (err, stdout, stderr) => {
t.is(err.code, 1);
t.match(stderr, 'The --concurrency and -c flags must be a nonnegative integer.');
t.end();
});
});
});

['--concurrency', '-c'].forEach(concurrencyFlag => {
test(`works when ${concurrencyFlag} is provided with a value`, t => {
execCli([`${concurrencyFlag}=1`, 'test.js'], {dirname: 'fixture/concurrency'}, err => {
t.ifError(err);
t.end();
Expand Down

0 comments on commit b6eef5a

Please sign in to comment.