Skip to content

Commit 6021057

Browse files
committed
Add a helper tool for swift-testing
Since the xctest and swift-testing are being unified into one product we need an additional tool (just like `swiftpm-xctest-helper`) to load and run swift-testing tests from the unified bundle.
1 parent 125b3eb commit 6021057

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

Package.swift

+8
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,14 @@ let package = Package(
738738
swiftLanguageVersions: [.v5]
739739
)
740740

741+
#if canImport(Darwin)
742+
package.targets.append(contentsOf: [
743+
.executableTarget(
744+
name: "swiftpm-testing-helper"
745+
)
746+
])
747+
#endif
748+
741749
// Workaround SPM's attempt to link in executables which does not work on all
742750
// platforms.
743751
#if !os(Windows)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2014-2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Darwin.C
14+
15+
@main
16+
struct Entrypoint {
17+
static func main() throws {
18+
let args = CommandLine.arguments
19+
if args.count >= 3, args[1] == "--test-bundle-path" {
20+
let bundlePath = args[2]
21+
guard let image = dlopen(bundlePath, RTLD_LAZY | RTLD_FIRST) else {
22+
let errorMessage: String = dlerror().flatMap {
23+
#if compiler(>=6)
24+
String(validatingCString: $0)
25+
#else
26+
String(validatingUTF8: $0)
27+
#endif
28+
} ?? "An unknown error occurred."
29+
fatalError("Failed to open test bundle at path \(bundlePath): \(errorMessage)")
30+
}
31+
defer {
32+
dlclose(image)
33+
}
34+
35+
// Find and call the main function from the image. This function may
36+
// link to the copy of Swift Testing included with Xcode, or may link to
37+
// a copy that's included as a package dependency.
38+
let main = dlsym(image, "main").map {
39+
unsafeBitCast(
40+
$0,
41+
to: (@convention(c) (CInt, UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>) -> CInt).self
42+
)
43+
}
44+
if let main {
45+
exit(main(CommandLine.argc, CommandLine.unsafeArgv))
46+
}
47+
}
48+
}
49+
}

Utilities/bootstrap

+7
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,14 @@ def install(args):
435435
def install_swiftpm(prefix, args):
436436
# Install the swift-package-manager tool and create symlinks to it.
437437
cli_tool_dest = os.path.join(prefix, "bin")
438+
aux_tool_dest = os.path.join(prefix, "libexec", "swift", "pm")
439+
438440
install_binary(args, "swift-package-manager", os.path.join(cli_tool_dest, "swift-package"), destination_is_directory=False)
441+
442+
# `swiftpm-testing-helper` only exists on Darwin platforms
443+
if os.path.exists(os.path.join(args.bin_dir, "swiftpm-testing-helper")):
444+
install_binary(args, "swiftpm-testing-helper", aux_tool_dest)
445+
439446
for tool in ["swift-build", "swift-test", "swift-run", "swift-package-collection", "swift-package-registry", "swift-sdk", "swift-experimental-sdk"]:
440447
src = "swift-package"
441448
dest = os.path.join(cli_tool_dest, tool)

0 commit comments

Comments
 (0)