|
| 1 | +import * as readline from "readline"; |
| 2 | +import { Readable } from "stream"; |
| 3 | +import { |
| 4 | + INamedPipeReader, |
| 5 | + UnixNamedPipeReader, |
| 6 | + WindowsNamedPipeReader, |
| 7 | +} from "./TestEventStreamReader"; |
| 8 | +import { ITestRunState } from "./TestRunState"; |
| 9 | + |
| 10 | +// All events produced by a swift-testing run will be one of these three types. |
| 11 | +export type SwiftTestEvent = MetadataRecord | TestRecord | EventRecord; |
| 12 | + |
| 13 | +interface VersionedRecord { |
| 14 | + version: number; |
| 15 | +} |
| 16 | + |
| 17 | +interface MetadataRecord extends VersionedRecord { |
| 18 | + kind: "metadata"; |
| 19 | + payload: Metadata; |
| 20 | +} |
| 21 | + |
| 22 | +interface TestRecord extends VersionedRecord { |
| 23 | + kind: "test"; |
| 24 | + payload: Test; |
| 25 | +} |
| 26 | + |
| 27 | +export type EventRecordPayload = |
| 28 | + | RunStarted |
| 29 | + | TestStarted |
| 30 | + | TestEnded |
| 31 | + | TestCaseStarted |
| 32 | + | TestCaseEnded |
| 33 | + | IssueRecorded |
| 34 | + | TestSkipped |
| 35 | + | RunEnded; |
| 36 | + |
| 37 | +export interface EventRecord extends VersionedRecord { |
| 38 | + kind: "event"; |
| 39 | + payload: EventRecordPayload; |
| 40 | +} |
| 41 | + |
| 42 | +interface Metadata { |
| 43 | + [key: string]: object; // Currently unstructured content |
| 44 | +} |
| 45 | + |
| 46 | +interface Test { |
| 47 | + kind: "suite" | "function" | "parameterizedFunction"; |
| 48 | + id: string; |
| 49 | + name: string; |
| 50 | + testCases?: TestCase[]; |
| 51 | + sourceLocation: SourceLocation; |
| 52 | +} |
| 53 | + |
| 54 | +interface TestCase { |
| 55 | + id: string; |
| 56 | + displayName: string; |
| 57 | +} |
| 58 | + |
| 59 | +// Event types |
| 60 | +interface RunStarted { |
| 61 | + kind: "runStarted"; |
| 62 | +} |
| 63 | + |
| 64 | +interface RunEnded { |
| 65 | + kind: "runEnded"; |
| 66 | +} |
| 67 | + |
| 68 | +interface BaseEvent { |
| 69 | + timestamp: number; |
| 70 | + message: EventMessage[]; |
| 71 | + testID: string; |
| 72 | +} |
| 73 | + |
| 74 | +interface TestStarted extends BaseEvent { |
| 75 | + kind: "testStarted"; |
| 76 | +} |
| 77 | + |
| 78 | +interface TestEnded extends BaseEvent { |
| 79 | + kind: "testEnded"; |
| 80 | +} |
| 81 | + |
| 82 | +interface TestCaseStarted extends BaseEvent { |
| 83 | + kind: "testCaseStarted"; |
| 84 | +} |
| 85 | + |
| 86 | +interface TestCaseEnded extends BaseEvent { |
| 87 | + kind: "testCaseEnded"; |
| 88 | +} |
| 89 | + |
| 90 | +interface TestSkipped extends BaseEvent { |
| 91 | + kind: "testSkipped"; |
| 92 | +} |
| 93 | + |
| 94 | +interface IssueRecorded extends BaseEvent { |
| 95 | + kind: "issueRecorded"; |
| 96 | + sourceLocation: SourceLocation; |
| 97 | +} |
| 98 | + |
| 99 | +export interface EventMessage { |
| 100 | + text: string; |
| 101 | +} |
| 102 | + |
| 103 | +export interface SourceLocation { |
| 104 | + _filePath: string; |
| 105 | + line: number; |
| 106 | + column: number; |
| 107 | +} |
| 108 | + |
| 109 | +export class SwiftTestingOutputParser { |
| 110 | + /** |
| 111 | + * Watches for test events on the named pipe at the supplied path. |
| 112 | + * As events are read they are parsed and recorded in the test run state. |
| 113 | + */ |
| 114 | + public async watch( |
| 115 | + path: string, |
| 116 | + runState: ITestRunState, |
| 117 | + pipeReader?: INamedPipeReader |
| 118 | + ): Promise<void> { |
| 119 | + // Creates a reader based on the platform unless being provided in a test context. |
| 120 | + const reader = pipeReader ?? this.createReader(path); |
| 121 | + const readlinePipe = new Readable({ |
| 122 | + read() {}, |
| 123 | + }); |
| 124 | + |
| 125 | + // Use readline to automatically chunk the data into lines, |
| 126 | + // and then take each line and parse it as JSON. |
| 127 | + const rl = readline.createInterface({ |
| 128 | + input: readlinePipe, |
| 129 | + crlfDelay: Infinity, |
| 130 | + }); |
| 131 | + |
| 132 | + rl.on("line", line => this.parse(JSON.parse(line), runState)); |
| 133 | + |
| 134 | + reader.start(readlinePipe); |
| 135 | + } |
| 136 | + |
| 137 | + private createReader(path: string): INamedPipeReader { |
| 138 | + return process.platform === "win32" |
| 139 | + ? new WindowsNamedPipeReader(path) |
| 140 | + : new UnixNamedPipeReader(path); |
| 141 | + } |
| 142 | + |
| 143 | + private testName(id: string): string { |
| 144 | + const nameMatcher = /^(.*\(.*\))\/(.*)\.swift:\d+:\d+$/; |
| 145 | + const matches = id.match(nameMatcher); |
| 146 | + return !matches ? id : matches[1]; |
| 147 | + } |
| 148 | + |
| 149 | + private parse(item: SwiftTestEvent, runState: ITestRunState) { |
| 150 | + if (item.kind === "event") { |
| 151 | + if (item.payload.kind === "testCaseStarted" || item.payload.kind === "testStarted") { |
| 152 | + const testName = this.testName(item.payload.testID); |
| 153 | + const testIndex = runState.getTestItemIndex(testName, undefined); |
| 154 | + runState.started(testIndex, item.payload.timestamp); |
| 155 | + } else if (item.payload.kind === "testSkipped") { |
| 156 | + const testName = this.testName(item.payload.testID); |
| 157 | + const testIndex = runState.getTestItemIndex(testName, undefined); |
| 158 | + runState.skipped(testIndex); |
| 159 | + } else if (item.payload.kind === "issueRecorded") { |
| 160 | + const testName = this.testName(item.payload.testID); |
| 161 | + const testIndex = runState.getTestItemIndex(testName, undefined); |
| 162 | + const sourceLocation = item.payload.sourceLocation; |
| 163 | + item.payload.message.forEach(message => { |
| 164 | + runState.recordIssue(testIndex, message.text, { |
| 165 | + file: sourceLocation._filePath, |
| 166 | + line: sourceLocation.line, |
| 167 | + column: sourceLocation.column, |
| 168 | + }); |
| 169 | + }); |
| 170 | + } else if (item.payload.kind === "testCaseEnded" || item.payload.kind === "testEnded") { |
| 171 | + const testName = this.testName(item.payload.testID); |
| 172 | + const testIndex = runState.getTestItemIndex(testName, undefined); |
| 173 | + runState.completed(testIndex, { timestamp: item.payload.timestamp }); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments