Skip to content

Commit 34e86f9

Browse files
committed
test: rewrite test-child-process-spawn-args
- Use ESM for top-level await - Avoid concurrent child processes in the test - Use `events.once` instead of Promise wrapper - Assert directly on sets PR-URL: #58546 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent f72ce2e commit 34e86f9

File tree

2 files changed

+50
-55
lines changed

2 files changed

+50
-55
lines changed

test/parallel/test-child-process-spawn-args.js

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// This test confirms that `undefined`, `null`, and `[]`
2+
// can be used as a placeholder for the second argument (`args`) of `spawn()`.
3+
// Previously, there was a bug where using `undefined` for the second argument
4+
// caused the third argument (`options`) to be ignored.
5+
// See https://github.com/nodejs/node/issues/24912.
6+
7+
import * as common from '../common/index.mjs';
8+
import tmpdir from '../common/tmpdir.js';
9+
10+
import assert from 'node:assert';
11+
import { spawn } from 'node:child_process';
12+
import { once } from 'node:events';
13+
14+
tmpdir.refresh();
15+
16+
const command = common.isWindows ? 'cd' : 'pwd';
17+
const options = { cwd: tmpdir.path };
18+
19+
if (common.isWindows) {
20+
// This test is not the case for Windows based systems
21+
// unless the `shell` options equals to `true`
22+
options.shell = true;
23+
}
24+
25+
const testCases = [
26+
undefined,
27+
null,
28+
[],
29+
];
30+
31+
const expectedResult = new Set([tmpdir.path.trim().toLowerCase()]);
32+
33+
const actualResults = new Set();
34+
35+
for (const testCase of testCases) {
36+
const subprocess = spawn(command, testCase, options);
37+
38+
let accumulatedData = '';
39+
40+
subprocess.stdout.setEncoding('utf8');
41+
subprocess.stdout.on('data', common.mustCall((data) => {
42+
accumulatedData += data;
43+
}));
44+
45+
await once(subprocess.stdout, 'end');
46+
47+
actualResults.add(accumulatedData.trim().toLowerCase());
48+
}
49+
50+
assert.deepStrictEqual(actualResults, expectedResult);

0 commit comments

Comments
 (0)