Skip to content

Commit 330d0fb

Browse files
committed
Add enforcement for returned issues of kind 'apiMisused' as described in proposal
1 parent a770f27 commit 330d0fb

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

Sources/Testing/Traits/IssueHandlingTrait.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,20 @@ extension IssueHandlingTrait: TestScoping {
121121
}
122122

123123
if let newIssue {
124-
// Prohibit assigning the issue's kind to system.
125-
if case .system = newIssue.kind {
124+
// Validate the value of the returned issue's 'kind' property.
125+
switch (issue.kind, newIssue.kind) {
126+
case (_, .system):
127+
// Prohibited by ST-0011.
126128
preconditionFailure("Issue returned by issue handling closure cannot have kind 'system': \(newIssue)")
129+
case (.apiMisused, .apiMisused):
130+
// This is permitted, but must be listed explicitly before the
131+
// wildcard case below.
132+
break
133+
case (_, .apiMisused):
134+
// Prohibited by ST-0011.
135+
preconditionFailure("Issue returned by issue handling closure cannot have kind 'apiMisused' when the passed-in issue had a different kind: \(newIssue)")
136+
default:
137+
break
127138
}
128139

129140
var event = event

Tests/TestingTests/Traits/IssueHandlingTraitTests.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,27 @@ struct IssueHandlingTraitTests {
216216
}.run(configuration: configuration)
217217
}
218218

219+
@Test("An API misused issue can be returned by issue handler closure when the original issue had that kind")
220+
func returningAPIMisusedIssue() async throws {
221+
var configuration = Configuration()
222+
configuration.eventHandler = { event, context in
223+
if case let .issueRecorded(issue) = event.kind, case .unconditional = issue.kind {
224+
issue.record()
225+
}
226+
}
227+
228+
let handler = IssueHandlingTrait.compactMapIssues { issue in
229+
guard case .apiMisused = issue.kind else {
230+
return Issue.record("Expected an issue of kind 'apiMisused': \(issue)")
231+
}
232+
return issue
233+
}
234+
235+
await Test(handler) {
236+
Issue(kind: .apiMisused).record()
237+
}.run(configuration: configuration)
238+
}
239+
219240
#if !SWT_NO_EXIT_TESTS
220241
@Test("Disallow assigning kind to .system")
221242
func disallowAssigningSystemKind() async throws {
@@ -229,5 +250,18 @@ struct IssueHandlingTraitTests {
229250
}.run()
230251
}
231252
}
253+
254+
@Test("Disallow assigning kind to .apiMisused")
255+
func disallowAssigningAPIMisusedKind() async throws {
256+
await #expect(processExitsWith: .failure) {
257+
await Test(.compactMapIssues { issue in
258+
var issue = issue
259+
issue.kind = .apiMisused
260+
return issue
261+
}) {
262+
Issue.record("A non-system issue")
263+
}.run()
264+
}
265+
}
232266
#endif
233267
}

0 commit comments

Comments
 (0)