-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathEvent.HumanReadableOutputRecorder.swift
455 lines (409 loc) · 15.3 KB
/
Event.HumanReadableOutputRecorder.swift
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
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//
extension Event {
/// A type which handles ``Event`` instances and outputs representations of
/// them as human-readable messages.
///
/// This type can be used compositionally to produce output in other
/// human-readable formats such as rich text or HTML.
///
/// The format of the output is not meant to be machine-readable and is
/// subject to change. For machine-readable output, use ``JUnitXMLRecorder``.
@_spi(ForToolsIntegrationOnly)
public struct HumanReadableOutputRecorder: Sendable, ~Copyable {
/// A type describing a human-readable message produced by an instance of
/// ``Event/HumanReadableOutputRecorder``.
public struct Message: Sendable {
/// The symbol associated with this message, if any.
var symbol: Symbol?
/// The human-readable message.
var stringValue: String
}
/// A type that contains mutable context for
/// ``Event/ConsoleOutputRecorder``.
private struct _Context {
/// The instant at which the run started.
var runStartInstant: Test.Clock.Instant?
/// The instant at which the current iteration started.
var iterationStartInstant: Test.Clock.Instant?
/// The number of tests started or skipped during the run.
///
/// This value does not include test suites.
var testCount = 0
/// The number of test suites started or skipped during the run.
var suiteCount = 0
/// A type describing data tracked on a per-test basis.
struct TestData {
/// The instant at which the test started.
var startInstant: Test.Clock.Instant
/// The number of issues recorded for the test.
var issueCount = 0
/// The number of known issues recorded for the test.
var knownIssueCount = 0
}
/// Data tracked on a per-test basis.
var testData = Graph<String, TestData?>()
}
/// This event recorder's mutable context about events it has received,
/// which may be used to inform how subsequent events are written.
private var _context = Locked(rawValue: _Context())
/// Initialize a new human-readable event recorder.
///
/// Output from the testing library is converted to "messages" using
/// ``Event/HumanReadableOutputRecorder/record(_:in:verbosely:)``. The
/// format of those messages is, as the type's name suggests, not meant to
/// be machine-readable and is subject to change.
public init() {}
}
}
// MARK: -
extension Event.HumanReadableOutputRecorder {
/// Get a string representing an array of comments, formatted for output.
///
/// - Parameters:
/// - comments: The comments that should be formatted.
///
/// - Returns: A formatted string representing `comments`, or `nil` if there
/// are none.
private func _formattedComments(_ comments: [Comment]) -> [Message] {
// Insert an arrow character at the start of each comment, then indent any
// additional lines in the comment to align them with the arrow.
comments.lazy
.flatMap { comment in
let lines = comment.rawValue.split(whereSeparator: \.isNewline)
if let firstLine = lines.first {
let remainingLines = lines.dropFirst()
return CollectionOfOne(Message(symbol: .details, stringValue: String(firstLine))) + remainingLines.lazy
.map(String.init)
.map { Message(stringValue: $0) }
}
return []
}
}
/// Get a string representing the comments attached to a test, formatted for
/// output.
///
/// - Parameters:
/// - test: The test whose comments should be formatted.
///
/// - Returns: A formatted string representing the comments attached to `test`,
/// or `nil` if there are none.
private func _formattedComments(for test: Test) -> [Message] {
_formattedComments(test.comments(from: Comment.self))
}
/// Get the total number of issues recorded in a graph of test data
/// structures.
///
/// - Parameters:
/// - graph: The graph to walk while counting issues.
///
/// - Returns: A tuple containing the number of issues recorded in `graph`.
private func _issueCounts(in graph: Graph<String, Event.HumanReadableOutputRecorder._Context.TestData?>?) -> (issueCount: Int, knownIssueCount: Int, totalIssueCount: Int, description: String) {
guard let graph else {
return (0, 0, 0, "")
}
let issueCount = graph.compactMap(\.value?.issueCount).reduce(into: 0, +=)
let knownIssueCount = graph.compactMap(\.value?.knownIssueCount).reduce(into: 0, +=)
let totalIssueCount = issueCount + knownIssueCount
// Construct a string describing the issue counts.
let description = switch (issueCount > 0, knownIssueCount > 0) {
case (true, true):
" with \(totalIssueCount.counting("issue")) (including \(knownIssueCount.counting("known issue")))"
case (false, true):
" with \(knownIssueCount.counting("known issue"))"
case (true, false):
" with \(totalIssueCount.counting("issue"))"
case(false, false):
""
}
return (issueCount, knownIssueCount, totalIssueCount, description)
}
}
/// Generate a title for the specified test (either "Test" or "Suite"),
/// capitalized and suitable for use as the leading word of a human-readable
/// message string.
///
/// - Parameters:
/// - test: The test to generate a description for, if any.
///
/// - Returns: A human-readable title for the specified test. Defaults to "Test"
/// if `test` is `nil`.
private func _capitalizedTitle(for test: Test?) -> String {
test?.isSuite == true ? "Suite" : "Test"
}
extension Test.Case {
/// The arguments of this test case, formatted for presentation, prefixed by
/// their corresponding parameter label when available.
///
/// - Parameters:
/// - includeTypeNames: Whether the qualified type name of each argument's
/// runtime type should be included. Defaults to `false`.
fileprivate func labeledArguments(includingQualifiedTypeNames includeTypeNames: Bool = false) -> String {
arguments.lazy
.map { argument in
let valueDescription = String(describingForTest: argument.value)
let label = argument.parameter.secondName ?? argument.parameter.firstName
let labeledArgument = if label == "_" {
valueDescription
} else {
"\(label) → \(valueDescription)"
}
if includeTypeNames {
let typeInfo = TypeInfo(describingTypeOf: argument.value)
return "\(labeledArgument) (\(typeInfo.qualifiedName))"
} else {
return labeledArgument
}
}
.joined(separator: ", ")
}
}
// MARK: -
extension Event.HumanReadableOutputRecorder {
/// Record the specified event by generating zero or more messages that
/// describe it.
///
/// - Parameters:
/// - event: The event to record.
/// - eventContext: The context associated with the event.
/// - verbose: Whether or not to record verbose output.
///
/// - Returns: An array of zero or more messages that can be displayed to the
/// user.
@discardableResult public func record(
_ event: borrowing Event,
in eventContext: borrowing Event.Context,
verbosely verbose: Bool = false
) -> [Message] {
let test = eventContext.test
var testName: String
if let displayName = test?.displayName {
testName = "\"\(displayName)\""
} else if let test {
testName = test.name
} else {
testName = "«unknown»"
}
let instant = event.instant
switch event.kind {
case .runStarted:
_context.withLock { context in
context.runStartInstant = instant
}
var comments = [Comment]()
if verbose {
comments.append("Swift Version: \(swiftStandardLibraryVersion)")
}
comments.append("Testing Library Version: \(testingLibraryVersion)")
if verbose {
#if targetEnvironment(simulator)
comments.append("OS Version (Simulator): \(simulatorVersion)")
comments.append("OS Version (Host): \(operatingSystemVersion)")
#else
comments.append("OS Version: \(operatingSystemVersion)")
#endif
}
return CollectionOfOne(
Message(
symbol: .default,
stringValue: "Test run started."
)
) + _formattedComments(comments)
case let .iterationStarted(index):
if let iterationCount = Configuration.current?.repetitionPolicy.maximumIterationCount, iterationCount > 1 {
_context.withLock { context in
context.iterationStartInstant = instant
}
return [
Message(
symbol: .default,
stringValue: "Iteration \(index + 1) started."
)
]
}
case .planStepStarted, .planStepEnded:
// Suppress events of these kinds from output as they are not generally
// interesting in human-readable output.
break
case .testStarted:
let test = test!
_context.withLock { context in
context.testData[test.id.keyPathRepresentation] = .init(startInstant: instant)
if test.isSuite {
context.suiteCount += 1
} else {
context.testCount += 1
}
}
return [
Message(
symbol: .default,
stringValue: "\(_capitalizedTitle(for: test)) \(testName) started."
)
]
case .testEnded:
let test = test!
let id = test.id
let testDataGraph = _context.rawValue.testData.subgraph(at: id.keyPathRepresentation)
let testData = testDataGraph?.value ?? .init(startInstant: instant)
let issues = _issueCounts(in: testDataGraph)
let duration = testData.startInstant.descriptionOfDuration(to: instant)
return if issues.issueCount > 0 {
CollectionOfOne(
Message(
symbol: .fail,
stringValue: "\(_capitalizedTitle(for: test)) \(testName) failed after \(duration)\(issues.description)."
)
) + _formattedComments(for: test)
} else {
[
Message(
symbol: .pass(knownIssueCount: issues.knownIssueCount),
stringValue: "\(_capitalizedTitle(for: test)) \(testName) passed after \(duration)\(issues.description)."
)
]
}
case let .testSkipped(skipInfo):
let test = test!
_context.withLock { context in
if test.isSuite {
context.suiteCount += 1
} else {
context.testCount += 1
}
}
return if let comment = skipInfo.comment {
[
Message(symbol: .skip, stringValue: "\(_capitalizedTitle(for: test)) \(testName) skipped: \"\(comment.rawValue)\"")
]
} else {
[
Message(symbol: .skip, stringValue: "\(_capitalizedTitle(for: test)) \(testName) skipped.")
]
}
case .expectationChecked:
// Suppress events of this kind from output as they are not generally
// interesting in human-readable output.
break
case let .issueRecorded(issue):
if let test {
let id = test.id.keyPathRepresentation
_context.withLock { context in
var testData = context.testData[id] ?? .init(startInstant: instant)
if issue.isKnown {
testData.knownIssueCount += 1
} else {
testData.issueCount += 1
}
context.testData[id] = testData
}
}
let parameterCount = if let parameters = test?.parameters {
parameters.count
} else {
0
}
let labeledArguments = if let testCase = eventContext.testCase {
testCase.labeledArguments()
} else {
""
}
let symbol: Event.Symbol
let known: String
if issue.isKnown {
symbol = .pass(knownIssueCount: 1)
known = " known"
} else {
symbol = .fail
known = "n"
}
var additionalMessages = [Message]()
if case let .expectationFailed(expectation) = issue.kind, let differenceDescription = expectation.differenceDescription {
additionalMessages.append(Message(symbol: .difference, stringValue: differenceDescription))
}
additionalMessages += _formattedComments(issue.comments)
if verbose, case let .expectationFailed(expectation) = issue.kind {
let expression = expectation.evaluatedExpression
func addMessage(about expression: Expression) {
let description = expression.expandedDescription(includingTypeNames: true, includingParenthesesIfNeeded: false)
additionalMessages.append(Message(symbol: .details, stringValue: description))
}
let subexpressions = expression.subexpressions
if subexpressions.isEmpty {
addMessage(about: expression)
} else {
for subexpression in subexpressions {
addMessage(about: subexpression)
}
}
}
let atSourceLocation = issue.sourceLocation.map { " at \($0)" } ?? ""
let primaryMessage: Message = if parameterCount == 0 {
Message(
symbol: symbol,
stringValue: "\(_capitalizedTitle(for: test)) \(testName) recorded a\(known) issue\(atSourceLocation): \(issue.kind)"
)
} else {
Message(
symbol: symbol,
stringValue: "\(_capitalizedTitle(for: test)) \(testName) recorded a\(known) issue with \(parameterCount.counting("argument")) \(labeledArguments)\(atSourceLocation): \(issue.kind)"
)
}
return CollectionOfOne(primaryMessage) + additionalMessages
case .testCaseStarted:
guard let testCase = eventContext.testCase, testCase.isParameterized else {
break
}
return [
Message(
symbol: .default,
stringValue: "Passing \(testCase.arguments.count.counting("argument")) \(testCase.labeledArguments(includingQualifiedTypeNames: verbose)) to \(testName)"
)
]
case .testCaseEnded:
break
case let .iterationEnded(index):
guard let iterationStartInstant = _context.rawValue.iterationStartInstant else {
break
}
let duration = iterationStartInstant.descriptionOfDuration(to: instant)
return [
Message(
symbol: .default,
stringValue: "Iteration \(index + 1) ended after \(duration)."
)
]
case .runEnded:
let context = _context.rawValue
let testCount = context.testCount
let issues = _issueCounts(in: context.testData)
let runStartInstant = context.runStartInstant ?? instant
let duration = runStartInstant.descriptionOfDuration(to: instant)
return if issues.issueCount > 0 {
[
Message(
symbol: .fail,
stringValue: "Test run with \(testCount.counting("test")) failed after \(duration)\(issues.description)."
)
]
} else {
[
Message(
symbol: .pass(knownIssueCount: issues.knownIssueCount),
stringValue: "Test run with \(testCount.counting("test")) passed after \(duration)\(issues.description)."
)
]
}
}
return []
}
}
// MARK: - Codable
extension Event.HumanReadableOutputRecorder.Message: Codable {}