1 | var path = require('path')
|
2 | var optimist = require('optimist')
|
3 | var fs = require('graceful-fs')
|
4 |
|
5 | var Server = require('./server')
|
6 | var helper = require('./helper')
|
7 | var constant = require('./constants')
|
8 |
|
9 | var processArgs = function (argv, options, fs, path) {
|
10 | if (argv.help) {
|
11 | console.log(optimist.help())
|
12 | process.exit(0)
|
13 | }
|
14 |
|
15 | if (argv.version) {
|
16 | console.log('Karma version: ' + constant.VERSION)
|
17 | process.exit(0)
|
18 | }
|
19 |
|
20 |
|
21 | Object.getOwnPropertyNames(argv).forEach(function (name) {
|
22 | var argumentValue = argv[name]
|
23 | if (name !== '_' && name !== '$0') {
|
24 | if (Array.isArray(argumentValue)) {
|
25 |
|
26 | argumentValue = argumentValue.pop()
|
27 | }
|
28 | options[helper.dashToCamel(name)] = argumentValue
|
29 | }
|
30 | })
|
31 |
|
32 | if (helper.isString(options.autoWatch)) {
|
33 | options.autoWatch = options.autoWatch === 'true'
|
34 | }
|
35 |
|
36 | if (helper.isString(options.colors)) {
|
37 | options.colors = options.colors === 'true'
|
38 | }
|
39 |
|
40 | if (helper.isString(options.failOnEmptyTestSuite)) {
|
41 | options.failOnEmptyTestSuite = options.failOnEmptyTestSuite === 'true'
|
42 | }
|
43 |
|
44 | if (helper.isString(options.logLevel)) {
|
45 | var logConstant = constant['LOG_' + options.logLevel.toUpperCase()]
|
46 | if (helper.isDefined(logConstant)) {
|
47 | options.logLevel = logConstant
|
48 | } else {
|
49 | console.error('Log level must be one of disable, error, warn, info, or debug.')
|
50 | process.exit(1)
|
51 | }
|
52 | } else if (helper.isDefined(options.logLevel)) {
|
53 | console.error('Log level must be one of disable, error, warn, info, or debug.')
|
54 | process.exit(1)
|
55 | }
|
56 |
|
57 | if (helper.isString(options.singleRun)) {
|
58 | options.singleRun = options.singleRun === 'true'
|
59 | }
|
60 |
|
61 | if (helper.isString(options.browsers)) {
|
62 | options.browsers = options.browsers.split(',')
|
63 | }
|
64 |
|
65 | if (options.reportSlowerThan === false) {
|
66 | options.reportSlowerThan = 0
|
67 | }
|
68 |
|
69 | if (helper.isString(options.reporters)) {
|
70 | options.reporters = options.reporters.split(',')
|
71 | }
|
72 |
|
73 | if (helper.isString(options.removedFiles)) {
|
74 | options.removedFiles = options.removedFiles.split(',')
|
75 | }
|
76 |
|
77 | if (helper.isString(options.addedFiles)) {
|
78 | options.addedFiles = options.addedFiles.split(',')
|
79 | }
|
80 |
|
81 | if (helper.isString(options.changedFiles)) {
|
82 | options.changedFiles = options.changedFiles.split(',')
|
83 | }
|
84 |
|
85 | if (helper.isString(options.refresh)) {
|
86 | options.refresh = options.refresh === 'true'
|
87 | }
|
88 |
|
89 | var configFile = argv._.shift()
|
90 |
|
91 | if (!configFile) {
|
92 |
|
93 | if (fs.existsSync('./karma.conf.js')) {
|
94 | configFile = './karma.conf.js'
|
95 | } else if (fs.existsSync('./karma.conf.coffee')) {
|
96 | configFile = './karma.conf.coffee'
|
97 | } else if (fs.existsSync('./.config/karma.conf.js')) {
|
98 | configFile = './.config/karma.conf.js'
|
99 | } else if (fs.existsSync('./.config/karma.conf.coffee')) {
|
100 | configFile = './.config/karma.conf.coffee'
|
101 | }
|
102 | }
|
103 |
|
104 | options.configFile = configFile ? path.resolve(configFile) : null
|
105 |
|
106 | return options
|
107 | }
|
108 |
|
109 | var parseClientArgs = function (argv) {
|
110 |
|
111 | var clientArgs = []
|
112 | argv = argv.slice(2)
|
113 | var idx = argv.indexOf('--')
|
114 | if (idx !== -1) {
|
115 | clientArgs = argv.slice(idx + 1)
|
116 | }
|
117 | return clientArgs
|
118 | }
|
119 |
|
120 |
|
121 | var argsBeforeDoubleDash = function (argv) {
|
122 | var idx = argv.indexOf('--')
|
123 |
|
124 | return idx === -1 ? argv : argv.slice(0, idx)
|
125 | }
|
126 |
|
127 | var describeShared = function () {
|
128 | optimist
|
129 | .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
|
130 | 'Usage:\n' +
|
131 | ' $0 <command>\n\n' +
|
132 | 'Commands:\n' +
|
133 | ' start [<configFile>] [<options>] Start the server / do single run.\n' +
|
134 | ' init [<configFile>] Initialize a config file.\n' +
|
135 | ' run [<options>] [ -- <clientArgs>] Trigger a test run.\n' +
|
136 | ' completion Shell completion for karma.\n\n' +
|
137 | 'Run --help with particular command to see its description and available options.')
|
138 | .describe('help', 'Print usage and options.')
|
139 | .describe('version', 'Print current version.')
|
140 | }
|
141 |
|
142 | var describeInit = function () {
|
143 | optimist
|
144 | .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
|
145 | 'INIT - Initialize a config file.\n\n' +
|
146 | 'Usage:\n' +
|
147 | ' $0 init [<configFile>]')
|
148 | .describe('log-level', '<disable | error | warn | info | debug> Level of logging.')
|
149 | .describe('colors', 'Use colors when reporting and printing logs.')
|
150 | .describe('no-colors', 'Do not use colors when reporting or printing logs.')
|
151 | .describe('help', 'Print usage and options.')
|
152 | }
|
153 |
|
154 | var describeStart = function () {
|
155 | optimist
|
156 | .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
|
157 | 'START - Start the server / do a single run.\n\n' +
|
158 | 'Usage:\n' +
|
159 | ' $0 start [<configFile>] [<options>]')
|
160 | .describe('port', '<integer> Port where the server is running.')
|
161 | .describe('auto-watch', 'Auto watch source files and run on change.')
|
162 | .describe('no-auto-watch', 'Do not watch source files.')
|
163 | .describe('log-level', '<disable | error | warn | info | debug> Level of logging.')
|
164 | .describe('colors', 'Use colors when reporting and printing logs.')
|
165 | .describe('no-colors', 'Do not use colors when reporting or printing logs.')
|
166 | .describe('reporters', 'List of reporters (available: dots, progress, junit, growl, coverage).')
|
167 | .describe('browsers', 'List of browsers to start (eg. --browsers Chrome,ChromeCanary,Firefox).')
|
168 | .describe('capture-timeout', '<integer> Kill browser if does not capture in given time [ms].')
|
169 | .describe('single-run', 'Run the test when browsers captured and exit.')
|
170 | .describe('no-single-run', 'Disable single-run.')
|
171 | .describe('report-slower-than', '<integer> Report tests that are slower than given time [ms].')
|
172 | .describe('fail-on-empty-test-suite', 'Fail on empty test suite.')
|
173 | .describe('no-fail-on-empty-test-suite', 'Do not fail on empty test suite.')
|
174 | .describe('help', 'Print usage and options.')
|
175 | }
|
176 |
|
177 | var describeRun = function () {
|
178 | optimist
|
179 | .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
|
180 | 'RUN - Run the tests (requires running server).\n\n' +
|
181 | 'Usage:\n' +
|
182 | ' $0 run [<configFile>] [<options>] [ -- <clientArgs>]')
|
183 | .describe('port', '<integer> Port where the server is listening.')
|
184 | .describe('no-refresh', 'Do not re-glob all the patterns.')
|
185 | .describe('fail-on-empty-test-suite', 'Fail on empty test suite.')
|
186 | .describe('no-fail-on-empty-test-suite', 'Do not fail on empty test suite.')
|
187 | .describe('help', 'Print usage.')
|
188 | }
|
189 |
|
190 | var describeCompletion = function () {
|
191 | optimist
|
192 | .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
|
193 | 'COMPLETION - Bash/ZSH completion for karma.\n\n' +
|
194 | 'Installation:\n' +
|
195 | ' $0 completion >> ~/.bashrc\n')
|
196 | .describe('help', 'Print usage.')
|
197 | }
|
198 |
|
199 | exports.process = function () {
|
200 | var argv = optimist.parse(argsBeforeDoubleDash(process.argv.slice(2)))
|
201 | var options = {
|
202 | cmd: argv._.shift()
|
203 | }
|
204 |
|
205 | switch (options.cmd) {
|
206 | case 'start':
|
207 | describeStart()
|
208 | break
|
209 |
|
210 | case 'run':
|
211 | describeRun()
|
212 | options.clientArgs = parseClientArgs(process.argv)
|
213 | break
|
214 |
|
215 | case 'init':
|
216 | describeInit()
|
217 | break
|
218 |
|
219 | case 'completion':
|
220 | describeCompletion()
|
221 | break
|
222 |
|
223 | default:
|
224 | describeShared()
|
225 | if (!options.cmd) {
|
226 | processArgs(argv, options, fs, path)
|
227 | console.error('Command not specified.')
|
228 | } else {
|
229 | console.error('Unknown command "' + options.cmd + '".')
|
230 | }
|
231 | optimist.showHelp()
|
232 | process.exit(1)
|
233 | }
|
234 |
|
235 | return processArgs(argv, options, fs, path)
|
236 | }
|
237 |
|
238 | exports.run = function () {
|
239 | var config = exports.process()
|
240 |
|
241 | switch (config.cmd) {
|
242 | case 'start':
|
243 | new Server(config).start()
|
244 | break
|
245 | case 'run':
|
246 | require('./runner').run(config)
|
247 | break
|
248 | case 'init':
|
249 | require('./init').init(config)
|
250 | break
|
251 | case 'completion':
|
252 | require('./completion').completion(config)
|
253 | break
|
254 | }
|
255 | }
|
256 |
|
257 |
|
258 | exports.processArgs = processArgs
|
259 | exports.parseClientArgs = parseClientArgs
|
260 | exports.argsBeforeDoubleDash = argsBeforeDoubleDash
|