summaryrefslogtreecommitdiff
path: root/tests/node_compat/test/parallel/test-child-process-execfile.js
blob: df04e447f65e50c4714f260fb84d1b580f10d8b0 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// deno-fmt-ignore-file
// deno-lint-ignore-file

// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 18.8.0
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually

// TODO(PolarETech): The args passed to execFile() should not need to
// include "runner.ts".

// TODO(cjihrig): See inline TODO comments below.

'use strict';

const common = require('../common');
const assert = require('assert');
const { execFile, execFileSync } = require('child_process');
const { getSystemErrorName } = require('util');
const fixtures = require('../common/fixtures');
const os = require('os');

const fixture = fixtures.path('exit.js');
const echoFixture = fixtures.path('echo.js');
const execOpts = { encoding: 'utf8', shell: true };

{
  execFile(
    process.execPath,
    ['runner.ts', fixture, 42],
    common.mustCall((e) => {
      // Check that arguments are included in message
      assert.strictEqual(e.message.trim(),
                         `Command failed: ${process.execPath} runner.ts ${fixture} 42`);
      assert.strictEqual(e.code, 42);
    })
  );
}

{
  // Verify that negative exit codes can be translated to UV error names.
  const errorString = `Error: Command failed: ${process.execPath}`;
  const code = -1;
  const callback = common.mustCall((err, stdout, stderr) => {
    assert.strictEqual(err.toString().trim(), errorString);
    assert.strictEqual(err.code, getSystemErrorName(code));
    assert.strictEqual(err.killed, true);
    assert.strictEqual(err.signal, null);
    assert.strictEqual(err.cmd, process.execPath);
    assert.strictEqual(stdout.trim(), '');
    assert.strictEqual(stderr.trim(), '');
  });
  const child = execFile(process.execPath, callback);

  child.kill();
  child.emit('close', code, null);
}

{
  // Verify the shell option works properly
  execFile(process.execPath, ['runner.ts', fixture, 0], execOpts, common.mustSucceed());
}

{
  // Verify that the signal option works properly
  const ac = new AbortController();
  const { signal } = ac;

  const test = () => {
    const check = common.mustCall((err) => {
      assert.strictEqual(err.code, 'ABORT_ERR');
      assert.strictEqual(err.name, 'AbortError');
      assert.strictEqual(err.signal, undefined);
    });
    execFile(process.execPath, ['runner.ts', echoFixture, 0], { signal }, check);
  };

  // Verify that it still works the same way now that the signal is aborted.
  test();
  ac.abort();
}

{
  // Verify that does not spawn a child if already aborted
  const signal = AbortSignal.abort();

  const check = common.mustCall((err) => {
    assert.strictEqual(err.code, 'ABORT_ERR');
    assert.strictEqual(err.name, 'AbortError');
    assert.strictEqual(err.signal, undefined);
  });
  execFile(process.execPath, ['runner.ts', echoFixture, 0], { signal }, check);
}

{
  // Verify that if something different than Abortcontroller.signal
  // is passed, ERR_INVALID_ARG_TYPE is thrown
  assert.throws(() => {
    const callback = common.mustNotCall(() => {});

    execFile(process.execPath, ['runner.ts', echoFixture, 0], { signal: 'hello' }, callback);
  }, { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' });
}
{
  // Verify that the process completing removes the abort listener
  const ac = new AbortController();
  const { signal } = ac;

  const callback = common.mustCall((err) => {
    // TODO(cjihrig): The assertion on the next line currently fails because
    // kEvents is not defined. See TODO comment in _events.mjs.
    // assert.strictEqual(getEventListeners(ac.signal).length, 0);
    assert.strictEqual(err, null);
  });
  execFile(process.execPath, ['runner.ts', fixture, 0], { signal }, callback);
}

// Verify the execFile() stdout is the same as execFileSync().
{
  const file = 'echo';
  const args = ['foo', 'bar'];

  // Test with and without `{ shell: true }`
  [
    // Skipping shell-less test on Windows because its echo command is a shell built-in command.
    ...(common.isWindows ? [] : [{ encoding: 'utf8' }]),
    { shell: true, encoding: 'utf8' },
  ].forEach((options) => {
    const execFileSyncStdout = execFileSync(file, args, options);
    assert.strictEqual(execFileSyncStdout, `foo bar${os.EOL}`);

    execFile(file, args, options, common.mustCall((_, stdout) => {
      assert.strictEqual(stdout, execFileSyncStdout);
    }));
  });
}