|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// This test confirms that `undefined`, `null`, and `[]` |
| 4 | +// can be used as a placeholder for the second argument (`args`) of `spawn()`. |
| 5 | +// Previously, there was a bug where using `undefined` for the second argument |
| 6 | +// caused the third argument (`options`) to be ignored. |
| 7 | +// See https://github.com/nodejs/node/issues/24912. |
| 8 | + |
| 9 | +const assert = require('assert'); |
| 10 | +const { spawn } = require('child_process'); |
| 11 | + |
| 12 | +const common = require('../common'); |
| 13 | +const tmpdir = require('../common/tmpdir'); |
| 14 | + |
| 15 | +const command = common.isWindows ? 'cd' : 'pwd'; |
| 16 | +const options = { cwd: tmpdir.path }; |
| 17 | + |
| 18 | +if (common.isWindows) { |
| 19 | + // This test is not the case for Windows based systems |
| 20 | + // unless the `shell` options equals to `true` |
| 21 | + |
| 22 | + options.shell = true; |
| 23 | +} |
| 24 | + |
| 25 | +const testCases = [ |
| 26 | + undefined, |
| 27 | + null, |
| 28 | + [], |
| 29 | +]; |
| 30 | + |
| 31 | +const expectedResult = tmpdir.path.trim().toLowerCase(); |
| 32 | + |
| 33 | +(async () => { |
| 34 | + const results = await Promise.all( |
| 35 | + testCases.map((testCase) => { |
| 36 | + return new Promise((resolve) => { |
| 37 | + const subprocess = spawn(command, testCase, options); |
| 38 | + |
| 39 | + let accumulatedData = Buffer.alloc(0); |
| 40 | + |
| 41 | + subprocess.stdout.on('data', common.mustCall((data) => { |
| 42 | + accumulatedData = Buffer.concat([accumulatedData, data]); |
| 43 | + })); |
| 44 | + |
| 45 | + subprocess.stdout.on('end', () => { |
| 46 | + resolve(accumulatedData.toString().trim().toLowerCase()); |
| 47 | + }); |
| 48 | + }); |
| 49 | + }) |
| 50 | + ); |
| 51 | + |
| 52 | + assert.deepStrictEqual([...new Set(results)], [expectedResult]); |
| 53 | +})(); |
0 commit comments