| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0d772a9 commit b1c5443
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,21 +1,23 @@ | |||
| 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 patch 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.prototype.spawn; | |
| 17 | 19 | ||
| 18 | - ChildProcess.prototype.spawn = function() { | ||
| 20 | + mock.method(ChildProcess.prototype, 'spawn', function() { | ||
| 19 | 21 | const err = original.apply(this, arguments); | |
| 20 | 22 | ||
| 21 | 23 | this.stdout.destroy(); | |
@@ -24,7 +26,7 @@ ChildProcess.prototype.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, opts] = common.escapePOSIXShell`"${process.execPath}" "${__filename}" child`; | |
@@ -33,32 +35,32 @@ function createChild(options, callback) { | |||
| 33 | 35 | return cp.exec(cmd, options, common.mustCall(callback)); | |
| 34 | 36 | } | |
| 35 | 37 | ||
| 36 | - // Verify that normal execution of a child process is handled. | ||
| 37 | - { | ||
| 38 | + test('normal execution of a child process is handled', (_, done) => { | ||
| 38 | 39 | createChild({}, (err, stdout, stderr) => { | |
| 39 | 40 | assert.strictEqual(err, null); | |
| 40 | 41 | assert.strictEqual(stdout, ''); | |
| 41 | 42 | assert.strictEqual(stderr, ''); | |
| 43 | + done(); | ||
| 42 | 44 | }); | |
| 43 | - } | ||
| 45 | + }); | ||
| 44 | 46 | ||
| 45 | - // Verify that execution with an error event is handled. | ||
| 46 | - { | ||
| 47 | + test('execution with an error event is handled', (_, done) => { | ||
| 47 | 48 | const error = new Error('foo'); | |
| 48 | 49 | const child = createChild({}, (err, stdout, stderr) => { | |
| 49 | 50 | assert.strictEqual(err, error); | |
| 50 | 51 | assert.strictEqual(stdout, ''); | |
| 51 | 52 | assert.strictEqual(stderr, ''); | |
| 53 | + done(); | ||
| 52 | 54 | }); | |
| 53 | 55 | ||
| 54 | 56 | child.emit('error', error); | |
| 55 | - } | ||
| 57 | + }); | ||
| 56 | 58 | ||
| 57 | - // Verify that execution with a killed process is handled. | ||
| 58 | - { | ||
| 59 | + test('execution with a killed process is handled', (_, done) => { | ||
| 59 | 60 | createChild({ timeout: 1 }, (err, stdout, stderr) => { | |
| 60 | 61 | assert.strictEqual(err.killed, true); | |
| 61 | 62 | assert.strictEqual(stdout, ''); | |
| 62 | 63 | assert.strictEqual(stderr, ''); | |
| 64 | + done(); | ||
| 63 | 65 | }); | |
| 64 | - } | ||
| 66 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments