Repository navigation
Expand file tree
/
Copy pathOperationOutputExcerpt.test.ts
More file actions
303 lines (276 loc) · 13.1 KB
/
Copy pathOperationOutputExcerpt.test.ts
File metadata and controls
303 lines (276 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import {
OperationOutputExcerpt,
clipLine,
isErrorLine,
normalizeExcerptLine
} from '../OperationOutputExcerpt';
const ESC: string = String.fromCharCode(27);
// Shaped like a Heft plugin that fails to load: the cause is on one line, followed by a long require stack.
const REQUIRE_STACK_FAILURE: string = [
'',
'Internal Error: Could not load plugin from "/repo/tools/example-plugin/lib/ExamplePlugin.js": ' +
"Error: Cannot find module '/repo/tools/example-plugin/lib/ExamplePlugin.js'",
'Require stack:',
...Array.from(
{ length: 8 },
(unused, i) =>
`- /repo/common/temp/node_modules/.pnpm/@rushstack+heft@1.2.3/node_modules/@rushstack/heft/lib/m${i}.js`
),
'',
'You have encountered a software defect. Please consider reporting the issue to the maintainers of this application.',
''
].join('\n');
// Shaped like Heft's Jest plugin output: prefixed lines, a message, a code frame and stack frames per test.
function jestFailure(testNames: ReadonlyArray<string>): string {
const lines: string[] = [];
for (const [index, testName] of testNames.entries()) {
lines.push(
`[test:jest] ● ${testName}`,
'[test:jest] ',
'[test:jest] 1',
'[test:jest] ',
'[test:jest] ERROR: The following environment variables were found with the "RUSH_" prefix,',
'[test:jest] but they are not recognized by this version of Rush: RUSH_EXAMPLE',
'[test:jest] ',
'[test:jest] 11 | if (result.status !== 0) {',
'[test:jest] > 12 | throw new Error(`${result.status}`);',
'[test:jest] | ^',
'[test:jest] 13 | }',
'[test:jest] ',
`[test:jest] at runStep (src/test/example.test.ts:12:11)`,
`[test:jest] at Object.runStep (src/test/example.test.ts:${40 + index}:3)`,
'[test:jest] '
);
}
lines.push(`[test:jest] Error: ${testNames.length} Jest tests failed`, 'Encountered 1 error', '');
return lines.join('\n');
}
describe(normalizeExcerptLine.name, () => {
it('removes colors, carriage-return overwrites and the whitespace after a task prefix', () => {
expect(normalizeExcerptLine(`${ESC}[31merror${ESC}[39m TS2304: x`)).toBe('error TS2304: x');
expect(normalizeExcerptLine('progress 10%\rprogress 100%\r')).toBe('progress 100%');
expect(normalizeExcerptLine('[build:lint] Warning: src/x.ts:1:1')).toBe(
'[build:lint] Warning: src/x.ts:1:1'
);
expect(normalizeExcerptLine(`${ESC}]8;;https://example.com${String.fromCharCode(7)}link`)).toBe('link');
});
it('drops empty, prefix-only and noise lines', () => {
for (const line of [
'',
' ',
'[test:jest] ',
' at Object.<anonymous> (src/x.test.ts:12:19)',
' at async Promise.all (index 0)',
' at new Promise (<anonymous>)',
'Require stack:',
'- /repo/node_modules/x/lib/index.js',
'- C:\\repo\\node_modules\\x\\lib\\index.js',
'[test:jest] > 12 | throw new Error();',
'[test:jest] | ^',
' ---- build started ---- ',
'-------------------- Finished (12.407s) --------------------',
'Invoking: heft run --only build -- --clean ',
'Invoking (incremental): heft run --only build --',
'This project was not found in the build cache.',
'Build cache hit.',
'Caching build output folders: lib, lib-commonjs',
'Successfully set cache entry.'
]) {
expect(normalizeExcerptLine(line)).toBeUndefined();
}
});
it('keeps English lines that merely start with "at"', () => {
expect(normalizeExcerptLine('at least one project failed')).toBe('at least one project failed');
});
it('clips very long lines, keeping their start and end', () => {
const line: string | undefined = normalizeExcerptLine(
`start ${'x'.repeat(10000)} Cannot find module 'y'`
);
expect(line?.length).toBe(300);
expect(line?.startsWith('start ')).toBe(true);
expect(line?.endsWith("Cannot find module 'y'")).toBe(true);
});
});
describe(isErrorLine.name, () => {
it.each([
'src/x.ts(1,1): error TS1005: expected',
'src/x.ts:1:1 - error TS2304: Cannot find name',
'[test:jest] ● suite › test',
'Error: 3 Jest tests failed',
'npm ERR! code ELIFECYCLE',
'ERR_PNPM_FETCH_401 GET https://registry.example/pkg: Unauthorized',
'Encountered 1 error',
'FAIL src/x.test.ts',
'Could not load plugin'
])('recognizes %s', (line) => {
expect(isErrorLine(line)).toBe(true);
});
it.each([
'Found 0 errors. Watching for file changes.',
'Linting finished with no errors',
'Compiling 12 files'
])('ignores %s', (line) => {
expect(isErrorLine(line)).toBe(false);
});
});
describe(clipLine.name, () => {
it('leaves short lines alone', () => {
expect(clipLine('short', 10)).toBe('short');
});
it('keeps the start and end of long lines', () => {
expect(clipLine('abcdefghijklmnopqrstuvwxyz', 10)).toBe('abcdef…xyz');
});
});
describe(OperationOutputExcerpt.name, () => {
it('shows the cause of a plugin load failure instead of its require stack', () => {
const excerpt: OperationOutputExcerpt = new OperationOutputExcerpt();
excerpt.append(REQUIRE_STACK_FAILURE, 'stderr');
const lines: string[] = excerpt.getExcerpt(8);
expect(lines).toHaveLength(2);
expect(lines[0]).toMatch(/^Internal Error: Could not load plugin/);
expect(lines[0]).toContain("Cannot find module '");
expect(lines[1]).toMatch(/^You have encountered a software defect/);
});
it('shows the failing tests, the message and the summary of a Jest failure', () => {
const excerpt: OperationOutputExcerpt = new OperationOutputExcerpt();
excerpt.append('[test:jest] PASS src/test/other.test.ts\n', 'stdout');
excerpt.append(jestFailure(['test one', 'test two', 'test three']), 'stderr');
expect(excerpt.getExcerpt(8)).toEqual([
'[test:jest] ● test one',
'[test:jest] ERROR: The following environment variables were found with the "RUSH_" prefix,',
'[test:jest] but they are not recognized by this version of Rush: RUSH_EXAMPLE',
'[test:jest] ● test two',
'[test:jest] ● test three',
'[test:jest] Error: 3 Jest tests failed'
]);
expect(excerpt.getExcerpt(3)).toEqual([
'[test:jest] ● test one',
'[test:jest] Error: 3 Jest tests failed'
]);
});
it('finds errors that tools report on stdout', () => {
const excerpt: OperationOutputExcerpt = new OperationOutputExcerpt();
excerpt.append('[build:typescript] Using TypeScript version 5.8.2\n', 'stdout');
excerpt.append('[build:typescript] src/x.ts:1:1 - error TS2304: Cannot find name "y".\n', 'stdout');
excerpt.append('[build:typescript] Encountered 1 error\n', 'stderr');
expect(excerpt.getExcerpt(8)).toEqual([
'[build:typescript] src/x.ts:1:1 - error TS2304: Cannot find name "y".'
]);
});
it("does not spend the error lines of a short excerpt on Rush's own cache and invocation lines", () => {
const excerpt: OperationOutputExcerpt = new OperationOutputExcerpt();
excerpt.append('This project was not found in the build cache.\n', 'stdout');
excerpt.append('Invoking (initial): heft run --only build -- --clean \n', 'stdout');
excerpt.append('[build:typescript] Using TypeScript version 5.8.2\n', 'stdout');
excerpt.append('[build:typescript] src/x.ts:1:1 - error TS2304: Cannot find name "y".\n', 'stderr');
excerpt.append('[build:lint] Using ESLint version 9.37.0\n', 'stdout');
excerpt.append('Error: Encountered 1 error\n', 'stderr');
expect(excerpt.getExcerpt(3)).toEqual([
'[build:typescript] src/x.ts:1:1 - error TS2304: Cannot find name "y".'
]);
});
it('takes the line after an error from the same stream, not from the interleaved other stream', () => {
const excerpt: OperationOutputExcerpt = new OperationOutputExcerpt();
excerpt.append(
"src/x.ts(3,7): error TS2322: Type 'string' is not assignable to type 'number'.\n",
'stderr'
);
excerpt.append('[build:lint] still linting file 4\n', 'stdout');
excerpt.append(' The expected type comes from property "x".\n', 'stderr');
excerpt.append("src/y.ts(1,1): error TS2304: Cannot find name 'z'.\n", 'stderr');
excerpt.append('[build:lint] still linting file 5\n', 'stdout');
excerpt.append(' Did you mean "y"?\n', 'stderr');
excerpt.append('Encountered 2 errors\n', 'stderr');
expect(excerpt.getExcerpt(5)).toEqual([
"src/x.ts(3,7): error TS2322: Type 'string' is not assignable to type 'number'.",
'The expected type comes from property "x".',
"src/y.ts(1,1): error TS2304: Cannot find name 'z'.",
'Did you mean "y"?'
]);
});
it('shows a diagnostic that a tool repeats in its summary once, without the error count', () => {
const excerpt: OperationOutputExcerpt = new OperationOutputExcerpt();
const message: string = "src/index.ts:3:7 - (TS2322) Type 'string' is not assignable to type 'number'.";
excerpt.append(`[build:typescript] Error: ${message}\n`, 'stdout');
excerpt.append('Encountered 1 error\n', 'stderr');
excerpt.append(` [build:typescript] ${message}\n`, 'stderr');
expect(excerpt.getExcerpt(8)).toEqual([`[build:typescript] Error: ${message}`]);
});
it('keeps an error count that reports more errors than the excerpt shows', () => {
const excerpt: OperationOutputExcerpt = new OperationOutputExcerpt();
for (let i: number = 0; i < 12; i++) {
excerpt.append(`src/x${i}.ts:1:1 - error TS2304: Cannot find name "y${i}".\n`, 'stdout');
}
excerpt.append('Encountered 12 errors\n', 'stderr');
expect(excerpt.getExcerpt(3)).toEqual([
'src/x0.ts:1:1 - error TS2304: Cannot find name "y0".',
'src/x1.ts:1:1 - error TS2304: Cannot find name "y1".',
'Encountered 12 errors'
]);
});
it('leaves out the lines before an error that names a source location, but not before other errors', () => {
const located: OperationOutputExcerpt = new OperationOutputExcerpt();
located.append('[build] @x/a: start\n', 'stdout');
located.append("ERROR in src/index.ts:4:1: Unexpected token '}'\n", 'stdout');
expect(located.getExcerpt(8)).toEqual(["ERROR in src/index.ts:4:1: Unexpected token '}'"]);
const unlocated: OperationOutputExcerpt = new OperationOutputExcerpt();
unlocated.append('src/a.ts(1,1): something unexpected\n', 'stdout');
unlocated.append('Build failed\n', 'stdout');
expect(unlocated.getExcerpt(8)).toEqual(['src/a.ts(1,1): something unexpected', 'Build failed']);
});
it('does not take a host and port for a source location', () => {
for (const error of [
'npm ERR! connect ECONNREFUSED registry.example.com:443',
'Error: request to https://pkgs.dev.azure.com:443/x failed'
]) {
const excerpt: OperationOutputExcerpt = new OperationOutputExcerpt();
excerpt.append('Installing @x/a from the registry\n', 'stderr');
excerpt.append(`${error}\n`, 'stderr');
expect(excerpt.getExcerpt(8)).toEqual(['Installing @x/a from the registry', error]);
}
});
it('shows a message that is repeated in another case once', () => {
const excerpt: OperationOutputExcerpt = new OperationOutputExcerpt();
excerpt.append("Error: Cannot find module 'X'\n", 'stdout');
excerpt.append("error: cannot find module 'x'\n", 'stderr');
expect(excerpt.getExcerpt(8)).toEqual(["Error: Cannot find module 'X'"]);
});
it('always keeps the last line, which usually is the tool summary', () => {
const excerpt: OperationOutputExcerpt = new OperationOutputExcerpt();
for (let i: number = 0; i < 20; i++) {
excerpt.append(`error ${i}\n`, 'stderr');
}
const lines: string[] = excerpt.getExcerpt(8);
expect(lines).toHaveLength(8);
expect(lines[0]).toBe('error 0');
expect(lines[lines.length - 1]).toBe('error 19');
});
it('joins lines split across chunks and records an unterminated last line', () => {
const excerpt: OperationOutputExcerpt = new OperationOutputExcerpt();
excerpt.append('src/x.ts(1,1): err', 'stdout');
excerpt.append('or TS1005: expected\nlast line without newline', 'stdout');
expect(excerpt.getExcerpt(8)).toEqual([
'src/x.ts(1,1): error TS1005: expected',
'last line without newline'
]);
});
it('does not repeat a line that was written to both streams', () => {
const excerpt: OperationOutputExcerpt = new OperationOutputExcerpt();
excerpt.append('Error: boom\n', 'stdout');
excerpt.append('Error: boom\n', 'stderr');
expect(excerpt.getExcerpt(8)).toEqual(['Error: boom']);
});
it('keeps memory use flat for huge output', () => {
const excerpt: OperationOutputExcerpt = new OperationOutputExcerpt();
for (let i: number = 0; i < 100000; i++) {
excerpt.append(`line ${i} ${'x'.repeat(100)}\n`, 'stdout');
}
excerpt.append('x'.repeat(200000), 'stdout');
expect(excerpt.lineCount).toBe(100001);
const lines: string[] = excerpt.getExcerpt(8);
expect(lines).toHaveLength(8);
expect(lines[lines.length - 1]).toMatch(/^x+…x+$/);
});
});