Skip to content

Commit e99da31

Browse files
authoredMay 7, 2020
fix(cli): restore command line help contents (#3502)
These were lost after migration to yargs as CLI parser. Fixes #3474
1 parent 4f2fe56 commit e99da31

File tree

3 files changed

+239
-76
lines changed

3 files changed

+239
-76
lines changed
 

‎lib/cli.js

+39-76
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ const helper = require('./helper')
1010
const constant = require('./constants')
1111

1212
function processArgs (argv, options, fs, path) {
13-
if (argv.help) {
14-
console.log(yargs.help())
15-
process.exit(0)
16-
}
17-
18-
if (argv.version) {
19-
console.log(`Karma version: ${constant.VERSION}`)
20-
process.exit(0)
21-
}
22-
2313
// TODO(vojta): warn/throw when unknown argument (probably mispelled)
2414
Object.getOwnPropertyNames(argv).forEach(function (name) {
2515
let argumentValue = argv[name]
@@ -130,6 +120,10 @@ function processArgs (argv, options, fs, path) {
130120

131121
options.configFile = configFile ? path.resolve(configFile) : null
132122

123+
if (options.cmd === 'run') {
124+
options.clientArgs = parseClientArgs(process.argv)
125+
}
126+
133127
return options
134128
}
135129

@@ -151,39 +145,44 @@ function argsBeforeDoubleDash (argv) {
151145
return idx === -1 ? argv : argv.slice(0, idx)
152146
}
153147

154-
function describeShared () {
155-
yargs
148+
function describeRoot () {
149+
return yargs
156150
.usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
151+
'Run --help with particular command to see its description and available options.\n\n' +
157152
'Usage:\n' +
158-
' $0 <command>\n\n' +
159-
'Commands:\n' +
160-
' start [<configFile>] [<options>] Start the server / do single run.\n' +
161-
' init [<configFile>] Initialize a config file.\n' +
162-
' run [<options>] [ -- <clientArgs>] Trigger a test run.\n' +
163-
' completion Shell completion for karma.\n\n' +
164-
'Run --help with particular command to see its description and available options.')
153+
' $0 <command>')
154+
.command('init', 'Initialize a config file.', describeInit)
155+
.command('start', 'Start the server / do a single run.', describeStart)
156+
.command('run', 'Trigger a test run.', describeRun)
157+
.command('stop', 'Stop the server.', describeStop)
158+
.command('completion', 'Shell completion for karma.', describeCompletion)
159+
.demandCommand(1, 'Command not specified.')
160+
.strictCommands()
165161
.describe('help', 'Print usage and options.')
166162
.describe('version', 'Print current version.')
167163
}
168164

169-
function describeInit () {
165+
function describeInit (yargs) {
170166
yargs
171167
.usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
172168
'INIT - Initialize a config file.\n\n' +
173169
'Usage:\n' +
174-
' $0 init [<configFile>]')
170+
' $0 init [configFile]')
171+
.strictCommands(false)
172+
.version(false)
175173
.describe('log-level', '<disable | error | warn | info | debug> Level of logging.')
176174
.describe('colors', 'Use colors when reporting and printing logs.')
177175
.describe('no-colors', 'Do not use colors when reporting or printing logs.')
178-
.describe('help', 'Print usage and options.')
179176
}
180177

181-
function describeStart () {
178+
function describeStart (yargs) {
182179
yargs
183180
.usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
184181
'START - Start the server / do a single run.\n\n' +
185182
'Usage:\n' +
186-
' $0 start [<configFile>] [<options>]')
183+
' $0 start [configFile]')
184+
.strictCommands(false)
185+
.version(false)
187186
.describe('port', '<integer> Port where the server is running.')
188187
.describe('auto-watch', 'Auto watch source files and run on change.')
189188
.describe('detached', 'Detach the server.')
@@ -201,93 +200,57 @@ function describeStart () {
201200
.describe('no-fail-on-empty-test-suite', 'Do not fail on empty test suite.')
202201
.describe('fail-on-failing-test-suite', 'Fail on failing test suite.')
203202
.describe('no-fail-on-failing-test-suite', 'Do not fail on failing test suite.')
204-
.describe('help', 'Print usage and options.')
205203
}
206204

207-
function describeRun () {
205+
function describeRun (yargs) {
208206
yargs
209207
.usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
210208
'RUN - Run the tests (requires running server).\n\n' +
211209
'Usage:\n' +
212-
' $0 run [<configFile>] [<options>] [ -- <clientArgs>]')
210+
' $0 run [configFile] [-- <clientArgs>]')
211+
.strictCommands(false)
212+
.version(false)
213213
.describe('port', '<integer> Port where the server is listening.')
214214
.describe('no-refresh', 'Do not re-glob all the patterns.')
215215
.describe('fail-on-empty-test-suite', 'Fail on empty test suite.')
216216
.describe('no-fail-on-empty-test-suite', 'Do not fail on empty test suite.')
217-
.describe('help', 'Print usage.')
218217
.describe('log-level', '<disable | error | warn | info | debug> Level of logging.')
219218
.describe('colors', 'Use colors when reporting and printing logs.')
220219
.describe('no-colors', 'Do not use colors when reporting or printing logs.')
221220
}
222221

223-
function describeStop () {
222+
function describeStop (yargs) {
224223
yargs
225224
.usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
226225
'STOP - Stop the server (requires running server).\n\n' +
227226
'Usage:\n' +
228-
' $0 run [<configFile>] [<options>]')
227+
' $0 stop [configFile]')
228+
.strictCommands(false)
229+
.version(false)
229230
.describe('port', '<integer> Port where the server is listening.')
230231
.describe('log-level', '<disable | error | warn | info | debug> Level of logging.')
231-
.describe('help', 'Print usage.')
232232
}
233233

234-
function describeCompletion () {
234+
function describeCompletion (yargs) {
235235
yargs
236236
.usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
237237
'COMPLETION - Bash/ZSH completion for karma.\n\n' +
238238
'Installation:\n' +
239-
' $0 completion >> ~/.bashrc\n')
240-
.describe('help', 'Print usage.')
239+
' $0 completion >> ~/.bashrc')
240+
.strictCommands(false)
241+
.version(false)
241242
}
242243

243244
function printRunnerProgress (data) {
244245
process.stdout.write(data)
245246
}
246247

247-
exports.process = function () {
248-
const argv = yargs.parse(argsBeforeDoubleDash(process.argv.slice(2)))
249-
const options = {
250-
cmd: argv._.shift()
251-
}
252-
253-
switch (options.cmd) {
254-
case 'start':
255-
describeStart()
256-
break
257-
258-
case 'run':
259-
describeRun()
260-
options.clientArgs = parseClientArgs(process.argv)
261-
break
262-
263-
case 'stop':
264-
describeStop()
265-
break
266-
267-
case 'init':
268-
describeInit()
269-
break
270-
271-
case 'completion':
272-
describeCompletion()
273-
break
274-
275-
default:
276-
describeShared()
277-
if (!options.cmd) {
278-
processArgs(argv, options, fs, path)
279-
console.error('Command not specified.')
280-
} else {
281-
console.error('Unknown command "' + options.cmd + '".')
282-
}
283-
yargs.showHelp()
284-
process.exit(1)
285-
}
286-
287-
return processArgs(argv, options, fs, path)
248+
exports.process = () => {
249+
const argv = describeRoot().parse(argsBeforeDoubleDash(process.argv.slice(2)))
250+
return processArgs(argv, { cmd: argv._.shift() }, fs, path)
288251
}
289252

290-
exports.run = function () {
253+
exports.run = () => {
291254
const config = exports.process()
292255

293256
switch (config.cmd) {

‎test/e2e/cli.feature

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
Feature: CLI
2+
In order to use Karma
3+
As a person who wants to write great tests
4+
I want command line interface to provide help and useful errors.
5+
6+
Scenario: Top-level CLI help
7+
When I execute Karma with arguments: "--help"
8+
Then the stdout is exactly:
9+
"""
10+
Karma - Spectacular Test Runner for JavaScript.
11+
12+
Run --help with particular command to see its description and available options.
13+
14+
Usage:
15+
karma <command>
16+
17+
Commands:
18+
karma init Initialize a config file.
19+
karma start Start the server / do a single run.
20+
karma run Trigger a test run.
21+
karma stop Stop the server.
22+
karma completion Shell completion for karma.
23+
24+
Options:
25+
--help Print usage and options. [boolean]
26+
--version Print current version. [boolean]
27+
"""
28+
29+
Scenario: Current version
30+
When I execute Karma with arguments: "--version"
31+
Then the stdout matches RegExp:
32+
"""
33+
^\d\.\d\.\d$
34+
"""
35+
36+
Scenario: Error when command is unknown
37+
When I execute Karma with arguments: "strat"
38+
Then the stderr is exactly:
39+
"""
40+
Karma - Spectacular Test Runner for JavaScript.
41+
42+
Run --help with particular command to see its description and available options.
43+
44+
Usage:
45+
karma <command>
46+
47+
Commands:
48+
karma init Initialize a config file.
49+
karma start Start the server / do a single run.
50+
karma run Trigger a test run.
51+
karma stop Stop the server.
52+
karma completion Shell completion for karma.
53+
54+
Options:
55+
--help Print usage and options. [boolean]
56+
--version Print current version. [boolean]
57+
58+
Unknown command: strat
59+
"""
60+
61+
Scenario: Init command help
62+
When I execute Karma with arguments: "init --help"
63+
Then the stdout is exactly:
64+
"""
65+
Karma - Spectacular Test Runner for JavaScript.
66+
67+
INIT - Initialize a config file.
68+
69+
Usage:
70+
karma init [configFile]
71+
72+
Options:
73+
--help Print usage and options. [boolean]
74+
--log-level <disable | error | warn | info | debug> Level of logging.
75+
--colors Use colors when reporting and printing logs.
76+
--no-colors Do not use colors when reporting or printing logs.
77+
"""
78+
79+
Scenario: Start command help
80+
When I execute Karma with arguments: "start --help"
81+
Then the stdout is exactly:
82+
"""
83+
Karma - Spectacular Test Runner for JavaScript.
84+
85+
START - Start the server / do a single run.
86+
87+
Usage:
88+
karma start [configFile]
89+
90+
Options:
91+
--help Print usage and options. [boolean]
92+
--port <integer> Port where the server is running.
93+
--auto-watch Auto watch source files and run on change.
94+
--detached Detach the server.
95+
--no-auto-watch Do not watch source files.
96+
--log-level <disable | error | warn | info | debug> Level
97+
of logging.
98+
--colors Use colors when reporting and printing logs.
99+
--no-colors Do not use colors when reporting or printing
100+
logs.
101+
--reporters List of reporters (available: dots, progress,
102+
junit, growl, coverage).
103+
--browsers List of browsers to start (eg. --browsers
104+
Chrome,ChromeCanary,Firefox).
105+
--capture-timeout <integer> Kill browser if does not capture in
106+
given time [ms].
107+
--single-run Run the test when browsers captured and exit.
108+
--no-single-run Disable single-run.
109+
--report-slower-than <integer> Report tests that are slower than
110+
given time [ms].
111+
--fail-on-empty-test-suite Fail on empty test suite.
112+
--no-fail-on-empty-test-suite Do not fail on empty test suite.
113+
--fail-on-failing-test-suite Fail on failing test suite.
114+
--no-fail-on-failing-test-suite Do not fail on failing test suite.
115+
"""
116+
117+
Scenario: Run command help
118+
When I execute Karma with arguments: "run --help"
119+
Then the stdout is exactly:
120+
"""
121+
Karma - Spectacular Test Runner for JavaScript.
122+
123+
RUN - Run the tests (requires running server).
124+
125+
Usage:
126+
karma run [configFile] [-- <clientArgs>]
127+
128+
Options:
129+
--help Print usage and options. [boolean]
130+
--port <integer> Port where the server is listening.
131+
--no-refresh Do not re-glob all the patterns.
132+
--fail-on-empty-test-suite Fail on empty test suite.
133+
--no-fail-on-empty-test-suite Do not fail on empty test suite.
134+
--log-level <disable | error | warn | info | debug> Level
135+
of logging.
136+
--colors Use colors when reporting and printing logs.
137+
--no-colors Do not use colors when reporting or printing
138+
logs.
139+
"""
140+
141+
Scenario: Stop command help
142+
When I execute Karma with arguments: "stop --help"
143+
Then the stdout is exactly:
144+
"""
145+
Karma - Spectacular Test Runner for JavaScript.
146+
147+
STOP - Stop the server (requires running server).
148+
149+
Usage:
150+
karma stop [configFile]
151+
152+
Options:
153+
--help Print usage and options. [boolean]
154+
--port <integer> Port where the server is listening.
155+
--log-level <disable | error | warn | info | debug> Level of logging.
156+
"""
157+
158+
Scenario: Completion command help
159+
When I execute Karma with arguments: "completion --help"
160+
Then the stdout is exactly:
161+
"""
162+
Karma - Spectacular Test Runner for JavaScript.
163+
164+
COMPLETION - Bash/ZSH completion for karma.
165+
166+
Installation:
167+
karma completion >> ~/.bashrc
168+
169+
Options:
170+
--help Print usage and options. [boolean]
171+
"""

‎test/e2e/step_definitions/core_steps.js

+29
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ When('I {command} Karma with additional arguments: {string}', async function (co
4747
await this.runForegroundProcess(`${command} ${this.configFile} ${args}`)
4848
})
4949

50+
When('I execute Karma with arguments: {string}', async function (args) {
51+
await this.runForegroundProcess(args)
52+
})
53+
5054
Then(/^it passes with(:? (no\sdebug|like|regexp))?:$/, { timeout: 10 * 1000 }, function (mode, expectedOutput, callback) {
5155
const noDebug = mode === 'no debug'
5256
const like = mode === 'like'
@@ -105,6 +109,31 @@ Then('it fails with like:', function (expectedOutput, callback) {
105109
}
106110
})
107111

112+
Then(/^the (stdout|stderr) (is exactly|contains|matches RegExp):$/, function (outputType, comparison, expectedOutput) {
113+
const actualOutput = (outputType === 'stdout' ? this.lastRun.stdout : this.lastRun.stderr).trim()
114+
expectedOutput = expectedOutput.trim()
115+
116+
switch (comparison) {
117+
case 'is exactly':
118+
if (actualOutput !== expectedOutput) {
119+
throw new Error(`Expected output to be exactly as above, but got:\n\n${actualOutput}`)
120+
}
121+
break
122+
case 'contains':
123+
if (!actualOutput.includes(expectedOutput)) {
124+
throw new Error(`Expected output to contain the above text, but got:\n\n${actualOutput}`)
125+
}
126+
break
127+
case 'matches RegExp':
128+
if (!(new RegExp(expectedOutput).test(actualOutput))) {
129+
throw new Error(`Expected output to match the above RegExp, but got:\n\n${actualOutput}`)
130+
}
131+
break
132+
default:
133+
throw new Error(`Unknown comparison type: ${comparison}`)
134+
}
135+
})
136+
108137
Then(/^The (server|stopper) is dead(:? with exit code (\d+))?$/, function (stopperOrServer, code, callback) {
109138
const server = stopperOrServer === 'server'
110139
setTimeout(() => {

0 commit comments

Comments
 (0)
Please sign in to comment.