-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
verify_spec.js
496 lines (422 loc) · 12 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
require('../../spec_helper')
const _ = require('lodash')
const os = require('os')
const mockfs = require('mock-fs')
const Promise = require('bluebird')
const { stripIndent } = require('common-tags')
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 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/Cypress.app/binary_state.json'
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(_, 'random').returns('222')
util.exec
.withArgs(executablePath, ['--smoke-test', '--ping=222'])
.resolves(spawnedProcess)
})
afterEach(() => {
Stdout.restore()
})
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('is noop when binary is already verified', () => {
// make it think the executable exists and is verified
createfs({
alreadyVerified: true,
executable: mockfs.file({ mode: 0777 }),
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: 0777 }),
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()))
})
})
describe('with force: true', () => {
beforeEach(() => {
createfs({
alreadyVerified: true,
executable: mockfs.file({ mode: 0777 }),
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: 0777 }),
packageVersion,
})
})
it('finds ping value in the verbose output', () => {
return verify.start().then(() => {
snapshot('verbose stdout output 1', normalize(stdout.toString()))
})
})
})
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: 0666 }),
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: 0777 }),
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: 0777 }),
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: 0777 }),
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: 0777 }),
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: 0777 }),
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: 0777 }),
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')
err.stack = 'xvfb? no dice'
xvfb.start.rejects(err)
return verify.start().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: 0777 }),
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: 0777 }),
packageVersion,
customDir: '/real/custom',
})
util.exec
.withArgs(realEnvBinaryPath, ['--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()))
})
})
;['darwin', 'linux', 'win32'].forEach((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())
)
})
})
})
})
})
function createfs ({ alreadyVerified, executable, packageVersion, customDir }) {
let mockFiles = {
[customDir ? customDir : '/cache/Cypress/1.2.3/Cypress.app']: {
'binary_state.json': `{"verified": ${alreadyVerified}}`,
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: 0777,
})
}
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')
}