Skip to content

Commit b376f4f

Browse files
committed
Don't crash if tests are enumerated before their test cases are evaluated
1 parent 3b5e6ed commit b376f4f

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Sources/Testing/EntryPoints/ABIv0/Encoded/ABIv0.EncodedTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ extension ABIv0 {
8181
let testIsParameterized = test.isParameterized
8282
isParameterized = testIsParameterized
8383
if testIsParameterized {
84-
_testCases = test.testCases?.map(EncodedTestCase.init(encoding:))
84+
_testCases = test.uncheckedTestCases?.map(EncodedTestCase.init(encoding:))
8585
}
8686
}
8787
name = test.name

Sources/Testing/Test.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,23 @@ public struct Test: Sendable {
128128
}
129129
}
130130

131+
/// Equivalent to ``testCases``, but without requiring that the test cases be
132+
/// evaluated first.
133+
///
134+
/// Most callers should not use this property and should prefer ``testCases``
135+
/// since it will help catch logic errors in the testing library. Use this
136+
/// property if you are interested in the test's test cases, but the test has
137+
/// not been evaluated by an instance of ``Runner/Plan`` (e.g. if you are
138+
/// implementing `swift test list`.)
139+
var uncheckedTestCases: (some Sequence<Test.Case>)? {
140+
testCasesState.flatMap { testCasesState in
141+
if case let .evaluated(testCases) = testCasesState {
142+
return testCases
143+
}
144+
return nil
145+
}
146+
}
147+
131148
/// Evaluate this test's cases if they have not been evaluated yet.
132149
///
133150
/// The arguments of a test are captured into a closure so they can be lazily

0 commit comments

Comments
 (0)