|
1 | 1 | 'use strict';
|
2 | 2 | // Flags: --expose-internals
|
3 | 3 | const common = require('../common');
|
4 |
| -const assert = require('assert'); |
5 |
| -const cp = require('child_process'); |
6 | 4 |
|
7 | 5 | if (process.argv[2] === 'child') {
|
8 | 6 | setTimeout(() => {}, common.platformTimeout(100));
|
9 | 7 | return;
|
10 | 8 | }
|
11 | 9 |
|
| 10 | +const assert = require('node:assert'); |
| 11 | +const cp = require('node:child_process'); |
| 12 | +const { mock, test } = require('node:test'); |
| 13 | +const { ChildProcess } = require('internal/child_process'); |
| 14 | + |
12 | 15 | // Monkey spawn() to create a child process normally, but destroy the
|
13 | 16 | // stdout and stderr streams. This replicates the conditions where the streams
|
14 | 17 | // cannot be properly created.
|
15 |
| -const ChildProcess = require('internal/child_process').ChildProcess; |
16 | 18 | const original = ChildProcess..spawn;
|
17 | 19 |
|
18 |
| -ChildProcess..spawn = function() { |
| 20 | +mock.method(ChildProcess., 'spawn', function() { |
19 | 21 | const err = original.apply(this, arguments);
|
20 | 22 |
|
21 | 23 | this.stdout.destroy();
|
@@ -24,40 +26,40 @@ ChildProcess..spawn = function() {
|
24 | 26 | this.stderr = null;
|
25 | 27 |
|
26 | 28 | return err;
|
27 |
| -}; |
| 29 | +}); |
28 | 30 |
|
29 | 31 | function createChild(options, callback) {
|
30 | 32 | const cmd = `"${process.execPath}" "${__filename}" child`;
|
31 | 33 |
|
32 | 34 | return cp.exec(cmd, options, common.mustCall(callback));
|
33 | 35 | }
|
34 | 36 |
|
35 |
| -// Verify that normal execution of a child process is handled. |
36 |
| -{ |
| 37 | +test('normal execution of a child process is handled', (_, done) => { |
37 | 38 | createChild({}, (err, stdout, stderr) => {
|
38 | 39 | assert.strictEqual(err, null);
|
39 | 40 | assert.strictEqual(stdout, '');
|
40 | 41 | assert.strictEqual(stderr, '');
|
| 42 | +done(); |
41 | 43 | });
|
42 |
| -} |
| 44 | +}); |
43 | 45 |
|
44 |
| -// Verify that execution with an error event is handled. |
45 |
| -{ |
| 46 | +test('execution with an error event is handled', (_, done) => { |
46 | 47 | const error = new Error('foo');
|
47 | 48 | const child = createChild({}, (err, stdout, stderr) => {
|
48 | 49 | assert.strictEqual(err, error);
|
49 | 50 | assert.strictEqual(stdout, '');
|
50 | 51 | assert.strictEqual(stderr, '');
|
| 52 | +done(); |
51 | 53 | });
|
52 | 54 |
|
53 | 55 | child.emit('error', error);
|
54 |
| -} |
| 56 | +}); |
55 | 57 |
|
56 |
| -// Verify that execution with a killed process is handled. |
57 |
| -{ |
| 58 | +test('execution with a killed process is handled', (_, done) => { |
58 | 59 | createChild({ timeout: 1 }, (err, stdout, stderr) => {
|
59 | 60 | assert.strictEqual(err.killed, true);
|
60 | 61 | assert.strictEqual(stdout, '');
|
61 | 62 | assert.strictEqual(stderr, '');
|
| 63 | +done(); |
62 | 64 | });
|
63 |
| -} |
| 65 | +}); |
0 commit comments