/
smoke.js
350 lines (266 loc) · 10.4 KB
/
smoke.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
const _ = require('lodash')
const fse = require('fs-extra')
const cp = require('child_process')
const execa = require('execa')
const path = require('path')
const Promise = require('bluebird')
const os = require('os')
const verify = require('../../cli/lib/tasks/verify')
const Fixtures = require('@tooling/system-tests')
const { scaffoldCommonNodeModules } = require('@tooling/system-tests/lib/dep-installer')
const fs = Promise.promisifyAll(fse)
const canRecordVideo = () => {
return os.platform() !== 'win32'
}
const shouldSkipProjectTest = () => {
return os.platform() === 'win32'
}
const runSmokeTest = function (buildAppExecutable, timeoutSeconds = 30) {
const rand = String(_.random(0, 1000))
console.log(`executable path ${buildAppExecutable}`)
console.log(`timeout ${timeoutSeconds} seconds`)
const hasRightResponse = function (stdout) {
// there could be more debug lines in the output, so find 1 line with
// expected random value
const lines = stdout.split('\n').map((s) => {
return s.trim()
})
return lines.includes(rand)
}
const args = []
if (verify.needsSandbox()) {
args.push('--no-sandbox')
}
// separate any Electron command line arguments from Cypress args
args.push('--')
args.push('--smoke-test')
args.push(`--ping=${rand}`)
const options = {
timeout: timeoutSeconds * 1000,
}
return execa(`${buildAppExecutable}`, args, options)
.catch((err) => {
console.error('smoke test failed with error %s', err.message)
throw err
}).then(({ stdout }) => {
stdout = stdout.replace(/\s/, '')
if (!hasRightResponse(stdout)) {
throw new Error(`Stdout: '${stdout}' did not match the random number: '${rand}'`)
}
console.log('smoke test response', stdout)
return console.log('smokeTest passes')
})
}
const runProjectTest = function (buildAppExecutable, e2e) {
if (shouldSkipProjectTest()) {
console.log('skipping project test')
return Promise.resolve()
}
return new Promise((resolve, reject) => {
const env = _.omit(process.env, 'CYPRESS_INTERNAL_ENV')
if (!canRecordVideo()) {
console.log('cannot record video on this platform yet, disabling')
env.CYPRESS_VIDEO_RECORDING = 'false'
}
const args = [
`--run-project=${e2e}`,
`--spec=${e2e}/cypress/e2e/simple_passing.cy.js`,
]
if (verify.needsSandbox()) {
args.push('--no-sandbox')
}
const options = {
stdio: 'inherit', env,
}
console.log('running project test')
console.log(buildAppExecutable, args.join(' '))
return cp.spawn(buildAppExecutable, args, options)
.on('exit', (code) => {
if (code === 0) {
return resolve()
}
return reject(new Error(`running project tests failed with: '${code}' errors.`))
})
})
}
const runFailingProjectTest = function (buildAppExecutable, e2e) {
if (shouldSkipProjectTest()) {
console.log('skipping failing project test')
return Promise.resolve()
}
console.log('running failing project test')
const verifyScreenshots = function () {
const screenshot1 = path.join(e2e, 'cypress', 'screenshots', 'simple_failing.cy.js', 'simple failing spec -- fails1 (failed).png')
const screenshot2 = path.join(e2e, 'cypress', 'screenshots', 'simple_failing.cy.js', 'simple failing spec -- fails2 (failed).png')
return Promise.all([
fs.statAsync(screenshot1),
fs.statAsync(screenshot2),
])
}
const spawn = () => {
return new Promise((resolve, reject) => {
const env = _.omit(process.env, 'CYPRESS_INTERNAL_ENV')
const args = [
`--run-project=${e2e}`,
`--spec=${e2e}/cypress/e2e/simple_failing.cy.js`,
]
if (verify.needsSandbox()) {
args.push('--no-sandbox')
}
const options = {
stdio: 'inherit',
env,
}
return cp.spawn(buildAppExecutable, args, options)
.on('exit', (code) => {
if (code === 2) {
return resolve()
}
return reject(new Error(`running project tests failed with: '${code}' errors.`))
})
})
}
return spawn()
.then(verifyScreenshots)
}
const runV8SnapshotProjectTest = function (buildAppExecutable, e2e) {
if (shouldSkipProjectTest()) {
console.log('skipping failing project test')
return Promise.resolve()
}
console.log('running v8 snapshot project test')
const spawn = () => {
return new Promise((resolve, reject) => {
const env = _.omit(process.env, 'CYPRESS_INTERNAL_ENV')
const args = [
`--run-project=${e2e}`,
`--spec=${e2e}/cypress/e2e/simple_v8_snapshot.cy.js`,
]
if (verify.needsSandbox()) {
args.push('--no-sandbox')
}
const options = {
stdio: 'inherit',
env,
}
return cp.spawn(buildAppExecutable, args, options)
.on('exit', (code) => {
if (code === 0) {
return resolve()
}
return reject(new Error(`running project tests failed with: '${code}' errors.`))
})
})
}
return spawn()
}
const runErroringProjectTest = function (buildAppExecutable, e2e, testName, errorMessage) {
return new Promise((resolve, reject) => {
const env = _.omit(process.env, 'CYPRESS_INTERNAL_ENV')
if (!canRecordVideo()) {
console.log('cannot record video on this platform yet, disabling')
env.CYPRESS_VIDEO_RECORDING = 'false'
}
const args = [
`--run-project=${e2e}`,
`--spec=${e2e}/cypress/e2e/simple_passing.cy.js`,
]
if (verify.needsSandbox()) {
args.push('--no-sandbox')
}
const options = {
stdio: ['inherit', 'inherit', 'pipe'], env,
}
console.log('running project test')
console.log(buildAppExecutable, args.join(' '))
const childProcess = cp.spawn(buildAppExecutable, args, options)
let errorOutput = ''
childProcess.stderr.on('data', (data) => {
errorOutput += data.toString()
})
childProcess.on('exit', (code) => {
if (code === 0) {
return reject(new Error(`running project tests should have failed for test: ${testName}`))
}
if (!errorOutput.includes(errorMessage)) {
return reject(new Error(`running project tests failed with errors: ${errorOutput} but did not include the expected error message: '${errorMessage}'`))
}
return resolve()
})
})
}
const runIntegrityTest = async function (buildAppExecutable, buildAppDir, e2e) {
const testCorruptingFile = async (file, errorMessage) => {
const contents = await fs.readFile(file)
// Backup state
await fs.move(file, `${file}.bak`)
// Modify app
await fs.writeFile(file, Buffer.concat([contents, Buffer.from(`\nconsole.log('modified code')`)]))
await runErroringProjectTest(buildAppExecutable, e2e, `corrupting ${file}`, errorMessage)
// Restore original state
await fs.move(`${file}.bak`, file, { overwrite: true })
}
await testCorruptingFile(path.join(buildAppDir, 'index.js'), 'Integrity check failed for main index.js file')
await testCorruptingFile(path.join(buildAppDir, 'packages', 'server', 'index.jsc'), 'Integrity check failed for main server index.jsc file')
const testAlteringEntryPoint = async (additionalCode, errorMessage) => {
const packageJsonContents = await fs.readJSON(path.join(buildAppDir, 'package.json'))
// Backup state
await fs.move(path.join(buildAppDir, 'package.json'), path.join(buildAppDir, 'package.json.bak'))
// Modify app
await fs.writeJSON(path.join(buildAppDir, 'package.json'), {
...packageJsonContents,
main: 'index2.js',
})
await fs.writeFile(path.join(buildAppDir, 'index2.js'), `${additionalCode}\nrequire("./index.js")`)
await runErroringProjectTest(buildAppExecutable, e2e, 'altering entry point', errorMessage)
// Restore original state
await fs.move(path.join(buildAppDir, 'package.json.bak'), path.join(buildAppDir, 'package.json'), { overwrite: true })
await fs.remove(path.join(buildAppDir, 'index2.js'))
}
await testAlteringEntryPoint('console.log("simple alteration")', 'Integrity check failed with expected stack length 9 but got 10')
await testAlteringEntryPoint('console.log("accessing " + global.getSnapshotResult())', 'getSnapshotResult can only be called once')
function compareGlobals () {
const childProcess = require('child_process')
const nodeGlobalKeys = JSON.parse(childProcess.spawnSync('node -p "const x = Object.getOwnPropertyNames(global);JSON.stringify(x)"', { shell: true, encoding: 'utf8' }).stdout)
const extraKeys = Object.getOwnPropertyNames(global).filter((key) => {
return !nodeGlobalKeys.includes(key)
})
console.error(`extra keys in electron process: ${extraKeys}`)
}
const allowList = ['regeneratorRuntime', '__core-js_shared__', 'getSnapshotResult', 'supportTypeScript']
await testAlteringEntryPoint(`(${compareGlobals.toString()})()`, `extra keys in electron process: ${allowList}\nIntegrity check failed with expected stack length 9 but got 10`)
const testTemporarilyRewritingEntryPoint = async () => {
const file = path.join(buildAppDir, 'index.js')
const backupFile = path.join(buildAppDir, 'index.js.bak')
const contents = await fs.readFile(file)
// Backup state
await fs.move(file, backupFile)
// Modify app
await fs.writeFile(file, `console.log("rewritten code");const fs=require('fs');fs.writeFileSync('${file}',fs.readFileSync('${backupFile}'));${contents}`)
await runErroringProjectTest(buildAppExecutable, e2e, 'temporarily rewriting index.js', 'Integrity check failed with expected column number 4364 but got')
// Restore original state
await fs.move(backupFile, file, { overwrite: true })
}
await testTemporarilyRewritingEntryPoint()
}
const test = async function (buildAppExecutable, buildAppDir) {
await scaffoldCommonNodeModules()
await Fixtures.scaffoldProject('e2e')
const e2e = Fixtures.projectPath('e2e')
await runSmokeTest(buildAppExecutable)
await runProjectTest(buildAppExecutable, e2e)
await runFailingProjectTest(buildAppExecutable, e2e)
if (!['1', 'true'].includes(process.env.DISABLE_SNAPSHOT_REQUIRE)) {
await runIntegrityTest(buildAppExecutable, buildAppDir, e2e)
await runV8SnapshotProjectTest(buildAppExecutable, e2e)
}
Fixtures.remove()
}
module.exports = {
test,
}
if (require.main === module) {
const buildAppExecutable = path.join(__dirname, `../../build/${os.platform()}-unpacked/Cypress`)
console.log('Script invoked directly, running smoke tests.')
test(buildAppExecutable)
}