Skip to content

Commit 690f3bb

Browse files
committed
bit more cheese
1 parent 5536aa3 commit 690f3bb

File tree

3 files changed

+40
-59
lines changed

3 files changed

+40
-59
lines changed

Sources/Testing/ABI/Encoded/ABI.EncodedIssue.swift

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ extension ABI {
5858
/// - Warning: Errors are not yet part of the JSON schema.
5959
var _error: EncodedError<V>?
6060

61+
/// The comment associated with the call to `withKnownIssue()` that
62+
/// generated this issue.
63+
///
64+
/// - Warning: This field is not yet part of the JSON schema.
65+
var _knownIssueComment: String?
66+
6167
init(encoding issue: borrowing Issue, in eventContext: borrowing Event.Context) {
6268
// >= v0
6369
isKnown = issue.isKnown
@@ -80,6 +86,9 @@ extension ABI {
8086
if let error = issue.error {
8187
_error = EncodedError(encoding: error, in: eventContext)
8288
}
89+
if let knownIssueContext = issue.knownIssueContext {
90+
_knownIssueComment = knownIssueContext.comment?.rawValue
91+
}
8392
}
8493
}
8594
}
@@ -89,47 +98,3 @@ extension ABI {
8998

9099
extension ABI.EncodedIssue: Codable {}
91100
extension ABI.EncodedIssue.Severity: Codable {}
92-
93-
// MARK: - Converting back to an Issue
94-
95-
extension Issue {
96-
/// Attempt to reconstruct an instance of ``Issue`` from the given encoded
97-
/// event.
98-
///
99-
/// - Parameters:
100-
/// - event: The event that may contain an encoded issue.
101-
///
102-
/// If `event` does not represent an issue, this initializer returns `nil`.
103-
init?<V>(_ event: ABI.EncodedEvent<V>) {
104-
guard let issue = event.issue else {
105-
return nil
106-
}
107-
108-
// Translate the issue back into a "real" issue and record it
109-
// in the parent process. This translation is, of course, lossy
110-
// due to the process boundary, but we make a best effort.
111-
let comments: [Comment] = event.messages.map(\.text).map(Comment.init(rawValue:))
112-
let issueKind: Issue.Kind = if let error = issue._error {
113-
.errorCaught(error)
114-
} else {
115-
// TODO: improve fidelity of issue kind reporting (especially those without associated values)
116-
.unconditional
117-
}
118-
let severity: Issue.Severity = switch issue.severity {
119-
case .warning:
120-
.warning
121-
case nil, .error:
122-
.error
123-
}
124-
let sourceContext = SourceContext(
125-
backtrace: nil, // `issue._backtrace` will have the wrong address space.
126-
sourceLocation: issue.sourceLocation
127-
)
128-
self.init(kind: issueKind, severity: severity, comments: comments, sourceContext: sourceContext)
129-
if issue.isKnown {
130-
// The known issue comment, if there was one, is already included in
131-
// the `comments` array above.
132-
knownIssueContext = Issue.KnownIssueContext()
133-
}
134-
}
135-
}

Sources/Testing/Events/Event+FallbackHandler.swift

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,26 @@ extension Event {
3333
sourceLocation: event._sourceLocation
3434
)
3535
lazy var skipInfo = SkipInfo(comment: comments.first, sourceContext: sourceContext)
36-
if let issue = Issue(event) {
37-
issue.record()
36+
if let issue = event.issue {
37+
// Translate the issue back into a "real" issue and record it in the
38+
// parent process. This translation is, of course, lossy due to the ABI
39+
// and/or process boundary, but we make a best effort.
40+
let issueKind: Issue.Kind = if let error = issue._error {
41+
.errorCaught(error)
42+
} else {
43+
// TODO: improve fidelity of issue kind reporting (especially those without associated values)
44+
.unconditional
45+
}
46+
let severity: Issue.Severity = switch issue.severity {
47+
case .warning: .warning
48+
case nil, .error: .error
49+
}
50+
var issueCopy = Issue(kind: issueKind, severity: severity, comments: comments, sourceContext: sourceContext)
51+
if issue.isKnown {
52+
issueCopy.knownIssueContext = Issue.KnownIssueContext()
53+
issueCopy.knownIssueContext?.comment = issue._knownIssueComment.map(Comment.init(rawValue:))
54+
}
55+
issueCopy.record()
3856
} else if let attachment = event.attachment {
3957
Attachment.record(attachment, sourceLocation: event._sourceLocation!)
4058
} else if case .testCancelled = event.kind {
@@ -44,17 +62,6 @@ extension Event {
4462
}
4563
}
4664

47-
/// The implementation of ``fallbackEventHandler``.
48-
///
49-
/// - Parameters:
50-
/// - abi: The ABI version to use for decoding `recordJSON`.
51-
/// - recordJSON: The JSON encoding of an event record.
52-
///
53-
/// - Throws: Any error that prevented handling the encoded record.
54-
private static func _fallbackEventHandler<V>(_ abi: V.Type, _ recordJSON: UnsafeRawBufferPointer) throws where V: ABI.Version {
55-
try handle(recordJSON, encodedWith: abi)
56-
}
57-
5865
/// The fallback event handler to set when Swift Testing is the active testing
5966
/// library.
6067
private static let _fallbackEventHandler: FallbackEventHandler = { recordJSONSchemaVersionNumber, recordJSONBaseAddress, recordJSONByteCount, _ in
@@ -63,7 +70,7 @@ extension Event {
6370
.flatMap(ABI.version(forVersionNumber:))
6471
if let abi {
6572
let recordJSON = UnsafeRawBufferPointer(start: recordJSONBaseAddress, count: recordJSONByteCount)
66-
try! Self._fallbackEventHandler(abi, recordJSON)
73+
try! Self.handle(recordJSON, encodedWith: abi)
6774
}
6875
}
6976

Tests/TestingTests/EventTests.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private func MockXCTAssert(_ condition: Bool, _ message: String, _ sourceLocatio
128128
}
129129
}
130130

131-
private func MockXCTAttachmentAdd(_ string: String, named name: String) {
131+
private func MockXCTAttachmentAdd(_ string: String, named name: String, _ sourceLocation: SourceLocation = #_sourceLocation) {
132132
#expect(throws: Never.self) {
133133
guard let fallbackEventHandler = fallbackEventHandler() else {
134134
return
@@ -150,6 +150,15 @@ private func MockXCTAttachmentAdd(_ string: String, named name: String) {
150150
"_preferredName": name
151151
],
152152
"messages": [],
153+
"_comments": [
154+
"comment #1",
155+
],
156+
"_sourceLocation": [
157+
"fileID": sourceLocation.fileID,
158+
"_filePath": sourceLocation._filePath,
159+
"line": sourceLocation.line,
160+
"column": sourceLocation.column,
161+
]
153162
],
154163
]
155164

0 commit comments

Comments
 (0)