Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / karma / lib / cli.js
1 var path = require('path')
2 var optimist = require('optimist')
3 var helper = require('./helper')
4 var constant = require('./constants')
5 var fs = require('fs')
6
7 var processArgs = function (argv, options, fs, path) {
8   if (argv.help) {
9     console.log(optimist.help())
10     process.exit(0)
11   }
12
13   if (argv.version) {
14     console.log('Karma version: ' + constant.VERSION)
15     process.exit(0)
16   }
17
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()
25       }
26       options[helper.dashToCamel(name)] = argumentValue
27     }
28   })
29
30   if (helper.isString(options.autoWatch)) {
31     options.autoWatch = options.autoWatch === 'true'
32   }
33
34   if (helper.isString(options.colors)) {
35     options.colors = options.colors === 'true'
36   }
37
38   if (helper.isString(options.logLevel)) {
39     options.logLevel = constant['LOG_' + options.logLevel.toUpperCase()] || constant.LOG_DISABLE
40   }
41
42   if (helper.isString(options.singleRun)) {
43     options.singleRun = options.singleRun === 'true'
44   }
45
46   if (helper.isString(options.browsers)) {
47     options.browsers = options.browsers.split(',')
48   }
49
50   if (options.reportSlowerThan === false) {
51     options.reportSlowerThan = 0
52   }
53
54   if (helper.isString(options.reporters)) {
55     options.reporters = options.reporters.split(',')
56   }
57
58   if (helper.isString(options.removedFiles)) {
59     options.removedFiles = options.removedFiles.split(',')
60   }
61
62   if (helper.isString(options.addedFiles)) {
63     options.addedFiles = options.addedFiles.split(',')
64   }
65
66   if (helper.isString(options.changedFiles)) {
67     options.changedFiles = options.changedFiles.split(',')
68   }
69
70   if (helper.isString(options.refresh)) {
71     options.refresh = options.refresh === 'true'
72   }
73
74   var configFile = argv._.shift()
75
76   if (!configFile) {
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'
82     }
83   }
84
85   options.configFile = configFile ? path.resolve(configFile) : null
86
87   return options
88 }
89
90 var parseClientArgs = function (argv) {
91   // extract any args after '--' as clientArgs
92   var clientArgs = []
93   argv = argv.slice(2)
94   var idx = argv.indexOf('--')
95   if (idx !== -1) {
96     clientArgs = argv.slice(idx + 1)
97   }
98   return clientArgs
99 }
100
101 // return only args that occur before `--`
102 var argsBeforeDoubleDash = function (argv) {
103   var idx = argv.indexOf('--')
104
105   return idx === -1 ? argv : argv.slice(0, idx)
106 }
107
108 var describeShared = function () {
109   optimist
110     .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
111       'Usage:\n' +
112       '  $0 <command>\n\n' +
113       'Commands:\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.')
121 }
122
123 var describeInit = function () {
124   optimist
125     .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
126       'INIT - Initialize a config file.\n\n' +
127       'Usage:\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.')
133 }
134
135 var describeStart = function () {
136   optimist
137     .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
138       'START - Start the server / do a single run.\n\n' +
139       'Usage:\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.')
154 }
155
156 var describeRun = function () {
157   optimist
158     .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
159       'RUN - Run the tests (requires running server).\n\n' +
160       'Usage:\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.')
165 }
166
167 var describeCompletion = function () {
168   optimist
169     .usage('Karma - Spectacular Test Runner for JavaScript.\n\n' +
170       'COMPLETION - Bash/ZSH completion for karma.\n\n' +
171       'Installation:\n' +
172       '  $0 completion >> ~/.bashrc\n')
173     .describe('help', 'Print usage.')
174 }
175
176 exports.process = function () {
177   var argv = optimist.parse(argsBeforeDoubleDash(process.argv.slice(2)))
178   var options = {
179     cmd: argv._.shift()
180   }
181
182   switch (options.cmd) {
183     case 'start':
184       describeStart()
185       break
186
187     case 'run':
188       describeRun()
189       options.clientArgs = parseClientArgs(process.argv)
190       break
191
192     case 'init':
193       describeInit()
194       break
195
196     case 'completion':
197       describeCompletion()
198       break
199
200     default:
201       describeShared()
202       if (!options.cmd) {
203         processArgs(argv, options, fs, path)
204         console.error('Command not specified.')
205       } else {
206         console.error('Unknown command "' + options.cmd + '".')
207       }
208       optimist.showHelp()
209       process.exit(1)
210   }
211
212   return processArgs(argv, options, fs, path)
213 }
214
215 exports.run = function () {
216   var config = exports.process()
217
218   switch (config.cmd) {
219     case 'start':
220       require('./server').start(config)
221       break
222     case 'run':
223       require('./runner').run(config)
224       break
225     case 'init':
226       require('./init').init(config)
227       break
228     case 'completion':
229       require('./completion').completion(config)
230       break
231   }
232 }
233
234 // just for testing
235 exports.processArgs = processArgs
236 exports.parseClientArgs = parseClientArgs
237 exports.argsBeforeDoubleDash = argsBeforeDoubleDash