-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathverify_spec.js
844 lines (702 loc) · 21.7 KB
/
verify_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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
/* eslint-disable no-restricted-properties */
require('../../spec_helper')
const path = require('path')
const _ = require('lodash')
const os = require('os')
const cp = require('child_process')
const Promise = require('bluebird')
const { stripIndent } = require('common-tags')
const mockfs = require('mock-fs')
const mockedEnv = require('mocked-env')
const fs = require(`${lib}/fs`)
const util = require(`${lib}/util`)
const logger = require(`${lib}/logger`)
const xvfb = require(`${lib}/exec/xvfb`)
const verify = require(`${lib}/tasks/verify`)
const Stdout = require('../../support/stdout')
const normalize = require('../../support/normalize')
const snapshot = require('../../support/snapshot')
const { mockSpawn } = require('../../support/spawn-mock')
const packageVersion = '1.2.3'
const cacheDir = '/cache/Cypress'
const executablePath = '/cache/Cypress/1.2.3/Cypress.app/Contents/MacOS/Cypress'
const binaryStatePath = '/cache/Cypress/1.2.3/binary_state.json'
const DEFAULT_VERIFY_TIMEOUT = 30000
let stdout
let spawnedProcess
/* eslint-disable no-octal */
context('lib/tasks/verify', () => {
require('mocha-banner').register()
beforeEach(() => {
stdout = Stdout.capture()
spawnedProcess = {
code: 0,
stderr: sinon.stub(),
stdout: '222',
}
os.platform.returns('darwin')
os.release.returns('0.0.0')
sinon.stub(util, 'getCacheDir').returns(cacheDir)
sinon.stub(util, 'isCi').returns(false)
sinon.stub(util, 'pkgVersion').returns(packageVersion)
sinon.stub(util, 'exec')
sinon.stub(xvfb, 'start').resolves()
sinon.stub(xvfb, 'stop').resolves()
sinon.stub(xvfb, 'isNeeded').returns(false)
sinon.stub(Promise.prototype, 'delay').resolves()
sinon.stub(process, 'geteuid').returns(1000)
sinon.stub(_, 'random').returns('222')
util.exec
.withArgs(executablePath, ['--no-sandbox', '--smoke-test', '--ping=222'])
.resolves(spawnedProcess)
})
afterEach(() => {
Stdout.restore()
})
it('has verify task timeout', () => {
expect(verify.VERIFY_TEST_RUNNER_TIMEOUT_MS).to.eql(DEFAULT_VERIFY_TIMEOUT)
})
it('accepts custom verify task timeout', () => {
process.env.CYPRESS_VERIFY_TIMEOUT = '500000'
delete require.cache[require.resolve(`${lib}/tasks/verify`)]
const newVerifyInstance = require(`${lib}/tasks/verify`)
expect(newVerifyInstance.VERIFY_TEST_RUNNER_TIMEOUT_MS).to.eql(500000)
})
it('accepts custom verify task timeout from npm', () => {
process.env.npm_config_CYPRESS_VERIFY_TIMEOUT = '500000'
delete require.cache[require.resolve(`${lib}/tasks/verify`)]
const newVerifyInstance = require(`${lib}/tasks/verify`)
expect(newVerifyInstance.VERIFY_TEST_RUNNER_TIMEOUT_MS).to.eql(500000)
})
it('falls back to default verify task timeout if custom value is invalid', () => {
process.env.CYPRESS_VERIFY_TIMEOUT = 'foobar'
delete require.cache[require.resolve(`${lib}/tasks/verify`)]
const newVerifyInstance = require(`${lib}/tasks/verify`)
expect(newVerifyInstance.VERIFY_TEST_RUNNER_TIMEOUT_MS).to.eql(DEFAULT_VERIFY_TIMEOUT)
})
it('logs error and exits when no version of Cypress is installed', () => {
return verify
.start()
.then(() => {
throw new Error('should have caught error')
})
.catch((err) => {
logger.error(err)
snapshot(
'no version of Cypress installed 1',
normalize(stdout.toString()),
)
})
})
it('adds --no-sandbox when user is root', () => {
// make it think the executable exists
createfs({
alreadyVerified: false,
executable: mockfs.file({ mode: 0o777 }),
packageVersion,
})
process.geteuid.returns(0) // user is root
util.exec.resolves({
stdout: '222',
stderr: '',
})
return verify.start()
.then(() => {
expect(util.exec).to.be.calledWith(executablePath, ['--no-sandbox', '--smoke-test', '--ping=222'])
})
})
it('adds --no-sandbox when user is non-root', () => {
// make it think the executable exists
createfs({
alreadyVerified: false,
executable: mockfs.file({ mode: 0o777 }),
packageVersion,
})
process.geteuid.returns(1000) // user is non-root
util.exec.resolves({
stdout: '222',
stderr: '',
})
return verify.start()
.then(() => {
expect(util.exec).to.be.calledWith(executablePath, ['--no-sandbox', '--smoke-test', '--ping=222'])
})
})
it('is noop when binary is already verified', () => {
// make it think the executable exists and is verified
createfs({
alreadyVerified: true,
executable: mockfs.file({ mode: 0o777 }),
packageVersion,
})
return verify.start().then(() => {
// nothing should have been logged to stdout
// since no verification took place
expect(stdout.toString()).to.be.empty
expect(util.exec).not.to.be.called
})
})
it('logs warning when installed version does not match verified version', () => {
createfs({
alreadyVerified: true,
executable: mockfs.file({ mode: 0o777 }),
packageVersion: 'bloop',
})
return verify
.start()
.then(() => {
throw new Error('should have caught error')
})
.catch(() => {
return snapshot(
'warning installed version does not match verified version 1',
normalize(stdout.toString()),
)
})
})
it('logs error and exits when executable cannot be found', () => {
return verify
.start()
.then(() => {
throw new Error('should have caught error')
})
.catch((err) => {
logger.error(err)
snapshot('executable cannot be found 1', normalize(stdout.toString()))
})
})
it('logs error when child process hangs', () => {
createfs({
alreadyVerified: false,
executable: mockfs.file({ mode: 0o777 }),
packageVersion,
})
sinon.stub(cp, 'spawn').withArgs('/cache/Cypress/1.2.3/Cypress.app/Contents/MacOS/Cypress').callsFake(mockSpawn((cp) => {
cp.stderr.write('some stderr')
cp.stdout.write('some stdout')
}))
util.exec.restore()
return verify
.start({ smokeTestTimeout: 1 })
.catch((err) => {
logger.error(err)
})
.then(() => {
snapshot(normalize(slice(stdout.toString())))
})
})
it('logs error when child process returns incorrect stdout (stderr when exists)', () => {
createfs({
alreadyVerified: false,
executable: mockfs.file({ mode: 0o777 }),
packageVersion,
})
sinon.stub(cp, 'spawn').callsFake(mockSpawn((cp) => {
cp.stderr.write('some stderr')
cp.stdout.write('some stdout')
cp.emit('exit', 0, null)
cp.end()
}))
util.exec.restore()
return verify
.start()
.catch((err) => {
logger.error(err)
})
.then(() => {
snapshot(normalize(slice(stdout.toString())))
})
})
it('logs error when child process returns incorrect stdout (stdout when no stderr)', () => {
createfs({
alreadyVerified: false,
executable: mockfs.file({ mode: 0o777 }),
packageVersion,
})
sinon.stub(cp, 'spawn').callsFake(mockSpawn((cp) => {
cp.stdout.write('some stdout')
cp.emit('exit', 0, null)
cp.end()
}))
util.exec.restore()
return verify
.start()
.catch((err) => {
logger.error(err)
})
.then(() => {
snapshot(normalize(slice(stdout.toString())))
})
})
describe('FORCE_COLOR', () => {
let previousForceColors
beforeEach(() => {
previousForceColors = process.env.FORCE_COLOR
process.env.FORCE_COLOR = true
})
afterEach(() => {
process.env.FORCE_COLOR = previousForceColors
})
// @see https://github.com/cypress-io/cypress/issues/28982
it('sets FORCE_COLOR to 0 when piping stdioOptions to to the smoke test to avoid ANSI in binary smoke test', () => {
createfs({
alreadyVerified: false,
executable: mockfs.file({ mode: 0o777 }),
packageVersion,
})
util.exec.resolves({
stdout: '222',
stderr: '',
})
return verify.start()
.then(() => {
expect(util.exec).to.be.calledWith(executablePath, ['--no-sandbox', '--smoke-test', '--ping=222'],
sinon.match({
env: {
FORCE_COLOR: 0,
},
}))
})
})
})
describe('with force: true', () => {
beforeEach(() => {
createfs({
alreadyVerified: true,
executable: mockfs.file({ mode: 0o777 }),
packageVersion,
})
})
it('shows full path to executable when verifying', () => {
return verify.start({ force: true }).then(() => {
snapshot('verification with executable 1', normalize(stdout.toString()))
})
})
it('clears verified version from state if verification fails', () => {
util.exec.restore()
sinon
.stub(util, 'exec')
.withArgs(executablePath)
.rejects({
code: 1,
stderr: 'an error about dependencies',
})
return verify
.start({ force: true })
.then(() => {
throw new Error('Should have thrown')
})
.catch((err) => {
logger.error(err)
})
.then(() => {
return fs.pathExistsAsync(binaryStatePath)
})
.then((exists) => {
return expect(exists).to.eq(false)
})
.then(() => {
return snapshot(
'fails verifying Cypress 1',
normalize(slice(stdout.toString())),
)
})
})
})
describe('smoke test with DEBUG output', () => {
beforeEach(() => {
const stdoutWithDebugOutput = stripIndent`
some debug output
date: more debug output
222
after that more text
`
util.exec.withArgs(executablePath).resolves({
stdout: stdoutWithDebugOutput,
})
createfs({
alreadyVerified: false,
executable: mockfs.file({ mode: 0o777 }),
packageVersion,
})
})
it('finds ping value in the verbose output', () => {
return verify.start().then(() => {
snapshot('verbose stdout output 1', normalize(stdout.toString()))
})
})
})
describe('smoke test retries on bad display with our Xvfb', () => {
let restore
beforeEach(() => {
restore = mockedEnv({
DISPLAY: 'test-display',
})
createfs({
alreadyVerified: false,
executable: mockfs.file({ mode: 0o777 }),
packageVersion,
})
util.exec.restore()
sinon.spy(logger, 'warn')
})
afterEach(() => {
restore()
})
it('successfully retries with our Xvfb on Linux', () => {
// initially we think the user has everything set
xvfb.isNeeded.returns(false)
sinon.stub(util, 'isPossibleLinuxWithIncorrectDisplay').returns(true)
sinon.stub(util, 'exec').callsFake(() => {
const firstSpawnError = new Error('')
// this message contains typical Gtk error shown if X11 is incorrect
// like in the case of DISPLAY=987
firstSpawnError.stderr = stripIndent`
[some noise here] Gtk: cannot open display: 987
and maybe a few other lines here with weird indent
`
firstSpawnError.stdout = ''
// the second time the binary returns expected ping
util.exec.withArgs(executablePath).resolves({
stdout: '222',
})
return Promise.reject(firstSpawnError)
})
return verify.start().then(() => {
expect(util.exec).to.have.been.calledTwice
// user should have been warned
expect(logger.warn).to.have.been.calledWithMatch(
'This is likely due to a misconfigured DISPLAY environment variable.',
)
})
})
it('fails on both retries with our Xvfb on Linux', () => {
// initially we think the user has everything set
xvfb.isNeeded.returns(false)
sinon.stub(util, 'isPossibleLinuxWithIncorrectDisplay').returns(true)
sinon.stub(util, 'exec').callsFake(() => {
os.platform.returns('linux')
expect(xvfb.start).to.not.have.been.called
const firstSpawnError = new Error('')
// this message contains typical Gtk error shown if X11 is incorrect
// like in the case of DISPLAY=987
firstSpawnError.stderr = stripIndent`
[some noise here] Gtk: cannot open display: 987
and maybe a few other lines here with weird indent
`
firstSpawnError.stdout = ''
// the second time it runs, it fails for some other reason
const secondMessage = stripIndent`
[some noise here] Gtk: cannot open display: 987
some other error
again with
some weird indent
`
util.exec.withArgs(executablePath).rejects(new Error(secondMessage))
return Promise.reject(firstSpawnError)
})
return verify.start().then(() => {
throw new Error('Should have failed')
})
.catch((e) => {
expect(util.exec).to.have.been.calledTwice
// second time around we should have called Xvfb
expect(xvfb.start).to.have.been.calledOnce
expect(xvfb.stop).to.have.been.calledOnce
// user should have been warned
expect(logger.warn).to.have.been.calledWithMatch('DISPLAY was set to: "test-display"')
snapshot('tried to verify twice, on the first try got the DISPLAY error', e.message)
})
})
})
it('logs an error if Cypress executable does not exist', () => {
createfs({
alreadyVerified: false,
executable: false,
packageVersion,
})
return verify
.start()
.then(() => {
throw new Error('Should have thrown')
})
.catch((err) => {
stdout = Stdout.capture()
logger.error(err)
return snapshot('no Cypress executable 1', normalize(stdout.toString()))
})
})
it('logs an error if Cypress executable does not have permissions', () => {
mockfs.restore()
createfs({
alreadyVerified: false,
executable: mockfs.file({ mode: 0o666 }),
packageVersion,
})
return verify
.start()
.then(() => {
throw new Error('Should have thrown')
})
.catch((err) => {
stdout = Stdout.capture()
logger.error(err)
return snapshot(
'Cypress non-executable permissions 1',
normalize(stdout.toString()),
)
})
})
it('logs and runs when current version has not been verified', () => {
createfs({
alreadyVerified: false,
executable: mockfs.file({ mode: 0o777 }),
packageVersion,
})
return verify.start().then(() => {
return snapshot(
'current version has not been verified 1',
normalize(stdout.toString()),
)
})
})
it('logs and runs when installed version is different than package version', () => {
createfs({
alreadyVerified: false,
executable: mockfs.file({ mode: 0o777 }),
packageVersion: '7.8.9',
})
return verify.start().then(() => {
return snapshot(
'different version installed 1',
normalize(stdout.toString()),
)
})
})
it('is silent when logLevel is silent', () => {
createfs({
alreadyVerified: false,
executable: mockfs.file({ mode: 0o777 }),
packageVersion,
})
process.env.npm_config_loglevel = 'silent'
return verify.start().then(() => {
return snapshot(
'silent verify 1',
normalize(`[no output]${stdout.toString()}`),
)
})
})
it('turns off Opening Cypress...', () => {
createfs({
alreadyVerified: true,
executable: mockfs.file({ mode: 0o777 }),
packageVersion: '7.8.9',
})
return verify
.start({
welcomeMessage: false,
})
.then(() => {
return snapshot('no welcome message 1', normalize(stdout.toString()))
})
})
it('logs error when fails smoke test unexpectedly without stderr', () => {
createfs({
alreadyVerified: false,
executable: mockfs.file({ mode: 0o777 }),
packageVersion,
})
util.exec.restore()
sinon.stub(util, 'exec').rejects({
stderr: '',
stdout: '',
message: 'Error: EPERM NOT PERMITTED',
})
return verify
.start()
.then(() => {
throw new Error('Should have thrown')
})
.catch((err) => {
stdout = Stdout.capture()
logger.error(err)
return snapshot('fails with no stderr 1', normalize(stdout.toString()))
})
})
describe('on linux', () => {
beforeEach(() => {
xvfb.isNeeded.returns(true)
createfs({
alreadyVerified: false,
executable: mockfs.file({ mode: 0o777 }),
packageVersion,
})
})
it('starts xvfb', () => {
return verify.start().then(() => {
expect(xvfb.start).to.be.called
})
})
it('stops xvfb on spawned process close', () => {
return verify.start().then(() => {
expect(xvfb.stop).to.be.called
})
})
it('logs error and exits when starting xvfb fails', () => {
const err = new Error('test without xvfb')
xvfb.start.restore()
err.nonZeroExitCode = true
err.stack = 'xvfb? no dice'
sinon.stub(xvfb._xvfb, 'startAsync').rejects(err)
return verify.start()
.then(() => {
throw new Error('should have thrown')
})
.catch((err) => {
expect(xvfb.stop).to.be.calledOnce
logger.error(err)
snapshot('xvfb fails 1', normalize(slice(stdout.toString())))
})
})
})
describe('when running in CI', () => {
beforeEach(() => {
createfs({
alreadyVerified: false,
executable: mockfs.file({ mode: 0o777 }),
packageVersion,
})
util.isCi.returns(true)
})
it('uses verbose renderer', () => {
return verify.start().then(() => {
snapshot('verifying in ci 1', normalize(stdout.toString()))
})
})
it('logs error when binary not found', () => {
mockfs({})
return verify
.start()
.then(() => {
throw new Error('Should have thrown')
})
.catch((err) => {
logger.error(err)
snapshot('error binary not found in ci 1', normalize(stdout.toString()))
})
})
})
describe('when env var CYPRESS_RUN_BINARY', () => {
it('can validate and use executable', () => {
const envBinaryPath = '/custom/Contents/MacOS/Cypress'
const realEnvBinaryPath = `/real${envBinaryPath}`
process.env.CYPRESS_RUN_BINARY = envBinaryPath
createfs({
alreadyVerified: false,
executable: mockfs.file({ mode: 0o777 }),
packageVersion,
customDir: '/real/custom',
})
util.exec
.withArgs(realEnvBinaryPath, ['--no-sandbox', '--smoke-test', '--ping=222'])
.resolves(spawnedProcess)
return verify.start().then(() => {
expect(util.exec.firstCall.args[0]).to.equal(realEnvBinaryPath)
snapshot('valid CYPRESS_RUN_BINARY 1', normalize(stdout.toString()))
})
})
_.each(['darwin', 'linux', 'win32'], (platform) => {
return it('can log error to user', () => {
process.env.CYPRESS_RUN_BINARY = '/custom/'
os.platform.returns(platform)
return verify
.start()
.then(() => {
throw new Error('Should have thrown')
})
.catch((err) => {
logger.error(err)
snapshot(
`${platform}: error when invalid CYPRESS_RUN_BINARY 1`,
normalize(stdout.toString()),
)
})
})
})
})
// tests for when Electron needs "--no-sandbox" CLI flag
context('.needsSandbox', () => {
it('needs --no-sandbox on Linux as a root', () => {
os.platform.returns('linux')
process.geteuid.returns(0) // user is root
expect(verify.needsSandbox()).to.be.true
})
it('needs --no-sandbox on Linux as a non-root', () => {
os.platform.returns('linux')
process.geteuid.returns(1000) // user is non-root
expect(verify.needsSandbox()).to.be.true
})
it('needs --no-sandbox on Mac as a non-root', () => {
os.platform.returns('darwin')
process.geteuid.returns(1000) // user is non-root
expect(verify.needsSandbox()).to.be.true
})
it('does not need --no-sandbox on Windows', () => {
os.platform.returns('win32')
expect(verify.needsSandbox()).to.be.false
})
})
})
// TODO this needs documentation with examples badly.
function createfs ({ alreadyVerified, executable, packageVersion, customDir }) {
if (!customDir) {
customDir = '/cache/Cypress/1.2.3/Cypress.app'
}
// binary state is stored one folder higher than the runner itself
// see https://github.com/cypress-io/cypress/issues/6089
const binaryStateFolder = path.join(customDir, '..')
const binaryState = {
verified: alreadyVerified,
}
const binaryStateText = JSON.stringify(binaryState)
let mockFiles = {
[binaryStateFolder]: {
'binary_state.json': binaryStateText,
},
[customDir]: {
Contents: {
MacOS: executable
? {
Cypress: executable,
}
: {},
Resources: {
app: {
'package.json': `{"version": "${packageVersion}"}`,
},
},
},
},
}
if (customDir) {
mockFiles['/custom/Contents/MacOS/Cypress'] = mockfs.symlink({
path: '/real/custom/Contents/MacOS/Cypress',
mode: 0o777,
})
}
return mockfs(mockFiles)
}
function slice (str) {
// strip answer and split by new lines
str = str.split('\n')
// find the line about verifying cypress can run
const index = _.findIndex(str, (line) => {
return line.includes('Verifying Cypress can run')
})
// get rid of whatever the next line is because
// i cannot figure out why this line fails in CI
// its likely due to some UTF code
str.splice(index + 1, 1, 'STRIPPED')
return str.join('\n')
}