Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export class ExecProcess implements Result {
protected _resolveClose?: () => void;
protected _processClosed: Promise<void>;
protected _thrownError?: Error;
protected _outputPromise?: Promise<Output>;

public get process(): ChildProcess | undefined {
return this._process;
Expand Down Expand Up @@ -284,7 +285,8 @@ export class ExecProcess implements Result {
onfulfilled?: ((value: Output) => TResult1 | PromiseLike<TResult1>) | null,
onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null
): Promise<TResult1 | TResult2> {
return this._waitForOutput().then(onfulfilled, onrejected);
this._outputPromise ??= this._waitForOutput();
return this._outputPromise.then(onfulfilled, onrejected);
}

protected _streamOut?: Readable;
Expand Down Expand Up @@ -360,6 +362,7 @@ export class ExecProcess implements Result {

protected _resetState(): void {
this._aborted = false;
this._outputPromise = undefined;
this._processClosed = new Promise<void>((resolve) => {
this._resolveClose = resolve;
});
Expand Down
58 changes: 58 additions & 0 deletions src/test/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down