Skip to content

Apply toolset's testRunner property in swift test #8254

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

Merged
merged 10 commits into from
Jan 29, 2025
Merged
5 changes: 3 additions & 2 deletions Fixtures/Miscellaneous/EchoExecutable/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ let package = Package(
.executable(name: "secho", targets: ["secho"])
],
targets: [
.target(name: "secho", dependencies: [])
])
.target(name: "secho", dependencies: []),
.testTarget(name: "TestSuite")
])
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import XCTest

final class TestCase: XCTestCase {
func testFoo() {
XCTAssertTrue(true)
}
}
1 change: 1 addition & 0 deletions Fixtures/Miscellaneous/EchoExecutable/echo.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/sh

echo sentinel
echo "$@"
1 change: 1 addition & 0 deletions Fixtures/Miscellaneous/EchoExecutable/toolset.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"debugger": { "path": "echo.sh" },
"testRunner": { "path": "echo.sh" },
"schemaVersion" : "1.0",
"rootPath" : "."
}
36 changes: 22 additions & 14 deletions Sources/Commands/SwiftTestCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -948,23 +948,31 @@ final class TestRunner {
/// Constructs arguments to execute XCTest.
private func args(forTestAt testPath: AbsolutePath) throws -> [String] {
var args: [String] = []

if let runner = self.toolchain.swiftSDK.toolset.knownTools[.testRunner], let runnerPath = runner.path {
args.append(runnerPath.pathString)
args.append(contentsOf: runner.extraCLIOptions)
args.append(testPath.relative(to: localFileSystem.currentWorkingDirectory!).pathString)
args.append(contentsOf: self.additionalArguments)
} else {
#if os(macOS)
switch library {
case .xctest:
guard let xctestPath = self.toolchain.xctestPath else {
throw TestError.xcodeNotInstalled
switch library {
case .xctest:
guard let xctestPath = self.toolchain.xctestPath else {
throw TestError.xcodeNotInstalled
}
args += [xctestPath.pathString]
case .swiftTesting:
let helper = try self.toolchain.getSwiftTestingHelper()
args += [helper.pathString, "--test-bundle-path", testPath.pathString]
}
args += [xctestPath.pathString]
case .swiftTesting:
let helper = try self.toolchain.getSwiftTestingHelper()
args += [helper.pathString, "--test-bundle-path", testPath.pathString]
args += self.additionalArguments
args += [testPath.pathString]
#else
args += [testPath.pathString]
args += self.additionalArguments
#endif
}
args += additionalArguments
args += [testPath.pathString]
#else
args += [testPath.pathString]
args += additionalArguments
#endif

if library == .swiftTesting {
// HACK: tell the test bundle/executable that we want to run Swift Testing, not XCTest.
Expand Down
3 changes: 0 additions & 3 deletions Sources/SPMBuildCore/BuildParameters/BuildParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,6 @@ public struct BuildParameters: Encodable {
case .library(.automatic), .plugin:
fatalError()
case .test:
guard !self.triple.isWasm else {
return try RelativePath(validating: "\(product.name).wasm")
}
let base = "\(product.name).xctest"
if self.triple.isDarwin() {
return try RelativePath(validating: "\(base)/Contents/MacOS/\(product.name)")
Expand Down
4 changes: 2 additions & 2 deletions Tests/BuildTests/CrossCompilationBuildPlanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ final class CrossCompilationBuildPlanTests: XCTestCase {
[
result.plan.destinationBuildParameters.toolchain.swiftCompilerPath.pathString,
"-L", buildPath.pathString,
"-o", buildPath.appending(components: "PkgPackageTests.wasm").pathString,
"-o", buildPath.appending(components: "PkgPackageTests.xctest").pathString,
"-module-name", "PkgPackageTests",
"-emit-executable",
"@\(buildPath.appending(components: "PkgPackageTests.product", "Objects.LinkFileList"))",
Expand All @@ -220,7 +220,7 @@ final class CrossCompilationBuildPlanTests: XCTestCase {
)

let testPathExtension = try testBuildDescription.binaryPath.extension
XCTAssertEqual(testPathExtension, "wasm")
XCTAssertEqual(testPathExtension, "xctest")
}

func testMacros() async throws {
Expand Down
5 changes: 2 additions & 3 deletions Tests/CommandsTests/RunCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ final class RunCommandTests: CommandsTestCase {
["--toolset", "\(fixturePath)/toolset.json"], packagePath: fixturePath)

// We only expect tool's output on the stdout stream.
XCTAssertMatch(stdout, .contains("""
\(fixturePath)/.build
"""))
XCTAssertMatch(stdout, .contains("\(fixturePath)/.build"))
XCTAssertMatch(stdout, .contains("sentinel"))

// swift-build-tool output should go to stderr.
XCTAssertMatch(stderr, .regex("Compiling"))
Expand Down
18 changes: 18 additions & 0 deletions Tests/CommandsTests/TestCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ final class TestCommandTests: CommandsTestCase {
XCTAssert(stdout.contains("Swift Package Manager"), "got stdout:\n" + stdout)
}

// `runner.sh` script from the toolset won't work on Windows
#if !os(Windows)
func testToolsetRunner() async throws {
try await fixture(name: "Miscellaneous/EchoExecutable") { fixturePath in
let (stdout, stderr) = try await SwiftPM.Test.execute(
["--toolset", "\(fixturePath)/toolset.json"], packagePath: fixturePath)

// We only expect tool's output on the stdout stream.
XCTAssertMatch(stdout, .contains("sentinel"))
XCTAssertMatch(stdout, .contains("\(fixturePath)"))

// swift-build-tool output should go to stderr.
XCTAssertMatch(stderr, .regex("Compiling"))
XCTAssertMatch(stderr, .contains("Linking"))
}
}
#endif

func testNumWorkersParallelRequirement() async throws {
#if !os(macOS)
// Running swift-test fixtures on linux is not yet possible.
Expand Down