• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23const common = require('../common');
24const tmpdir = require('../common/tmpdir');
25tmpdir.refresh();
26
27const assert = require('assert');
28
29const { execFileSync, execSync, spawnSync } = require('child_process');
30const { getSystemErrorName } = require('util');
31
32const TIMER = 200;
33let SLEEP = 2000;
34if (common.isWindows) {
35  // Some of the windows machines in the CI need more time to launch
36  // and receive output from child processes.
37  // https://github.com/nodejs/build/issues/3014
38  SLEEP = 10000;
39}
40
41const execOpts = { encoding: 'utf8', shell: true };
42
43// Verify that stderr is not accessed when a bad shell is used
44assert.throws(
45  function() { execSync('exit -1', { shell: 'bad_shell' }); },
46  /spawnSync bad_shell ENOENT/
47);
48assert.throws(
49  function() { execFileSync('exit -1', { shell: 'bad_shell' }); },
50  /spawnSync bad_shell ENOENT/
51);
52
53let caught = false;
54let ret, err;
55const start = Date.now();
56try {
57  const cmd = `"${process.execPath}" -e "setTimeout(function(){}, ${SLEEP});"`;
58  ret = execSync(cmd, { timeout: TIMER });
59} catch (e) {
60  caught = true;
61  assert.strictEqual(getSystemErrorName(e.errno), 'ETIMEDOUT');
62  err = e;
63} finally {
64  assert.strictEqual(ret, undefined,
65                     `should not have a return value, received ${ret}`);
66  assert.ok(caught, 'execSync should throw');
67  const end = Date.now() - start;
68  assert(end < SLEEP);
69  assert(err.status > 128 || err.signal, `status: ${err.status}, signal: ${err.signal}`);
70}
71
72assert.throws(function() {
73  execSync('iamabadcommand');
74}, /Command failed: iamabadcommand/);
75
76const msg = 'foobar';
77const msgBuf = Buffer.from(`${msg}\n`);
78
79// console.log ends every line with just '\n', even on Windows.
80
81const cmd = `"${process.execPath}" -e "console.log('${msg}');"`;
82
83{
84  const ret = execSync(cmd);
85  assert.strictEqual(ret.length, msgBuf.length);
86  assert.deepStrictEqual(ret, msgBuf);
87}
88
89{
90  const ret = execSync(cmd, { encoding: 'utf8' });
91  assert.strictEqual(ret, `${msg}\n`);
92}
93
94const args = [
95  '-e',
96  `console.log("${msg}");`,
97];
98{
99  const ret = execFileSync(process.execPath, args);
100  assert.deepStrictEqual(ret, msgBuf);
101}
102
103{
104  const ret = execFileSync(process.execPath, args, { encoding: 'utf8' });
105  assert.strictEqual(ret, `${msg}\n`);
106}
107
108// Verify that the cwd option works.
109// See https://github.com/nodejs/node-v0.x-archive/issues/7824.
110{
111  const cwd = tmpdir.path;
112  const cmd = common.isWindows ? 'echo %cd%' : 'pwd';
113  const response = execSync(cmd, { cwd });
114
115  assert.strictEqual(response.toString().trim(), cwd);
116}
117
118// Verify that stderr is not accessed when stdio = 'ignore'.
119// See https://github.com/nodejs/node-v0.x-archive/issues/7966.
120{
121  assert.throws(function() {
122    execSync('exit -1', { stdio: 'ignore' });
123  }, /Command failed: exit -1/);
124}
125
126// Verify the execFileSync() behavior when the child exits with a non-zero code.
127{
128  const args = ['-e', 'process.exit(1)'];
129  const spawnSyncResult = spawnSync(process.execPath, args);
130  const spawnSyncKeys = Object.keys(spawnSyncResult).sort();
131  assert.deepStrictEqual(spawnSyncKeys, [
132    'output',
133    'pid',
134    'signal',
135    'status',
136    'stderr',
137    'stdout',
138  ]);
139
140  assert.throws(() => {
141    execFileSync(process.execPath, args);
142  }, (err) => {
143    const msg = `Command failed: ${process.execPath} ${args.join(' ')}`;
144
145    assert(err instanceof Error);
146    assert.strictEqual(err.message, msg);
147    assert.strictEqual(err.status, 1);
148    assert.strictEqual(typeof err.pid, 'number');
149    spawnSyncKeys
150      .filter((key) => key !== 'pid')
151      .forEach((key) => {
152        assert.deepStrictEqual(err[key], spawnSyncResult[key]);
153      });
154    return true;
155  });
156}
157
158// Verify the shell option works properly
159execFileSync(process.execPath, [], execOpts);
160