Skip to content

Commit

Permalink
Merge pull request #967 from oclif/mdonnalley/no-timeout-in-non-tty
Browse files Browse the repository at this point in the history
fix: only set timeout for TTY
  • Loading branch information
iowillhoit committed Feb 22, 2024
2 parents fa66417 + 7ede36d commit 9a39d7e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
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

0 comments on commit 9a39d7e

Please sign in to comment.