Skip to content

Commit b1feeda

Browse files
authored
[5.6] Re-add class name to '__allTests' for test discovery (#4234)
Motivation: In #3992 the class name was stripped from the static array of tests (i.e. `__allTests_CLASSNAME` became `__allTests`). The class name ensured the variable was uniquely named. Without it, in a test class which is derived from another, it is not unique and results in compile-time errors. Modifications: - Reintroduce the class name - Add tests Result: Dervied test classes do not result in compilation errors on Linux. (cherry picked from commit 27cd3df)
1 parent 6647fa0 commit b1feeda

File tree

6 files changed

+42
-2
lines changed

6 files changed

+42
-2
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// swift-tools-version:5.6
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "Subclass",
6+
targets: [
7+
.target(name: "Subclass"),
8+
.testTarget(name: "SubclassTests", dependencies: ["Subclass"]),
9+
]
10+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
struct Subclass {
2+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import XCTest
2+
@testable import Subclass
3+
4+
class SubclassTestsBase: XCTestCase {
5+
func test1() {
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import XCTest
2+
@testable import Subclass
3+
4+
class SubclassTestsDerived: SubclassTestsBase {
5+
override func test1() {
6+
}
7+
}

Sources/Build/BuildOperationBuildSystemDelegateHandler.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ final class TestDiscoveryCommand: CustomLLBuildCommand {
7474
stream <<< "\n"
7575
stream <<< "fileprivate extension " <<< className <<< " {" <<< "\n"
7676
stream <<< indent(4) <<< "@available(*, deprecated, message: \"Not actually deprecated. Marked as deprecated to allow inclusion of deprecated tests (which test deprecated functionality) without warnings\")" <<< "\n"
77-
stream <<< indent(4) <<< "static let __allTests = [" <<< "\n"
77+
// 'className' provides uniqueness for derived class.
78+
stream <<< indent(4) <<< "static let __allTests__\(className) = [" <<< "\n"
7879
for method in testMethods {
7980
stream <<< indent(8) <<< method.allTestsEntry <<< ",\n"
8081
}
@@ -90,7 +91,7 @@ final class TestDiscoveryCommand: CustomLLBuildCommand {
9091

9192
for iterator in testsByClassNames {
9293
let className = iterator.key
93-
stream <<< indent(8) <<< "testCase(\(className).__allTests),\n"
94+
stream <<< indent(8) <<< "testCase(\(className).__allTests__\(className)),\n"
9495
}
9596

9697
stream <<< """

Tests/FunctionalTests/TestDiscoveryTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,17 @@ class TestDiscoveryTests: XCTestCase {
127127
}
128128
#endif
129129
}
130+
131+
func testSubclassedTestClassTests() throws {
132+
#if os(macOS)
133+
try XCTSkipIf(true)
134+
#endif
135+
try fixture(name: "Miscellaneous/TestDiscovery/Subclass") { fixturePath in
136+
let (stdout, _) = try executeSwiftTest(fixturePath)
137+
// in "swift test" test output goes to stdout
138+
XCTAssertMatch(stdout, .contains("SubclassTestsBase.test1"))
139+
XCTAssertMatch(stdout, .contains("SubclassTestsDerived.test1"))
140+
XCTAssertMatch(stdout, .contains("Executed 2 tests"))
141+
}
142+
}
130143
}

0 commit comments

Comments
 (0)