Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift Testing related cherry-picks #1443

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ x_defaults:
platform: ubuntu2004
environment:
CC: clang
SWIFT_VERSION: "5.9.2"
SWIFT_VERSION: "6.0.2"
SWIFT_HOME: "$HOME/swift-$SWIFT_VERSION"
PATH: "$SWIFT_HOME/usr/bin:$PATH"
linux_common: &linux_common
Expand Down Expand Up @@ -66,7 +66,9 @@ tasks:

macos_last_green:
name: "Last Green Bazel"
bazel: last_green
# FIXME: Use last_green when
# https://github.com/bazelbuild/bazel/issues/24375 is fixed
# bazel: last_green
<<: *mac_common

macos_latest_head_deps:
Expand Down Expand Up @@ -99,7 +101,9 @@ tasks:

ubuntu2004_last_green:
name: "Last Green Bazel"
bazel: last_green
# FIXME: Use last_green when
# https://github.com/bazelbuild/bazel/issues/24375 is fixed
# bazel: last_green
shell_commands:
- "echo --- Downloading and extracting Swift $SWIFT_VERSION to $SWIFT_HOME"
- "mkdir $SWIFT_HOME"
Expand Down
10 changes: 9 additions & 1 deletion swift/toolchains/xcode_swift_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,19 @@ def _swift_linkopts_cc_info(
),
)

def _test_linking_context(apple_toolchain, target_triple, toolchain_label):
def _test_linking_context(
apple_toolchain,
target_triple,
toolchain_label,
xcode_config):
"""Returns a `CcLinkingContext` containing linker flags for test binaries.

Args:
apple_toolchain: The `apple_common.apple_toolchain()` object.
target_triple: The target triple `struct`.
toolchain_label: The label of the Swift toolchain that will act as the
owner of the linker input propagating the flags.
xcode_config: The Xcode configuration.

Returns:
A `CcLinkingContext` that will provide linker flags to `swift_test`
Expand All @@ -229,6 +234,8 @@ def _test_linking_context(apple_toolchain, target_triple, toolchain_label):
"-Wl,-weak_framework,XCTest",
"-Wl,-weak-lXCTestSwiftSupport",
]
if _is_xcode_at_least_version(xcode_config, "16.0"):
linkopts.append("-Wl,-weak_framework,Testing")

if platform_developer_framework_dir:
linkopts.extend([
Expand Down Expand Up @@ -619,6 +626,7 @@ def _xcode_swift_toolchain_impl(ctx):
apple_toolchain = apple_toolchain,
target_triple = target_triple,
toolchain_label = ctx.label,
xcode_config = xcode_config,
)

# `--define=SWIFT_USE_TOOLCHAIN_ROOT=<path>` is a rapid development feature
Expand Down
1 change: 0 additions & 1 deletion tools/test_discoverer/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ swift_binary(
name = "test_discoverer",
srcs = [
"DiscoveredTests.swift",
"ObjcTestPrinter.swift",
"SymbolCollector.swift",
"SymbolGraphTestPrinter.swift",
"SymbolKitExtensions.swift",
Expand Down
135 changes: 0 additions & 135 deletions tools/test_discoverer/ObjcTestPrinter.swift

This file was deleted.

109 changes: 26 additions & 83 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 @@ -78,19 +78,23 @@ struct SymbolGraphTestPrinter {

fileprivate extension \(className) {
\(availabilityAttribute)
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 @@ -99,126 +103,65 @@ struct SymbolGraphTestPrinter {
contents += """

\(availabilityAttribute)
func collect\(allTestsIdentifier(for: discoveredModule))(into collector: inout ShardingFilteringTestCollector) {
@MainActor
func \(allTestsIdentifier(for: discoveredModule))() -> [XCTestCaseEntry] {
return [

"""

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

"""
}

contents += """
]
}

"""

createTextFile(at: url, contents: contents)
}

/// Prints the main test runner to a Swift source file.
func printTestRunner(toFileAt url: URL) {
/// Returns the Swift source code for the test runner.
func testRunnerSource() -> String {
guard !discoveredTests.modules.isEmpty else {
// If no tests were discovered, the user likely wrote non-XCTest-style tests that pass or fail
// based on the exit code of the process. Generate an empty source file here, which will be
// harmlessly compiled as an empty module, and the user's `main` from their own sources will
// be used instead.
createTextFile(at: url, contents: "// No tests discovered; this is intentionally empty.\n")
return
return """
@MainActor
private func __allDiscoveredXCTests() -> [XCTestCaseEntry] { [] }

"""
}

var contents = """
import BazelTestObservation
import Foundation
import XCTest

@main
\(availabilityAttribute)
struct Runner {
static func main() {
if let xmlObserver = BazelXMLTestObserver.default {
XCTestObservationCenter.shared.addTestObserver(xmlObserver)
}
do {
var testCollector = try ShardingFilteringTestCollector()
@MainActor
private func __allDiscoveredXCTests() -> [XCTestCaseEntry] {
var allTests: [XCTestCaseEntry] = []

"""

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

"""
}

// We don't pass the test filter as an argument because we've already filtered the tests in the
// collector; this lets us do better filtering (i.e., regexes) than XCTest itself allows.
contents += """
XCTMain(testCollector.testsToRun)
} catch {
print("ERROR: \\(error); exiting.")
exit(1)
}
}
}

"""

contents += createShardingFilteringTestCollector(
extraProperties: "private(set) var testsToRun: [XCTestCaseEntry] = []\n")
contents += """
extension ShardingFilteringTestCollector {
mutating func addTests<T: XCTestCase>(
_ suiteName: String,
_ tests: [(String, (T) -> () -> Void)]
) {
guard shardCount != 0 || filter != nil else {
// If we're not sharding or filtering, just add all the tests.
testsToRun.append(testCase(tests))
return
}
var shardTests: [(String, (T) -> () -> Void)] = []
for test in tests {
guard isIncludedByFilter("\\(suiteName)/\\(test.0)") else {
continue
}
if isIncludedInShard() {
shardTests.append(test)
}
seenTestCount += 1
}
testsToRun.append(testCase(shardTests))
}

mutating func addTests<T: XCTestCase>(
_ suiteName: String,
_ tests: [(String, (T) -> () throws -> Void)]
) {
guard shardCount != 0 || filter != nil else {
// If we're not sharding or filtering, just add all the tests.
testsToRun.append(testCase(tests))
return
}
var shardTests: [(String, (T) -> () throws -> Void)] = []
for test in tests {
guard isIncludedByFilter("\\(suiteName)/\\(test.0)") else {
continue
}
if isIncludedInShard() {
shardTests.append(test)
}
seenTestCount += 1
}
testsToRun.append(testCase(shardTests))
}
return allTests
}

"""

createTextFile(at: url, contents: contents)
return contents
}
}
Loading