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

fix: only set timeout for TTY #967

Merged
merged 3 commits into from
Feb 22, 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
3 changes: 2 additions & 1 deletion src/cli-ux/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ function normal(options: IPromptConfig, retries = 100): Promise<string> {
output: process.stdout,
})
let timeout: NodeJS.Timeout
if (options.timeout) {
// Only set the timeout if the input is a TTY
if (options.timeout && options.isTTY) {
timeout = setTimeout(() => ac.abort(), options.timeout)
signal.addEventListener(
'abort',
Expand Down
13 changes: 12 additions & 1 deletion test/cli-ux/prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('prompt', () => {
expect(answer).to.equal('answer')
})

it('should not require input', async () => {
it('should not require input if required = false', async () => {
stubReadline([''])
const answer = await ux.prompt('Require input?', {required: false})
expect(answer).to.equal('')
Expand All @@ -45,6 +45,17 @@ describe('prompt', () => {
expect(answer).to.equal('default')
})

it('should timeout after provided timeout', async () => {
stubReadline([''])
sandbox.stub(process, 'stdin').value({isTTY: true})
try {
await ux.prompt('Require input?', {timeout: 10})
expect.fail('should have thrown')
} catch (error: any) {
expect(error.message).to.equal('Prompt timeout')
}
})

it('should confirm with y', async () => {
stubReadline(['y'])
const answer = await ux.confirm('yes/no?')
Expand Down
Loading