Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 12 additions & 1 deletion packages/aws-cdk/lib/toolkit/cli-io-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ export class CliIoHost implements IIoHost {
private corkedCounter = 0;
private readonly corkedLoggingBuffer: IoMessage<any>[] = [];

/**
* Stream to use for the input
*
* Should only be used for unit testing. Defaults to `process.stdin`
* which is what you usually want.
*/
public testInputStream?: NodeJS.ReadableStream;

private constructor(props: CliIoHostProps = {}) {
this._currentAction = props.currentAction ?? 'none' as ToolkitAction;
this._isTTY = props.isTTY ?? process.stdout.isTTY ?? false;
Expand Down Expand Up @@ -372,7 +380,9 @@ export class CliIoHost implements IIoHost {
// Basic confirmation prompt
// We treat all requests with a boolean response as confirmation prompts
if (isConfirmationPrompt(msg)) {
const confirmed = await promptly.confirm(`${chalk.cyan(msg.message)} (y/n)`);
const confirmed = await promptly.confirm(`${chalk.cyan(msg.message)} (y/n)`, {
input: this.testInputStream,
});
if (!confirmed) {
throw new ToolkitError('Aborted by user');
}
Expand All @@ -383,6 +393,7 @@ export class CliIoHost implements IIoHost {
const prompt = extractPromptInfo(msg);
const answer = await promptly.prompt(`${chalk.cyan(msg.message)} (${prompt.default})`, {
default: prompt.default,
input: this.testInputStream,
});
return prompt.convertAnswer(answer);
});
Expand Down
17 changes: 0 additions & 17 deletions packages/aws-cdk/test/_helpers/prompts.ts

This file was deleted.

28 changes: 18 additions & 10 deletions packages/aws-cdk/test/toolkit/cli-io-host.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { PassThrough } from 'stream';
import * as chalk from 'chalk';
import { CliIoHost, IoMessage, IoMessageLevel } from '../../lib/toolkit/cli-io-host';
import { sendResponse } from '../_helpers/prompts';
import { CliIoHost, IoMessage, IoMessageLevel, IoRequest } from '../../lib/toolkit/cli-io-host';

let passThrough: PassThrough;

const ioHost = CliIoHost.instance({
logLevel: 'trace',
Expand All @@ -19,6 +21,7 @@ describe('CliIoHost', () => {
ioHost.isTTY = process.stdout.isTTY ?? false;
ioHost.isCI = false;
ioHost.currentAction = 'synth';
ioHost.testInputStream = passThrough = new PassThrough();

defaultMessage = {
time: new Date('2024-01-01T12:00:00'),
Expand Down Expand Up @@ -243,8 +246,7 @@ describe('CliIoHost', () => {

describe('boolean', () => {
test('respond "yes" to a confirmation prompt', async () => {
sendResponse('y');
const response = await ioHost.requestResponse({
const response = await requestResponse('y', {
time: new Date(),
level: 'info',
action: 'synth',
Expand All @@ -258,8 +260,7 @@ describe('CliIoHost', () => {
});

test('respond "no" to a confirmation prompt', async () => {
sendResponse('n');
await expect(() => ioHost.requestResponse({
await expect(() => requestResponse('n', {
time: new Date(),
level: 'info',
action: 'synth',
Expand All @@ -279,8 +280,7 @@ describe('CliIoHost', () => {
// simulate the enter key
['\x0A', 'cat'],
])('receives %p and returns %p', async (input, expectedResponse) => {
sendResponse(input);
const response = await ioHost.requestResponse({
const response = await requestResponse(input, {
time: new Date(),
level: 'info',
action: 'synth',
Expand All @@ -300,8 +300,7 @@ describe('CliIoHost', () => {
// simulate the enter key
['\x0A', 1],
])('receives %p and return %p', async (input, expectedResponse) => {
sendResponse(input);
const response = await ioHost.requestResponse({
const response = await requestResponse(input, {
time: new Date(),
level: 'info',
action: 'synth',
Expand Down Expand Up @@ -378,3 +377,12 @@ describe('CliIoHost', () => {
});
});
});

/**
* Do a requestResponse cycle with the global ioHost, while sending input on the global fake input stream
*/
async function requestResponse<DataType, ResponseType>(input: string, msg: IoRequest<DataType, ResponseType>): Promise<ResponseType> {
const promise = ioHost.requestResponse(msg);
passThrough.write(input + '\n');
return promise;
}
Loading