• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2// Copyright Joyent, Inc. and other Node contributors.
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the
6// "Software"), to deal in the Software without restriction, including
7// without limitation the rights to use, copy, modify, merge, publish,
8// distribute, sublicense, and/or sell copies of the Software, and to permit
9// persons to whom the Software is furnished to do so, subject to the
10// following conditions:
11//
12// The above copyright notice and this permission notice shall be included
13// in all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
18// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21// USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23'use strict';
24require('../common');
25const assert = require('assert');
26const { internalBinding } = require('internal/test/binding');
27const Process = internalBinding('process_wrap').Process;
28const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap');
29const pipe = new Pipe(PipeConstants.SOCKET);
30const p = new Process();
31
32let processExited = false;
33let gotPipeEOF = false;
34let gotPipeData = false;
35
36p.onexit = function(exitCode, signal) {
37  console.log('exit');
38  p.close();
39  pipe.readStart();
40
41  assert.strictEqual(exitCode, 0);
42  assert.strictEqual(signal, '');
43
44  processExited = true;
45};
46
47pipe.onread = function(arrayBuffer) {
48  assert.ok(processExited);
49  if (arrayBuffer) {
50    gotPipeData = true;
51  } else {
52    gotPipeEOF = true;
53    pipe.close();
54  }
55};
56
57p.spawn({
58  file: process.execPath,
59  args: [process.execPath, '-v'],
60  stdio: [
61    { type: 'ignore' },
62    { type: 'pipe', handle: pipe },
63    { type: 'ignore' },
64  ]
65});
66
67// 'this' safety
68// https://github.com/joyent/node/issues/6690
69assert.throws(function() {
70  const notp = { spawn: p.spawn };
71  notp.spawn({
72    file: process.execPath,
73    args: [process.execPath, '-v'],
74    stdio: [
75      { type: 'ignore' },
76      { type: 'pipe', handle: pipe },
77      { type: 'ignore' },
78    ]
79  });
80}, TypeError);
81
82process.on('exit', function() {
83  assert.ok(processExited);
84  assert.ok(gotPipeEOF);
85  assert.ok(gotPipeData);
86});
87