Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add test for error case of iterator #1000

Merged
merged 1 commit into from
Dec 17, 2024
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
2 changes: 2 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,8 @@ export class ProcessPromise extends Promise<ProcessOutput> {
}

if (last) yield last

if ((await this.exitCode) !== 0) throw this._output
}

// Stream-like API
Expand Down
26 changes: 26 additions & 0 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,32 @@ describe('core', () => {
assert.equal(chunks[0], 'Chunk1', 'First chunk should be "Chunk1"')
assert.equal(chunks[3], 'Chunk4', 'Second chunk should be "Chunk4"')
})

it('should process all output before handling a non-zero exit code', async () => {
const process = $`sleep 0.1; echo foo; sleep 0.1; echo bar; sleep 0.1; exit 1;`

const chunks = []

let errorCaught = null
try {
for await (const chunk of process) {
chunks.push(chunk)
}
} catch (err) {
errorCaught = err
}

assert.equal(chunks.length, 2, 'Should have received 2 chunks')
assert.equal(chunks[0], 'foo', 'First chunk should be "foo"')
assert.equal(chunks[1], 'bar', 'Second chunk should be "bar"')

assert.ok(errorCaught, 'An error should have been caught')
assert.equal(
errorCaught.exitCode,
1,
'The process exit code should be 1'
)
})
})

test('quiet() mode is working', async () => {
Expand Down
Loading