-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathIssue.swift
591 lines (533 loc) · 22.4 KB
/
Issue.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
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//
// 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
//
/// A type describing a failure or warning which occurred during a test.
public struct Issue: Sendable {
/// Kinds of issues which may be recorded.
public enum Kind: Sendable {
/// An issue which occurred unconditionally, for example by using
/// ``Issue/record(_:sourceLocation:)``.
case unconditional
/// An issue due to a failed expectation, such as those produced by
/// ``expect(_:_:sourceLocation:)``.
///
/// - Parameters:
/// - expectation: The expectation that failed.
indirect case expectationFailed(_ expectation: Expectation)
/// An issue due to a confirmation being confirmed the wrong number of
/// times.
///
/// - Parameters:
/// - actual: The number of times ``Confirmation/confirm(count:)`` was
/// actually called.
/// - expected: The expected number of times
/// ``Confirmation/confirm(count:)`` should have been called.
///
/// This issue can occur when calling ``confirmation(_:expectedCount:isolation:sourceLocation:_:)-5mqz2``
/// or ``confirmation(_:expectedCount:isolation:sourceLocation:_:)-l3il``
/// when the confirmation passed to these functions' `body` closures is
/// confirmed too few or too many times.
indirect case confirmationMiscounted(actual: Int, expected: any RangeExpression & Sendable)
/// An issue due to an `Error` being thrown by a test function and caught by
/// the testing library.
///
/// - Parameters:
/// - error: The error which was associated with this issue.
indirect case errorCaught(_ error: any Error)
/// An issue due to a test reaching its time limit and timing out.
///
/// - Parameters:
/// - timeLimitComponents: The time limit reached by the test.
//
// @Comment {
// - Bug: The associated value of this enumeration case should be an
// instance of `Duration`, but the testing library's deployment target
// predates the introduction of that type.
// }
indirect case timeLimitExceeded(timeLimitComponents: (seconds: Int64, attoseconds: Int64))
/// A known issue was expected, but was not recorded.
case knownIssueNotRecorded
/// An issue due to an `Error` being thrown while attempting to save an
/// attachment to a test report or to disk.
///
/// - Parameters:
/// - error: The error which was associated with this issue.
case valueAttachmentFailed(_ error: any Error)
/// An issue occurred due to misuse of the testing library.
case apiMisused
/// An issue due to a failure in the underlying system, not due to a failure
/// within the tests being run.
case system
}
/// The kind of issue this value represents.
public var kind: Kind
/// An enumeration representing the level of severity of a recorded issue.
///
/// The supported levels, in increasing order of severity, are:
///
/// - ``warning``
/// - ``error``
@_spi(Experimental)
public enum Severity: Sendable {
/// The severity level for an issue which should be noted but is not
/// necessarily an error.
///
/// An issue with warning severity does not cause the test it's associated
/// with to be marked as a failure, but is noted in the results.
case warning
/// The severity level for an issue which represents an error in a test.
///
/// An issue with error severity causes the test it's associated with to be
/// marked as a failure.
case error
}
/// The severity of this issue.
@_spi(Experimental)
public var severity: Severity
/// Any comments provided by the developer and associated with this issue.
///
/// If no comment was supplied when the issue occurred, the value of this
/// property is the empty array.
public var comments: [Comment]
/// A ``SourceContext`` indicating where and how this issue occurred.
@_spi(ForToolsIntegrationOnly)
public var sourceContext: SourceContext
/// Whether or not this issue is known to occur.
@_spi(ForToolsIntegrationOnly)
public var isKnown: Bool = false
/// Initialize an issue instance with the specified details.
///
/// - Parameters:
/// - kind: The kind of issue this value represents.
/// - severity: The severity of this issue. The default value is
/// ``Severity-swift.enum/error``.
/// - comments: An array of comments describing the issue. This array may be
/// empty.
/// - sourceContext: A ``SourceContext`` indicating where and how this issue
/// occurred.
init(
kind: Kind,
severity: Severity = .error,
comments: [Comment],
sourceContext: SourceContext
) {
self.kind = kind
self.severity = severity
self.comments = comments
self.sourceContext = sourceContext
}
/// Initialize an issue instance representing a caught error.
///
/// - Parameters:
/// - error: The error which was caught which this issue is describing.
/// - sourceLocation: The source location of the issue. This value is used
/// to construct an instance of ``SourceContext``.
///
/// The ``sourceContext`` property will have a ``SourceContext/backtrace``
/// property whose value is the backtrace for the first throw of `error`.
init(
for error: any Error,
sourceLocation: SourceLocation? = nil
) {
let sourceContext = SourceContext(backtrace: Backtrace(forFirstThrowOf: error), sourceLocation: sourceLocation)
self.init(
kind: .errorCaught(error),
comments: [],
sourceContext: sourceContext
)
}
/// The error which was associated with this issue, if any.
///
/// The value of this property is non-`nil` when ``kind-swift.property`` is
/// ``Kind-swift.enum/errorCaught(_:)``.
public var error: (any Error)? {
if case let .errorCaught(error) = kind {
return error
}
return nil
}
/// The location in source where this issue occurred, if available.
public var sourceLocation: SourceLocation? {
get {
sourceContext.sourceLocation
}
set {
sourceContext.sourceLocation = newValue
}
}
}
extension Issue.Severity: Comparable {}
// MARK: - CustomStringConvertible, CustomDebugStringConvertible
extension Issue: CustomStringConvertible, CustomDebugStringConvertible {
public var description: String {
let joinedComments = if comments.isEmpty {
""
} else {
": " + comments.lazy
.map(\.rawValue)
.joined(separator: "\n")
}
return "\(kind) (\(severity))\(joinedComments)"
}
public var debugDescription: String {
let joinedComments = if comments.isEmpty {
""
} else {
": " + comments.lazy
.map(\.rawValue)
.joined(separator: "\n")
}
return "\(kind)\(sourceLocation.map { " at \($0)" } ?? "") (\(severity))\(joinedComments)"
}
}
/// An empty protocol defining a type that conforms to `RangeExpression<Int>`.
///
/// In the future, when our minimum deployment target supports casting a value
/// to a constrained existential type ([SE-0353](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0353-constrained-existential-types.md#effect-on-abi-stability)),
/// we can remove this protocol and cast to `RangeExpression<Int>` instead.
private protocol _RangeExpressionOverIntValues: RangeExpression & Sequence where Bound == Int, Element == Int {}
extension ClosedRange<Int>: _RangeExpressionOverIntValues {}
extension PartialRangeFrom<Int>: _RangeExpressionOverIntValues {}
extension Range<Int>: _RangeExpressionOverIntValues {}
extension Issue.Kind: CustomStringConvertible {
public var description: String {
switch self {
case .unconditional:
// Although the failure is unconditional at the point it is recorded, the
// code that recorded the issue may not be unconditionally executing, so
// we shouldn't describe it as unconditional (we just don't know!)
return "Issue recorded"
case let .expectationFailed(expectation):
return if let mismatchedErrorDescription = expectation.mismatchedErrorDescription {
"Expectation failed: \(mismatchedErrorDescription)"
} else if let mismatchedExitConditionDescription = expectation.mismatchedExitConditionDescription {
"Expectation failed: \(mismatchedExitConditionDescription)"
} else {
"Expectation failed: \(expectation.evaluatedExpression.expandedDescription())"
}
case let .confirmationMiscounted(actual: actual, expected: expected):
if let expected = expected as? any _RangeExpressionOverIntValues {
let lowerBound = expected.first { _ in true }
if let lowerBound {
// Not actually an upper bound, just "any value greater than the lower
// bound." That's sufficient for us to determine if the range contains
// a single value.
let upperBound = expected.first { $0 > lowerBound }
if upperBound == nil {
return "Confirmation was confirmed \(actual.counting("time")), but expected to be confirmed \(lowerBound.counting("time"))"
}
}
}
return "Confirmation was confirmed \(actual.counting("time")), but expected to be confirmed \(String(describingForTest: expected)) time(s)"
case let .errorCaught(error):
return "Caught error: \(error)"
case let .timeLimitExceeded(timeLimitComponents: timeLimitComponents):
return "Time limit was exceeded: \(TimeValue(timeLimitComponents))"
case .knownIssueNotRecorded:
return "Known issue was not recorded"
case let .valueAttachmentFailed(error):
return "Caught error while saving attachment: \(error)"
case .apiMisused:
return "An API was misused"
case .system:
return "A system failure occurred"
}
}
}
extension Issue.Severity: CustomStringConvertible {
public var description: String {
switch self {
case .warning:
"warning"
case .error:
"error"
}
}
}
#if !SWT_NO_SNAPSHOT_TYPES
// MARK: - Snapshotting
extension Issue {
/// A serializable type describing a failure or warning which occurred during a test.
@_spi(ForToolsIntegrationOnly)
public struct Snapshot: Sendable, Codable {
/// The kind of issue this value represents.
public var kind: Kind.Snapshot
/// The severity of this issue.
@_spi(Experimental)
public var severity: Severity
/// Any comments provided by the developer and associated with this issue.
///
/// If no comment was supplied when the issue occurred, the value of this
/// property is the empty array.
public var comments: [Comment]
/// A ``SourceContext`` indicating where and how this issue occurred.
public var sourceContext: SourceContext
/// Whether or not this issue is known to occur.
public var isKnown: Bool = false
/// Initialize an issue instance with the specified details.
///
/// - Parameter issue: The original issue that gets snapshotted.
public init(snapshotting issue: borrowing Issue) {
if case .confirmationMiscounted = issue.kind {
// Work around poor stringification of this issue kind in Xcode 16.
self.kind = .unconditional
self.comments = CollectionOfOne("\(issue.kind)") + issue.comments
} else {
self.kind = Issue.Kind.Snapshot(snapshotting: issue.kind)
self.comments = issue.comments
}
self.severity = issue.severity
self.sourceContext = issue.sourceContext
self.isKnown = issue.isKnown
}
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.kind = try container.decode(Issue.Kind.Snapshot.self, forKey: .kind)
self.comments = try container.decode([Comment].self, forKey: .comments)
self.sourceContext = try container.decode(SourceContext.self, forKey: .sourceContext)
self.isKnown = try container.decode(Bool.self, forKey: .isKnown)
// Severity is a new field, so fall back to .error if it's not present.
self.severity = try container.decodeIfPresent(Issue.Severity.self, forKey: .severity) ?? .error
}
/// The error which was associated with this issue, if any.
///
/// The value of this property is non-`nil` when ``kind-swift.property`` is
/// ``Kind-swift.enum/errorCaught(_:)``.
public var error: (any Error)? {
if case let .errorCaught(error) = kind {
return error
}
return nil
}
/// The location in source where this issue occurred, if available.
public var sourceLocation: SourceLocation? {
get {
sourceContext.sourceLocation
}
set {
sourceContext.sourceLocation = newValue
}
}
}
}
extension Issue.Severity: Codable {}
extension Issue.Kind {
/// Serializable kinds of issues which may be recorded.
@_spi(ForToolsIntegrationOnly)
public enum Snapshot: Sendable, Codable {
/// An issue which occurred unconditionally, for example by using
/// ``Issue/record(_:sourceLocation:)``.
case unconditional
/// An issue due to a failed expectation, such as those produced by
/// ``expect(_:_:sourceLocation:)``.
///
/// - Parameters:
/// - expectation: The expectation that failed.
indirect case expectationFailed(_ expectation: Expectation.Snapshot)
/// An issue due to a confirmation being confirmed the wrong number of
/// times.
///
/// - Parameters:
/// - actual: The number of times ``Confirmation/confirm(count:)`` was
/// actually called.
/// - expected: The expected number of times
/// ``Confirmation/confirm(count:)`` should have been called.
///
/// This issue can occur when calling
/// ``confirmation(_:expectedCount:isolation:sourceLocation:_:)-5mqz2`` when
/// the confirmation passed to these functions' `body` closures is confirmed
/// too few or too many times.
indirect case confirmationMiscounted(actual: Int, expected: Int)
/// An issue due to an `Error` being thrown by a test function and caught by
/// the testing library.
///
/// - Parameters:
/// - error: A snapshot of the underlying error which was associated with
/// this issue.
indirect case errorCaught(_ error: ErrorSnapshot)
/// An issue due to a test reaching its time limit and timing out.
///
/// - Parameters:
/// - timeLimitComponents: The time limit reached by the test.
//
// @Comment {
// - Bug: The associated value of this enumeration case should be an
// instance of `Duration`, but the testing library's deployment target
// predates the introduction of that type.
// }
indirect case timeLimitExceeded(timeLimitComponents: (seconds: Int64, attoseconds: Int64))
/// A known issue was expected, but was not recorded.
case knownIssueNotRecorded
/// An issue occurred due to misuse of the testing library.
case apiMisused
/// An issue due to a failure in the underlying system, not due to a failure
/// within the tests being run.
case system
/// Initialize an instance of this type by snapshotting the specified issue
/// kind.
///
/// - Parameters:
/// - kind: The original issue kind to snapshot.
public init(snapshotting kind: Issue.Kind) {
self = switch kind {
case .unconditional:
.unconditional
case let .expectationFailed(expectation):
.expectationFailed(Expectation.Snapshot(snapshotting: expectation))
case .confirmationMiscounted:
.unconditional
case let .errorCaught(error), let .valueAttachmentFailed(error):
.errorCaught(ErrorSnapshot(snapshotting: error))
case let .timeLimitExceeded(timeLimitComponents: timeLimitComponents):
.timeLimitExceeded(timeLimitComponents: timeLimitComponents)
case .knownIssueNotRecorded:
.knownIssueNotRecorded
case .apiMisused:
.apiMisused
case .system:
.system
}
}
/// The keys used to encode ``Issue.Kind``.
private enum _CodingKeys: CodingKey {
case unconditional
case expectationFailed
case confirmationMiscounted
case errorCaught
case timeLimitExceeded
case knownIssueNotRecorded
case apiMisused
case system
/// The keys used to encode ``Issue.Kind.expectationFailed``.
enum _ExpectationFailedKeys: CodingKey {
case expectation
}
/// The keys used to encode ``Issue.Kind.confirmationMiscount``.
enum _ConfirmationMiscountedKeys: CodingKey {
case actual
case expected
}
/// The keys used to encode``Issue.Kind.errorCaught``.
enum _ErrorCaughtKeys: CodingKey {
case error
}
}
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: _CodingKeys.self)
if try container.decodeIfPresent(Bool.self, forKey: .unconditional) != nil {
self = .unconditional
} else if let expectationFailedContainer = try? container.nestedContainer(keyedBy: _CodingKeys._ExpectationFailedKeys.self,
forKey: .expectationFailed) {
self = .expectationFailed(try expectationFailedContainer.decode(Expectation.Snapshot.self, forKey: .expectation))
} else if let confirmationMiscountedContainer = try? container.nestedContainer(keyedBy: _CodingKeys._ConfirmationMiscountedKeys.self,
forKey: .confirmationMiscounted) {
self = .confirmationMiscounted(actual: try confirmationMiscountedContainer.decode(Int.self,
forKey: .actual),
expected: try confirmationMiscountedContainer.decode(Int.self,
forKey: .expected))
} else if let errorCaught = try? container.nestedContainer(keyedBy: _CodingKeys._ErrorCaughtKeys.self,
forKey: .errorCaught) {
self = .errorCaught(try errorCaught.decode(ErrorSnapshot.self, forKey: .error))
} else if let timeLimit = try container.decodeIfPresent(TimeValue.self, forKey: .timeLimitExceeded) {
self = .timeLimitExceeded(timeLimitComponents: timeLimit.components)
} else if try container.decodeIfPresent(Bool.self, forKey: .knownIssueNotRecorded) != nil {
self = .knownIssueNotRecorded
} else if try container.decodeIfPresent(Bool.self, forKey: .apiMisused) != nil {
self = .apiMisused
} else if try container.decodeIfPresent(Bool.self, forKey: .system) != nil {
self = .system
} else {
throw DecodingError.valueNotFound(
Self.self,
DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "Value found did not match any of the existing cases for Issue.Kind."
)
)
}
}
public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: _CodingKeys.self)
switch self {
case .unconditional:
try container.encode(true, forKey: .unconditional)
case let .expectationFailed(expectation):
var errorCaughtContainer = container.nestedContainer(keyedBy: _CodingKeys._ExpectationFailedKeys.self,
forKey: .expectationFailed)
try errorCaughtContainer.encode(expectation, forKey: .expectation)
case let .confirmationMiscounted(actual, expected):
var confirmationMiscountedContainer = container.nestedContainer(keyedBy: _CodingKeys._ConfirmationMiscountedKeys.self,
forKey: .confirmationMiscounted)
try confirmationMiscountedContainer.encode(actual, forKey: .actual)
try confirmationMiscountedContainer.encode(expected, forKey: .expected)
case let .errorCaught(error):
var errorCaughtContainer = container.nestedContainer(keyedBy: _CodingKeys._ErrorCaughtKeys.self, forKey: .errorCaught)
try errorCaughtContainer.encode(error, forKey: .error)
case let .timeLimitExceeded(timeLimitComponents):
try container.encode(TimeValue(timeLimitComponents), forKey: .timeLimitExceeded)
case .knownIssueNotRecorded:
try container.encode(true, forKey: .knownIssueNotRecorded)
case .apiMisused:
try container.encode(true, forKey: .apiMisused)
case .system:
try container.encode(true, forKey: .system)
}
}
}
}
// MARK: - Snapshot CustomStringConvertible, CustomDebugStringConvertible
extension Issue.Snapshot: CustomStringConvertible, CustomDebugStringConvertible {
public var description: String {
let joinedComments = if comments.isEmpty {
""
} else {
": " + comments.lazy
.map(\.rawValue)
.joined(separator: "\n")
}
return "\(kind) (\(severity))\(joinedComments)"
}
public var debugDescription: String {
let joinedComments = if comments.isEmpty {
""
} else {
": " + comments.lazy
.map(\.rawValue)
.joined(separator: "\n")
}
return "\(kind)\(sourceLocation.map { " at \($0)" } ?? "") (\(severity))\(joinedComments)"
}
}
extension Issue.Kind.Snapshot: CustomStringConvertible {
public var description: String {
switch self {
case .unconditional:
"Issue recorded"
case let .expectationFailed(expectation):
if let mismatchedErrorDescription = expectation.mismatchedErrorDescription {
"Expectation failed: \(mismatchedErrorDescription)"
} else {
"Expectation failed: \(expectation.evaluatedExpression.expandedDescription())"
}
case let .confirmationMiscounted(actual: actual, expected: expected):
"Confirmation was confirmed \(actual.counting("time")), but expected to be confirmed \(expected.counting("time"))"
case let .errorCaught(error):
"Caught error: \(error)"
case let .timeLimitExceeded(timeLimitComponents: timeLimitComponents):
"Time limit was exceeded: \(TimeValue(timeLimitComponents))"
case .knownIssueNotRecorded:
"Known issue was not recorded"
case .apiMisused:
"An API was misused"
case .system:
"A system failure occurred"
}
}
}
#endif