-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathSwiftTestingOutputParser.ts
570 lines (509 loc) · 20.1 KB
/
SwiftTestingOutputParser.ts
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2024 Apple Inc. and the VS Code Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import * as vscode from "vscode";
import * as readline from "readline";
import { Readable } from "stream";
import {
INamedPipeReader,
UnixNamedPipeReader,
WindowsNamedPipeReader,
} from "./TestEventStreamReader";
import { ITestRunState } from "./TestRunState";
import { TestClass } from "../TestDiscovery";
import { sourceLocationToVSCodeLocation } from "../../utilities/utilities";
import { exec } from "child_process";
// All events produced by a swift-testing run will be one of these three types.
// Detailed information about swift-testing's JSON schema is available here:
// https://github.com/apple/swift-testing/blob/main/Documentation/ABI/JSON.md
export type SwiftTestEvent = MetadataRecord | TestRecord | EventRecord;
interface VersionedRecord {
version: number;
}
interface MetadataRecord extends VersionedRecord {
kind: "metadata";
payload: Metadata;
}
interface TestRecord extends VersionedRecord {
kind: "test";
payload: TestSuite | TestFunction;
}
export type EventRecordPayload =
| RunStarted
| TestStarted
| TestEnded
| TestCaseStarted
| TestCaseEnded
| IssueRecorded
| TestSkipped
| RunEnded;
export interface EventRecord extends VersionedRecord {
kind: "event";
payload: EventRecordPayload;
}
interface Metadata {
[key: string]: object; // Currently unstructured content
}
interface TestBase {
id: string;
name: string;
_testCases?: TestCase[];
sourceLocation: SourceLocation;
}
interface TestSuite extends TestBase {
kind: "suite";
}
interface TestFunction extends TestBase {
kind: "function";
isParameterized: boolean;
}
export interface TestCase {
id: string;
displayName: string;
}
// Event types
interface RunStarted {
kind: "runStarted";
messages: EventMessage[];
}
interface RunEnded {
kind: "runEnded";
messages: EventMessage[];
}
interface Instant {
absolute: number;
since1970: number;
}
interface BaseEvent {
instant: Instant;
messages: EventMessage[];
testID: string;
}
interface TestCaseEvent {
sourceLocation: SourceLocation;
_testCase?: TestCase;
}
interface TestStarted extends BaseEvent {
kind: "testStarted";
}
interface TestEnded extends BaseEvent {
kind: "testEnded";
}
interface TestCaseStarted extends BaseEvent, TestCaseEvent {
kind: "testCaseStarted";
}
interface TestCaseEnded extends BaseEvent, TestCaseEvent {
kind: "testCaseEnded";
}
interface TestSkipped extends BaseEvent {
kind: "testSkipped";
}
interface IssueRecorded extends BaseEvent, TestCaseEvent {
kind: "issueRecorded";
issue: {
isKnown: boolean;
sourceLocation: SourceLocation;
};
}
export enum TestSymbol {
default = "default",
skip = "skip",
passWithKnownIssue = "passWithKnownIssue",
fail = "fail",
pass = "pass",
difference = "difference",
warning = "warning",
details = "details",
none = "none",
}
export interface EventMessage {
symbol: TestSymbol;
text: string;
}
export interface SourceLocation {
_filePath: string;
line: number;
column: number;
}
export class SwiftTestingOutputParser {
private completionMap = new Map<number, boolean>();
private testCaseMap = new Map<string, Map<string, TestCase>>();
private path?: string;
constructor(
public testRunStarted: () => void,
public addParameterizedTestCase: (testClass: TestClass, parentIndex: number) => void
) {}
/**
* Watches for test events on the named pipe at the supplied path.
* As events are read they are parsed and recorded in the test run state.
*/
public async watch(
path: string,
runState: ITestRunState,
pipeReader?: INamedPipeReader
): Promise<void> {
this.path = path;
// Creates a reader based on the platform unless being provided in a test context.
const reader = pipeReader ?? this.createReader(path);
const readlinePipe = new Readable({
read() {},
});
// Use readline to automatically chunk the data into lines,
// and then take each line and parse it as JSON.
const rl = readline.createInterface({
input: readlinePipe,
crlfDelay: Infinity,
});
rl.on("line", line => this.parse(JSON.parse(line), runState));
reader.start(readlinePipe);
}
/**
* Closes the FIFO pipe after a test run. This must be called at the
* end of a run regardless of the run's success or failure.
*/
public async close() {
if (!this.path) {
return;
}
await new Promise<void>(resolve => {
exec(`echo '{}' > ${this.path}`, () => {
resolve();
});
});
}
/**
* Parses stdout of a test run looking for lines that were not captured by
* a JSON event and injecting them in to the test run output.
* @param chunk A chunk of stdout emitted during a test run.
*/
public parseStdout(chunk: string, runState: ITestRunState) {
for (const line of chunk.split("\n")) {
if (line.trim().length > 0) {
runState.recordOutput(undefined, `${line}\r\n`);
}
}
}
private createReader(path: string): INamedPipeReader {
return process.platform === "win32"
? new WindowsNamedPipeReader(path)
: new UnixNamedPipeReader(path);
}
private testName(id: string): string {
const nameMatcher = /^(.*\(.*\))\/(.*)\.swift:\d+:\d+$/;
const matches = id.match(nameMatcher);
return !matches ? id : matches[1];
}
private testCaseId(testId: string, testCaseId: string): string {
const testCase = this.testCaseMap.get(testId)?.get(testCaseId);
return testCase ? `${testId}/${this.idFromTestCase(testCase)}` : testId;
}
// Test cases do not have a unique ID if their arguments are not serializable
// with Codable. If they aren't, their id appears as `argumentIDs: nil`, and we
// fall back to using the testCase display name as the test case ID. This isn't
// ideal because its possible to have multiple test cases with the same display name,
// but until we have a better solution for identifying test cases it will have to do.
// SEE: rdar://119522099.
private idFromTestCase(testCase: TestCase): string {
return testCase.id === "argumentIDs: nil" ? testCase.displayName : testCase.id;
}
private idFromOptionalTestCase(testID: string, testCase?: TestCase): string {
return testCase
? this.testCaseId(testID, this.idFromTestCase(testCase))
: this.testName(testID);
}
private parameterizedFunctionTestCaseToTestClass(
testId: string,
testCase: TestCase,
location: vscode.Location,
index: number
): TestClass {
return {
id: this.testCaseId(testId, this.idFromTestCase(testCase)),
label: testCase.displayName,
tags: [],
children: [],
style: "swift-testing",
location: location,
disabled: true,
sortText: `${index}`.padStart(8, "0"),
};
}
private buildTestCaseMapForParameterizedTest(record: TestRecord) {
const map = new Map<string, TestCase>();
(record.payload._testCases ?? []).forEach(testCase => {
map.set(this.idFromTestCase(testCase), testCase);
});
this.testCaseMap.set(record.payload.id, map);
}
private getTestCaseIndex(runState: ITestRunState, testID: string): number {
const fullNameIndex = runState.getTestItemIndex(testID, undefined);
if (fullNameIndex === -1) {
return runState.getTestItemIndex(this.testName(testID), undefined);
}
return fullNameIndex;
}
/**
* Partitions a collection of messages in to issues and details about the issues.
* This is used to print the issues first, followed by the details.
*/
private partitionIssueMessages(messages: EventMessage[]): {
issues: EventMessage[];
details: EventMessage[];
} {
return messages.reduce(
(buckets, message) => {
const key =
message.symbol === "details" ||
message.symbol === "default" ||
message.symbol === "none"
? "details"
: "issues";
return { ...buckets, [key]: [...buckets[key], message] };
},
{
issues: [],
details: [],
}
);
}
/*
* A multi line comment preceeding an issue will have a 'default' symbol for
* all lines except the first one. To match the swift-testing command line we
* should show no symbol on these lines.
*/
private transformIssueMessageSymbols(messages: EventMessage[]): EventMessage[] {
return messages.map(message => ({
...message,
symbol: message.symbol === "default" ? TestSymbol.none : message.symbol,
}));
}
private parse(item: SwiftTestEvent, runState: ITestRunState) {
if (
item.kind === "test" &&
item.payload.kind === "function" &&
item.payload.isParameterized &&
item.payload._testCases
) {
// Store a map of [Test ID, [Test Case ID, TestCase]] so we can quickly
// map an event.payload.testID back to a test case.
this.buildTestCaseMapForParameterizedTest(item);
const testName = this.testName(item.payload.id);
const testIndex = runState.getTestItemIndex(testName, undefined);
// If a test has test cases it is paramterized and we need to notify
// the caller that the TestClass should be added to the vscode.TestRun
// before it starts.
item.payload._testCases
.map((testCase, index) =>
this.parameterizedFunctionTestCaseToTestClass(
item.payload.id,
testCase,
sourceLocationToVSCodeLocation(
item.payload.sourceLocation._filePath,
item.payload.sourceLocation.line,
item.payload.sourceLocation.column
),
index
)
)
.flatMap(testClass => (testClass ? [testClass] : []))
.forEach(testClass => this.addParameterizedTestCase(testClass, testIndex));
} else if (item.kind === "event") {
if (item.payload.kind === "runStarted") {
// Notify the runner that we've recieved all the test cases and
// are going to start running tests now.
this.testRunStarted();
return;
} else if (item.payload.kind === "testStarted") {
const testName = this.testName(item.payload.testID);
const testIndex = runState.getTestItemIndex(testName, undefined);
runState.started(testIndex, item.payload.instant.absolute);
return;
} else if (item.payload.kind === "testCaseStarted") {
const testID = this.idFromOptionalTestCase(
item.payload.testID,
item.payload._testCase
);
const testIndex = this.getTestCaseIndex(runState, testID);
runState.started(testIndex, item.payload.instant.absolute);
return;
} else if (item.payload.kind === "testSkipped") {
const testName = this.testName(item.payload.testID);
const testIndex = runState.getTestItemIndex(testName, undefined);
runState.skipped(testIndex);
return;
} else if (item.payload.kind === "issueRecorded") {
const testID = this.idFromOptionalTestCase(
item.payload.testID,
item.payload._testCase
);
const testIndex = this.getTestCaseIndex(runState, testID);
const isKnown = item.payload.issue.isKnown;
const sourceLocation = item.payload.issue.sourceLocation;
const location = sourceLocationToVSCodeLocation(
sourceLocation._filePath,
sourceLocation.line,
sourceLocation.column
);
const messages = this.transformIssueMessageSymbols(item.payload.messages);
const { issues, details } = this.partitionIssueMessages(messages);
// Order the details after the issue text.
const additionalDetails = details
.map(message => MessageRenderer.render(message))
.join("\n");
issues.forEach(message => {
runState.recordIssue(
testIndex,
additionalDetails.length > 0
? `${MessageRenderer.render(message)}\n${additionalDetails}`
: MessageRenderer.render(message),
isKnown,
location
);
});
if (item.payload._testCase && testID !== item.payload.testID) {
const testIndex = this.getTestCaseIndex(runState, item.payload.testID);
messages.forEach(message => {
runState.recordIssue(testIndex, message.text, isKnown, location);
});
}
return;
} else if (item.payload.kind === "testEnded") {
const testName = this.testName(item.payload.testID);
const testIndex = runState.getTestItemIndex(testName, undefined);
// When running a single test the testEnded and testCaseEnded events
// have the same ID, and so we'd end the same test twice.
if (this.completionMap.get(testIndex)) {
return;
}
this.completionMap.set(testIndex, true);
runState.completed(testIndex, { timestamp: item.payload.instant.absolute });
return;
} else if (item.payload.kind === "testCaseEnded") {
const testID = this.idFromOptionalTestCase(
item.payload.testID,
item.payload._testCase
);
const testIndex = this.getTestCaseIndex(runState, testID);
// When running a single test the testEnded and testCaseEnded events
// have the same ID, and so we'd end the same test twice.
if (this.completionMap.get(testIndex)) {
return;
}
this.completionMap.set(testIndex, true);
runState.completed(testIndex, { timestamp: item.payload.instant.absolute });
return;
}
}
}
}
export class MessageRenderer {
/**
* Converts a swift-testing `EventMessage` to a colorized symbol and message text.
*
* @param message An event message, typically found on an `EventRecordPayload`.
* @returns A string colorized with ANSI escape codes.
*/
static render(message: EventMessage): string {
return `${SymbolRenderer.eventMessageSymbol(message.symbol)} ${MessageRenderer.colorize(message.symbol, message.text)}`;
}
private static colorize(symbolType: TestSymbol, message: string): string {
const ansiEscapeCodePrefix = "\u{001B}[";
const resetANSIEscapeCode = `${ansiEscapeCodePrefix}0m`;
switch (symbolType) {
case TestSymbol.details:
case TestSymbol.skip:
case TestSymbol.difference:
case TestSymbol.passWithKnownIssue:
return `${ansiEscapeCodePrefix}90m${message}${resetANSIEscapeCode}`;
default:
return message;
}
}
}
export class SymbolRenderer {
/**
* Converts a swift-testing symbol identifier in to a colorized unicode symbol.
*
* @param message An event message, typically found on an `EventRecordPayload`.
* @returns A string colorized with ANSI escape codes.
*/
static eventMessageSymbol(symbol: TestSymbol): string {
return this.colorize(symbol, this.symbol(symbol));
}
static ansiEscapeCodePrefix = "\u{001B}[";
static resetANSIEscapeCode = `${SymbolRenderer.ansiEscapeCodePrefix}0m`;
// This is adapted from
// https://github.com/apple/swift-testing/blob/786ade71421eb1d8a9c1d99c902cf1c93096e7df/Sources/Testing/Events/Recorder/Event.Symbol.swift#L102
public static symbol(symbol: TestSymbol): string {
if (process.platform === "win32") {
switch (symbol) {
case TestSymbol.default:
return "\u{25CA}"; // Unicode: LOZENGE
case TestSymbol.skip:
case TestSymbol.passWithKnownIssue:
case TestSymbol.fail:
return "\u{00D7}"; // Unicode: MULTIPLICATION SIGN
case TestSymbol.pass:
return "\u{221A}"; // Unicode: SQUARE ROOT
case TestSymbol.difference:
return "\u{00B1}"; // Unicode: PLUS-MINUS SIGN
case TestSymbol.warning:
return "\u{25B2}"; // Unicode: BLACK UP-POINTING TRIANGLE
case TestSymbol.details:
return "\u{2192}"; // Unicode: RIGHTWARDS ARROW
case TestSymbol.none:
return "";
}
} else {
switch (symbol) {
case TestSymbol.default:
return "\u{25C7}"; // Unicode: WHITE DIAMOND
case TestSymbol.skip:
case TestSymbol.passWithKnownIssue:
case TestSymbol.fail:
return "\u{2718}"; // Unicode: HEAVY BALLOT X
case TestSymbol.pass:
return "\u{2714}"; // Unicode: HEAVY CHECK MARK
case TestSymbol.difference:
return "\u{00B1}"; // Unicode: PLUS-MINUS SIGN
case TestSymbol.warning:
return "\u{26A0}\u{FE0E}"; // Unicode: WARNING SIGN + VARIATION SELECTOR-15 (disable emoji)
case TestSymbol.details:
return "\u{21B3}"; // Unicode: DOWNWARDS ARROW WITH TIP RIGHTWARDS
case TestSymbol.none:
return " ";
}
}
}
// This is adapted from
// https://github.com/apple/swift-testing/blob/786ade71421eb1d8a9c1d99c902cf1c93096e7df/Sources/Testing/Events/Recorder/Event.ConsoleOutputRecorder.swift#L164
private static colorize(symbolType: TestSymbol, symbol: string): string {
switch (symbolType) {
case TestSymbol.default:
case TestSymbol.details:
case TestSymbol.skip:
case TestSymbol.difference:
case TestSymbol.passWithKnownIssue:
return `${SymbolRenderer.ansiEscapeCodePrefix}90m${symbol}${SymbolRenderer.resetANSIEscapeCode}`;
case TestSymbol.pass:
return `${SymbolRenderer.ansiEscapeCodePrefix}92m${symbol}${SymbolRenderer.resetANSIEscapeCode}`;
case TestSymbol.fail:
return `${SymbolRenderer.ansiEscapeCodePrefix}91m${symbol}${SymbolRenderer.resetANSIEscapeCode}`;
case TestSymbol.warning:
return `${SymbolRenderer.ansiEscapeCodePrefix}93m${symbol}${SymbolRenderer.resetANSIEscapeCode}`;
case TestSymbol.none:
default:
return symbol;
}
}
}