Skip to content

Commit b6fa8b9

Browse files
authored
Clean up CLI module
* Remove 'ava --init foo.js' example * Stop --init, --watch and --update-snapshots from being configured in package.json * Declare flags in a more useful order * Remove --require error * Simplify custom flag validation * Rename 'source' option to 'sources'
1 parent fe7a8a1 commit b6fa8b9

File tree

7 files changed

+53
-89
lines changed

7 files changed

+53
-89
lines changed

lib/cli.js

Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const meow = require('meow');
77
const Promise = require('bluebird');
88
const pkgConf = require('pkg-conf');
99
const isCi = require('is-ci');
10-
const hasFlag = require('has-flag');
1110
const Api = require('../api');
1211
const colors = require('./colors');
1312
const VerboseReporter = require('./reporters/verbose');
@@ -32,86 +31,82 @@ exports.run = () => {
3231
3332
Options
3433
--init Add AVA to your project
34+
--watch, -w Re-run tests when tests and source files change
35+
--match, -m Only run tests with matching title (Can be repeated)
36+
--update-snapshots, -u Update snapshots
3537
--fail-fast Stop after first test failure
38+
--timeout, -T Set global timeout
3639
--serial, -s Run tests serially
37-
--tap, -t Generate TAP output
40+
--concurrency, -c Max number of test files running at the same time (Default: CPU cores)
3841
--verbose, -v Enable verbose output
39-
--no-cache Disable the transpiler cache
42+
--tap, -t Generate TAP output
43+
--no-cache Disable the compiler cache
4044
--color Force color output
4145
--no-color Disable color output
42-
--match, -m Only run tests with matching title (Can be repeated)
43-
--watch, -w Re-run tests when tests and source files change
44-
--timeout, -T Set global timeout
45-
--concurrency, -c Max number of test files running at the same time (Default: CPU cores)
46-
--update-snapshots, -u Update snapshots
4746
4847
Examples
4948
ava
5049
ava test.js test2.js
5150
ava test-*.js
5251
ava test
5352
ava --init
54-
ava --init foo.js
5553
5654
Default patterns when no arguments:
5755
test.js test-*.js test/**/*.js **/__tests__/**/*.js **/*.test.js
5856
`, {
5957
flags: {
58+
init: {
59+
type: 'boolean'
60+
},
61+
watch: {
62+
type: 'boolean',
63+
alias: 'w'
64+
},
6065
match: {
6166
type: 'string',
6267
alias: 'm',
6368
default: conf.match
6469
},
65-
timeout: {
66-
type: 'string',
67-
alias: 'T',
68-
default: conf.timeout
69-
},
70-
concurrency: {
71-
type: 'string',
72-
alias: 'c',
73-
default: conf.concurrency
74-
},
75-
init: {
70+
'update-snapshots': {
7671
type: 'boolean',
77-
default: conf.init
72+
alias: 'u'
7873
},
7974
'fail-fast': {
8075
type: 'boolean',
8176
default: conf.failFast
8277
},
78+
timeout: {
79+
type: 'string',
80+
alias: 'T',
81+
default: conf.timeout
82+
},
8383
serial: {
8484
type: 'boolean',
8585
alias: 's',
8686
default: conf.serial
8787
},
88-
tap: {
89-
type: 'boolean',
90-
alias: 't',
91-
default: conf.tap
88+
concurrency: {
89+
type: 'string',
90+
alias: 'c',
91+
default: conf.concurrency
9292
},
9393
verbose: {
9494
type: 'boolean',
9595
alias: 'v',
9696
default: conf.verbose
9797
},
98-
watch: {
98+
tap: {
9999
type: 'boolean',
100-
alias: 'w',
101-
default: conf.watch
100+
alias: 't',
101+
default: conf.tap
102102
},
103-
'update-snapshots': {
103+
cache: {
104104
type: 'boolean',
105-
alias: 'u',
106-
default: conf.updateSnapshots
105+
default: conf.cache !== false
107106
},
108107
color: {
109108
type: 'boolean',
110109
default: 'color' in conf ? conf.color : require('supports-color').stdout !== false
111-
},
112-
cache: {
113-
type: 'boolean',
114-
default: conf.cache !== false
115110
}
116111
}
117112
});
@@ -123,28 +118,23 @@ exports.run = () => {
123118
return;
124119
}
125120

126-
if (
127-
((hasFlag('--watch') || hasFlag('-w')) && (hasFlag('--tap') || hasFlag('-t'))) ||
128-
(conf.watch && conf.tap)
129-
) {
130-
throw new Error(colors.error(figures.cross) + ' The TAP reporter is not available when using watch mode.');
121+
if (cli.flags.watch && cli.flags.tap && !conf.tap) {
122+
throw new Error(`${colors.error(figures.cross)} The TAP reporter is not available when using watch mode.`);
131123
}
132124

133-
if ((hasFlag('--watch') || hasFlag('-w')) && isCi) {
134-
throw new Error(colors.error(figures.cross) + ' Watch mode is not available in CI, as it prevents AVA from terminating.');
125+
if (cli.flags.watch && isCi) {
126+
throw new Error(`${colors.error(figures.cross)} Watch mode is not available in CI, as it prevents AVA from terminating.`);
135127
}
136128

137-
if (cli.flags.concurrency === '') {
138-
throw new Error(colors.error(figures.cross) + ' The --concurrency and -c flags must be provided.');
139-
}
140-
141-
if (cli.flags.concurrency &&
142-
(!Number.isInteger(Number.parseFloat(cli.flags.concurrency)) || parseInt(cli.flags.concurrency, 10) < 0)) {
143-
throw new Error(colors.error(figures.cross) + ' The --concurrency and -c flags must be a nonnegative integer.');
129+
if (
130+
cli.flags.concurrency === '' ||
131+
(cli.flags.concurrency && (!Number.isInteger(Number.parseFloat(cli.flags.concurrency)) || parseInt(cli.flags.concurrency, 10) < 0))
132+
) {
133+
throw new Error(`${colors.error(figures.cross)} The --concurrency or -c flag must be provided with a nonnegative integer.`);
144134
}
145135

146-
if (hasFlag('--require') || hasFlag('-r')) {
147-
throw new Error(colors.error(figures.cross) + ' The --require and -r flags are deprecated. Requirements should be configured in package.json - see documentation.');
136+
if ('source' in conf) {
137+
throw new Error(`${colors.error(figures.cross)} The 'source' option has been renamed. Use 'sources' instead.`);
148138
}
149139

150140
// Copy resultant cli.flags into conf for use with Api and elsewhere
@@ -197,7 +187,7 @@ exports.run = () => {
197187

198188
if (conf.watch) {
199189
try {
200-
const watcher = new Watcher(logger, api, files, arrify(conf.source));
190+
const watcher = new Watcher(logger, api, files, arrify(conf.sources));
201191
watcher.observeStdin(process.stdin);
202192
} catch (err) {
203193
if (err.name === 'AvaError') {

package-lock.json

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@
101101
"find-cache-dir": "^1.0.0",
102102
"get-port": "^3.2.0",
103103
"globby": "^7.1.1",
104-
"has-flag": "^3.0.0",
105104
"hullabaloo-config-manager": "^2.0.0-beta.1",
106105
"ignore-by-default": "^1.0.0",
107106
"import-local": "^1.0.0",

readme.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,26 +162,25 @@ $ ava --help
162162

163163
Options
164164
--init Add AVA to your project
165+
--watch, -w Re-run tests when tests and source files change
166+
--match, -m Only run tests with matching title (Can be repeated)
167+
--update-snapshots, -u Update snapshots
165168
--fail-fast Stop after first test failure
169+
--timeout, -T Set global timeout
166170
--serial, -s Run tests serially
167-
--tap, -t Generate TAP output
171+
--concurrency, -c Max number of test files running at the same time (Default: CPU cores)
168172
--verbose, -v Enable verbose output
169-
--no-cache Disable the transpiler cache
173+
--tap, -t Generate TAP output
174+
--no-cache Disable the compiler cache
170175
--color Force color output
171176
--no-color Disable color output
172-
--match, -m Only run tests with matching title (Can be repeated)
173-
--watch, -w Re-run tests when tests and source files change
174-
--timeout, -T Set global timeout
175-
--concurrency, -c Max number of test files running at the same time (Default: CPU cores)
176-
--update-snapshots, -u Update snapshots
177177

178178
Examples
179179
ava
180180
ava test.js test2.js
181181
ava test-*.js
182182
ava test
183183
ava --init
184-
ava --init foo.js
185184

186185
Default patterns when no arguments:
187186
test.js test-*.js test/**/*.js **/__tests__/**/*.js **/*.test.js

test/cli.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -389,14 +389,6 @@ test('`"tap": true` config is ignored when --watch is given', t => {
389389
child.stderr.on('data', testOutput);
390390
});
391391

392-
test('bails when config contains `"tap": true` and `"watch": true`', t => {
393-
execCli(['test.js'], {dirname: 'fixture/watcher/tap-and-watch-in-conf'}, (err, stdout, stderr) => {
394-
t.is(err.code, 1);
395-
t.match(stderr, 'The TAP reporter is not available when using watch mode.');
396-
t.end();
397-
});
398-
});
399-
400392
['--watch', '-w'].forEach(watchFlag => {
401393
['--tap', '-t'].forEach(tapFlag => {
402394
test(`bails when ${tapFlag} reporter is used while ${watchFlag} is given`, t => {
@@ -423,7 +415,7 @@ test('bails when config contains `"tap": true` and `"watch": true`', t => {
423415
test(`bails when ${concurrencyFlag} is provided without value`, t => {
424416
execCli(['test.js', concurrencyFlag], {dirname: 'fixture/concurrency'}, (err, stdout, stderr) => {
425417
t.is(err.code, 1);
426-
t.match(stderr, 'The --concurrency and -c flags must be provided.');
418+
t.match(stderr, 'The --concurrency or -c flag must be provided with a nonnegative integer.');
427419
t.end();
428420
});
429421
});
@@ -433,7 +425,7 @@ test('bails when config contains `"tap": true` and `"watch": true`', t => {
433425
test(`bails when ${concurrencyFlag} is provided with an input that is a string`, t => {
434426
execCli([`${concurrencyFlag}=foo`, 'test.js', concurrencyFlag], {dirname: 'fixture/concurrency'}, (err, stdout, stderr) => {
435427
t.is(err.code, 1);
436-
t.match(stderr, 'The --concurrency and -c flags must be a nonnegative integer.');
428+
t.match(stderr, 'The --concurrency or -c flag must be provided with a nonnegative integer.');
437429
t.end();
438430
});
439431
});
@@ -443,7 +435,7 @@ test('bails when config contains `"tap": true` and `"watch": true`', t => {
443435
test(`bails when ${concurrencyFlag} is provided with an input that is a float`, t => {
444436
execCli([`${concurrencyFlag}=4.7`, 'test.js', concurrencyFlag], {dirname: 'fixture/concurrency'}, (err, stdout, stderr) => {
445437
t.is(err.code, 1);
446-
t.match(stderr, 'The --concurrency and -c flags must be a nonnegative integer.');
438+
t.match(stderr, 'The --concurrency or -c flag must be provided with a nonnegative integer.');
447439
t.end();
448440
});
449441
});
@@ -453,7 +445,7 @@ test('bails when config contains `"tap": true` and `"watch": true`', t => {
453445
test(`bails when ${concurrencyFlag} is provided with an input that is negative`, t => {
454446
execCli([`${concurrencyFlag}=-1`, 'test.js', concurrencyFlag], {dirname: 'fixture/concurrency'}, (err, stdout, stderr) => {
455447
t.is(err.code, 1);
456-
t.match(stderr, 'The --concurrency and -c flags must be a nonnegative integer.');
448+
t.match(stderr, 'The --concurrency or -c flag must be provided with a nonnegative integer.');
457449
t.end();
458450
});
459451
});

test/fixture/watcher/tap-and-watch-in-conf/package.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

test/fixture/watcher/tap-and-watch-in-conf/test.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)