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 assert = require('assert');
25const { spawn } = require('child_process');
26
27// Test stdio piping.
28{
29  const child = spawn(...common.pwdCommand, { stdio: ['pipe'] });
30  assert.notStrictEqual(child.stdout, null);
31  assert.notStrictEqual(child.stderr, null);
32}
33
34// Test stdio ignoring.
35{
36  const child = spawn(...common.pwdCommand, { stdio: 'ignore' });
37  assert.strictEqual(child.stdout, null);
38  assert.strictEqual(child.stderr, null);
39}
40
41// Asset options invariance.
42{
43  const options = { stdio: 'ignore' };
44  spawn(...common.pwdCommand, options);
45  assert.deepStrictEqual(options, { stdio: 'ignore' });
46}
47
48// Test stdout buffering.
49{
50  let output = '';
51  const child = spawn(...common.pwdCommand);
52
53  child.stdout.setEncoding('utf8');
54  child.stdout.on('data', function(s) {
55    output += s;
56  });
57
58  child.on('exit', common.mustCall(function(code) {
59    assert.strictEqual(code, 0);
60  }));
61
62  child.on('close', common.mustCall(function() {
63    assert.strictEqual(output.length > 1, true);
64    assert.strictEqual(output[output.length - 1], '\n');
65  }));
66}
67
68// Assert only one IPC pipe allowed.
69assert.throws(
70  () => {
71    spawn(
72      ...common.pwdCommand,
73      { stdio: ['pipe', 'pipe', 'pipe', 'ipc', 'ipc'] }
74    );
75  },
76  { code: 'ERR_IPC_ONE_PIPE', name: 'Error' }
77);
78