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
6 changes: 6 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
43 changes: 43 additions & 0 deletions src/test/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down