◐ Shell
clean mode source ↗

test: rewrite test-child-process-spawn-args · nodejs/node@34e86f9

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);