11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Flags: --expose-internals
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst cp = require('child_process');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
81cb0ef41Sopenharmony_ci  setTimeout(() => {}, common.platformTimeout(100));
91cb0ef41Sopenharmony_ci  return;
101cb0ef41Sopenharmony_ci}
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci// Monkey patch spawn() to create a child process normally, but destroy the
131cb0ef41Sopenharmony_ci// stdout and stderr streams. This replicates the conditions where the streams
141cb0ef41Sopenharmony_ci// cannot be properly created.
151cb0ef41Sopenharmony_ciconst ChildProcess = require('internal/child_process').ChildProcess;
161cb0ef41Sopenharmony_ciconst original = ChildProcess.prototype.spawn;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciChildProcess.prototype.spawn = function() {
191cb0ef41Sopenharmony_ci  const err = original.apply(this, arguments);
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  this.stdout.destroy();
221cb0ef41Sopenharmony_ci  this.stderr.destroy();
231cb0ef41Sopenharmony_ci  this.stdout = null;
241cb0ef41Sopenharmony_ci  this.stderr = null;
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  return err;
271cb0ef41Sopenharmony_ci};
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_cifunction createChild(options, callback) {
301cb0ef41Sopenharmony_ci  const cmd = `"${process.execPath}" "${__filename}" child`;
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  return cp.exec(cmd, options, common.mustCall(callback));
331cb0ef41Sopenharmony_ci}
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci// Verify that normal execution of a child process is handled.
361cb0ef41Sopenharmony_ci{
371cb0ef41Sopenharmony_ci  createChild({}, (err, stdout, stderr) => {
381cb0ef41Sopenharmony_ci    assert.strictEqual(err, null);
391cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, '');
401cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
411cb0ef41Sopenharmony_ci  });
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci// Verify that execution with an error event is handled.
451cb0ef41Sopenharmony_ci{
461cb0ef41Sopenharmony_ci  const error = new Error('foo');
471cb0ef41Sopenharmony_ci  const child = createChild({}, (err, stdout, stderr) => {
481cb0ef41Sopenharmony_ci    assert.strictEqual(err, error);
491cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, '');
501cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
511cb0ef41Sopenharmony_ci  });
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  child.emit('error', error);
541cb0ef41Sopenharmony_ci}
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci// Verify that execution with a killed process is handled.
571cb0ef41Sopenharmony_ci{
581cb0ef41Sopenharmony_ci  createChild({ timeout: 1 }, (err, stdout, stderr) => {
591cb0ef41Sopenharmony_ci    assert.strictEqual(err.killed, true);
601cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, '');
611cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, '');
621cb0ef41Sopenharmony_ci  });
631cb0ef41Sopenharmony_ci}
64