Skip to content

Generated code for XCTest on non-Darwin needs to be actor-isolated. #7566

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 1 commit into from
May 15, 2024
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
14 changes: 7 additions & 7 deletions Sources/Build/BuildOperationBuildSystemDelegateHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ final class TestDiscoveryCommand: CustomLLBuildCommand, TestBuildCommand {

fileprivate extension \#(className) {
@available(*, deprecated, message: "Not actually deprecated. Marked as deprecated to allow inclusion of deprecated tests (which test deprecated functionality) without warnings")
@MainActor
static let __allTests__\#(className) = [
\#(testMethods.map { $0.allTestsEntry }.joined(separator: ",\n "))
]
Expand All @@ -98,6 +99,7 @@ final class TestDiscoveryCommand: CustomLLBuildCommand, TestBuildCommand {
content +=
#"""
@available(*, deprecated, message: "Not actually deprecated. Marked as deprecated to allow inclusion of deprecated tests (which test deprecated functionality) without warnings")
@MainActor
func __\#(module)__allTests() -> [XCTestCaseEntry] {
return [
\#(testsByClassNames.map { "testCase(\($0.key).__allTests__\($0.key))" }
Expand Down Expand Up @@ -166,6 +168,7 @@ final class TestDiscoveryCommand: CustomLLBuildCommand, TestBuildCommand {
import XCTest

@available(*, deprecated, message: "Not actually deprecated. Marked as deprecated to allow inclusion of deprecated tests (which test deprecated functionality) without warnings")
@MainActor
public func __allDiscoveredTests() -> [XCTestCaseEntry] {
\#(testsKeyword) tests = [XCTestCaseEntry]()

Expand Down Expand Up @@ -264,18 +267,15 @@ final class TestEntryPointCommand: CustomLLBuildCommand, TestBuildCommand {
@main
@available(*, deprecated, message: "Not actually deprecated. Marked as deprecated to allow inclusion of deprecated tests (which test deprecated functionality) without warnings")
struct Runner {
#if os(WASI)
/// On WASI, we can't block the main thread, so XCTestMain is defined as async.
static func main() async {
\#(testObservabilitySetup)
#if os(WASI)
/// On WASI, we can't block the main thread, so XCTestMain is defined as async.
await XCTMain(__allDiscoveredTests()) as Never
}
#else
static func main() {
\#(testObservabilitySetup)
#else
XCTMain(__allDiscoveredTests()) as Never
#endif
}
#endif
}
"""#
)
Expand Down
10 changes: 10 additions & 0 deletions Tests/CommandsTests/TestCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,14 @@ final class TestCommandTests: CommandsTestCase {
}
}
}

#if !canImport(Darwin)
func testGeneratedMainIsConcurrencySafe_XCTest() throws {
let strictConcurrencyFlags = ["-Xswiftc", "-strict-concurrency=complete"]
try fixture(name: "Miscellaneous/TestDiscovery/Simple") { fixturePath in
let (_, stderr) = try SwiftPM.Test.execute(strictConcurrencyFlags, packagePath: fixturePath)
XCTAssertNoMatch(stderr, .contains("is not concurrency-safe"))
}
}
#endif
}