Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit 1103cee

Browse files
authored
fix(cli-integ): interactive cdk import test is unstable (#76)
Various enhancements in this PR: - Don't register a callback on `stderr` because it creates duplicate output. - Shift the interactions array before writing to `stdin` to prevent possible interaction reuse. - On interaction match, reset the `lastLine` we match on to prevent repeated matches due to tty echoing. - Make prompt matching in `cdk import` test be more specific to the requested input.
1 parent 199fe27 commit 1103cee

3 files changed

Lines changed: 40 additions & 17 deletions

File tree

packages/@aws-cdk-testing/cli-integ/lib/process.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ class PtyProcess implements IProcess {
9090
this.process.onData((e) => callback(Buffer.from(e)));
9191
}
9292

93-
public onStderr(callback: (chunk: Buffer) => void): void {
94-
// in a pty all streams are the same
95-
return this.onStdout(callback);
93+
public onStderr(_callback: (chunk: Buffer) => void): void {
94+
// https://github.com/microsoft/node-pty/issues/71
95+
throw new Error(`Cannot register callback for 'stderr'. A tty does not have separate output and error channels`);
9696
}
9797

9898
public onExit(callback: (exitCode: number) => void): void {

packages/@aws-cdk-testing/cli-integ/lib/shell.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,42 @@ export async function shell(command: string[], options: ShellOptions = {}): Prom
6060
if (interaction) {
6161

6262
if (interaction.prompt.test(lastLine.get())) {
63+
6364
// subprocess expects a user input now.
64-
// we have to write the input AFTER the child has started
65-
// reading, so we do this with a small delay.
65+
// first, shift the interactions to ensure the same interaction is not reused
66+
remainingInteractions.shift();
67+
68+
// then, reset the last line to prevent repeated matches caused by tty echoing
69+
lastLine.reset();
70+
71+
// now write the input with a slight delay to ensure
72+
// the child process has already started reading.
6673
setTimeout(() => {
6774
child.writeStdin(interaction.input + (interaction.end ?? os.EOL));
68-
remainingInteractions.shift();
6975
}, 500);
7076
}
7177

7278
}
7379

7480
});
7581

76-
child.onStderr(chunk => {
77-
if (show === 'always') {
78-
writeToOutputs(chunk.toString('utf-8'));
79-
}
80-
if (options.captureStderr ?? true) {
81-
stderr.push(chunk);
82-
}
83-
});
82+
if (tty && options.captureStderr === false) {
83+
// in a tty stderr goes to the same fd as stdout
84+
throw new Error(`Cannot disable 'captureStderr' in tty`);
85+
}
86+
87+
if (!tty) {
88+
// in a tty stderr goes to the same fd as stdout, so onStdout
89+
// is sufficient.
90+
child.onStderr(chunk => {
91+
if (show === 'always') {
92+
writeToOutputs(chunk.toString('utf-8'));
93+
}
94+
if (options.captureStderr ?? true) {
95+
stderr.push(chunk);
96+
}
97+
});
98+
}
8499

85100
child.onError(reject);
86101

@@ -309,4 +324,8 @@ class LastLine {
309324
public get(): string {
310325
return this.lastLine;
311326
}
327+
328+
public reset() {
329+
this.lastLine = '';
330+
}
312331
}

packages/@aws-cdk-testing/cli-integ/tests/cli-integ-tests/cdk-import-interactive.integtest.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ integTest('cdk import prompts the user for sns topic arns', withDefaultFixture(a
2727
await fixture.cdk(['import', fullStackName], {
2828
interact: [
2929
{
30-
prompt: /\(empty to skip\)/,
30+
prompt: /Topic1.*\(empty to skip\):/,
3131
input: topic1Arn,
3232
},
3333
{
34-
prompt: /\(empty to skip\)/,
34+
prompt: /Topic2.*\(empty to skip\):/,
3535
input: topic2Arn,
3636
}
37-
]
37+
],
38+
modEnv: {
39+
// disable coloring because it messes up prompt matching.
40+
FORCE_COLOR: '0'
41+
}
3842
});
3943

4044
// assert the stack now has the two topics

0 commit comments

Comments
 (0)