Skip to content

Commit

Permalink
Affix the class name to distinguish the __allTests arrays generated…
Browse files Browse the repository at this point in the history
… in extensions.

This prevents a collision when one discovered test class subclasses another. SwiftPM at HEAD currently doesn't do this (it used to), leading to bugs like SR-15955. This seems like a regression, so I'm fixing it in our tool.

PiperOrigin-RevId: 433481652
  • Loading branch information
allevato authored and swiple-rules-gardener committed Mar 9, 2022
1 parent 9337089 commit 3e90cda
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions tools/test_discoverer/TestPrinter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ private func generatedTestEntry(for method: DiscoveredTests.Method) -> String {
}
}

/// Returns the Swift identifier that represents the generated array of test entries for the given
/// test class.
private func allTestsIdentifier(for testClass: DiscoveredTests.Class) -> String {
return "__allTests__\(testClass.name)"
}

/// Returns the Swift identifier that represents the generated function that returns the combined
/// test entries for all the test classes in the given module.
private func allTestsIdentifier(for module: DiscoveredTests.Module) -> String {
return "\(module.name)__allTests"
}

/// Prints discovered test entries and a test runner as Swift source code to be compiled in order to
/// run the tests.
struct TestPrinter {
Expand Down Expand Up @@ -76,7 +88,7 @@ struct TestPrinter {
fileprivate extension \(className) {
\(availabilityAttribute)
static let __allTests = [
static let \(allTestsIdentifier(for: testClass)) = [
"""

Expand All @@ -97,14 +109,15 @@ struct TestPrinter {
contents += """
\(availabilityAttribute)
func __\(moduleName)__allTests() -> [XCTestCaseEntry] {
func \(allTestsIdentifier(for: discoveredModule))() -> [XCTestCaseEntry] {
return [
"""

for className in sortedClassNames {
let testClass = discoveredModule.classes[className]!
contents += """
testCase(\(className).__allTests),
testCase(\(className).\(allTestsIdentifier(for: testClass))),
"""
}
Expand Down Expand Up @@ -132,8 +145,9 @@ struct TestPrinter {
"""

for moduleName in discoveredTests.modules.keys.sorted() {
let module = discoveredTests.modules[moduleName]!
contents += """
tests += __\(moduleName)__allTests()
tests += \(allTestsIdentifier(for: module))()
"""
}
Expand Down

1 comment on commit 3e90cda

@keith
Copy link
Member

@keith keith commented on 3e90cda Mar 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.