Skip to content

Commit 70a9e99

Browse files
committed
replace run_tests_in_ci.js
1 parent c2b7d7f commit 70a9e99

File tree

4 files changed

+114
-207
lines changed

4 files changed

+114
-207
lines changed

packages/firestore/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"test:lite:browser:nameddb": "karma start --single-run --lite --databaseId=test-db",
2929
"test:lite:browser:debug": "karma start --browsers=Chrome --lite --auto-watch",
3030
"test": "run-s lint test:all",
31-
"test:ci": "node ../../scripts/run_tests_in_ci2.js -s test:all:ci",
31+
"test:ci": "node ../../scripts/run_tests_in_ci.js -s test:all:ci",
3232
"test:all:ci": "run-s test:browser test:travis test:lite:browser test:browser:prod:nameddb test:lite:browser:nameddb",
3333
"test:all": "run-p test:browser test:lite:browser test:travis test:minified test:browser:prod:nameddb test:lite:browser:nameddb",
3434
"test:browser": "karma start --single-run",

packages/firestore/test/integration/api/aggregation.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ apiDescribe('Count queries', persistence => {
5656

5757
it("count query doesn't use converter", () => {
5858
const testDocs = {
59-
a: { author: 'authorA', title: 'titleA' },
59+
a: { author: 'authorA', title: 'titleX' },
6060
b: { author: 'authorB', title: 'titleB' }
6161
};
6262
const throwingConverter = {

scripts/run_tests_in_ci.js

Lines changed: 112 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2020 Google LLC
3+
* Copyright 2023 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -17,77 +17,126 @@
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 child_process = require('node:child_process');
21+
const fs = require('node:fs');
2222

2323
const LOGDIR = process.env.CI ? process.env.HOME : '/tmp';
24-
// Maps the packages where we should not run `test:all` and instead isolate the cross-browser tests.
25-
// TODO(dwyfrequency): Update object with `storage` and `firestore` packages.
26-
const crossBrowserPackages = {
27-
'packages/auth': 'test:browser:unit',
28-
'packages/auth-compat': 'test:browser:unit',
29-
'packages/firestore': 'test:browser:unit',
30-
'packages/firestore-compat': 'test:browser'
31-
};
32-
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'
37-
});
38-
writeFileSync(
39-
path.join(LOGDIR, `${safeName}-ci-summary.txt`),
40-
`${status}: ${name}`,
41-
{ encoding: 'utf8' }
24+
25+
async function main() {
26+
const { scriptName, workingDir } = parseArgs();
27+
const { name } = require(`${workingDir}/package.json`);
28+
logPrefix = name;
29+
30+
const logFilePath = path.join(LOGDIR, `${makeSafePath(name)}-ci-log.txt`);
31+
const testProcessExitCode = await runTestProcess(
32+
workingDir,
33+
scriptName,
34+
logFilePath
35+
);
36+
37+
const summaryFilePath = path.join(
38+
LOGDIR,
39+
`${makeSafePath(name)}-ci-summary.txt`
4240
);
41+
writeSummaryFile(summaryFilePath, name, testProcessExitCode === 0);
42+
43+
await printFile(logFilePath);
44+
45+
process.exit(testProcessExitCode);
4346
}
4447

45-
const argv = yargs.options({
46-
d: {
47-
type: 'string',
48-
desc: 'current working directory',
49-
default: '.'
50-
},
51-
s: {
52-
type: 'string',
53-
desc: 'the npm script to run',
54-
default: 'test'
48+
async function runTestProcess(workingDir, scriptName, logFilePath) {
49+
log(`Saving test process output to file: ${logFilePath}`);
50+
const logFileHandle = fs.openSync(logFilePath, 'w');
51+
try {
52+
const args = ['yarn', '--cwd', workingDir, scriptName];
53+
log(`Starting test process: ${args.join(' ')}`);
54+
const proc = child_process.spawn(args[0], args.splice(1), {
55+
stdio: ['inherit', logFileHandle, logFileHandle]
56+
});
57+
proc.once('spawn', () => log(`Started test process with PID: ${proc.pid}`));
58+
const exitCode = await new Promise((resolve, reject) => {
59+
proc.once('close', resolve);
60+
proc.once('error', reject);
61+
});
62+
log(`Test process completed with exit code: ${exitCode}`);
63+
return exitCode === 0 ? true : false;
64+
} finally {
65+
fs.close(logFileHandle);
5566
}
56-
}).argv;
67+
}
5768

58-
(async () => {
59-
const myPath = argv.d;
60-
let scriptName = argv.s;
61-
const dir = path.resolve(myPath);
62-
const { name } = require(`${dir}/package.json`);
69+
function writeSummaryFile(summaryFilePath, name, testProcessSuccessful) {
70+
const statusString = testProcessSuccessful ? 'Success' : 'Failure';
71+
const line = `${statusString}: ${name}`;
72+
log(`Writing summary to file ${summaryFilePath}: ${line}`);
73+
fs.writeFileSync(summaryFilePath, line, { encoding: 'utf8' });
74+
}
6375

64-
let stdout = '';
65-
let stderr = '';
66-
try {
67-
if (process.env?.BROWSERS) {
68-
for (const package in crossBrowserPackages) {
69-
if (dir.endsWith(package)) {
70-
scriptName = crossBrowserPackages[package];
71-
}
72-
}
76+
async function printFile(path) {
77+
log('========================================================');
78+
log(`==== BEGIN ${path}`);
79+
log('========================================================');
80+
const readStream = fs.createReadStream(path);
81+
readStream.pipe(process.stdout);
82+
await new Promise((resolve, reject) => {
83+
readStream.once('end', resolve);
84+
readStream.once('error', reject);
85+
});
86+
log('========================================================');
87+
log(`==== END ${path}`);
88+
log('========================================================');
89+
}
90+
91+
let logPrefix = '';
92+
93+
function log() {
94+
console.log('run_tests_in_ci.js', logPrefix, ...arguments);
95+
}
96+
97+
function makeSafePath(s) {
98+
return s.replace(/@/g, 'at_').replace(/\//g, '_');
99+
}
100+
101+
function parseArgs() {
102+
const argv = yargs.options({
103+
d: {
104+
type: 'string',
105+
desc: 'current working directory',
106+
default: '.'
107+
},
108+
s: {
109+
type: 'string',
110+
desc: 'the npm script to run',
111+
default: 'test'
73112
}
74-
const testProcess = spawn('yarn', ['--cwd', dir, scriptName]);
113+
}).argv;
75114

76-
testProcess.childProcess.stdout.on('data', data => {
77-
stdout += data.toString();
78-
});
79-
testProcess.childProcess.stderr.on('data', data => {
80-
stderr += data.toString();
81-
});
115+
return {
116+
scriptName: resolveScriptNameArg(argv.s),
117+
workingDir: path.resolve(argv.d)
118+
};
119+
}
120+
121+
function resolveScriptNameArg(scriptName) {
122+
// Maps the packages where we should not run `test:all` and instead isolate the cross-browser tests.
123+
// TODO(dwyfrequency): Update object with `storage` and `firestore` packages.
124+
const crossBrowserPackages = {
125+
'packages/auth': 'test:browser:unit',
126+
'packages/auth-compat': 'test:browser:unit',
127+
'packages/firestore': 'test:browser:unit',
128+
'packages/firestore-compat': 'test:browser'
129+
};
82130

83-
await testProcess;
84-
console.log('Success: ' + name);
85-
writeLogs('Success', name, stdout + '\n' + stderr);
86-
} catch (e) {
87-
console.error('Failure: ' + name);
88-
console.log(stdout);
89-
console.error(stderr);
90-
writeLogs('Failure', name, stdout + '\n' + stderr);
91-
process.exit(1);
131+
if (process.env?.BROWSERS) {
132+
for (const package in crossBrowserPackages) {
133+
if (dir.endsWith(package)) {
134+
scriptName = crossBrowserPackages[package];
135+
}
136+
}
92137
}
93-
})();
138+
139+
return scriptName;
140+
}
141+
142+
main();

scripts/run_tests_in_ci2.js

Lines changed: 0 additions & 142 deletions
This file was deleted.

0 commit comments

Comments
 (0)