-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathtest.js
More file actions
113 lines (106 loc) · 4.33 KB
/
test.js
File metadata and controls
113 lines (106 loc) · 4.33 KB
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
import * as nodePlatform from "../platforms/node.js"
import { instantiate } from "../instantiate.js"
import { testBrowser } from "../test.js"
import { parseArgs } from "node:util"
import path from "node:path"
import { writeFileSync } from "node:fs"
function splitArgs(args) {
// Split arguments into two parts by "--"
const part1 = []
const part2 = []
let index = 0
while (index < args.length) {
if (args[index] === "--") {
index++
break
}
part1.push(args[index])
index++
}
while (index < args.length) {
part2.push(args[index])
index++
}
return [part1, part2]
}
const [testJsArgs, testFrameworkArgs] = splitArgs(process.argv.slice(2))
const args = parseArgs({
args: testJsArgs,
options: {
prelude: { type: "string" },
environment: { type: "string" },
inspect: { type: "boolean" },
"coverage-file": { type: "string" },
},
})
const harnesses = {
node: async ({ preludeScript }) => {
try {
let options = await nodePlatform.defaultNodeSetup({
args: testFrameworkArgs,
onExit: (code) => {
// swift-testing returns EX_UNAVAILABLE (which is 69 in wasi-libc) for "no tests found"
if (code !== 0 && code !== 69) {
const stack = new Error().stack
console.error(`Test failed with exit code ${code}`)
console.error(stack)
return
}
// Extract the coverage file from the wasm module
const filePath = "default.profraw"
const destinationPath = args.values["coverage-file"] ?? filePath
const profraw = options.wasi.extractFile?.(filePath)
if (profraw) {
console.log(`Saved ${filePath} to ${destinationPath}`);
writeFileSync(destinationPath, profraw);
}
},
/* #if USE_SHARED_MEMORY */
spawnWorker: nodePlatform.createDefaultWorkerFactory(preludeScript)
/* #endif */
})
if (preludeScript) {
const prelude = await import(preludeScript)
if (prelude.setupOptions) {
options = prelude.setupOptions(options, { isMainThread: true })
}
}
process.on("beforeExit", () => {
// NOTE: "beforeExit" is fired when the process exits gracefully without calling `process.exit`
// Either XCTest or swift-testing should always call `process.exit` through `proc_exit` even
// if the test succeeds. So exiting gracefully means something went wrong (e.g. withUnsafeContinuation is leaked)
// Therefore, we exit with code 1 to indicate that the test execution failed.
console.error(`
=================================================================================================
Detected that the test execution ended without a termination signal from the testing framework.
Hint: This typically means that a continuation leak occurred.
=================================================================================================`)
process.exit(1)
})
await instantiate(options)
} catch (e) {
if (e instanceof WebAssembly.CompileError) {
// Check Node.js major version
const nodeVersion = process.versions.node.split(".")[0]
const minNodeVersion = 20
if (nodeVersion < minNodeVersion) {
console.error(`Hint: Node.js version ${nodeVersion} is not supported, please use version ${minNodeVersion} or later.`)
}
}
throw e
}
},
browser: async ({ preludeScript }) => {
process.exit(await testBrowser({ preludeScript, inspect: args.values.inspect, args: testFrameworkArgs }));
}
}
const harness = harnesses[args.values.environment ?? "node"]
if (!harness) {
console.error(`Invalid environment: ${args.values.environment}`)
process.exit(1)
}
const options = {}
if (args.values.prelude) {
options.preludeScript = path.resolve(process.cwd(), args.values.prelude)
}
await harness(options)