From 271b66b18ad4689a55180bc751a20d547ec2fb8b Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Thu, 22 Feb 2024 11:02:03 -0700 Subject: [PATCH] chore: use options.isTTY --- src/cli-ux/prompt.ts | 2 +- test/cli-ux/prompt.test.ts | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/cli-ux/prompt.ts b/src/cli-ux/prompt.ts index 7735b357c..afed56bc4 100644 --- a/src/cli-ux/prompt.ts +++ b/src/cli-ux/prompt.ts @@ -37,7 +37,7 @@ function normal(options: IPromptConfig, retries = 100): Promise { }) 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', diff --git a/test/cli-ux/prompt.test.ts b/test/cli-ux/prompt.test.ts index 387be7dca..0f28c02ef 100644 --- a/test/cli-ux/prompt.test.ts +++ b/test/cli-ux/prompt.test.ts @@ -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('') @@ -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?')