Skip to content

Commit

Permalink
fix: propagate rejection on pipe()
Browse files Browse the repository at this point in the history
closes google#640
  • Loading branch information
antongolub committed Sep 16, 2024
1 parent 3552922 commit 112bdca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ export class ProcessPromise extends Promise<ProcessOutput> {
}
this._piped = true
if (dest instanceof ProcessPromise) {
const _reject = this._reject
this._reject = function (v) {
_reject(v)
dest._reject(v)
}
dest.stdio('pipe')
dest._prerun = this.run.bind(this)
dest._postrun = () => {
Expand Down
21 changes: 21 additions & 0 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,27 @@ describe('core', () => {
assert.equal(stdout, 'HELLO WORLD\n')
})

test('propagates rejection', async () => {
const p1 = $`exit 1`
const p2 = p1.pipe($`echo hello`)

try {
await p1
} catch (e) {
assert.equal(e.exitCode, 1)
}

try {
await p2
} catch (e) {
assert.equal(e.exitCode, 1)
}

const p3 = await $({ nothrow: true })`echo hello && exit 1`.pipe($`cat`)
assert.equal(p3.exitCode, 0)
assert.equal(p3.stdout.trim(), 'hello')
})

test('accepts Writable', async () => {
let contents = ''
let stream = new Writable({
Expand Down

0 comments on commit 112bdca

Please sign in to comment.