Commit 7a7232c

Anton Golub <antongolub@antongolub.com>
2024-09-17 16:12:36
fix: respect piped PromiseProcess nothrow option (#901)
* test: extend pipe rejection suite * chore: enhance pipe rejection hook * fix: respect piped Process nothrow option
1 parent 5038ec5
Changed files (2)
src/core.ts
@@ -478,11 +478,7 @@ 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)
-      }
+      this.catch((e) => (dest.isNothrow() ? noop : dest._reject(e)))
       dest.stdio('pipe')
       dest._prerun = this.run.bind(this)
       dest._postrun = () => {
test/core.test.js
@@ -413,6 +413,21 @@ describe('core', () => {
         const p3 = await $({ nothrow: true })`echo hello && exit 1`.pipe($`cat`)
         assert.equal(p3.exitCode, 0)
         assert.equal(p3.stdout.trim(), 'hello')
+
+        const p4 = $`exit 1`.pipe($`echo hello`)
+        try {
+          await p4
+        } catch (e) {
+          assert.equal(e.exitCode, 1)
+        }
+
+        const p5 = $`echo foo && exit 1`
+        const [r1, r2] = await Promise.allSettled([
+          p5.pipe($({ nothrow: true })`cat`),
+          p5.pipe($`cat`),
+        ])
+        assert.equal(r1.value.exitCode, 0)
+        assert.equal(r2.reason.exitCode, 1)
       })
     })