• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2// Flags: --expose-internals
3
4const common = require('../common');
5const assert = require('assert');
6const getValidStdio = require('internal/child_process').getValidStdio;
7
8const expectedError = { code: 'ERR_INVALID_OPT_VALUE', name: 'TypeError' };
9
10// Should throw if string and not ignore, pipe, or inherit
11assert.throws(() => getValidStdio('foo'), expectedError);
12
13// Should throw if not a string or array
14assert.throws(() => getValidStdio(600), expectedError);
15
16// Should populate stdio with undefined if len < 3
17{
18  const stdio1 = [];
19  const result = getValidStdio(stdio1, false);
20  assert.strictEqual(stdio1.length, 3);
21  assert.strictEqual(result.hasOwnProperty('stdio'), true);
22  assert.strictEqual(result.hasOwnProperty('ipc'), true);
23  assert.strictEqual(result.hasOwnProperty('ipcFd'), true);
24}
25
26// Should throw if stdio has ipc and sync is true
27const stdio2 = ['ipc', 'ipc', 'ipc'];
28assert.throws(() => getValidStdio(stdio2, true),
29              { code: 'ERR_IPC_SYNC_FORK', name: 'Error' }
30);
31
32// Should throw if stdio is not a valid input
33{
34  const stdio = ['foo'];
35  assert.throws(() => getValidStdio(stdio, false),
36                { code: 'ERR_INVALID_SYNC_FORK_INPUT', name: 'TypeError' }
37  );
38}
39
40// Should throw if stdio is not a valid option
41{
42  const stdio = [{ foo: 'bar' }];
43  assert.throws(() => getValidStdio(stdio), expectedError);
44}
45
46if (common.isMainThread) {
47  const stdio3 = [process.stdin, process.stdout, process.stderr];
48  const result = getValidStdio(stdio3, false);
49  assert.deepStrictEqual(result, {
50    stdio: [
51      { type: 'fd', fd: 0 },
52      { type: 'fd', fd: 1 },
53      { type: 'fd', fd: 2 },
54    ],
55    ipc: undefined,
56    ipcFd: undefined
57  });
58} else {
59  common.printSkipMessage(
60    'stdio is not associated with file descriptors in Workers');
61}
62