Skip to content

Commit 3440898

Browse files
authored
Ensure that when .serialized is applied to a parameterized @test func, its test cases are serialized (#1188)
This fixes a regression introduced in the changes for #901. When `.serialized` is applied directly to a parameterized `@Test` function, not to a containing suite, its test cases are no longer serialized. This PR resolves and restores the original behavior by ensuring there is a non-`nil` scope provider returned for the test function. ### 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. Fixes rdar://154529146
1 parent 072692c commit 3440898

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

Sources/Testing/Traits/ParallelizationTrait.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public struct ParallelizationTrait: TestTrait, SuiteTrait {}
3131
// MARK: - TestScoping
3232

3333
extension ParallelizationTrait: TestScoping {
34+
public func scopeProvider(for test: Test, testCase: Test.Case?) -> Self? {
35+
// When applied to a test function, this trait should provide scope to the
36+
// test function itself, not its individual test cases, since that allows
37+
// Runner to correctly interpret the configuration setting to disable
38+
// parallelization.
39+
test.isSuite || testCase == nil ? self : nil
40+
}
41+
3442
public func provideScope(for test: Test, testCase: Test.Case?, performing function: @Sendable () async throws -> Void) async throws {
3543
guard var configuration = Configuration.current else {
3644
throw SystemError(description: "There is no current Configuration when attempting to provide scope for test '\(test.name)'. Please file a bug report at https://github.com/swiftlang/swift-testing/issues/new")

Tests/TestingTests/TestSupport/TestingAdditions.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,25 @@ extension Runner {
103103
/// - fileID: The `#fileID` string whose module should be used to locate
104104
/// the test function to run.
105105
/// - configuration: The configuration to use for running.
106+
init(
107+
selecting testName: String,
108+
inModuleOf fileID: String = #fileID,
109+
configuration: Configuration = .init()
110+
) async {
111+
let plan = await Runner.Plan(selecting: testName, inModuleOf: fileID, configuration: configuration)
112+
self.init(plan: plan, configuration: configuration)
113+
}
114+
}
115+
116+
extension Runner.Plan {
117+
/// Initialize an instance of this type that selects the free test function
118+
/// named `testName` in the module specified in `fileID`.
119+
///
120+
/// - Parameters:
121+
/// - testName: The name of the test function this instance should run.
122+
/// - fileID: The `#fileID` string whose module should be used to locate
123+
/// the test function to run.
124+
/// - configuration: The configuration to use for running.
106125
init(
107126
selecting testName: String,
108127
inModuleOf fileID: String = #fileID,
@@ -116,9 +135,7 @@ extension Runner {
116135

117136
await self.init(configuration: configuration)
118137
}
119-
}
120138

121-
extension Runner.Plan {
122139
/// Initialize an instance of this type with the specified suite type.
123140
///
124141
/// - Parameters:

Tests/TestingTests/Traits/ParallelizationTraitTests.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212

1313
@Suite("Parallelization Trait Tests", .tags(.traitRelated))
1414
struct ParallelizationTraitTests {
15-
@Test(".serialized trait serializes parameterized test")
16-
func serializesParameterizedTestFunction() async {
15+
@Test(".serialized trait serializes parameterized test", arguments: await [
16+
Runner.Plan(selecting: OuterSuite.self),
17+
Runner.Plan(selecting: "globalParameterized(i:)"),
18+
])
19+
func serializesParameterizedTestFunction(plan: Runner.Plan) async {
1720
var configuration = Configuration()
1821
configuration.isParallelizationEnabled = true
1922

@@ -33,7 +36,6 @@ struct ParallelizationTraitTests {
3336
}
3437
}
3538

36-
let plan = await Runner.Plan(selecting: OuterSuite.self, configuration: configuration)
3739
let runner = Runner(plan: plan, configuration: configuration)
3840
await runner.run()
3941

@@ -59,3 +61,8 @@ private struct OuterSuite {
5961
}
6062
}
6163
}
64+
65+
@Test(.hidden, .serialized, arguments: 0 ..< 10_000)
66+
private func globalParameterized(i: Int) {
67+
Issue.record("PARAMETERIZED\(i)")
68+
}

0 commit comments

Comments
 (0)