-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathhelpers.js
234 lines (186 loc) · 5.12 KB
/
helpers.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
/* eslint-disable no-console, prefer-rest-params */
const { codeFrameColumns } = require('@babel/code-frame')
const fs = require('fs-extra')
const _ = require('lodash')
const path = require('path')
const { expect } = require('chai')
const bluebird = require('bluebird')
const Debug = require('debug')
const chalk = require('chalk')
const stripAnsi = require('strip-ansi')
const cypress = require('cypress')
const debug = Debug('test')
const rootDir = process.cwd()
const cp = require('child_process')
const _spawn = cp.spawn
cp.spawn = function () {
arguments[2].stdio = 'pipe'
const ret = _spawn.apply(this, arguments)
return ret
}
afterEach(function () {
const err = this.currentTest.err
if (err) {
mapError(err)
}
})
beforeEach(function () {
this.currentTest.timeout(50000)
})
exports.runTest = async (options = {}) => {
if (!options.spec) {
throw new Error('options.spec not supplied')
}
let parsedSpecOptions = {}
if (!_.isArray(options.spec)) {
const fileStr = (await fs.readFile(options.spec)).toString()
const match = /\/\*\s*EXPECT:\s*({.*})\s*\*\//s.exec(fileStr)
if (match) {
console.log(match[1])
parsedSpecOptions = require('json5').parse(match[1])
}
}
const opts = _.defaults(options, {
spec: '',
expectedResults: {
totalFailed: 0,
},
stdoutInclude: null,
browser: 'electron',
exit: true,
})
_.merge(opts, parsedSpecOptions)
if (_.isString(opts.stdoutInclude)) {
opts.stdoutInclude = [opts.stdoutInclude]
}
console.log(chalk.cyanBright(`starting test run: ${opts.spec}`))
const stdio = captureStdio(process.stdout)
let stdout
_.extend(process.env, {
FAKE_CWD_PATH: '/[cwd]',
DEBUG_COLORS: '1',
// prevent any Compression progress
// messages from showing up
VIDEO_COMPRESSION_THROTTLE: 120000,
// don't fail our own tests running from forked PR's
CYPRESS_INTERNAL_SYSTEM_TESTS: '1',
CYPRESS_ENV: 'test',
})
return cypress.run({
spec: opts.spec,
browser: opts.browser,
exit: opts.exit,
dev: true,
})
.finally(() => {
stdout = stdio.toString()
stdio.restore()
})
.then((res) => {
expect(res).includes(opts.expectedResults)
})
.then(() => {
if (opts.stdoutInclude) {
_.forEach(opts.stdoutInclude, (v) => {
expect(stdout).include(v)
console.log(`${chalk.bold('run matched stdout:')}\n${v}`)
})
}
console.log(`${chalk.bold('run matched these results:')} ${JSON.stringify(opts.expectedResults, null, 2)}`)
})
}
const mapError = async (e) => {
const slicedStack = e.stack.split('\n') //.slice(1)
debug({ slicedStack })
const lastSrcStack = _.findIndex(
slicedStack,
(v) => !v.includes('node_modules') && v.split(path.sep).length > 2,
)
debug({ lastSrcStack })
const entryNodeModuleStack = null //slicedStack[lastSrcStack - 1]
debug({ entryNodeModuleStack })
const entryNodeModuleRE = /node_modules\/(.*?)\//.exec(
entryNodeModuleStack,
)
let entryNodeModule
if (entryNodeModuleRE) {
entryNodeModule = entryNodeModuleRE[1]
}
// debug({ entryNodeModule })
let codeFrame
debug({ stack: e.stack.split('\n'), rootDir })
const srcStackArr = await bluebird
.resolve(
e.stack
.split('\n')
.filter(
(v, i) => {
return i === 0 ||
(!v.includes('/node_modules/')) // && v.includes(rootDir))
},
),
)
.mapSeries(async (v) => {
const match = /^(\W+)(at[^(]*)\(?(.+?)(:)(\d+)(:)(\d+)(\)?)/.exec(v)
debug({ mapStack: v, match })
if (match) {
const relativePath = match[3] //path.relative(rootDir, match[3])
match[3] = relativePath
if (!codeFrame) {
codeFrame = await getCodeFrame(match)
}
match[3] = chalk.rgb(72, 160, 191)(relativePath)
return match.slice(1).join('')
}
return v
})
const srcStack = srcStackArr.join('\n')
const srcStackShort = srcStackArr.slice(1, 2).join('\n')
debug(srcStack)
console.log(chalk.dim(srcStack))
console.log(codeFrame)
console.log(`
☠️ ${
entryNodeModule ? ` [${chalk.bold(entryNodeModule)}] ` : ''
}${chalk.red(e.message)}
${srcStackShort}
`)
}
const getCodeFrame = async (info) => {
if (await fs.pathExists(info[3])) {
const location = { start: { line: +info[5], column: +info[7] } }
const rawlines = (await fs.readFile(info[3])).toString()
// .split('\n')
// .slice(location.start.line - 2, location.start.line + 2)
// .join('\n')
// debug({ path: info[1], location })
const result = codeFrameColumns(rawlines, location, {
highlightCode: true,
linesAbove: 2,
linesBelow: 3,
})
return `\n${result}`
}
}
const captureStdio = (stdio, tty) => {
let logs = []
let passThrough = null
const write = stdio.write
const isTTY = stdio.isTTY
stdio.write = function (str) {
logs.push(str)
if (passThrough) {
return write.apply(this, [passThrough(str)])
}
}
if (tty !== undefined) stdio.isTTY = tty
return {
toString: () => {
return stripAnsi(logs.join(''))
},
restore () {
stdio.write = write
stdio.isTTY = isTTY
},
}
}