diff --git a/src/cli-ux/prompt.ts b/src/cli-ux/prompt.ts index 0befdb805..afed56bc4 100644 --- a/src/cli-ux/prompt.ts +++ b/src/cli-ux/prompt.ts @@ -36,7 +36,8 @@ function normal(options: IPromptConfig, retries = 100): Promise { 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', diff --git a/test/cli-ux/prompt.test.ts b/test/cli-ux/prompt.test.ts index 387be7dca..1afe3922b 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,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?')