Skip to content

Commit

Permalink
test: verify shell option internals
Browse files Browse the repository at this point in the history
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: nodejs#10924
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
cjihrig authored and italoacasas committed Jan 27, 2017
1 parent 94c6719 commit 0d8de77
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions test/parallel/test-child-process-spawnsync-shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,59 @@ const env = cp.spawnSync(`"${process.execPath}" -pe process.env.BAZ`, {
});

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;
const result = cp.spawnSync(cmd, { shell });

assert.strictEqual(result.file, shellOutput);
assert.deepStrictEqual(result.args,
[shellOutput, ...shellFlags, outputCmd]);
assert.strictEqual(result.options.shell, shell);
assert.strictEqual(result.options.file, result.file);
assert.deepStrictEqual(result.options.args, result.args);
assert.strictEqual(result.options.windowsVerbatimArguments,
windowsVerbatim);
}

// 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;
}

0 comments on commit 0d8de77

Please sign in to comment.