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 9const common = require('../common'); 10const tmpdir = require('../common/tmpdir'); 11 12const assert = require('assert'); 13const { spawn } = require('child_process'); 14 15tmpdir.refresh(); 16 17const command = common.isWindows ? 'cd' : 'pwd'; 18const options = { cwd: tmpdir.path }; 19 20if (common.isWindows) { 21 // This test is not the case for Windows based systems 22 // unless the `shell` options equals to `true` 23 24 options.shell = true; 25} 26 27const testCases = [ 28 undefined, 29 null, 30 [], 31]; 32 33const expectedResult = tmpdir.path.trim().toLowerCase(); 34 35(async () => { 36 const results = await Promise.all( 37 testCases.map((testCase) => { 38 return new Promise((resolve) => { 39 const subprocess = spawn(command, testCase, options); 40 41 let accumulatedData = Buffer.alloc(0); 42 43 subprocess.stdout.on('data', common.mustCall((data) => { 44 accumulatedData = Buffer.concat([accumulatedData, data]); 45 })); 46 47 subprocess.stdout.on('end', () => { 48 resolve(accumulatedData.toString().trim().toLowerCase()); 49 }); 50 }); 51 }) 52 ); 53 54 assert.deepStrictEqual([...new Set(results)], [expectedResult]); 55})().then(common.mustCall()); 56