• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const child_process = require('child_process');
5const { promisify } = require('util');
6
7const exec = promisify(child_process.exec);
8const execFile = promisify(child_process.execFile);
9
10{
11  const promise = exec(`${process.execPath} -p 42`);
12
13  assert(promise.child instanceof child_process.ChildProcess);
14  promise.then(common.mustCall((obj) => {
15    assert.deepStrictEqual(obj, { stdout: '42\n', stderr: '' });
16  }));
17}
18
19{
20  const promise = execFile(process.execPath, ['-p', '42']);
21
22  assert(promise.child instanceof child_process.ChildProcess);
23  promise.then(common.mustCall((obj) => {
24    assert.deepStrictEqual(obj, { stdout: '42\n', stderr: '' });
25  }));
26}
27
28{
29  const promise = exec('doesntexist');
30
31  assert(promise.child instanceof child_process.ChildProcess);
32  promise.catch(common.mustCall((err) => {
33    assert(err.message.includes('doesntexist'));
34  }));
35}
36
37{
38  const promise = execFile('doesntexist', ['-p', '42']);
39
40  assert(promise.child instanceof child_process.ChildProcess);
41  promise.catch(common.mustCall((err) => {
42    assert(err.message.includes('doesntexist'));
43  }));
44}
45const failingCodeWithStdoutErr =
46  'console.log(42);console.error(43);process.exit(1)';
47{
48  exec(`${process.execPath} -e "${failingCodeWithStdoutErr}"`)
49    .catch(common.mustCall((err) => {
50      assert.strictEqual(err.code, 1);
51      assert.strictEqual(err.stdout, '42\n');
52      assert.strictEqual(err.stderr, '43\n');
53    }));
54}
55
56{
57  execFile(process.execPath, ['-e', failingCodeWithStdoutErr])
58    .catch(common.mustCall((err) => {
59      assert.strictEqual(err.code, 1);
60      assert.strictEqual(err.stdout, '42\n');
61      assert.strictEqual(err.stderr, '43\n');
62    }));
63}
64