11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { ChildProcess } = require('child_process');
61cb0ef41Sopenharmony_ciassert.strictEqual(typeof ChildProcess, 'function');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci{
91cb0ef41Sopenharmony_ci  // Verify that invalid options to spawn() throw.
101cb0ef41Sopenharmony_ci  const child = new ChildProcess();
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci  [undefined, null, 'foo', 0, 1, NaN, true, false].forEach((options) => {
131cb0ef41Sopenharmony_ci    assert.throws(() => {
141cb0ef41Sopenharmony_ci      child.spawn(options);
151cb0ef41Sopenharmony_ci    }, {
161cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
171cb0ef41Sopenharmony_ci      name: 'TypeError',
181cb0ef41Sopenharmony_ci      message: 'The "options" argument must be of type object.' +
191cb0ef41Sopenharmony_ci               `${common.invalidArgTypeHelper(options)}`
201cb0ef41Sopenharmony_ci    });
211cb0ef41Sopenharmony_ci  });
221cb0ef41Sopenharmony_ci}
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci{
251cb0ef41Sopenharmony_ci  // Verify that spawn throws if file is not a string.
261cb0ef41Sopenharmony_ci  const child = new ChildProcess();
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  [undefined, null, 0, 1, NaN, true, false, {}].forEach((file) => {
291cb0ef41Sopenharmony_ci    assert.throws(() => {
301cb0ef41Sopenharmony_ci      child.spawn({ file });
311cb0ef41Sopenharmony_ci    }, {
321cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
331cb0ef41Sopenharmony_ci      name: 'TypeError',
341cb0ef41Sopenharmony_ci      message: 'The "options.file" property must be of type string.' +
351cb0ef41Sopenharmony_ci               `${common.invalidArgTypeHelper(file)}`
361cb0ef41Sopenharmony_ci    });
371cb0ef41Sopenharmony_ci  });
381cb0ef41Sopenharmony_ci}
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci{
411cb0ef41Sopenharmony_ci  // Verify that spawn throws if envPairs is not an array or undefined.
421cb0ef41Sopenharmony_ci  const child = new ChildProcess();
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  [null, 0, 1, NaN, true, false, {}, 'foo'].forEach((envPairs) => {
451cb0ef41Sopenharmony_ci    assert.throws(() => {
461cb0ef41Sopenharmony_ci      child.spawn({ envPairs, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] });
471cb0ef41Sopenharmony_ci    }, {
481cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
491cb0ef41Sopenharmony_ci      name: 'TypeError',
501cb0ef41Sopenharmony_ci      message: 'The "options.envPairs" property must be an instance of Array.' +
511cb0ef41Sopenharmony_ci              common.invalidArgTypeHelper(envPairs)
521cb0ef41Sopenharmony_ci    });
531cb0ef41Sopenharmony_ci  });
541cb0ef41Sopenharmony_ci}
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci{
571cb0ef41Sopenharmony_ci  // Verify that spawn throws if args is not an array or undefined.
581cb0ef41Sopenharmony_ci  const child = new ChildProcess();
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  [null, 0, 1, NaN, true, false, {}, 'foo'].forEach((args) => {
611cb0ef41Sopenharmony_ci    assert.throws(() => {
621cb0ef41Sopenharmony_ci      child.spawn({ file: 'foo', args });
631cb0ef41Sopenharmony_ci    }, {
641cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
651cb0ef41Sopenharmony_ci      name: 'TypeError',
661cb0ef41Sopenharmony_ci      message: 'The "options.args" property must be an instance of Array.' +
671cb0ef41Sopenharmony_ci               common.invalidArgTypeHelper(args)
681cb0ef41Sopenharmony_ci    });
691cb0ef41Sopenharmony_ci  });
701cb0ef41Sopenharmony_ci}
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci// Test that we can call spawn
731cb0ef41Sopenharmony_ciconst child = new ChildProcess();
741cb0ef41Sopenharmony_cichild.spawn({
751cb0ef41Sopenharmony_ci  file: process.execPath,
761cb0ef41Sopenharmony_ci  args: ['--interactive'],
771cb0ef41Sopenharmony_ci  cwd: process.cwd(),
781cb0ef41Sopenharmony_ci  stdio: 'pipe'
791cb0ef41Sopenharmony_ci});
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ciassert.strictEqual(Object.hasOwn(child, 'pid'), true);
821cb0ef41Sopenharmony_ciassert(Number.isInteger(child.pid));
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci// Try killing with invalid signal
851cb0ef41Sopenharmony_ciassert.throws(
861cb0ef41Sopenharmony_ci  () => { child.kill('foo'); },
871cb0ef41Sopenharmony_ci  { code: 'ERR_UNKNOWN_SIGNAL', name: 'TypeError' }
881cb0ef41Sopenharmony_ci);
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ciassert.strictEqual(child.kill(), true);
91