Skip to content

Commit fd4755d

Browse files
jbedardclydin
authored andcommitted
test: watch full stdout when waiting for match
1 parent 2cfc3f9 commit fd4755d

File tree

3 files changed

+55
-58
lines changed

3 files changed

+55
-58
lines changed

tests/legacy-cli/e2e/tests/build/bundle-budgets.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default async function () {
1717
];
1818
});
1919

20-
const errorMessage = await expectToFail(() => ng('build'));
20+
const { message: errorMessage } = await expectToFail(() => ng('build'));
2121
if (!/Error.+budget/.test(errorMessage)) {
2222
throw new Error('Budget error: all, max error.');
2323
}

tests/legacy-cli/e2e/utils/process.ts

+52-55
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
2929
// Create a separate instance to prevent unintended global changes to the color configuration
3030
const colors = ansiColors.create();
3131

32-
let stdout = '';
33-
let stderr = '';
3432
const cwd = options.cwd ?? process.cwd();
3533
const env = options.env ?? process.env;
3634
console.log(
@@ -69,48 +67,65 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
6967
}
7068

7169
const childProcess = child_process.spawn(cmd, args, spawnOptions);
72-
childProcess.stdout!.on('data', (data: Buffer) => {
73-
stdout += data.toString('utf-8');
74-
if (options.silent) {
75-
return;
76-
}
77-
data
78-
.toString('utf-8')
79-
.split(/[\n\r]+/)
80-
.filter((line) => line !== '')
81-
.forEach((line) => console.log(' ' + line));
82-
});
83-
84-
childProcess.stderr!.on('data', (data: Buffer) => {
85-
stderr += data.toString('utf-8');
86-
if (options.silent) {
87-
return;
88-
}
89-
data
90-
.toString('utf-8')
91-
.split(/[\n\r]+/)
92-
.filter((line) => line !== '')
93-
.forEach((line) => console.error(colors.yellow(' ' + line)));
94-
});
9570

9671
_processes.push(childProcess);
9772

9873
// Create the error here so the stack shows who called this function.
9974
const error = new Error();
10075

101-
// Return log info about the current process status
102-
function envDump() {
103-
return [
104-
`ENV:${JSON.stringify(spawnOptions.env, null, 2)}`,
105-
`STDOUT:\n${stdout}`,
106-
`STDERR:\n${stderr}`,
107-
].join('\n\n');
108-
}
109-
11076
return new Promise<ProcessOutput>((resolve, reject) => {
77+
let stdout = '';
78+
let stderr = '';
11179
let matched = false;
11280

113-
childProcess.on('exit', (code: number) => {
81+
// Return log info about the current process status
82+
function envDump() {
83+
return [
84+
`ENV:${JSON.stringify(spawnOptions.env, null, 2)}`,
85+
`STDOUT:\n${stdout}`,
86+
`STDERR:\n${stderr}`,
87+
].join('\n\n');
88+
}
89+
90+
childProcess.stdout!.on('data', (data: Buffer) => {
91+
stdout += data.toString('utf-8');
92+
93+
if (options.waitForMatch && stdout.match(options.waitForMatch)) {
94+
resolve({ stdout, stderr });
95+
matched = true;
96+
}
97+
98+
if (options.silent) {
99+
return;
100+
}
101+
102+
data
103+
.toString('utf-8')
104+
.split(/[\n\r]+/)
105+
.filter((line) => line !== '')
106+
.forEach((line) => console.log(' ' + line));
107+
});
108+
109+
childProcess.stderr!.on('data', (data: Buffer) => {
110+
stderr += data.toString('utf-8');
111+
112+
if (options.waitForMatch && stderr.match(options.waitForMatch)) {
113+
resolve({ stdout, stderr });
114+
matched = true;
115+
}
116+
117+
if (options.silent) {
118+
return;
119+
}
120+
121+
data
122+
.toString('utf-8')
123+
.split(/[\n\r]+/)
124+
.filter((line) => line !== '')
125+
.forEach((line) => console.error(colors.yellow(' ' + line)));
126+
});
127+
128+
childProcess.on('close', (code: number) => {
114129
_processes = _processes.filter((p) => p !== childProcess);
115130

116131
if (options.waitForMatch && !matched) {
@@ -134,24 +149,6 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
134149
reject(`Process error - "${cmd} ${args.join(' ')}": ${err}...\n\n${envDump()}\n`);
135150
});
136151

137-
if (options.waitForMatch) {
138-
const match = options.waitForMatch;
139-
140-
childProcess.stdout!.on('data', (data: Buffer) => {
141-
if (data.toString().match(match)) {
142-
resolve({ stdout, stderr });
143-
matched = true;
144-
}
145-
});
146-
147-
childProcess.stderr!.on('data', (data: Buffer) => {
148-
if (data.toString().match(match)) {
149-
resolve({ stdout, stderr });
150-
matched = true;
151-
}
152-
});
153-
}
154-
155152
// Provide input to stdin if given.
156153
if (options.stdin) {
157154
childProcess.stdin!.write(options.stdin);
@@ -192,14 +189,14 @@ export function waitForAnyProcessOutputToMatch(
192189

193190
childProcess.stdout!.on('data', (data: Buffer) => {
194191
stdout += data.toString();
195-
if (data.toString().match(match)) {
192+
if (stdout.match(match)) {
196193
resolve({ stdout, stderr });
197194
}
198195
});
199196

200197
childProcess.stderr!.on('data', (data: Buffer) => {
201198
stderr += data.toString();
202-
if (data.toString().match(match)) {
199+
if (stderr.match(match)) {
203200
resolve({ stdout, stderr });
204201
}
205202
});

tests/legacy-cli/e2e/utils/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { mkdtemp, realpath, rm } from 'fs/promises';
33
import { tmpdir } from 'os';
44
import path from 'path';
55

6-
export function expectToFail(fn: () => Promise<any>, errorMessage?: string): Promise<any> {
6+
export function expectToFail(fn: () => Promise<any>, errorMessage?: string): Promise<Error> {
77
return fn().then(
88
() => {
99
const functionSource = fn.name || (<any>fn).source || fn.toString();
@@ -13,7 +13,7 @@ export function expectToFail(fn: () => Promise<any>, errorMessage?: string): Pro
1313
);
1414
},
1515
(err) => {
16-
return err;
16+
return err instanceof Error ? err : new Error(err);
1717
},
1818
);
1919
}

0 commit comments

Comments
 (0)