summaryrefslogtreecommitdiff
path: root/tests/node_compat/test/common/child_process.js
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2024-06-11 12:41:44 +0100
committerGitHub <noreply@github.com>2024-06-11 11:41:44 +0000
commit6a356aff1380e79d67738c5b43aa2b5fee76600d (patch)
treebe4aadc62a523ff280820958a1a3829f1a18ca7d /tests/node_compat/test/common/child_process.js
parent3d41b486da7dcba49c8a18b45425e356c329d986 (diff)
chore: sync up Node.js test files for v20.11.1 (#24066)
Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
Diffstat (limited to 'tests/node_compat/test/common/child_process.js')
-rw-r--r--tests/node_compat/test/common/child_process.js106
1 files changed, 98 insertions, 8 deletions
diff --git a/tests/node_compat/test/common/child_process.js b/tests/node_compat/test/common/child_process.js
index d46ec39d1..e30ec0c00 100644
--- a/tests/node_compat/test/common/child_process.js
+++ b/tests/node_compat/test/common/child_process.js
@@ -8,7 +8,9 @@
'use strict';
const assert = require('assert');
+const { spawnSync, execFileSync } = require('child_process');
const common = require('./');
+const util = require('util');
// Workaround for Windows Server 2008R2
// When CMD is used to launch a process and CMD is killed too quickly, the
@@ -20,14 +22,13 @@ function cleanupStaleProcess(filename) {
process.once('beforeExit', () => {
const basename = filename.replace(/.*[/\\]/g, '');
try {
- require('child_process')
- .execFileSync(`${process.env.SystemRoot}\\System32\\wbem\\WMIC.exe`, [
- 'process',
- 'where',
- `commandline like '%${basename}%child'`,
- 'delete',
- '/nointeractive',
- ]);
+ execFileSync(`${process.env.SystemRoot}\\System32\\wbem\\WMIC.exe`, [
+ 'process',
+ 'where',
+ `commandline like '%${basename}%child'`,
+ 'delete',
+ '/nointeractive',
+ ]);
} catch {
// Ignore failures, there might not be any stale process to clean up.
}
@@ -48,9 +49,98 @@ function logAfterTime(time) {
}, time);
}
+function checkOutput(str, check) {
+ if ((check instanceof RegExp && !check.test(str)) ||
+ (typeof check === 'string' && check !== str)) {
+ return { passed: false, reason: `did not match ${util.inspect(check)}` };
+ }
+ if (typeof check === 'function') {
+ try {
+ check(str);
+ } catch (error) {
+ return {
+ passed: false,
+ reason: `did not match expectation, checker throws:\n${util.inspect(error)}`,
+ };
+ }
+ }
+ return { passed: true };
+}
+
+function expectSyncExit(child, {
+ status,
+ signal,
+ stderr: stderrCheck,
+ stdout: stdoutCheck,
+ trim = false,
+}) {
+ const failures = [];
+ let stderrStr, stdoutStr;
+ if (status !== undefined && child.status !== status) {
+ failures.push(`- process terminated with status ${child.status}, expected ${status}`);
+ }
+ if (signal !== undefined && child.signal !== signal) {
+ failures.push(`- process terminated with signal ${child.signal}, expected ${signal}`);
+ }
+
+ function logAndThrow() {
+ const tag = `[process ${child.pid}]:`;
+ console.error(`${tag} --- stderr ---`);
+ console.error(stderrStr === undefined ? child.stderr.toString() : stderrStr);
+ console.error(`${tag} --- stdout ---`);
+ console.error(stdoutStr === undefined ? child.stdout.toString() : stdoutStr);
+ console.error(`${tag} status = ${child.status}, signal = ${child.signal}`);
+ throw new Error(`${failures.join('\n')}`);
+ }
+
+ // If status and signal are not matching expectations, fail early.
+ if (failures.length !== 0) {
+ logAndThrow();
+ }
+
+ if (stderrCheck !== undefined) {
+ stderrStr = child.stderr.toString();
+ const { passed, reason } = checkOutput(trim ? stderrStr.trim() : stderrStr, stderrCheck);
+ if (!passed) {
+ failures.push(`- stderr ${reason}`);
+ }
+ }
+ if (stdoutCheck !== undefined) {
+ stdoutStr = child.stdout.toString();
+ const { passed, reason } = checkOutput(trim ? stdoutStr.trim() : stdoutStr, stdoutCheck);
+ if (!passed) {
+ failures.push(`- stdout ${reason}`);
+ }
+ }
+ if (failures.length !== 0) {
+ logAndThrow();
+ }
+ return { child, stderr: stderrStr, stdout: stdoutStr };
+}
+
+function spawnSyncAndExit(...args) {
+ const spawnArgs = args.slice(0, args.length - 1);
+ const expectations = args[args.length - 1];
+ const child = spawnSync(...spawnArgs);
+ return expectSyncExit(child, expectations);
+}
+
+function spawnSyncAndExitWithoutError(...args) {
+ const spawnArgs = args.slice(0, args.length);
+ const expectations = args[args.length - 1];
+ const child = spawnSync(...spawnArgs);
+ return expectSyncExit(child, {
+ status: 0,
+ signal: null,
+ ...expectations,
+ });
+}
+
module.exports = {
cleanupStaleProcess,
logAfterTime,
kExpiringChildRunTime,
kExpiringParentTimer,
+ spawnSyncAndExit,
+ spawnSyncAndExitWithoutError,
};