Skip to content

Commit 83391e7

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 83391e7

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

Package.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,16 @@ 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+
dependencies: ["Commands"],
746+
exclude: ["CMakeLists.txt"]
747+
)
748+
])
749+
#endif
750+
741751
// Workaround SPM's attempt to link in executables which does not work on all
742752
// platforms.
743753
#if !os(Windows)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2014-2017 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 Commands
14+
import Darwin.C
15+
16+
@main
17+
struct Entrypoint {
18+
static func main() async throws {
19+
// HACK: use the swift-test executable as a host for the .xctest bundle
20+
// when running Swift Testing tests.
21+
let args = CommandLine.arguments
22+
if args.count >= 3, args[1] == "--test-bundle-path" {
23+
let bundlePath = args[2]
24+
guard let image = dlopen(bundlePath, RTLD_LAZY) else {
25+
let errorMessage: String = dlerror().flatMap {
26+
#if compiler(>=6)
27+
String(validatingCString: $0)
28+
#else
29+
String(validatingUTF8: $0)
30+
#endif
31+
} ?? "An unknown error occurred."
32+
fatalError("Failed to open test bundle at path \(bundlePath): \(errorMessage)")
33+
}
34+
defer {
35+
dlclose(image)
36+
}
37+
38+
// Find and call the main function from the image. This function may
39+
// link to the copy of Swift Testing included with Xcode, or may link to
40+
// a copy that's included as a package dependency.
41+
let main = dlsym(image, "main").map {
42+
unsafeBitCast(
43+
$0,
44+
to: (@convention(c) (CInt, UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>) -> CInt).self
45+
)
46+
}
47+
if let main {
48+
exit(main(CommandLine.argc, CommandLine.unsafeArgv))
49+
}
50+
}
51+
52+
await SwiftTestCommand.main()
53+
}
54+
}

Utilities/bootstrap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,11 @@ 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, "usr", "libexec", "swift", "pm")
439+
438440
install_binary(args, "swift-package-manager", os.path.join(cli_tool_dest, "swift-package"), destination_is_directory=False)
441+
install_binary(args, "swiftpm-testing-helper", aux_tool_dest, destination_is_directory=True)
442+
439443
for tool in ["swift-build", "swift-test", "swift-run", "swift-package-collection", "swift-package-registry", "swift-sdk", "swift-experimental-sdk"]:
440444
src = "swift-package"
441445
dest = os.path.join(cli_tool_dest, tool)

0 commit comments

Comments
 (0)