diff --git a/src/main.ts b/src/main.ts index aa9dc0c..6f5d9ac 100644 --- a/src/main.ts +++ b/src/main.ts @@ -135,6 +135,7 @@ export class ExecProcess implements Result { protected _resolveClose?: () => void; protected _processClosed: Promise; protected _thrownError?: Error; + protected _outputPromise?: Promise; public get process(): ChildProcess | undefined { return this._process; @@ -284,7 +285,8 @@ export class ExecProcess implements Result { onfulfilled?: ((value: Output) => TResult1 | PromiseLike) | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike) | null ): Promise { - return this._waitForOutput().then(onfulfilled, onrejected); + this._outputPromise ??= this._waitForOutput(); + return this._outputPromise.then(onfulfilled, onrejected); } protected _streamOut?: Readable; @@ -360,6 +362,7 @@ export class ExecProcess implements Result { protected _resetState(): void { this._aborted = false; + this._outputPromise = undefined; this._processClosed = new Promise((resolve) => { this._resolveClose = resolve; }); diff --git a/src/test/main_test.ts b/src/test/main_test.ts index a852620..972616a 100644 --- a/src/test/main_test.ts +++ b/src/test/main_test.ts @@ -101,6 +101,64 @@ describe.for(variants)('exec ($name)', ({x, isAsync}) => { }); describe('exec (async)', () => { + test('preserves output across repeated awaits', async () => { + const proc = x(process.execPath, [ + '-e', + "console.log('stdout'); console.error('stderr')" + ]); + const expected = {stdout: 'stdout\n', stderr: 'stderr\n', exitCode: 0}; + + expect(await proc).toEqual(expected); + expect(await proc).toEqual(expected); + }); + + test('shares complete output between concurrent then handlers', async () => { + const proc = x(process.execPath, [ + '-e', + "console.log('stdout'); console.error('stderr')" + ]); + const results = await Promise.all([ + proc.then((output) => output.stdout), + proc.then((output) => output.stderr) + ]); + + expect(results).toEqual(['stdout\n', 'stderr\n']); + }); + + test('preserves failed command output across repeated awaits', async () => { + const proc = x( + process.execPath, + [ + '-e', + "console.log('stdout'); console.error('stderr'); process.exitCode = 1" + ], + {throwOnError: true} + ); + const error = await proc.then(undefined, (reason) => reason); + + expect(error).toBeInstanceOf(NonZeroExitError); + expect(error).toMatchObject({ + output: {stdout: 'stdout\n', stderr: 'stderr\n', exitCode: 1} + }); + expect(await proc.then(undefined, (reason) => reason)).toBe(error); + }); + + test('reads fresh output after spawning the process again', async () => { + const proc = new ExecProcess(process.execPath, [ + '-e', + 'process.stdout.write(String(process.pid))' + ]); + proc.spawn(); + const first = await proc; + + proc.spawn(); + const second = await proc; + + expect(second.stdout).toBe(String(proc.pid)); + expect(second.stdout).not.toBe(first.stdout); + expect(second.exitCode).toBe(0); + }); + test.each(['ignore', 'inherit'] as const)( 'async iteration completes with stdio=%s', async (stdio) => {