Skip to content

Commit

Permalink
Fix small issue when iterating and stdout option is ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Sep 4, 2024
1 parent 9f89dde commit beb2760
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
11 changes: 6 additions & 5 deletions source/iterable.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ export const lineIterator = async function * (resultPromise, {state}, streamName
}

state.isIterating = true;
const instance = await resultPromise.nodeChildProcess;
const stream = instance[streamName];
if (!stream) {
return;
}

try {
const instance = await resultPromise.nodeChildProcess;
const stream = instance[streamName];
if (!stream) {
return;
}

let buffer = '';
for await (const chunk of stream.iterator({destroyOnReturn: false})) {
const lines = `${buffer}${chunk}`.split(/\r?\n/);
Expand Down
18 changes: 17 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ const nodePrintStderr = nodeEval(`console.error("${testString}")`);
const nodePrintBoth = nodeEval(`console.log("${testString}");
setTimeout(() => {
console.error("${secondTestString}");
}, 0)`);
}, 0);`);
const nodePrintBothFail = nodeEval(`console.log("${testString}");
setTimeout(() => {
console.error("${secondTestString}");
process.exit(2);
}, 0);`);
const nodePrintFail = nodeEval(`console.log("${testString}");
process.exit(2);`);
const nodePrintSleep = nodeEval(`setTimeout(() => {
Expand Down Expand Up @@ -483,6 +488,17 @@ test('promise[Symbol.asyncIterator] has no iterations if only options.stdout + o
t.is(output, '');
});

test('promise.stdout has no iterations but waits for the subprocess if options.stdout "ignore"', async t => {
const promise = nanoSpawn(...nodePrintBothFail, {stdout: 'ignore'});
const error = await t.throwsAsync(arrayFromAsync(promise.stdout));
assertFail(t, error);
const promiseError = await t.throwsAsync(promise);
t.is(promiseError, error);
t.is(promiseError.stdout, '');
t.is(promiseError.stderr, '');
t.is(promiseError.output, '');
});

test('result.stdout is an empty string if options.stdout "ignore"', async t => {
const {stdout, stderr, output} = await nanoSpawn(...nodePrintBoth, {stdout: 'ignore'});
t.is(stdout, '');
Expand Down

0 comments on commit beb2760

Please sign in to comment.