Skip to content

Commit 39c384a

Browse files
authored
Add 'any' to generated test runner code (#7705)
Using the upcoming existential any feature via '-Xswiftc -enable-upcoming-feature -Xswiftc ExistentialAny' causes build failures on non-Darwin platforms as a test observer class is generated which doesn't use explicit 'any'. This is an issue for library maintainers who want to enforce this in CI.
1 parent 90c80e6 commit 39c384a

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

Sources/Build/TestObservation.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public func generateTestObservationCode(buildParameters: BuildParameters) -> Str
3434
return "\(buildParameters.testOutputPath)"
3535
}
3636
37-
private func write(record: Encodable) {
37+
private func write(record: any Encodable) {
3838
let lock = FileLock(at: URL(fileURLWithPath: self.testOutputPath + ".lock"))
3939
_ = try? lock.withLock {
4040
self._write(record: record)
4141
}
4242
}
4343
44-
private func _write(record: Encodable) {
44+
private func _write(record: any Encodable) {
4545
if let data = try? JSONEncoder().encode(record) {
4646
if let fileHandle = FileHandle(forWritingAtPath: self.testOutputPath) {
4747
defer { fileHandle.closeFile() }
@@ -435,7 +435,7 @@ public func generateTestObservationCode(buildParameters: BuildParameters) -> Str
435435
}
436436
437437
extension TestErrorInfo {
438-
init(_ error: Swift.Error) {
438+
init(_ error: any Swift.Error) {
439439
self.init(description: "\\(error)", type: "\\(Swift.type(of: error))")
440440
}
441441
}

Tests/CommandsTests/TestCommandTests.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class TestCommandTests: CommandsTestCase {
2020
private func execute(_ args: [String], packagePath: AbsolutePath? = nil) async throws -> (stdout: String, stderr: String) {
2121
try await SwiftPM.Test.execute(args, packagePath: packagePath)
2222
}
23-
23+
2424
func testUsage() async throws {
2525
let stdout = try await execute(["-help"]).stdout
2626
XCTAssert(stdout.contains("USAGE: swift test"), "got stdout:\n" + stdout)
@@ -47,7 +47,7 @@ final class TestCommandTests: CommandsTestCase {
4747
}
4848
}
4949
}
50-
50+
5151
func testNumWorkersValue() async throws {
5252
#if !os(macOS)
5353
// Running swift-test fixtures on linux is not yet possible.
@@ -303,6 +303,16 @@ final class TestCommandTests: CommandsTestCase {
303303
}
304304
#endif
305305

306+
#if !canImport(Darwin)
307+
func testGeneratedMainIsExistentialAnyClean() async throws {
308+
let existentialAnyFlags = ["-Xswiftc", "-enable-upcoming-feature", "-Xswiftc", "ExistentialAny"]
309+
try await fixture(name: "Miscellaneous/TestDiscovery/Simple") { fixturePath in
310+
let (_, stderr) = try await SwiftPM.Test.execute(existentialAnyFlags, packagePath: fixturePath)
311+
XCTAssertNoMatch(stderr, .contains("error: use of protocol"))
312+
}
313+
}
314+
#endif
315+
306316
func testLibraryEnvironmentVariable() async throws {
307317
try await fixture(name: "Miscellaneous/CheckTestLibraryEnvironmentVariable") { fixturePath in
308318
await XCTAssertAsyncNoThrow(try await SwiftPM.Test.execute(packagePath: fixturePath))

0 commit comments

Comments
 (0)