Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 0 additions & 17 deletions packages/aws-cdk/test/_helpers/prompts.ts

This file was deleted.

31 changes: 21 additions & 10 deletions packages/aws-cdk/test/toolkit/cli-io-host.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
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',
});

// Mess with the 'process' global so we can replace its 'process.stdin' member
global.process = { ...process };

describe('CliIoHost', () => {
let mockStdout: jest.Mock;
let mockStderr: jest.Mock;
Expand All @@ -19,6 +24,7 @@ describe('CliIoHost', () => {
ioHost.isTTY = process.stdout.isTTY ?? false;
ioHost.isCI = false;
ioHost.currentAction = 'synth';
(process as any).stdin = passThrough = new PassThrough();

defaultMessage = {
time: new Date('2024-01-01T12:00:00'),
Expand Down Expand Up @@ -243,8 +249,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 +263,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 +283,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 +303,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 +380,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