Skip to content

Commit

Permalink
chore: use options.isTTY
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Feb 22, 2024
1 parent 8a97f20 commit 271b66b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/cli-ux/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function normal(options: IPromptConfig, retries = 100): Promise<string> {
})
let timeout: NodeJS.Timeout
// Only set the timeout if the input is a TTY
if (options.timeout && process.stdin.isTTY) {
if (options.timeout && options.isTTY) {
timeout = setTimeout(() => ac.abort(), options.timeout)
signal.addEventListener(
'abort',
Expand Down
12 changes: 11 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,16 @@ describe('prompt', () => {
expect(answer).to.equal('default')
})

it('should timeout after provided timeout', async () => {
stubReadline([''])
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 271b66b

Please sign in to comment.