source: trip-planner-front/node_modules/karma/lib/cli.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 11.2 KB
Line 
1'use strict'
2
3const path = require('path')
4const yargs = require('yargs')
5const fs = require('graceful-fs')
6
7const Server = require('./server')
8const helper = require('./helper')
9const constant = require('./constants')
10const cfg = require('./config')
11
12function processArgs (argv, options, fs, path) {
13 Object.getOwnPropertyNames(argv).forEach(function (name) {
14 let argumentValue = argv[name]
15 if (name !== '_' && name !== '$0') {
16 if (Array.isArray(argumentValue)) {
17 argumentValue = argumentValue.pop() // If the same argument is defined multiple times, override.
18 }
19 options[helper.dashToCamel(name)] = argumentValue
20 }
21 })
22
23 if (helper.isString(options.autoWatch)) {
24 options.autoWatch = options.autoWatch === 'true'
25 }
26
27 if (helper.isString(options.colors)) {
28 options.colors = options.colors === 'true'
29 }
30
31 if (helper.isString(options.failOnEmptyTestSuite)) {
32 options.failOnEmptyTestSuite = options.failOnEmptyTestSuite === 'true'
33 }
34
35 if (helper.isString(options.failOnFailingTestSuite)) {
36 options.failOnFailingTestSuite = options.failOnFailingTestSuite === 'true'
37 }
38
39 if (helper.isString(options.formatError)) {
40 let required
41 try {
42 required = require(options.formatError)
43 } catch (err) {
44 console.error('Could not require formatError: ' + options.formatError, err)
45 }
46 // support exports.formatError and module.exports = function
47 options.formatError = required.formatError || required
48 if (!helper.isFunction(options.formatError)) {
49 console.error(`Format error must be a function, got: ${typeof options.formatError}`)
50 process.exit(1)
51 }
52 }
53
54 if (helper.isString(options.logLevel)) {
55 const logConstant = constant['LOG_' + options.logLevel.toUpperCase()]
56 if (helper.isDefined(logConstant)) {
57 options.logLevel = logConstant
58 } else {
59 console.error('Log level must be one of disable, error, warn, info, or debug.')
60 process.exit(1)
61 }
62 } else if (helper.isDefined(options.logLevel)) {
63 console.error('Log level must be one of disable, error, warn, info, or debug.')
64 process.exit(1)
65 }
66
67 if (helper.isString(options.singleRun)) {
68 options.singleRun = options.singleRun === 'true'
69 }
70
71 if (helper.isString(options.browsers)) {
72 options.browsers = options.browsers.split(',')
73 }
74
75 if (options.reportSlowerThan === false) {
76 options.reportSlowerThan = 0
77 }
78
79 if (helper.isString(options.reporters)) {
80 options.reporters = options.reporters.split(',')
81 }
82
83 if (helper.isString(options.removedFiles)) {
84 options.removedFiles = options.removedFiles.split(',')
85 }
86
87 if (helper.isString(options.addedFiles)) {
88 options.addedFiles = options.addedFiles.split(',')
89 }
90
91 if (helper.isString(options.changedFiles)) {
92 options.changedFiles = options.changedFiles.split(',')
93 }
94
95 if (helper.isString(options.refresh)) {
96 options.refresh = options.refresh === 'true'
97 }
98
99 let configFile = argv.configFile
100
101 if (!configFile) {
102 // default config file (if exists)
103 if (fs.existsSync('./karma.conf.js')) {
104 configFile = './karma.conf.js'
105 } else if (fs.existsSync('./karma.conf.coffee')) {
106 configFile = './karma.conf.coffee'
107 } else if (fs.existsSync('./karma.conf.ts')) {
108 configFile = './karma.conf.ts'
109 } else if (fs.existsSync('./.config/karma.conf.js')) {
110 configFile = './.config/karma.conf.js'
111 } else if (fs.existsSync('./.config/karma.conf.coffee')) {
112 configFile = './.config/karma.conf.coffee'
113 } else if (fs.existsSync('./.config/karma.conf.ts')) {
114 configFile = './.config/karma.conf.ts'
115 }
116 }
117
118 options.configFile = configFile ? path.resolve(configFile) : null
119
120 if (options.cmd === 'run') {
121 options.clientArgs = parseClientArgs(process.argv)
122 }
123
124 return options
125}
126
127function parseClientArgs (argv) {
128 // extract any args after '--' as clientArgs
129 let clientArgs = []
130 argv = argv.slice(2)
131 const idx = argv.indexOf('--')
132 if (idx !== -1) {
133 clientArgs = argv.slice(idx + 1)
134 }
135 return clientArgs
136}
137
138// return only args that occur before `--`
139function argsBeforeDoubleDash (argv) {
140 const idx = argv.indexOf('--')
141
142 return idx === -1 ? argv : argv.slice(0, idx)
143}
144
145function describeRoot () {
146 return yargs
147 .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
148 'Run --help with particular command to see its description and available options.\n\n' +
149 'Usage:\n' +
150 ' $0 <command>')
151 .command('init [configFile]', 'Initialize a config file.', describeInit)
152 .command('start [configFile]', 'Start the server / do a single run.', describeStart)
153 .command('run [configFile]', 'Trigger a test run.', describeRun)
154 .command('stop [configFile]', 'Stop the server.', describeStop)
155 .command('completion', 'Shell completion for karma.', describeCompletion)
156 .demandCommand(1, 'Command not specified.')
157 .strictCommands()
158 .describe('help', 'Print usage and options.')
159 .describe('version', 'Print current version.')
160}
161
162function describeInit (yargs) {
163 yargs
164 .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
165 'INIT - Initialize a config file.\n\n' +
166 'Usage:\n' +
167 ' $0 init [configFile]')
168 .strictCommands(false)
169 .version(false)
170 .positional('configFile', {
171 describe: 'Name of the generated Karma configuration file',
172 type: 'string'
173 })
174 .describe('log-level', '<disable | error | warn | info | debug> Level of logging.')
175 .describe('colors', 'Use colors when reporting and printing logs.')
176 .describe('no-colors', 'Do not use colors when reporting or printing logs.')
177}
178
179function describeStart (yargs) {
180 yargs
181 .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
182 'START - Start the server / do a single run.\n\n' +
183 'Usage:\n' +
184 ' $0 start [configFile]')
185 .strictCommands(false)
186 .version(false)
187 .positional('configFile', {
188 describe: 'Path to the Karma configuration file',
189 type: 'string'
190 })
191 .describe('port', '<integer> Port where the server is running.')
192 .describe('auto-watch', 'Auto watch source files and run on change.')
193 .describe('detached', 'Detach the server.')
194 .describe('no-auto-watch', 'Do not watch source files.')
195 .describe('log-level', '<disable | error | warn | info | debug> Level of logging.')
196 .describe('colors', 'Use colors when reporting and printing logs.')
197 .describe('no-colors', 'Do not use colors when reporting or printing logs.')
198 .describe('reporters', 'List of reporters (available: dots, progress, junit, growl, coverage).')
199 .describe('browsers', 'List of browsers to start (eg. --browsers Chrome,ChromeCanary,Firefox).')
200 .describe('capture-timeout', '<integer> Kill browser if does not capture in given time [ms].')
201 .describe('single-run', 'Run the test when browsers captured and exit.')
202 .describe('no-single-run', 'Disable single-run.')
203 .describe('report-slower-than', '<integer> Report tests that are slower than given time [ms].')
204 .describe('fail-on-empty-test-suite', 'Fail on empty test suite.')
205 .describe('no-fail-on-empty-test-suite', 'Do not fail on empty test suite.')
206 .describe('fail-on-failing-test-suite', 'Fail on failing test suite.')
207 .describe('no-fail-on-failing-test-suite', 'Do not fail on failing test suite.')
208 .option('format-error', {
209 describe: 'A path to a file that exports the format function.',
210 type: 'string'
211 })
212}
213
214function describeRun (yargs) {
215 yargs
216 .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
217 'RUN - Run the tests (requires running server).\n\n' +
218 'Usage:\n' +
219 ' $0 run [configFile] [-- <clientArgs>]')
220 .strictCommands(false)
221 .version(false)
222 .positional('configFile', {
223 describe: 'Path to the Karma configuration file',
224 type: 'string'
225 })
226 .describe('port', '<integer> Port where the server is listening.')
227 .describe('no-refresh', 'Do not re-glob all the patterns.')
228 .describe('fail-on-empty-test-suite', 'Fail on empty test suite.')
229 .describe('no-fail-on-empty-test-suite', 'Do not fail on empty test suite.')
230 .describe('log-level', '<disable | error | warn | info | debug> Level of logging.')
231 .describe('colors', 'Use colors when reporting and printing logs.')
232 .describe('no-colors', 'Do not use colors when reporting or printing logs.')
233 .option('removed-files', {
234 describe: 'Comma-separated paths to removed files. Useful when automatic file watching is disabled.',
235 type: 'string'
236 })
237 .option('changed-files', {
238 describe: 'Comma-separated paths to changed files. Useful when automatic file watching is disabled.',
239 type: 'string'
240 })
241 .option('added-files', {
242 describe: 'Comma-separated paths to added files. Useful when automatic file watching is disabled.',
243 type: 'string'
244 })
245}
246
247function describeStop (yargs) {
248 yargs
249 .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
250 'STOP - Stop the server (requires running server).\n\n' +
251 'Usage:\n' +
252 ' $0 stop [configFile]')
253 .strictCommands(false)
254 .version(false)
255 .positional('configFile', {
256 describe: 'Path to the Karma configuration file',
257 type: 'string'
258 })
259 .describe('port', '<integer> Port where the server is listening.')
260 .describe('log-level', '<disable | error | warn | info | debug> Level of logging.')
261}
262
263function describeCompletion (yargs) {
264 yargs
265 .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
266 'COMPLETION - Bash/ZSH completion for karma.\n\n' +
267 'Installation:\n' +
268 ' $0 completion >> ~/.bashrc')
269 .version(false)
270}
271
272function printRunnerProgress (data) {
273 process.stdout.write(data)
274}
275
276exports.process = () => {
277 const argv = describeRoot().parse(argsBeforeDoubleDash(process.argv.slice(2)))
278 return processArgs(argv, { cmd: argv._.shift() }, fs, path)
279}
280
281exports.run = async () => {
282 const cliOptions = exports.process()
283 const cmd = cliOptions.cmd // prevent config from changing the command
284 const cmdNeedsConfig = cmd === 'start' || cmd === 'run' || cmd === 'stop'
285 if (cmdNeedsConfig) {
286 let config
287 try {
288 config = await cfg.parseConfig(
289 cliOptions.configFile,
290 cliOptions,
291 {
292 promiseConfig: true,
293 throwErrors: true
294 }
295 )
296 } catch (karmaConfigException) {
297 // The reject reason/exception isn't used to log a message since
298 // parseConfig already calls a configured logger method with an almost
299 // identical message.
300
301 // The `run` function is a private application, not a public API. We don't
302 // need to worry about process.exit vs throw vs promise rejection here.
303 process.exit(1)
304 }
305 switch (cmd) {
306 case 'start': {
307 const server = new Server(config)
308 await server.start()
309 return server
310 }
311 case 'run':
312 return require('./runner')
313 .run(config)
314 .on('progress', printRunnerProgress)
315 case 'stop':
316 return require('./stopper').stop(config)
317 }
318 } else {
319 switch (cmd) {
320 case 'init':
321 return require('./init').init(cliOptions)
322 case 'completion':
323 return require('./completion').completion(cliOptions)
324 }
325 }
326}
327
328// just for testing
329exports.processArgs = processArgs
330exports.parseClientArgs = parseClientArgs
331exports.argsBeforeDoubleDash = argsBeforeDoubleDash
Note: See TracBrowser for help on using the repository browser.