Skip to content

Commit 2082a88

Browse files
committed
Tests: Convert StaticBinaryLibrary Test to Swift Testing
When SwiftPM exeuctes tests using the native build system, the bundle path was failing on MacOS as the `Bundle.allBundles` did not contain the `.xctest` bundle. If we fail to find the .xctest bundle, we inspect the command line argument for the `--test-bundle-path` argument and contruct the macOS bundle root based on the argument value. In addition, convert the StaticBinaryLibraryTests to Swift Testing to validate the logic works.
1 parent 6e8b312 commit 2082a88

File tree

4 files changed

+43
-26
lines changed

4 files changed

+43
-26
lines changed

Sources/_InternalTestSupport/SwiftPMProduct.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,11 @@ extension SwiftPM {
5757
}
5858

5959
public static func xctestBinaryPath(for executableName: RelativePath) -> AbsolutePath {
60-
#if canImport(Darwin)
61-
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
62-
return try! AbsolutePath(AbsolutePath(validating: bundle.bundlePath).parentDirectory, executableName)
60+
do {
61+
return try resolveBinDir().appending(executableName)
62+
} catch {
63+
fatalError("Unable to determine xctestBinaryPath")
6364
}
64-
fatalError()
65-
#else
66-
return try! AbsolutePath(validating: CommandLine.arguments.first!, relativeTo: localFileSystem.currentWorkingDirectory!)
67-
.parentDirectory.appending(executableName)
68-
#endif
6965
}
7066
}
7167

Sources/_InternalTestSupport/Toolchain.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,29 @@ import struct TSCBasic.StringError
2121
import struct TSCUtility.SerializedDiagnostics
2222

2323
#if os(macOS)
24-
private func macOSBundleRoot() throws -> AbsolutePath {
24+
func nextItem<T: Equatable>(in array: [T], after item: T) -> T? {
25+
for (index, element) in array.enumerated() {
26+
if element == item {
27+
let nextIndex = index + 1
28+
return nextIndex < array.count ? array[nextIndex] : nil
29+
}
30+
}
31+
return nil // Item not found or it's the last item
32+
}
33+
34+
package func macOSBundleRoot() throws -> AbsolutePath {
2535
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
2636
return try AbsolutePath(validating: bundle.bundlePath).parentDirectory
2737
}
28-
fatalError()
38+
if let testBundlePath = nextItem(in: ProcessInfo.processInfo.arguments, after: "--test-bundle-path") {
39+
let binDir = AbsolutePath(testBundlePath).parentDirectory.parentDirectory.parentDirectory.parentDirectory
40+
return binDir
41+
}
42+
fatalError("Unable to find macOS bundle root")
2943
}
3044
#endif
3145

32-
private func resolveBinDir() throws -> AbsolutePath {
46+
package func resolveBinDir() throws -> AbsolutePath {
3347
#if os(macOS)
3448
return try macOSBundleRoot()
3549
#else

Tests/BasicsTests/ObservabilitySystemTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ struct ObservabilitySystemTest {
7575
#expect(diagnostic2_metadata.testKey3 == mergedMetadata2.testKey3)
7676

7777
let diagnostic2_5 = try #require(result.check(diagnostic: "error 2.5", severity: .error))
78-
let diagnostic2_5_metadata = try #require(diagnostic2.metadata)
78+
let diagnostic2_5_metadata = try #require(diagnostic2_5.metadata)
7979
#expect(diagnostic2_5_metadata.testKey1 == mergedMetadata2.testKey1)
8080
#expect(diagnostic2_5_metadata.testKey2 == mergedMetadata2.testKey2)
8181
#expect(diagnostic2_5_metadata.testKey3 == mergedMetadata2.testKey3)

Tests/FunctionalTests/StaticBinaryLibrary.swift

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,34 @@
99
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
import Foundation
1213
import DriverSupport
1314
import PackageModel
1415
import TSCBasic
15-
import XCTest
16+
import Testing
1617
import _InternalTestSupport
1718

18-
final class StaticBinaryLibraryTests: XCTestCase {
19-
func testStaticLibrary() async throws {
20-
try XCTSkipOnWindows(because: "https://github.com/swiftlang/swift-package-manager/issues/8657")
19+
struct StaticBinaryLibraryTests {
20+
@Test(
21+
.bug("https://github.com/swiftlang/swift-package-manager/issues/8657")
22+
)
23+
func staticLibrary() async throws {
2124

22-
try await fixture(name: "BinaryLibraries") { fixturePath in
23-
let (stdout, stderr) = try await executeSwiftRun(
24-
fixturePath.appending("Static").appending("Package1"),
25-
"Example",
26-
extraArgs: ["--experimental-prune-unused-dependencies"]
27-
)
28-
XCTAssertEqual(stdout, """
29-
42
30-
42
25+
try await withKnownIssue {
26+
try await fixture(name: "BinaryLibraries") { fixturePath in
27+
let (stdout, stderr) = try await executeSwiftRun(
28+
fixturePath.appending("Static").appending("Package1"),
29+
"Example",
30+
extraArgs: ["--experimental-prune-unused-dependencies"]
31+
)
32+
#expect(stdout == """
33+
42
34+
42
3135
32-
""")
36+
""")
37+
}
38+
} when: {
39+
ProcessInfo.hostOperatingSystem == .windows
3340
}
3441
}
3542
}

0 commit comments

Comments
 (0)