-
Notifications
You must be signed in to change notification settings - Fork 2
/
testutil.js
264 lines (235 loc) · 6.75 KB
/
testutil.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
import fs from "fs"
import path from "path"
import util from "util"
import process from "process"
import child_process from "child_process"
import { jest } from "@jest/globals"
import Tool from "./tool"
const execAsync = util.promisify(child_process.exec)
export class TestTool extends Tool {
static tool = "test"
static envVar = "TESTENV_ROOT"
static installer = "testenv"
}
export const testCwd = "tests-cwd"
// runAction returns a promise that wraps the subprocess action execution and
// allows for capturing error output if DEBUG is enabled
export async function runAction(name, env) {
// const index = path.join(__dirname, name + '.js')
const index = `${name}.js`
let tool = new TestTool()
env = env ? { ...process.env, ...env } : env
return tool
.subprocess(`node ../${index}`, {
env: env,
cwd: path.resolve(testCwd),
silent: true,
})
.catch((err) => {
throw new Error(
`subprocess failed code ${err.exitCode}\n${err.stdout}\n${err.stderr}`,
)
})
}
/**
* Run `name`.js in a subprocess using `env` as its environment.
*
* @param {String} name
* @param {Object} env
* @returns
*/
export async function runJS(name, env) {
const index = `${name}.js`
env = env ? { ...process.env, ...env } : env
const opts = {
env: env,
cwd: path.resolve(testCwd),
timeout: 5000,
}
return execAsync(`node ../${index}`, opts)
}
// runActionExpectError returns a promise that wraps the subprocess action
// execution and allows for capturing error output if DEBUG is enabled
export async function runActionExpectError(name, env) {
// const index = path.join(__dirname, name + '.js')
const index = `${name}.js`
let tool = new TestTool()
env = env ? { ...process.env, ...env } : env
return tool.subprocess(`node ${index}`, { env: env, silent: true })
}
/**
* Return an object with IGNORE_INSTALLED set based on the TEST_PRE_INSTALL
* environment variable to allow testing against pre-installed versions.
*/
export function ignoreInstalled() {
if (process.env.TEST_PRE_INSTALL) {
return {}
}
process.env.IGNORE_INSTALLED = "true"
return { IGNORE_INSTALLED: "true" }
}
/**
* Removes any path containing *name* from the PATH parts.
*
* @param {string} name - Name to search for in the PATH parts.
* @returns Cleaned path.
*/
export function cleanPath(name) {
if (!name) return process.env.PATH
let path = process.env.PATH
path = path.split(":")
path = path.filter((i) => !i.includes(name))
path = Array.from(new Set(path))
path = path.join(":")
return path
}
/**
* Create a Node.js version information. Every property but the version
* and the LTS info is statically set.
* @param {object} options
* @param {string} [options.lts] LTS version name
* @param {string} options.version Node.js version
* @returns {object} Node.js version information
*/
export function createNodeVersion({ lts, version }) {
return {
date: "2022-06-16",
files: [
"aix-ppc64",
"headers",
"linux-arm64",
"linux-armv7l",
"linux-ppc64le",
"linux-s390x",
"linux-x64",
"osx-arm64-tar",
"osx-x64-pkg",
"osx-x64-tar",
"src",
"win-x64-7z",
"win-x64-exe",
"win-x64-msi",
"win-x64-zip",
"win-x86-7z",
"win-x86-exe",
"win-x86-msi",
"win-x86-zip",
],
modules: "108",
name: "Node.js",
npm: "8.12.1",
openssl: "3.0.3+quic",
security: false,
url: `https://nodejs.org/download/release/${version}/`,
uv: "1.43.0",
v8: "10.2.154.4",
zlib: "1.2.11",
lts: lts || false,
version,
}
}
/**
* Helper to clean up automatically installed tools during test suites so we
* don't have collisions and false positives.
*/
const startupEnv = { ...process.env }
export class Cleaner {
constructor(tool, name, files = []) {
this.tool = tool
this._name = name
this.files = files
this.roots = []
this.origEnv = { ...startupEnv }
// this.origEnv = {...process.env}
}
get name() {
return this._name ?? this.tool?.installer
}
get envVar() {
return this.tool.envVar
}
get root() {
if (this._root) return this._root
this._root = this.tool.tempRoot()
this.roots.push(this._root)
return this._root
}
set root(val) {
this.roots.push(val)
this._root = val
}
captureRoot(out) {
if (!out) return
const re = new RegExp(`::set-env name=${this.tool.envVar}::(.*)`)
const found = out.match(re)
if (!found) return
if (found.length > 1) this._root = found[1]
}
get clean() {
return this._clean.bind(this)
}
_clean() {
for (const name of this.files) {
this.rmSafe(name)
}
while (this.roots.length) {
this.cleanRoot(this.roots.pop())
}
if (this._root) {
this.cleanRoot(this._root)
delete this._root
}
}
cleanRoot(root) {
if (!root) return
this.rmSafe(root)
this.rmSafe(path.dirname(root))
if (root?.startsWith(process.env.RUNNER_TEMP)) {
this.rmSafe(root)
}
// Restore our original environment
process.env = this.origEnv
}
rmSafe(dir) {
const re = new RegExp(`${this.tool.tool}|${this.tool.installer}`)
if (!re.test(dir)) return
if (fs.existsSync(dir)) {
try {
fs.rmSync(dir, { recursive: true })
// eslint-disable-next-line no-unused-vars
} catch (e) {
// no errors for me
}
}
}
}
/**
* Helper to silence stdout and stderr when we don't care about the output.
*
* This is necessary because the GitHub Actions toolkit functions use
* `stdout.write` directly for most of the output, and jest does not capture
* that output.
*/
export class Mute {
static all() {
// Don't mute if debug is enabled
if (process.env.RUNNER_DEBUG) return
global.beforeAll?.(Mute.std)
global.afterAll?.(Mute.reset)
}
static stdout() {
jest.spyOn(process.stdout, "write").mockImplementation(() => {})
}
static stderr() {
jest.spyOn(process.stderr, "write").mockImplementation(() => {})
}
static get std() {
return () => {
this.stdout()
this.stderr()
}
}
static get reset() {
return jest.resetModules.bind(jest)
}
}