Skip to content

Commit 15892d8

Browse files
authored
Fix a crash when an issue is recorded on a detached task. (#592)
Given the following code: ```swift @test func f() async { await Task.detached { #expect(1 == 2) }.value } ``` An issue is recorded on a detached task, but Swift Testing is unable to determine at runtime which test it corresponds to. This should simply result in the issue being attributed to an arbitrary test, or to the aether, but #421 caused a regression that triggers an optional unwrap crash instead. This PR fixes the crash. :) ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent e4170a8 commit 15892d8

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

Sources/Testing/Events/Recorder/Event.HumanReadableOutputRecorder.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -262,15 +262,16 @@ extension Event.HumanReadableOutputRecorder {
262262
}
263263

264264
case let .issueRecorded(issue):
265-
let test = test!
266-
let id = test.id.keyPathRepresentation
267-
var testData = context.testData[id] ?? .init(startInstant: instant)
268-
if issue.isKnown {
269-
testData.knownIssueCount += 1
270-
} else {
271-
testData.issueCount += 1
265+
if let test {
266+
let id = test.id.keyPathRepresentation
267+
var testData = context.testData[id] ?? .init(startInstant: instant)
268+
if issue.isKnown {
269+
testData.knownIssueCount += 1
270+
} else {
271+
testData.issueCount += 1
272+
}
273+
context.testData[id] = testData
272274
}
273-
context.testData[id] = testData
274275

275276
default:
276277
// These events do not manipulate the context structure.

Tests/TestingTests/EventRecorderTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,16 @@ struct EventRecorderTests {
359359
}
360360
}
361361
#endif
362+
363+
@Test("Recorded issues may not have associated tests")
364+
func issueWithoutTest() {
365+
let issue = Issue(kind: .unconditional, comments: [], sourceContext: .init())
366+
let event = Event(.issueRecorded(issue), testID: nil, testCaseID: nil)
367+
let context = Event.Context(test: nil, testCase: nil)
368+
369+
let recorder = Event.HumanReadableOutputRecorder()
370+
_ = recorder.record(event, in: context)
371+
}
362372
}
363373

364374
// MARK: - Fixtures

0 commit comments

Comments
 (0)