Skip to content

Commit e80f35c

Browse files
cjihrigitaloacasas
authored andcommitted
test: verify shell option internals
This commit adds code coverage to the internals associated with the child_process shell option. This achieves the following: - Increased code coverage, which is currently only reported for Unix. - Ensures that all six code paths are covered, including the three Windows variations, and Android which is not tested at all on CI. PR-URL: #10924 Reviewed-By: Rich Trott <[email protected]>
1 parent 5a976de commit e80f35c

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

test/parallel/test-child-process-spawnsync-shell.js

+56
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,59 @@ const env = cp.spawnSync(`"${process.execPath}" -pe process.env.BAZ`, {
3535
});
3636

3737
assert.strictEqual(env.stdout.toString().trim(), 'buzz');
38+
39+
// Verify that the shell internals work properly across platforms.
40+
{
41+
const originalComspec = process.env.comspec;
42+
43+
// Enable monkey patching process.platform.
44+
const originalPlatform = process.platform;
45+
let platform = null;
46+
Object.defineProperty(process, 'platform', { get: () => platform });
47+
48+
function test(testPlatform, shell, shellOutput) {
49+
platform = testPlatform;
50+
51+
const cmd = 'not_a_real_command';
52+
const shellFlags = platform === 'win32' ? ['/d', '/s', '/c'] : ['-c'];
53+
const outputCmd = platform === 'win32' ? `"${cmd}"` : cmd;
54+
const windowsVerbatim = platform === 'win32' ? true : undefined;
55+
const result = cp.spawnSync(cmd, { shell });
56+
57+
assert.strictEqual(result.file, shellOutput);
58+
assert.deepStrictEqual(result.args,
59+
[shellOutput, ...shellFlags, outputCmd]);
60+
assert.strictEqual(result.options.shell, shell);
61+
assert.strictEqual(result.options.file, result.file);
62+
assert.deepStrictEqual(result.options.args, result.args);
63+
assert.strictEqual(result.options.windowsVerbatimArguments,
64+
windowsVerbatim);
65+
}
66+
67+
// Test Unix platforms with the default shell.
68+
test('darwin', true, '/bin/sh');
69+
70+
// Test Unix platforms with a user specified shell.
71+
test('darwin', '/bin/csh', '/bin/csh');
72+
73+
// Test Android platforms.
74+
test('android', true, '/system/bin/sh');
75+
76+
// Test Windows platforms with a user specified shell.
77+
test('win32', 'powershell.exe', 'powershell.exe');
78+
79+
// Test Windows platforms with the default shell and no comspec.
80+
delete process.env.comspec;
81+
test('win32', true, 'cmd.exe');
82+
83+
// Test Windows platforms with the default shell and a comspec value.
84+
process.env.comspec = 'powershell.exe';
85+
test('win32', true, process.env.comspec);
86+
87+
// Restore the original value of process.platform.
88+
platform = originalPlatform;
89+
90+
// Restore the original comspec environment variable if necessary.
91+
if (originalComspec)
92+
process.env.comspec = originalComspec;
93+
}

0 commit comments

Comments
 (0)