1 var path = require('path')
2 var optimist = require('optimist')
3 var helper = require('./helper')
4 var constant = require('./constants')
7 var processArgs = function (argv, options, fs, path) {
9 console.log(optimist.help())
14 console.log('Karma version: ' + constant.VERSION)
18 // TODO(vojta): warn/throw when unknown argument (probably mispelled)
19 Object.getOwnPropertyNames(argv).forEach(function (name) {
20 var argumentValue = argv[name]
21 if (name !== '_' && name !== '$0') {
22 if (Array.isArray(argumentValue)) {
23 // If the same argument is defined multiple times, override.
24 argumentValue = argumentValue.pop()
26 options[helper.dashToCamel(name)] = argumentValue
30 if (helper.isString(options.autoWatch)) {
31 options.autoWatch = options.autoWatch === 'true'
34 if (helper.isString(options.colors)) {
35 options.colors = options.colors === 'true'
38 if (helper.isString(options.logLevel)) {
39 options.logLevel = constant['LOG_' + options.logLevel.toUpperCase()] || constant.LOG_DISABLE
42 if (helper.isString(options.singleRun)) {
43 options.singleRun = options.singleRun === 'true'
46 if (helper.isString(options.browsers)) {
47 options.browsers = options.browsers.split(',')
50 if (options.reportSlowerThan === false) {
51 options.reportSlowerThan = 0
54 if (helper.isString(options.reporters)) {
55 options.reporters = options.reporters.split(',')
58 if (helper.isString(options.removedFiles)) {
59 options.removedFiles = options.removedFiles.split(',')
62 if (helper.isString(options.addedFiles)) {
63 options.addedFiles = options.addedFiles.split(',')
66 if (helper.isString(options.changedFiles)) {
67 options.changedFiles = options.changedFiles.split(',')
70 if (helper.isString(options.refresh)) {
71 options.refresh = options.refresh === 'true'
74 var configFile = argv._.shift()
77 // default config file (if exists)
78 if (fs.existsSync('./karma.conf.js')) {
79 configFile = './karma.conf.js'
80 } else if (fs.existsSync('./karma.conf.coffee')) {
81 configFile = './karma.conf.coffee'
85 options.configFile = configFile ? path.resolve(configFile) : null
90 var parseClientArgs = function (argv) {
91 // extract any args after '--' as clientArgs
94 var idx = argv.indexOf('--')
96 clientArgs = argv.slice(idx + 1)
101 // return only args that occur before `--`
102 var argsBeforeDoubleDash = function (argv) {
103 var idx = argv.indexOf('--')
105 return idx === -1 ? argv : argv.slice(0, idx)
108 var describeShared = function () {
110 .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
112 ' $0 <command>\n\n' +
114 ' start [<configFile>] [<options>] Start the server / do single run.\n' +
115 ' init [<configFile>] Initialize a config file.\n' +
116 ' run [<options>] [ -- <clientArgs>] Trigger a test run.\n' +
117 ' completion Shell completion for karma.\n\n' +
118 'Run --help with particular command to see its description and available options.')
119 .describe('help', 'Print usage and options.')
120 .describe('version', 'Print current version.')
123 var describeInit = function () {
125 .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
126 'INIT - Initialize a config file.\n\n' +
128 ' $0 init [<configFile>]')
129 .describe('log-level', '<disable | error | warn | info | debug> Level of logging.')
130 .describe('colors', 'Use colors when reporting and printing logs.')
131 .describe('no-colors', 'Do not use colors when reporting or printing logs.')
132 .describe('help', 'Print usage and options.')
135 var describeStart = function () {
137 .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
138 'START - Start the server / do a single run.\n\n' +
140 ' $0 start [<configFile>] [<options>]')
141 .describe('port', '<integer> Port where the server is running.')
142 .describe('auto-watch', 'Auto watch source files and run on change.')
143 .describe('no-auto-watch', 'Do not watch source files.')
144 .describe('log-level', '<disable | error | warn | info | debug> Level of logging.')
145 .describe('colors', 'Use colors when reporting and printing logs.')
146 .describe('no-colors', 'Do not use colors when reporting or printing logs.')
147 .describe('reporters', 'List of reporters (available: dots, progress, junit, growl, coverage).')
148 .describe('browsers', 'List of browsers to start (eg. --browsers Chrome,ChromeCanary,Firefox).')
149 .describe('capture-timeout', '<integer> Kill browser if does not capture in given time [ms].')
150 .describe('single-run', 'Run the test when browsers captured and exit.')
151 .describe('no-single-run', 'Disable single-run.')
152 .describe('report-slower-than', '<integer> Report tests that are slower than given time [ms].')
153 .describe('help', 'Print usage and options.')
156 var describeRun = function () {
158 .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
159 'RUN - Run the tests (requires running server).\n\n' +
161 ' $0 run [<configFile>] [<options>] [ -- <clientArgs>]')
162 .describe('port', '<integer> Port where the server is listening.')
163 .describe('no-refresh', 'Do not re-glob all the patterns.')
164 .describe('help', 'Print usage.')
167 var describeCompletion = function () {
169 .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
170 'COMPLETION - Bash/ZSH completion for karma.\n\n' +
172 ' $0 completion >> ~/.bashrc\n')
173 .describe('help', 'Print usage.')
176 exports.process = function () {
177 var argv = optimist.parse(argsBeforeDoubleDash(process.argv.slice(2)))
182 switch (options.cmd) {
189 options.clientArgs = parseClientArgs(process.argv)
203 processArgs(argv, options, fs, path)
204 console.error('Command not specified.')
206 console.error('Unknown command "' + options.cmd + '".')
212 return processArgs(argv, options, fs, path)
215 exports.run = function () {
216 var config = exports.process()
218 switch (config.cmd) {
220 require('./server').start(config)
223 require('./runner').run(config)
226 require('./init').init(config)
229 require('./completion').completion(config)
235 exports.processArgs = processArgs
236 exports.parseClientArgs = parseClientArgs
237 exports.argsBeforeDoubleDash = argsBeforeDoubleDash