Skip to content

Add support for correctly discovering async test methods on non-Apple platforms #3844

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

Merged
merged 3 commits into from
Nov 10, 2021
Merged
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: 10 additions & 0 deletions Fixtures/Miscellaneous/TestDiscovery/Async/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// swift-tools-version:4.2
import PackageDescription

let package = Package(
name: "Async",
targets: [
.target(name: "Async"),
.testTarget(name: "AsyncTests", dependencies: ["Async"]),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
struct Async {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import XCTest
@testable import Async

class AsyncTests: XCTestCase {

func testAsync() async {
}

@MainActor func testMainActor() async {
XCTAssertTrue(Thread.isMainThread)
}

func testNotAsync() {
XCTAssertTrue(Thread.isMainThread)
}
}
14 changes: 11 additions & 3 deletions Sources/Build/BuildOperationBuildSystemDelegateHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ class CustomLLBuildCommand: SPMLLBuild.ExternalCommand {
}
}

private extension IndexStore.TestCaseClass.TestMethod {

var allTestsEntry: String {
let baseName = name.hasSuffix("()") ? String(name.dropLast(2)) : name

return "(\"\(baseName)\", \(isAsync ? "asyncTest(\(baseName))" : baseName ))"
}
}

final class TestDiscoveryCommand: CustomLLBuildCommand {

private func write(
Expand All @@ -61,13 +70,12 @@ final class TestDiscoveryCommand: CustomLLBuildCommand {

for iterator in testsByClassNames {
let className = iterator.key
let testMethods = iterator.value.flatMap{ $0.methods }
let testMethods = iterator.value.flatMap{ $0.testMethods }
stream <<< "\n"
stream <<< "fileprivate extension " <<< className <<< " {" <<< "\n"
stream <<< indent(4) <<< "static let __allTests__\(className) = [" <<< "\n"
for method in testMethods {
let method = method.hasSuffix("()") ? String(method.dropLast(2)) : method
stream <<< indent(8) <<< "(\"\(method)\", \(method))," <<< "\n"
stream <<< indent(8) <<< method.allTestsEntry <<< ",\n"
}
stream <<< indent(4) <<< "]" <<< "\n"
stream <<< "}" <<< "\n"
Expand Down
13 changes: 13 additions & 0 deletions Tests/FunctionalTests/TestDiscoveryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ class TestDiscoveryTests: XCTestCase {
}
}

func testAsyncMethods() throws {
fixture(name: "Miscellaneous/TestDiscovery/Async") { path in
let (stdout, stderr) = try executeSwiftTest(path)
#if os(macOS)
XCTAssertMatch(stdout, .contains("module Async"))
XCTAssertMatch(stderr, .contains("Executed 3 tests"))
#else
XCTAssertMatch(stdout, .contains("module Async"))
XCTAssertMatch(stdout, .contains("Executed 3 tests"))
#endif
}
}

func testManifestOverride() throws {
#if os(macOS)
try XCTSkipIf(true)
Expand Down