Skip to content

Commit

Permalink
Additional changes to XCTest symbol graph test discovery for Swift 6 …
Browse files Browse the repository at this point in the history
…compatibility

Making all of the discovered tests returned by `static func`s instead of stored `static let`s removes some problematic `@Sendable`-related function conversions that were causing a runtime crash when Swift 6 more is enabled. SwiftPM avoids this problem by... not ever compiling the discovered test runner in Swift 6 mode even if the package requests it. 🫤

PiperOrigin-RevId: 673829386
(cherry picked from commit 6988259)
Signed-off-by: Brentley Jones <github@brentleyjones.com>
  • Loading branch information
allevato authored and brentleyjones committed Nov 18, 2024
1 parent e7a1f07 commit 29433e5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
31 changes: 18 additions & 13 deletions tools/test_discoverer/SymbolGraphTestPrinter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Foundation
/// function if necessary.
private func generatedTestEntry(for method: DiscoveredTests.Method) -> String {
if method.isAsync {
return "asyncTest(\(method.name))"
return "asyncTest({ type in type.\(method.name) })"
} else {
return method.name
}
Expand Down Expand Up @@ -66,7 +66,7 @@ struct SymbolGraphTestPrinter {

var contents = """
import XCTest
@preconcurrency @testable import \(moduleName)
@testable import \(moduleName)
"""

Expand All @@ -78,20 +78,23 @@ struct SymbolGraphTestPrinter {
fileprivate extension \(className) {
\(availabilityAttribute)
@MainActor
static let \(allTestsIdentifier(for: testClass)) = [
static func \(allTestsIdentifier(for: testClass))()
-> [(String, (\(className)) -> () throws -> Void)]
{
return [
"""

for testMethod in testClass.methods.sorted(by: { $0.name < $1.name }) {
contents += """
("\(testMethod.name)", \(generatedTestEntry(for: testMethod))),
("\(testMethod.name)", \(generatedTestEntry(for: testMethod))),
"""
}

contents += """
]
]
}
}
"""
Expand All @@ -101,20 +104,22 @@ struct SymbolGraphTestPrinter {
\(availabilityAttribute)
@MainActor
let \(allTestsIdentifier(for: discoveredModule)) = [
func \(allTestsIdentifier(for: discoveredModule))() -> [XCTestCaseEntry] {
return [
"""

for className in sortedClassNames {
let testClass = discoveredModule.classes[className]!
contents += """
testCase(\(className).\(allTestsIdentifier(for: testClass))),
testCase(\(className).\(allTestsIdentifier(for: testClass))()),
"""
}

contents += """
]
]
}
"""

Expand All @@ -130,30 +135,30 @@ struct SymbolGraphTestPrinter {
// be used instead.
return """
@MainActor
private let __allDiscoveredXCTests: [XCTestCaseEntry] = []
private func __allDiscoveredXCTests() -> [XCTestCaseEntry] { [] }
"""
}

var contents = """
\(availabilityAttribute)
@MainActor
private let __allDiscoveredXCTests: [XCTestCaseEntry] = {
private func __allDiscoveredXCTests() -> [XCTestCaseEntry] {
var allTests: [XCTestCaseEntry] = []
"""

for moduleName in discoveredTests.modules.keys.sorted() {
let module = discoveredTests.modules[moduleName]!
contents += """
allTests.append(contentsOf: \(allTestsIdentifier(for: module)))
allTests.append(contentsOf: \(allTestsIdentifier(for: module))())
"""
}

contents += """
return allTests
}()
}
"""

Expand Down
4 changes: 2 additions & 2 deletions tools/test_discoverer/TestDiscoverer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ struct TestDiscoverer: ParsableCommand {
exit(1)
}
do {
try XCTestRunner.run(__allDiscoveredXCTests)
try XCTestRunner.run(__allDiscoveredXCTests())
} catch {
print("Fatal error running XCTest tests: \\(error)")
exit(1)
Expand Down Expand Up @@ -160,7 +160,7 @@ struct TestDiscoverer: ParsableCommand {
// platforms.
contents.append("""
// Unused by the Objective-C XCTestRunner; tests are discovered by the runtime.
private let __allDiscoveredXCTests: () = ()
private func __allDiscoveredXCTests() {}
""")
} else {
Expand Down

0 comments on commit 29433e5

Please sign in to comment.