-
Notifications
You must be signed in to change notification settings - Fork 30.4k
/
Copy pathtest-child-process-spawnsync-shell.js
103 lines (82 loc) Β· 3.6 KB
/
test-child-process-spawnsync-shell.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Flags: --expose_internals
'use strict';
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');
const internalCp = require('internal/child_process');
const oldSpawnSync = internalCp.spawnSync;
// Verify that a shell is, in fact, executed
const doesNotExist = cp.spawnSync('does-not-exist', { shell: true });
assert.notStrictEqual(doesNotExist.file, 'does-not-exist');
assert.strictEqual(doesNotExist.error, undefined);
assert.strictEqual(doesNotExist.signal, null);
if (common.isWindows)
assert.strictEqual(doesNotExist.status, 1); // Exit code of cmd.exe
else
assert.strictEqual(doesNotExist.status, 127); // Exit code of /bin/sh
// Verify that passing arguments works
internalCp.spawnSync = common.mustCall(function(opts) {
assert.strictEqual(opts.args[opts.args.length - 1].replace(/"/g, ''),
'echo foo');
return oldSpawnSync(opts);
});
const echo = cp.spawnSync('echo', ['foo'], { shell: true });
internalCp.spawnSync = oldSpawnSync;
assert.strictEqual(echo.stdout.toString().trim(), 'foo');
// Verify that shell features can be used
const cmd = 'echo bar | cat';
const command = cp.spawnSync(cmd, { shell: true });
assert.strictEqual(command.stdout.toString().trim(), 'bar');
// Verify that the environment is properly inherited
const env = cp.spawnSync(`"${process.execPath}" -pe process.env.BAZ`, {
env: Object.assign({}, process.env, { BAZ: 'buzz' }),
shell: true
});
assert.strictEqual(env.stdout.toString().trim(), 'buzz');
// Verify that the shell internals work properly across platforms.
{
const originalComspec = process.env.comspec;
// Enable monkey patching process.platform.
const originalPlatform = process.platform;
let platform = null;
Object.defineProperty(process, 'platform', { get: () => platform });
function test(testPlatform, shell, shellOutput) {
platform = testPlatform;
const cmd = 'not_a_real_command';
const shellFlags = platform === 'win32' ? ['/d', '/s', '/c'] : ['-c'];
const outputCmd = platform === 'win32' ? `"${cmd}"` : cmd;
const windowsVerbatim = platform === 'win32' ? true : undefined;
internalCp.spawnSync = common.mustCall(function(opts) {
assert.strictEqual(opts.file, shellOutput);
assert.deepStrictEqual(opts.args,
[shellOutput, ...shellFlags, outputCmd]);
assert.strictEqual(opts.options.shell, shell);
assert.strictEqual(opts.options.file, opts.file);
assert.deepStrictEqual(opts.options.args, opts.args);
assert.strictEqual(opts.options.windowsHide, true);
assert.strictEqual(opts.options.windowsVerbatimArguments,
windowsVerbatim);
});
cp.spawnSync(cmd, { shell });
internalCp.spawnSync = oldSpawnSync;
}
// Test Unix platforms with the default shell.
test('darwin', true, '/bin/sh');
// Test Unix platforms with a user specified shell.
test('darwin', '/bin/csh', '/bin/csh');
// Test Android platforms.
test('android', true, '/system/bin/sh');
// Test Windows platforms with a user specified shell.
test('win32', 'powershell.exe', 'powershell.exe');
// Test Windows platforms with the default shell and no comspec.
delete process.env.comspec;
test('win32', true, 'cmd.exe');
// Test Windows platforms with the default shell and a comspec value.
process.env.comspec = 'powershell.exe';
test('win32', true, process.env.comspec);
// Restore the original value of process.platform.
platform = originalPlatform;
// Restore the original comspec environment variable if necessary.
if (originalComspec)
process.env.comspec = originalComspec;
}