Skip to content

Commit

Permalink
fix: wait for stderr and stdout to complete
Browse files Browse the repository at this point in the history
PR-URL: nodejs/node#43666
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
MoLow authored and aduh95 committed Jul 9, 2022
1 parent e29cd3f commit bee4a6a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/internal/main/test_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ const {
ArrayPrototypeSlice,
ArrayPrototypeSort,
Promise,
PromiseAll,
SafeArrayIterator,
SafeSet
} = require('#internal/per_context/primordials')
const {
prepareMainThreadExecution
} = require('#internal/bootstrap/pre_execution')
const { spawn } = require('child_process')
const { readdirSync, statSync } = require('fs')
const { finished } = require('#internal/streams/end-of-stream')
const console = require('#internal/console/global')
const {
codes: {
Expand Down Expand Up @@ -128,9 +131,10 @@ function runTestFile (path) {
stderr += chunk
})

child.once('exit', (code, signal) => {
child.once('exit', async (code, signal) => {
if (code !== 0 || signal !== null) {
if (!err) {
await PromiseAll(new SafeArrayIterator([finished(child.stderr), finished(child.stdout)]))
err = new ERR_TEST_FAILURE('test failed', kSubtestsFailed)
err.exitCode = code
err.signal = signal
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ exports.ObjectIsExtensible = obj => Object.isExtensible(obj)
exports.ObjectPrototypeHasOwnProperty = (obj, property) => Object.prototype.hasOwnProperty.call(obj, property)
exports.ReflectApply = (target, self, args) => Reflect.apply(target, self, args)
exports.Promise = Promise
exports.PromiseAll = iterator => Promise.all(iterator)
exports.PromiseResolve = val => Promise.resolve(val)
exports.SafeArrayIterator = class ArrayIterator {constructor (array) { this.array = array }[Symbol.iterator] () { return this.array.values() }}
exports.SafeMap = Map
exports.SafeSet = Set
exports.SafeWeakMap = WeakMap
Expand Down
22 changes: 22 additions & 0 deletions lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

let finished
try {
({ finished } = require('node:stream/promises'))
} catch {
// node:stream/promises is not available on Node.js 14.x
const { finished: eos } = require('node:stream')
finished = function finished (stream, opts) {
return new Promise((resolve, reject) => {
eos(stream, opts, (err) => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
}
}

module.exports = { finished }

0 comments on commit bee4a6a

Please sign in to comment.