-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
cli.spec.js
220 lines (176 loc) · 7.31 KB
/
cli.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
var optimist = require('optimist')
var path = require('path')
var mocks = require('mocks')
var cli = require('../../lib/cli')
var constant = require('../../lib/constants')
var loadFile = mocks.loadFile
describe('cli', () => {
var m
var e
var mockery
var fsMock = mocks.fs.create({
cwd: {'karma.conf.js': true},
cwd2: {'karma.conf.coffee': true},
cwd3: {'karma.conf.ts': true}
})
var currentCwd = null
var pathMock = {
resolve (p) {
return path.resolve(currentCwd, p)
}
}
var setCWD = (cwd) => {
currentCwd = cwd
fsMock._setCWD(cwd)
}
var processArgs = (args, opts) => {
var argv = optimist.parse(args)
return e.processArgs(argv, opts || {}, fsMock, pathMock)
}
beforeEach(() => {
setCWD('/')
mockery = {}
mockery.process = {exit: sinon.spy()}
mockery.console = {error: sinon.spy()}
// load file under test
m = loadFile(path.join(__dirname, '/../../lib/cli.js'), mockery, {
global: {},
console: mockery.console,
process: mockery.process,
require (path) {
if (path.indexOf('./') === 0) {
return require('../../lib/' + path)
} else {
return require(path)
}
}
})
e = m.exports
})
describe('processArgs', () => {
it('should override if multiple options given', () => {
// optimist parses --port 123 --port 456 as port = [123, 456] which makes no sense
var options = processArgs(['some.conf', '--port', '12', '--log-level', 'info', '--port', '34', '--log-level', 'debug'])
expect(options.port).to.equal(34)
expect(options.logLevel).to.equal('DEBUG')
})
it('should return camelCased options', () => {
var options = processArgs(['some.conf', '--port', '12', '--single-run'])
expect(options.configFile).to.exist
expect(options.port).to.equal(12)
expect(options.singleRun).to.equal(true)
})
it('should parse options without configFile and set default', () => {
setCWD('/cwd')
var options = processArgs(['--auto-watch', '--auto-watch-interval', '10'])
expect(path.resolve(options.configFile)).to.equal(path.resolve('/cwd/karma.conf.js'))
expect(options.autoWatch).to.equal(true)
expect(options.autoWatchInterval).to.equal(10)
})
it('should set default karma.conf.coffee config file if exists', () => {
setCWD('/cwd2')
var options = processArgs(['--port', '10'])
expect(path.resolve(options.configFile)).to.equal(path.resolve('/cwd2/karma.conf.coffee'))
})
it('should set default karma.conf.ts config file if exists', () => {
setCWD('/cwd3')
var options = processArgs(['--port', '10'])
expect(path.resolve(options.configFile)).to.equal(path.resolve('/cwd3/karma.conf.ts'))
})
it('should not set default config if neither exists', () => {
setCWD('/')
var options = processArgs([])
expect(options.configFile).to.equal(null)
})
it('should parse auto-watch, colors, singleRun to boolean', () => {
var options = processArgs(['--auto-watch', 'false', '--colors', 'false', '--single-run', 'false'])
expect(options.autoWatch).to.equal(false)
expect(options.colors).to.equal(false)
expect(options.singleRun).to.equal(false)
options = processArgs(['--auto-watch', 'true', '--colors', 'true', '--single-run', 'true'])
expect(options.autoWatch).to.equal(true)
expect(options.colors).to.equal(true)
expect(options.singleRun).to.equal(true)
})
it('should replace log-level constants', () => {
var options = processArgs(['--log-level', 'debug'])
expect(options.logLevel).to.equal(constant.LOG_DEBUG)
options = processArgs(['--log-level', 'error'])
expect(options.logLevel).to.equal(constant.LOG_ERROR)
options = processArgs(['--log-level', 'warn'])
expect(options.logLevel).to.equal(constant.LOG_WARN)
options = processArgs(['--log-level', 'foo'])
expect(mockery.process.exit).to.have.been.calledWith(1)
options = processArgs(['--log-level'])
expect(mockery.process.exit).to.have.been.calledWith(1)
})
it('should parse format-error into a function', () => {
// root export
var options = processArgs(['--format-error', '../../test/unit/fixtures/format-error-root'])
var formatErrorRoot = require('../../test/unit/fixtures/format-error-root')
expect(options.formatError).to.equal(formatErrorRoot)
// property export
options = processArgs(['--format-error', '../../test/unit/fixtures/format-error-property'])
var formatErrorProperty = require('../../test/unit/fixtures/format-error-property').formatError
expect(options.formatError).to.equal(formatErrorProperty)
})
it('should parse browsers into an array', () => {
var options = processArgs(['--browsers', 'Chrome,ChromeCanary,Firefox'])
expect(options.browsers).to.deep.equal(['Chrome', 'ChromeCanary', 'Firefox'])
})
it('should resolve configFile to absolute path', () => {
setCWD('/cwd')
var options = processArgs(['some/config.js'])
expect(path.resolve(options.configFile)).to.equal(path.resolve('/cwd/some/config.js'))
})
it('should parse report-slower-than to a number', () => {
var options = processArgs(['--report-slower-than', '2000'])
expect(options.reportSlowerThan).to.equal(2000)
options = processArgs(['--no-report-slower-than'])
expect(options.reportSlowerThan).to.equal(0)
})
it('should cast reporters to array', () => {
var options = processArgs(['--reporters', 'dots,junit'])
expect(options.reporters).to.deep.equal(['dots', 'junit'])
options = processArgs(['--reporters', 'dots'])
expect(options.reporters).to.deep.equal(['dots'])
})
it('should parse removed/added/changed files to array', () => {
var options = processArgs([
'--removed-files', 'r1.js,r2.js',
'--changed-files', 'ch1.js,ch2.js',
'--added-files', 'a1.js,a2.js'
])
expect(options.removedFiles).to.deep.equal(['r1.js', 'r2.js'])
expect(options.addedFiles).to.deep.equal(['a1.js', 'a2.js'])
expect(options.changedFiles).to.deep.equal(['ch1.js', 'ch2.js'])
})
it('should error on args with underscores', () => {
var expectedException
try {
var options = processArgs(['--no_browsers'])
expectedException = 'Should have thrown but got ' + options
} catch (e) {
expectedException = e
} finally {
expect(expectedException + '').to.equal('Error: Bad argument: no_browsers did you mean no-browsers')
}
})
})
describe('parseClientArgs', () => {
it('should return arguments after --', () => {
var args = cli.parseClientArgs(['node', 'karma.js', 'runArg', '--flag', '--', '--foo', '--bar', 'baz'])
expect(args).to.deep.equal(['--foo', '--bar', 'baz'])
})
it('should return empty args if -- is not present', () => {
var args = cli.parseClientArgs(['node', 'karma.js', 'runArg', '--flag', '--foo', '--bar', 'baz'])
expect(args).to.deep.equal([])
})
})
describe('argsBeforeDoubleDash', () => {
it('should return array of args that occur before --', () => {
var args = cli.argsBeforeDoubleDash(['aa', '--bb', 'value', '--', 'some', '--no-more'])
expect(args).to.deep.equal(['aa', '--bb', 'value'])
})
})
})