Skip to content

Tests: Convert StaticBinaryLibrary Test to Swift Testing #8685

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 4 additions & 8 deletions Sources/_InternalTestSupport/SwiftPMProduct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,11 @@ extension SwiftPM {
}

public static func xctestBinaryPath(for executableName: RelativePath) -> AbsolutePath {
#if canImport(Darwin)
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
return try! AbsolutePath(AbsolutePath(validating: bundle.bundlePath).parentDirectory, executableName)
do {
return try resolveBinDir().appending(executableName)
} catch {
fatalError("Unable to determine xctestBinaryPath")
}
fatalError()
#else
return try! AbsolutePath(validating: CommandLine.arguments.first!, relativeTo: localFileSystem.currentWorkingDirectory!)
.parentDirectory.appending(executableName)
#endif
}
}

Expand Down
20 changes: 17 additions & 3 deletions Sources/_InternalTestSupport/Toolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,29 @@ import struct TSCBasic.StringError
import struct TSCUtility.SerializedDiagnostics

#if os(macOS)
private func macOSBundleRoot() throws -> AbsolutePath {
func nextItem<T: Equatable>(in array: [T], after item: T) -> T? {
for (index, element) in array.enumerated() {
if element == item {
let nextIndex = index + 1
return nextIndex < array.count ? array[nextIndex] : nil
}
}
return nil // Item not found or it's the last item
}

package func macOSBundleRoot() throws -> AbsolutePath {
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
return try AbsolutePath(validating: bundle.bundlePath).parentDirectory
}
fatalError()
if let testBundlePath = nextItem(in: ProcessInfo.processInfo.arguments, after: "--test-bundle-path") {
let binDir = AbsolutePath(testBundlePath).parentDirectory.parentDirectory.parentDirectory.parentDirectory
return binDir
}
fatalError("Unable to find macOS bundle root")
}
#endif

private func resolveBinDir() throws -> AbsolutePath {
package func resolveBinDir() throws -> AbsolutePath {
#if os(macOS)
return try macOSBundleRoot()
#else
Expand Down
2 changes: 1 addition & 1 deletion Tests/BasicsTests/ObservabilitySystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ struct ObservabilitySystemTest {
#expect(diagnostic2_metadata.testKey3 == mergedMetadata2.testKey3)

let diagnostic2_5 = try #require(result.check(diagnostic: "error 2.5", severity: .error))
let diagnostic2_5_metadata = try #require(diagnostic2.metadata)
let diagnostic2_5_metadata = try #require(diagnostic2_5.metadata)
#expect(diagnostic2_5_metadata.testKey1 == mergedMetadata2.testKey1)
#expect(diagnostic2_5_metadata.testKey2 == mergedMetadata2.testKey2)
#expect(diagnostic2_5_metadata.testKey3 == mergedMetadata2.testKey3)
Expand Down
35 changes: 21 additions & 14 deletions Tests/FunctionalTests/StaticBinaryLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,34 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation
import DriverSupport
import PackageModel
import TSCBasic
import XCTest
import Testing
import _InternalTestSupport

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

try await fixture(name: "BinaryLibraries") { fixturePath in
let (stdout, stderr) = try await executeSwiftRun(
fixturePath.appending("Static").appending("Package1"),
"Example",
extraArgs: ["--experimental-prune-unused-dependencies"]
)
XCTAssertEqual(stdout, """
42
42
try await withKnownIssue {
try await fixture(name: "BinaryLibraries") { fixturePath in
let (stdout, stderr) = try await executeSwiftRun(
fixturePath.appending("Static").appending("Package1"),
"Example",
extraArgs: ["--experimental-prune-unused-dependencies"]
)
#expect(stdout == """
42
42

""")
""")
}
} when: {
ProcessInfo.hostOperatingSystem == .windows
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: given the whole test has issue(s) on Windows, isn't using the .disabled(if: ProcessInfo.hostOperatingSystem == .windows) trait more idiomatic here? Or just running the test on Windows provides some value?

Copy link
Contributor Author

@bkhouri bkhouri May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the issue is fixed on Windows, this will hopefully give us signal to fix the test, by removing the withKnownIssue :)

}
}
}