Skip to content

Commit

Permalink
Tests: Fix always succeeding XCTAssertAsyncNoThrow (#8026)
Browse files Browse the repository at this point in the history
The autoclosure expression `{ throw error }` just creates a closure that
won't be executed. It leads to the test always succeeding even if the
`expression` throws an error.

Fortunately, I didn't find any issue to enable the assertion except for
a single case
(ab1e784)
  • Loading branch information
kateinoigakukun authored Oct 7, 2024
1 parent e08708e commit 58f9aa4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import XCTest

final class CheckTestLibraryEnvironmentVariableTests: XCTestCase {
func testEnviromentVariable() throws {
let envvar = ProcessInfo.processInfo.environment["SWIFT_PM_TEST_LIBRARY"]
XCTAssertEqual(envvar, "XCTest")
let envvar = ProcessInfo.processInfo.environment["SWIFT_TESTING_ENABLED"]
XCTAssertEqual(envvar, "0")
}
}
2 changes: 1 addition & 1 deletion Sources/_InternalTestSupport/XCTAssertHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ package func XCTAssertAsyncNoThrow<T>(
do {
_ = try await expression()
} catch {
XCTAssertNoThrow({ throw error }, message(), file: file, line: line)
XCTAssertNoThrow(try { throw error }(), message(), file: file, line: line)
}
}

Expand Down
3 changes: 3 additions & 0 deletions Tests/CommandsTests/TestCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,14 @@ final class TestCommandTests: CommandsTestCase {
}
#endif

#if os(macOS)
// "SWIFT_TESTING_ENABLED" is set only on macOS, skip the check on other platforms.
func testLibraryEnvironmentVariable() async throws {
try await fixture(name: "Miscellaneous/CheckTestLibraryEnvironmentVariable") { fixturePath in
await XCTAssertAsyncNoThrow(try await SwiftPM.Test.execute(packagePath: fixturePath))
}
}
#endif

func testXCTestOnlyDoesNotLogAboutNoMatchingTests() async throws {
try await fixture(name: "Miscellaneous/TestDiscovery/Simple") { fixturePath in
Expand Down

0 comments on commit 58f9aa4

Please sign in to comment.