|
1 | 1 | /**
|
2 | 2 | * @license
|
3 |
| - * Copyright 2020 Google LLC |
| 3 | + * Copyright 2023 Google LLC |
4 | 4 | *
|
5 | 5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | 6 | * you may not use this file except in compliance with the License.
|
|
17 | 17 |
|
18 | 18 | const yargs = require('yargs');
|
19 | 19 | 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'); |
22 | 22 |
|
23 | 23 | 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` |
42 | 40 | );
|
| 41 | + writeSummaryFile(summaryFilePath, name, testProcessExitCode === 0); |
| 42 | + |
| 43 | + await printFile(logFilePath); |
| 44 | + |
| 45 | + process.exit(testProcessExitCode); |
43 | 46 | }
|
44 | 47 |
|
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); |
55 | 66 | }
|
56 |
| -}).argv; |
| 67 | +} |
57 | 68 |
|
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 | +} |
63 | 75 |
|
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' |
73 | 112 | }
|
74 |
| - const testProcess = spawn('yarn', ['--cwd', dir, scriptName]); |
| 113 | + }).argv; |
75 | 114 |
|
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 | + }; |
82 | 130 |
|
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 | + } |
92 | 137 | }
|
93 |
| -})(); |
| 138 | + |
| 139 | + return scriptName; |
| 140 | +} |
| 141 | + |
| 142 | +main(); |
0 commit comments