Skip to content

Commit f65342e

Browse files
authored
test: add test for error case of iterator (google#1000)
1 parent 7566081 commit f65342e

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/core.ts

+2
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,8 @@ export class ProcessPromise extends Promise<ProcessOutput> {
575575
}
576576

577577
if (last) yield last
578+
579+
if ((await this.exitCode) !== 0) throw this._output
578580
}
579581

580582
// Stream-like API

test/core.test.js

+26
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,32 @@ describe('core', () => {
817817
assert.equal(chunks[0], 'Chunk1', 'First chunk should be "Chunk1"')
818818
assert.equal(chunks[3], 'Chunk4', 'Second chunk should be "Chunk4"')
819819
})
820+
821+
it('should process all output before handling a non-zero exit code', async () => {
822+
const process = $`sleep 0.1; echo foo; sleep 0.1; echo bar; sleep 0.1; exit 1;`
823+
824+
const chunks = []
825+
826+
let errorCaught = null
827+
try {
828+
for await (const chunk of process) {
829+
chunks.push(chunk)
830+
}
831+
} catch (err) {
832+
errorCaught = err
833+
}
834+
835+
assert.equal(chunks.length, 2, 'Should have received 2 chunks')
836+
assert.equal(chunks[0], 'foo', 'First chunk should be "foo"')
837+
assert.equal(chunks[1], 'bar', 'Second chunk should be "bar"')
838+
839+
assert.ok(errorCaught, 'An error should have been caught')
840+
assert.equal(
841+
errorCaught.exitCode,
842+
1,
843+
'The process exit code should be 1'
844+
)
845+
})
820846
})
821847

822848
test('quiet() mode is working', async () => {

0 commit comments

Comments
 (0)