-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (70 loc) · 2.31 KB
/
Copy pathindex.js
File metadata and controls
79 lines (70 loc) · 2.31 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
import { execFileSync, execSync, spawnSync } from "child_process";
import * as fs from "fs";
import { fileURLToPath, pathToFileURL } from "url";
import * as path from "path";
import { exit } from "process";
const currentFileName = fileURLToPath(import.meta.url);
const currentDir = path.dirname(currentFileName);
const repoRoot = path.resolve(currentDir, "..");
const testsDir = path.join(repoRoot, "tests");
const rescriptConfig = JSON.parse(fs.readFileSync(path.join(repoRoot, "rescript.json"), "utf8"));
const compiledSuffix = rescriptConfig.suffix ?? ".js";
const runtimeTests = [
"FetchAPI/Headers__test.res",
"FetchAPI/Request__test.res",
"FetchAPI/Response__test.res",
"FetchAPI/URLSearchParams__test.res",
"URLAPI/URL__test.res",
];
const compiledRuntimeTests = runtimeTests.map((testFile) =>
testFile.replace(/\.res$/, compiledSuffix),
);
// Compile all tests
execSync("npm run build", { cwd: repoRoot, stdio: "inherit" });
const successGreen = "\x1b[32m";
const warningYellow = "\x1b[33m";
const errorRed = "\x1b[31m";
const resetColor = "\x1b[0m";
for (const testFile of compiledRuntimeTests) {
const absoluteTestFile = path.join(testsDir, testFile);
const result = spawnSync(
process.execPath,
[
"--input-type=module",
"--eval",
`await import(${JSON.stringify(pathToFileURL(absoluteTestFile).href)})`,
],
{
cwd: repoRoot,
encoding: "utf8",
},
);
if (result.status !== 0) {
console.log(`${errorRed}❌ Runtime test failed: ${testFile}${resetColor}`);
if (result.stdout) {
console.log(result.stdout);
}
if (result.stderr) {
console.error(result.stderr);
}
exit(result.status ?? 1);
}
console.log(`${successGreen}✓ ${testFile}${resetColor}`);
}
// Assert generated test output stayed in sync.
const gitDiff = execFileSync("git", ["ls-files", "--modified", "--", ...compiledRuntimeTests], {
cwd: testsDir,
}).toString();
if (!gitDiff) {
console.log(`${successGreen}✅ No unstaged generated test difference!${resetColor}`);
exit(0);
} else {
console.log(
`${warningYellow}⚠️ There are unstaged differences in generated tests! Did you break a test?\n${gitDiff}${resetColor}`,
);
execFileSync("git", ["--no-pager", "diff", "--", ...compiledRuntimeTests], {
stdio: "inherit",
cwd: testsDir,
});
exit(1);
}