Skip to content

Commit 5edef6a

Browse files
committed
run_tests_in_ci.js: use node's 'spawn' module directly, rather than the 'child-process-promise' library, so that capturing output is fully controlled and does not truncate.
1 parent 8e1fbfb commit 5edef6a

File tree

1 file changed

+48
-36
lines changed

1 file changed

+48
-36
lines changed

scripts/run_tests_in_ci.js

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
const yargs = require('yargs');
1919
const path = require('path');
20-
const { spawn } = require('child-process-promise');
21-
const { writeFileSync } = require('fs');
20+
const { spawn } = require('node:child_process');
21+
const fs = require('node:fs');
2222

2323
const LOGDIR = process.env.CI ? process.env.HOME : '/tmp';
2424
// Maps the packages where we should not run `test:all` and instead isolate the cross-browser tests.
@@ -30,16 +30,17 @@ const crossBrowserPackages = {
3030
'packages/firestore-compat': 'test:browser'
3131
};
3232

33-
function writeLogs(status, name, logText) {
34-
const safeName = name.replace(/@/g, 'at_').replace(/\//g, '_');
35-
writeFileSync(path.join(LOGDIR, `${safeName}-ci-log.txt`), logText, {
36-
encoding: 'utf8'
33+
function getPathSafeName(name) {
34+
return name.replace(/@/g, 'at_').replace(/\//g, '_');
35+
}
36+
37+
async function printFile(path) {
38+
const readStream = fs.createReadStream(path);
39+
readStream.pipe(process.stdout);
40+
await new Promise((resolve, reject) => {
41+
readStream.once('end', resolve);
42+
readStream.once('error', reject);
3743
});
38-
writeFileSync(
39-
path.join(LOGDIR, `${safeName}-ci-summary.txt`),
40-
`${status}: ${name}`,
41-
{ encoding: 'utf8' }
42-
);
4344
}
4445

4546
const argv = yargs.options({
@@ -60,33 +61,44 @@ const argv = yargs.options({
6061
let scriptName = argv.s;
6162
const dir = path.resolve(myPath);
6263
const { name } = require(`${dir}/package.json`);
64+
const safeName = name.replace(/@/g, 'at_').replace(/\//g, '_');
65+
const testOutputFile = path.join(
66+
LOGDIR,
67+
`${getPathSafeName(name)}-ci-log.txt`
68+
);
6369

64-
try {
65-
if (process.env?.BROWSERS) {
66-
for (const package in crossBrowserPackages) {
67-
if (dir.endsWith(package)) {
68-
scriptName = crossBrowserPackages[package];
69-
}
70+
if (process.env?.BROWSERS) {
71+
for (const package in crossBrowserPackages) {
72+
if (dir.endsWith(package)) {
73+
scriptName = crossBrowserPackages[package];
7074
}
7175
}
72-
const testProcessResult = await spawn('yarn', ['--cwd', dir, scriptName], {
73-
capture: ['stdout', 'stderr']
74-
});
75-
console.log('Success: ' + name);
76-
writeLogs(
77-
'Success',
78-
name,
79-
testProcessResult.stdout + '\n' + testProcessResult.stderr
80-
);
81-
} catch (e) {
82-
console.error('Failure: ' + name);
83-
console.log(e.stdout);
84-
console.error(e.stderr);
85-
writeLogs('Failure', name, e.stdout + '\n' + e.stderr);
86-
87-
// NOTE: Set `process.exitCode` rather than calling `process.exit()` because
88-
// the latter will exit forcefully even if stdout/stderr have not been fully
89-
// flushed, leading to truncated output.
90-
process.exitCode = 1;
9176
}
77+
78+
const testOutputFileHandle = fs.openSync(testOutputFile, 'w');
79+
const testProcess = spawn('yarn', ['--cwd', dir, scriptName], {
80+
stdio: ['inherit', testOutputFileHandle, testOutputFileHandle]
81+
});
82+
83+
const exitCode = await new Promise((resolve, reject) => {
84+
testProcess.once('close', resolve);
85+
testProcess.once('error', reject);
86+
}).finally(() => {
87+
fs.closeSync(testOutputFileHandle);
88+
});
89+
90+
const resultStr = exitCode === 0 ? 'Success' : 'Failure';
91+
console.log(`${resultStr}: ` + name);
92+
await printFile(testOutputFile);
93+
94+
fs.writeFileSync(
95+
path.join(LOGDIR, `${getPathSafeName(name)}-ci-summary.txt`),
96+
`${resultStr}: ${name}`,
97+
{ encoding: 'utf8' }
98+
);
99+
100+
// NOTE: Set `process.exitCode` rather than calling `process.exit()` because
101+
// the latter will exit forcefully even if stdout/stderr have not been fully
102+
// flushed, leading to truncated output.
103+
process.exitCode = exitCode;
92104
})();

0 commit comments

Comments
 (0)