/
args_spec.js
588 lines (471 loc) · 18.2 KB
/
args_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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
require('../spec_helper')
const path = require('path')
const os = require('os')
const snapshot = require('snap-shot-it')
const stripAnsi = require('strip-ansi')
const argsUtil = require(`${root}lib/util/args`)
const getWindowsProxyUtil = require(`${root}lib/util/get-windows-proxy`)
const cwd = process.cwd()
describe('lib/util/args', () => {
beforeEach(function () {
this.setup = (...args) => {
return argsUtil.toObject(args)
}
})
context('--smoke-test', () => {
it('sets pong to ping', function () {
const options = this.setup('--smoke-test', '--ping=123')
expect(options.pong).to.eq(123)
})
})
context('--project', () => {
it('sets projectRoot', function () {
const projectRoot = path.resolve(cwd, './foo/bar')
const options = this.setup('--project', './foo/bar')
expect(options.projectRoot).to.eq(projectRoot)
})
})
context('--run-project', () => {
it('sets projectRoot', function () {
const projectRoot = path.resolve(cwd, '/baz')
const options = this.setup('--run-project', '/baz')
expect(options.projectRoot).to.eq(projectRoot)
})
it('strips single double quote from the end', function () {
// https://github.com/cypress-io/cypress/issues/535
// NPM does not pass correctly options that end with backslash
const options = this.setup('--run-project', 'C:\\foo"')
expect(options.runProject).to.eq('C:\\foo')
})
it('does not strip if there are multiple double quotes', function () {
const options = this.setup('--run-project', '"foo bar"')
expect(options.runProject).to.eq('"foo bar"')
})
})
context('--spec', () => {
it('converts to array', function () {
const options = this.setup('--run-project', 'foo', '--spec', 'cypress/integration/a.js,cypress/integration/b.js,cypress/integration/c.js')
expect(options.spec[0]).to.eq(`${cwd}/cypress/integration/a.js`)
expect(options.spec[1]).to.eq(`${cwd}/cypress/integration/b.js`)
expect(options.spec[2]).to.eq(`${cwd}/cypress/integration/c.js`)
})
it('discards wrapping single quotes', function () {
const options = this.setup('--run-project', 'foo', '--spec', '\'cypress/integration/foo_spec.js\'')
expect(options.spec[0]).to.eq(`${cwd}/cypress/integration/foo_spec.js`)
})
})
context('--tag', () => {
it('converts to array', function () {
const options = this.setup('--run-project', 'foo', '--tag', 'nightly,production,build')
expect(options.tag[0]).to.eq('nightly')
expect(options.tag[1]).to.eq('production')
expect(options.tag[2]).to.eq('build')
})
})
context('--port', () => {
it('converts to Number', function () {
const options = this.setup('--port', '8080')
expect(options.config.port).to.eq(8080)
})
})
context('--env', () => {
it('converts to object literal', function () {
const options = this.setup('--env', 'foo=bar,version=0.12.1,host=localhost:8888,bar=qux=')
expect(options.config.env).to.deep.eq({
foo: 'bar',
version: '0.12.1',
host: 'localhost:8888',
bar: 'qux=',
})
})
it('throws if env string cannot be parsed', function () {
expect(() => {
return this.setup('--env', 'nonono')
}).to.throw
// now look at the error
try {
return this.setup('--env', 'nonono')
} catch (err) {
return snapshot('invalid env error', stripAnsi(err.message))
}
})
})
context('--reporterOptions', () => {
it('converts to object literal', function () {
const reporterOpts = {
mochaFile: 'path/to/results.xml',
testCaseSwitchClassnameAndName: true,
suiteTitleSeparatedBy: '.=|',
}
const options = this.setup('--reporterOptions', JSON.stringify(reporterOpts))
expect(options.config.reporterOptions).to.deep.eq(reporterOpts)
})
it('converts nested objects with mixed assignment usage', function () {
const reporterOpts = {
reporterEnabled: 'JSON, Spec',
jsonReporterOptions: {
toConsole: true,
},
}
// as a full blown object
let options = this.setup('--reporterOptions', JSON.stringify(reporterOpts))
expect(options.config.reporterOptions).to.deep.eq(reporterOpts)
// as mixed usage
const nestedJSON = JSON.stringify(reporterOpts.jsonReporterOptions)
options = this.setup(
'--reporterOptions',
`reporterEnabled=JSON,jsonReporterOptions=${nestedJSON}`,
)
expect(options.config.reporterOptions).to.deep.eq({
reporterEnabled: 'JSON',
jsonReporterOptions: {
toConsole: true,
},
})
})
it('throws if reporter string cannot be parsed', function () {
expect(() => {
return this.setup('--reporterOptions', 'abc')
}).to.throw
// now look at the error
try {
return this.setup('--reporterOptions', 'abc')
} catch (err) {
return snapshot('invalid reporter options error', stripAnsi(err.message))
}
})
})
context('--config', () => {
it('converts to object literal', function () {
const options = this.setup('--config', 'pageLoadTimeout=10000,waitForAnimations=false')
expect(options.config.pageLoadTimeout).eq(10000)
expect(options.config.waitForAnimations).eq(false)
})
it('converts straight JSON stringification', function () {
const config = {
pageLoadTimeout: 10000,
waitForAnimations: false,
}
const options = this.setup('--config', JSON.stringify(config))
expect(options.config).to.deep.eq(config)
})
it('converts nested usage with JSON stringification', function () {
const config = {
pageLoadTimeout: 10000,
waitForAnimations: false,
blocklistHosts: ['one.com', 'www.two.io'],
hosts: {
'foobar.com': '127.0.0.1',
},
}
// as a full blown object
let options = this.setup('--config', JSON.stringify(config))
expect(options.config).to.deep.eq(config)
// as mixed usage
const hosts = JSON.stringify(config.hosts)
const blocklistHosts = JSON.stringify(config.blocklistHosts)
options = this.setup(
'--config',
[
'pageLoadTimeout=10000',
'waitForAnimations=false',
`hosts=${hosts}`,
`blocklistHosts=${blocklistHosts}`,
].join(','),
)
expect(options.config).to.deep.eq(config)
})
it('whitelists config properties', function () {
const options = this.setup('--config', 'foo=bar,port=1111,supportFile=path/to/support_file')
expect(options.config.port).to.eq(1111)
expect(options.config.supportFile).to.eq('path/to/support_file')
expect(options).not.to.have.property('foo')
})
it('overrides port in config', function () {
let options = this.setup('--port', 2222, '--config', 'port=3333')
expect(options.config.port).to.eq(2222)
options = this.setup('--port', 2222)
expect(options.config.port).to.eq(2222)
})
it('throws if config string cannot be parsed', function () {
expect(() => {
return this.setup('--config', 'xyz')
}).to.throw
// now look at the error
try {
return this.setup('--config', 'xyz')
} catch (err) {
return snapshot('invalid config error', stripAnsi(err.message))
}
})
})
context('.toArray', () => {
beforeEach(function () {
this.obj = { config: { foo: 'bar' }, project: 'foo/bar' }
})
it('rejects values which have an cooresponding underscore\'d key', function () {
expect(argsUtil.toArray(this.obj)).to.deep.eq([
`--config=${JSON.stringify({ foo: 'bar' })}`,
'--project=foo/bar',
])
})
})
context('.toObject', () => {
beforeEach(function () {
this.hosts = { a: 'b', b: 'c' }
this.blocklistHosts = ['a.com', 'b.com']
this.specs = [
path.join(cwd, 'foo'),
path.join(cwd, 'bar'),
path.join(cwd, 'baz'),
]
this.env = {
foo: 'bar',
baz: 'quux',
bar: 'foo=quz',
}
this.config = {
env: this.env,
hosts: this.hosts,
requestTimeout: 1234,
blocklistHosts: this.blocklistHosts,
reporterOptions: {
foo: 'bar',
},
}
const s = (str) => {
return JSON.stringify(str)
}
this.obj = this.setup(
'--get-key',
'--env=foo=bar,baz=quux,bar=foo=quz',
'--config',
`requestTimeout=1234,blocklistHosts=${s(this.blocklistHosts)},hosts=${s(this.hosts)}`,
'--reporter-options=foo=bar',
'--spec=foo,bar,baz',
)
})
it('coerces booleans', function () {
expect(this.setup('--foo=true').foo).be.true
expect(this.setup('--no-record').record).to.be.false
expect(this.setup('--record=false').record).to.be.false
})
it('backs up env, config, reporterOptions, spec', function () {
expect(this.obj).to.deep.eq({
cwd,
_: [],
config: this.config,
getKey: true,
invokedFromCli: false,
spec: this.specs,
})
})
it('can transpose back to an array', function () {
const mergedConfig = JSON.stringify({
requestTimeout: this.config.requestTimeout,
blocklistHosts: this.blocklistHosts,
hosts: this.hosts,
env: this.env,
reporterOptions: {
foo: 'bar',
},
})
const args = argsUtil.toArray(this.obj)
expect(args).to.deep.eq([
`--config=${mergedConfig}`,
`--cwd=${cwd}`,
'--getKey=true',
`--spec=${JSON.stringify(this.specs)}`,
])
expect(argsUtil.toObject(args)).to.deep.eq({
cwd,
_: [],
getKey: true,
invokedFromCli: true,
config: this.config,
spec: this.specs,
})
})
})
context('--updating', () => {
// updating from 0.13.9 will omit the appPath + execPath so we must
// handle these missing arguments manually
it('slurps up appPath + execPath if updating and these are omitted', () => {
const argv = [
'/private/var/folders/wr/3xdzqnq16lz5r1j_xtl443580000gn/T/cypress/Cypress.app/Contents/MacOS/Cypress',
'/Applications/Cypress.app',
'/Applications/Cypress.app',
'--updating',
]
expect(argsUtil.toObject(argv)).to.deep.eq({
cwd,
_: [
'/private/var/folders/wr/3xdzqnq16lz5r1j_xtl443580000gn/T/cypress/Cypress.app/Contents/MacOS/Cypress',
'/Applications/Cypress.app',
'/Applications/Cypress.app',
],
config: {},
appPath: '/Applications/Cypress.app',
execPath: '/Applications/Cypress.app',
invokedFromCli: false,
updating: true,
})
})
it('does not slurp up appPath + execPath if updating and these are already present in args', () => {
const argv = [
'/private/var/folders/wr/3xdzqnq16lz5r1j_xtl443580000gn/T/cypress/Cypress.app/Contents/MacOS/Cypress',
'/Applications/Cypress.app1',
'/Applications/Cypress.app2',
'--app-path=a',
'--exec-path=e',
'--updating',
]
expect(argsUtil.toObject(argv)).to.deep.eq({
cwd,
_: [
'/private/var/folders/wr/3xdzqnq16lz5r1j_xtl443580000gn/T/cypress/Cypress.app/Contents/MacOS/Cypress',
'/Applications/Cypress.app1',
'/Applications/Cypress.app2',
],
config: {},
appPath: 'a',
execPath: 'e',
invokedFromCli: false,
updating: true,
})
})
})
context('with proxy', () => {
beforeEach(function () {
process.env = this.originalEnv
delete process.env.HTTP_PROXY
delete process.env.HTTPS_PROXY
delete process.env.NO_PROXY
delete process.env.http_proxy
delete process.env.https_proxy
return delete process.env.no_proxy
})
it('sets options from environment', function () {
process.env.HTTP_PROXY = 'http://foo-bar.baz:123'
process.env.NO_PROXY = 'a,b,c'
const options = this.setup()
expect(options.proxySource).to.be.undefined
expect(options.proxyServer).to.eq(process.env.HTTP_PROXY)
expect(options.proxyServer).to.eq('http://foo-bar.baz:123')
expect(options.proxyBypassList).to.eq('a,b,c,127.0.0.1,::1,localhost')
expect(process.env.HTTPS_PROXY).to.eq(process.env.HTTP_PROXY)
})
it('loads from Windows registry if not defined', function () {
sinon.stub(getWindowsProxyUtil, 'getWindowsProxy').returns({
httpProxy: 'http://quux.quuz',
noProxy: 'd,e,f',
})
sinon.stub(os, 'platform').returns('win32')
const options = this.setup()
expect(options.proxySource).to.eq('win32')
expect(options.proxyServer).to.eq('http://quux.quuz')
expect(options.proxyServer).to.eq(process.env.HTTP_PROXY)
expect(options.proxyServer).to.eq(process.env.HTTPS_PROXY)
expect(options.proxyBypassList).to.eq('d,e,f,127.0.0.1,::1,localhost')
expect(options.proxyBypassList).to.eq(process.env.NO_PROXY)
});
['', 'false', '0'].forEach((override) => {
it(`doesn't load from Windows registry if HTTP_PROXY overridden with string '${override}'`, function () {
sinon.stub(getWindowsProxyUtil, 'getWindowsProxy').returns()
sinon.stub(os, 'platform').returns('win32')
process.env.HTTP_PROXY = override
const options = this.setup()
expect(getWindowsProxyUtil.getWindowsProxy).to.not.called
expect(options.proxySource).to.be.undefined
expect(options.proxyServer).to.be.undefined
expect(options.proxyBypassList).to.be.undefined
expect(process.env.HTTP_PROXY).to.be.undefined
expect(process.env.HTTPS_PROXY).to.be.undefined
expect(process.env.NO_PROXY).to.eq('127.0.0.1,::1,localhost')
})
})
it('doesn\'t mess with env vars if Windows registry doesn\'t have proxy', function () {
sinon.stub(getWindowsProxyUtil, 'getWindowsProxy').returns()
sinon.stub(os, 'platform').returns('win32')
const options = this.setup()
expect(options.proxySource).to.be.undefined
expect(options.proxyServer).to.be.undefined
expect(options.proxyBypassList).to.be.undefined
expect(process.env.HTTP_PROXY).to.be.undefined
expect(process.env.HTTPS_PROXY).to.be.undefined
expect(process.env.NO_PROXY).to.eq('127.0.0.1,::1,localhost')
})
it('sets a default NO_PROXY', function () {
process.env.HTTP_PROXY = 'http://foo-bar.baz:123'
const options = this.setup()
expect(options.proxySource).to.be.undefined
expect(options.proxyServer).to.eq(process.env.HTTP_PROXY)
expect(options.proxyBypassList).to.eq('127.0.0.1,::1,localhost')
expect(options.proxyBypassList).to.eq(process.env.NO_PROXY)
})
it('does not add localhost to NO_PROXY if NO_PROXY contains <-loopback>', function () {
process.env.HTTP_PROXY = 'http://foo-bar.baz:123'
process.env.NO_PROXY = 'a,b,c,<-loopback>,d'
const options = this.setup()
expect(options.proxySource).to.be.undefined
expect(options.proxyServer).to.eq(process.env.HTTP_PROXY)
expect(options.proxyBypassList).to.eq('a,b,c,<-loopback>,d')
expect(options.proxyBypassList).to.eq(process.env.NO_PROXY)
})
it('sets a default localhost NO_PROXY if NO_PROXY = \'\'', function () {
process.env.HTTP_PROXY = 'http://foo-bar.baz:123'
process.env.NO_PROXY = ''
const options = this.setup()
expect(options.proxySource).to.be.undefined
expect(options.proxyServer).to.eq(process.env.HTTP_PROXY)
expect(options.proxyBypassList).to.eq('127.0.0.1,::1,localhost')
expect(options.proxyBypassList).to.eq(process.env.NO_PROXY)
})
it('does not set a default localhost NO_PROXY if NO_PROXY = \'<-loopback>\'', function () {
process.env.HTTP_PROXY = 'http://foo-bar.baz:123'
process.env.NO_PROXY = '<-loopback>'
const options = this.setup()
expect(options.proxySource).to.be.undefined
expect(options.proxyServer).to.eq(process.env.HTTP_PROXY)
expect(options.proxyBypassList).to.eq('<-loopback>')
expect(options.proxyBypassList).to.eq(process.env.NO_PROXY)
})
it('copies lowercase proxy vars to uppercase', function () {
process.env.http_proxy = 'http://foo-bar.baz:123'
process.env.https_proxy = 'https://foo-bar.baz:123'
process.env.no_proxy = 'http://no-proxy.holla'
expect(process.env.HTTP_PROXY).to.be.undefined
expect(process.env.HTTPS_PROXY).to.be.undefined
expect(process.env.NO_PROXY).to.be.undefined
const options = this.setup()
expect(process.env.HTTP_PROXY).to.eq('http://foo-bar.baz:123')
expect(process.env.HTTPS_PROXY).to.eq('https://foo-bar.baz:123')
expect(process.env.NO_PROXY).to.eq('http://no-proxy.holla,127.0.0.1,::1,localhost')
expect(options.proxySource).to.be.undefined
expect(options.proxyServer).to.eq(process.env.HTTP_PROXY)
expect(options.proxyBypassList).to.eq(process.env.NO_PROXY)
})
it('can use npm_config_proxy', function () {
process.env.npm_config_proxy = 'http://foo-bar.baz:123'
expect(process.env.HTTP_PROXY).to.be.undefined
const options = this.setup()
expect(process.env.HTTP_PROXY).to.eq('http://foo-bar.baz:123')
expect(process.env.HTTPS_PROXY).to.eq('http://foo-bar.baz:123')
expect(process.env.NO_PROXY).to.eq('127.0.0.1,::1,localhost')
expect(options.proxySource).to.be.undefined
expect(options.proxyServer).to.eq(process.env.HTTP_PROXY)
expect(options.proxyBypassList).to.eq(process.env.NO_PROXY)
})
it('can override npm_config_proxy with falsy HTTP_PROXY', function () {
process.env.npm_config_proxy = 'http://foo-bar.baz:123'
process.env.HTTP_PROXY = ''
const options = this.setup()
expect(process.env.HTTP_PROXY).to.be.undefined
expect(process.env.HTTPS_PROXY).to.be.undefined
expect(process.env.NO_PROXY).to.eq('127.0.0.1,::1,localhost')
expect(options.proxySource).to.be.undefined
expect(options.proxyServer).to.eq(process.env.HTTP_PROXY)
expect(options.proxyBypassList).to.be.undefined
})
})
})