-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathrun-shell.mjs
More file actions
240 lines (217 loc) · 7.39 KB
/
run-shell.mjs
File metadata and controls
240 lines (217 loc) · 7.39 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
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
#! /usr/bin/env node
/*
* Copyright (C) 2025 Apple Inc. All rights reserved.
* Copyright 2025 Google LLC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
import commandLineArgs from "command-line-args";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { fileURLToPath } from "url";
import { logGroup, logInfo, printHelp, runTest, sh } from "./helper.mjs";
const FILE_PATH = fileURLToPath(import.meta.url);
const SRC_DIR = path.dirname(path.dirname(FILE_PATH));
const CLI_PATH = path.join(SRC_DIR, "cli.js");
const UNIT_TEST_PATH = path.join(SRC_DIR, "tests", "unit-tests.js");
const TESTS = [
{
name: "UnitTests",
tags: ["all", "main", "unit"],
run(shell_binary) {
return runTest("UnitTests", () => sh(shell_binary, UNIT_TEST_PATH));
},
},
{
name: "CLI Help",
tags: ["all", "main", "help"],
run(shell_binary) {
return runCLITest("Cli Help", shell_binary, "--help");
},
},
{
name: "Single Suite",
tags: ["all", "main", "single"],
run(shell_binary) {
return runCLITest("Single Suite", shell_binary, "proxy-mobx");
},
},
{
name: "Tag No Prefetch",
tags: ["all", "main", "no-prefetch"],
run(shell_binary) {
return runCLITest(
"Tag No Prefetch",
shell_binary,
"proxy",
"argon2-wasm",
"--no-prefetch"
);
},
},
{
name: "Grouped with Details",
tags: ["all", "main", "group-details"],
run(shell_binary) {
return runCLITest("Grouped with Details", shell_binary, "SunSpider", "--group-details");
},
},
{
name: "Test Flag With Positional Args",
tags: ["all", "main", "test-flag"],
run(shell_binary) {
return runCLITest("Test Flag With Positional Args", shell_binary, "--test=proxy-mobx", "proxy");
},
},
{
name: "Disabled Suite",
tags: ["all", "disabled"],
run(shell_binary) {
return runCLITest("Disabled Suite", shell_binary, "disabled");
},
},
{
name: "Default Suite",
tags: ["all", "default"],
run(shell_binary) {
return runCLITest("Default Suite", shell_binary);
},
},
];
const VALID_TAGS = Array.from(new Set(TESTS.map((each) => each.tags).flat()));
const optionDefinitions = [
{
name: "shell",
type: String,
description: "Set the shell to test, choices are [jsc, v8, spidermonkey].",
},
{ name: "help", alias: "h", description: "Print this help text." },
{
name: "suite",
type: String,
defaultOption: true,
typeLabel: `choices: ${VALID_TAGS.join(", ")}`,
description: "Run a specific suite by name.",
},
];
const options = commandLineArgs(optionDefinitions);
if ("help" in options) {
printHelp("", optionDefinitions);
}
if (options.suite && !VALID_TAGS.includes(options.suite)) {
printHelp(
`Invalid suite: ${options.suite}. Choices are: ${VALID_TAGS.join(", ")}`,
optionDefinitions
);
}
const JS_SHELL = options?.shell;
if (!JS_SHELL) {
printHelp("No javascript shell specified, use --shell", optionDefinitions);
}
const SHELL_NAME = (function () {
switch (JS_SHELL) {
case "javascriptcore":
case "jsc": {
return "javascriptcore";
}
case "spidermonkey": {
return "spidermonkey";
}
case "v8": {
return "v8";
}
default: {
printHelp(
`Invalid shell "${JS_SHELL}", choices are: "jsc", "spidermonkey" and "v8)`,
optionDefinitions
);
}
}
})();
function convertCliArgs(cli, ...cliArgs) {
if (SHELL_NAME == "spidermonkey") return [cli, ...cliArgs];
return [cli, "--", ...cliArgs];
}
async function runTests() {
const shell_binary = await logGroup(`Installing JavaScript Shell: ${SHELL_NAME}`, testSetup);
const suiteFilter = options.suite || "all";
let success = true;
const testsToRun = TESTS.filter((test) => test.tags.includes(suiteFilter));
if (testsToRun.length === 0) {
console.error(`No suite found for filter: ${suiteFilter}`);
process.exit(1);
}
for (const test of testsToRun) {
success &&= await test.run(shell_binary);
}
if (!success) {
process.exit(1);
}
}
function jsvuOSName() {
const osName = () => {
switch (os.platform()) {
case "win32":
return "win";
case "darwin":
return "mac";
case "linux":
return "linux";
default:
throw new Error("Unsupported OS");
}
};
const osArch = () => {
switch (os.arch()) {
case "x64":
return "64";
case "arm64":
return "64arm";
default:
throw new Error("Unsupported architecture");
}
};
return `${osName()}${osArch()}`;
}
const DEFAULT_JSC_LOCATION =
"/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Helpers/jsc";
async function testSetup() {
await sh("jsvu", `--engines=${SHELL_NAME}`, `--os=${jsvuOSName()}`);
let shellBinary = path.join(os.homedir(), ".jsvu/bin", SHELL_NAME);
if (!fs.existsSync(shellBinary) && SHELL_NAME == "javascriptcore")
shellBinary = DEFAULT_JSC_LOCATION;
if (!fs.existsSync(shellBinary)) throw new Error(`Could not find shell binary: ${shellBinary}`);
logInfo(`Installed JavaScript Shell: ${shellBinary}`);
return shellBinary;
}
function runCLITest(name, shellBinary, ...args) {
return runTest(name, () => runShell(shellBinary, ...convertCliArgs(CLI_PATH, ...args)));
}
async function runShell(shellBinary, ...args) {
const result = await sh(shellBinary, ...args);
// JSC does not set a non-0 exit status on async exceptions.
if (SHELL_NAME == "javascriptcore" && result.stdoutString.includes("JetStream3 failed")) {
throw new Error("test failed");
}
}
setImmediate(runTests);