Skip to content

Commit 0f78bb9

Browse files
committed
backport fix for hanging tests (#6667)
1 parent 9bb5a42 commit 0f78bb9

File tree

2 files changed

+66
-25
lines changed

2 files changed

+66
-25
lines changed

scripts/ciTest.js

+14-25
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ var fs = require("fs");
55

66
var duneBinDir = require("./dune").duneBinDir;
77

8+
const { exec } = require("./utils.js");
9+
810
var ounitTest = false;
911
var mochaTest = false;
1012
var bsbTest = false;
@@ -37,7 +39,7 @@ if (all) {
3739
formatTest = true;
3840
}
3941

40-
function runTests() {
42+
async function runTests() {
4143
if (ounitTest) {
4244
cp.execSync(path.join(duneBinDir, "ounit_tests"), {
4345
stdio: [0, 1, 2],
@@ -56,7 +58,7 @@ function runTests() {
5658
console.log("Doing build_tests");
5759
var buildTestDir = path.join(__dirname, "..", "jscomp", "build_tests");
5860
var files = fs.readdirSync(buildTestDir);
59-
files.forEach(function (file) {
61+
for (const file of files) {
6062
var testDir = path.join(buildTestDir, file);
6163
if (file === "node_modules" || !fs.lstatSync(testDir).isDirectory()) {
6264
return;
@@ -65,23 +67,18 @@ function runTests() {
6567
console.warn(`input.js does not exist in ${testDir}`);
6668
} else {
6769
console.log(`testing ${file}`);
70+
6871
// note existsSync test already ensure that it is a directory
69-
cp.exec(
70-
`node input.js`,
71-
{ cwd: testDir, encoding: "utf8" },
72-
function (error, stdout, stderr) {
73-
console.log(stdout);
72+
const out = await exec(`node`, ["input.js"], { cwd: testDir });
73+
console.log(out.stdout);
7474

75-
if (error !== null) {
76-
console.log(`❌ error in ${file} with stderr:\n`, stderr);
77-
throw error;
78-
} else {
79-
console.log("✅ success in", file);
80-
}
81-
}
82-
);
75+
if (out.code === 0) {
76+
console.log("✅ success in", file);
77+
} else {
78+
console.log(`❌ error in ${file} with stderr:\n`, out.stderr);
79+
}
8380
}
84-
});
81+
}
8582
}
8683

8784
if (formatTest) {
@@ -92,12 +89,4 @@ function runTests() {
9289
}
9390
}
9491

95-
function main() {
96-
try {
97-
runTests();
98-
} catch (err) {
99-
console.error(err);
100-
process.exit(2);
101-
}
102-
}
103-
main();
92+
runTests();

scripts/utils.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const child_process = require("child_process");
2+
3+
const signals = {
4+
SIGINT: 2,
5+
SIGQUIT: 3,
6+
SIGKILL: 9,
7+
SIGTERM: 15,
8+
};
9+
10+
/**
11+
* @param {string} command
12+
* @param {Array<string>} args
13+
* @param {child_process.SpawnOptions} [options]
14+
*/
15+
async function exec(command, args, options) {
16+
const stdoutChunks = [];
17+
const stderrChunks = [];
18+
19+
const subprocess = child_process.spawn(command, args, {
20+
stdio: ["ignore", "pipe", "pipe"],
21+
...options,
22+
});
23+
24+
subprocess.stdout.on("data", chunk => {
25+
stdoutChunks.push(chunk);
26+
});
27+
28+
subprocess.stderr.on("data", chunk => {
29+
stderrChunks.push(chunk);
30+
});
31+
32+
return await new Promise((resolve, reject) => {
33+
subprocess.once("error", err => {
34+
reject(err);
35+
});
36+
37+
subprocess.once("close", (exitCode, signal) => {
38+
const stdout = Buffer.concat(stdoutChunks).toString("utf8");
39+
const stderr = Buffer.concat(stderrChunks).toString("utf8");
40+
41+
let code = exitCode ?? 1;
42+
if (signals[signal]) {
43+
// + 128 is standard POSIX practice, see also https://nodejs.org/api/process.html#exit-codes
44+
code = signals[signal] + 128;
45+
}
46+
47+
resolve({ code, stdout, stderr });
48+
});
49+
});
50+
}
51+
52+
exports.exec = exec;

0 commit comments

Comments
 (0)