Skip to content

Commit 27cd3df

Browse files
authored
Re-add class name to '__allTests' for test discovery (#4226)
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.
1 parent 79c70b1 commit 27cd3df

File tree

6 files changed

+44
-2
lines changed

6 files changed

+44
-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
@@ -77,7 +77,8 @@ final class TestDiscoveryCommand: CustomLLBuildCommand {
7777
stream <<< "\n"
7878
stream <<< "fileprivate extension " <<< className <<< " {" <<< "\n"
7979
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"
80-
stream <<< indent(4) <<< "static let __allTests = [" <<< "\n"
80+
// 'className' provides uniqueness for derived class.
81+
stream <<< indent(4) <<< "static let __allTests__\(className) = [" <<< "\n"
8182
for method in testMethods {
8283
stream <<< indent(8) <<< method.allTestsEntry <<< ",\n"
8384
}
@@ -93,7 +94,7 @@ final class TestDiscoveryCommand: CustomLLBuildCommand {
9394

9495
for iterator in testsByClassNames {
9596
let className = iterator.key
96-
stream <<< indent(8) <<< "testCase(\(className).__allTests),\n"
97+
stream <<< indent(8) <<< "testCase(\(className).__allTests__\(className)),\n"
9798
}
9899

99100
stream <<< """

Tests/FunctionalTests/TestDiscoveryTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,19 @@ class TestDiscoveryTests: XCTestCase {
118118
XCTAssertNoMatch(stderr, .contains("is deprecated"))
119119
}
120120
}
121+
122+
func testSubclassedTestClassTests() throws {
123+
#if os(macOS)
124+
try XCTSkipIf(true)
125+
#endif
126+
try fixture(name: "Miscellaneous/TestDiscovery/Subclass") { fixturePath in
127+
let (stdout, stderr) = try executeSwiftTest(fixturePath)
128+
// in "swift test" build output goes to stderr
129+
XCTAssertMatch(stderr, .contains("Build complete!"))
130+
// in "swift test" test output goes to stdout
131+
XCTAssertMatch(stdout, .contains("SubclassTestsBase.test1"))
132+
XCTAssertMatch(stdout, .contains("SubclassTestsDerived.test1"))
133+
XCTAssertMatch(stdout, .contains("Executed 2 tests"))
134+
}
135+
}
121136
}

0 commit comments

Comments
 (0)