diff --git a/src/main.ts b/src/main.ts index 0052faa..5fe1d05 100644 --- a/src/main.ts +++ b/src/main.ts @@ -209,6 +209,12 @@ export class ExecProcess implements Result { await this._processClosed; + const {stdin} = this._options; + + if (stdin && typeof stdin !== 'string') { + await stdin; + } + proc.removeAllListeners(); if (this._thrownError) { diff --git a/src/test/main_test.ts b/src/test/main_test.ts index 9507cee..38cd7b8 100644 --- a/src/test/main_test.ts +++ b/src/test/main_test.ts @@ -279,6 +279,49 @@ describe('exec (async)', () => { }).rejects.toThrow(NonZeroExitError); }); + test('async iterator throws when stdin process exits non-zero and throwOnError=true', async () => { + const source = x( + process.execPath, + ['-e', "console.log('foo'); process.exitCode = 1"], + {throwOnError: true} + ); + const destination = source.pipe(process.execPath, [ + '-e', + 'process.stdin.pipe(process.stdout)' + ]); + const lines: string[] = []; + try { + for await (const line of destination) { + lines.push(line); + } + expect.fail('Expected to throw'); + } catch (err) { + expect.assert(err instanceof NonZeroExitError); + expect(err.exitCode).toBe(1); + expect(err.result).toBe(source); + } + expect(lines).toEqual(['foo']); + expect(destination.exitCode).toBe(0); + }); + + test('async iterator does not throw when stdin process exits non-zero and throwOnError=false', async () => { + const source = x( + process.execPath, + ['-e', "console.log('foo'); process.exitCode = 1"], + {throwOnError: false} + ); + const destination = source.pipe(process.execPath, [ + '-e', + 'process.stdin.pipe(process.stdout)' + ]); + const lines: string[] = []; + for await (const line of destination) { + lines.push(line); + } + expect(lines).toEqual(['foo']); + expect(destination.exitCode).toBe(0); + }); + test('supports stdin passed as a string', async () => { let result = await x('node', ['-e', 'process.stdin.pipe(process.stdout)'], { stdin: 'foo\nbar'