Skip to content

Commit b6eef5a

Browse files
timdeschryvernovemberborn
authored andcommitted
Fail hard when --concurrency is set to invalid values (#1478)
* Update warning message when the flag is used without a value * Detect non-integer or negative integer values
1 parent 57f5007 commit b6eef5a

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

lib/cli.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,12 @@ exports.run = () => {
119119
}
120120

121121
if (cli.flags.concurrency === '') {
122-
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.');
122+
throw new Error(colors.error(figures.cross) + ' The --concurrency and -c flags must be provided.');
123+
}
124+
125+
if (cli.flags.concurrency &&
126+
(!Number.isInteger(Number.parseFloat(cli.flags.concurrency)) || parseInt(cli.flags.concurrency, 10) < 0)) {
127+
throw new Error(colors.error(figures.cross) + ' The --concurrency and -c flags must be a nonnegative integer.');
123128
}
124129

125130
if (hasFlag('--require') || hasFlag('-r')) {

test/cli.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,17 +414,47 @@ test('bails when config contains `"tap": true` and `"watch": true`', t => {
414414
});
415415

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

426426
['--concurrency', '-c'].forEach(concurrencyFlag => {
427-
test(`works when ${concurrencyFlag} provided with value`, t => {
427+
test(`bails when ${concurrencyFlag} is provided with an input that is a string`, t => {
428+
execCli([`${concurrencyFlag}=foo`, 'test.js', concurrencyFlag], {dirname: 'fixture/concurrency'}, (err, stdout, stderr) => {
429+
t.is(err.code, 1);
430+
t.match(stderr, 'The --concurrency and -c flags must be a nonnegative integer.');
431+
t.end();
432+
});
433+
});
434+
});
435+
436+
['--concurrency', '-c'].forEach(concurrencyFlag => {
437+
test(`bails when ${concurrencyFlag} is provided with an input that is a float`, t => {
438+
execCli([`${concurrencyFlag}=4.7`, 'test.js', concurrencyFlag], {dirname: 'fixture/concurrency'}, (err, stdout, stderr) => {
439+
t.is(err.code, 1);
440+
t.match(stderr, 'The --concurrency and -c flags must be a nonnegative integer.');
441+
t.end();
442+
});
443+
});
444+
});
445+
446+
['--concurrency', '-c'].forEach(concurrencyFlag => {
447+
test(`bails when ${concurrencyFlag} is provided with an input that is negative`, t => {
448+
execCli([`${concurrencyFlag}=-1`, 'test.js', concurrencyFlag], {dirname: 'fixture/concurrency'}, (err, stdout, stderr) => {
449+
t.is(err.code, 1);
450+
t.match(stderr, 'The --concurrency and -c flags must be a nonnegative integer.');
451+
t.end();
452+
});
453+
});
454+
});
455+
456+
['--concurrency', '-c'].forEach(concurrencyFlag => {
457+
test(`works when ${concurrencyFlag} is provided with a value`, t => {
428458
execCli([`${concurrencyFlag}=1`, 'test.js'], {dirname: 'fixture/concurrency'}, err => {
429459
t.ifError(err);
430460
t.end();

0 commit comments

Comments
 (0)