Skip to content

Commit b49a227

Browse files
authored
Pass all arguments when initing testing parameters in swift build (#7396)
Ensures that a fully-initialized `TestingParameters` structure is used with `swift build --build-tests` instead of using any default parameters (which will tend us toward the wrong output with packages that use `--enable-test-discovery` and still have LinuxMain.swift files.) Tested with swift-numerics on Ubuntu 22.04 aarch64; before the change, we'd hit the `fatalError()` call in that package's LinuxMain.swift file. After the change, we correctly run XCTest-based tests. Resolves #7389.
1 parent a5f9b6c commit b49a227

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// swift-tools-version:5.10
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "IgnoresLinuxMain",
6+
targets: [
7+
.testTarget(name: "IgnoresLinuxMainTests"),
8+
]
9+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import XCTest
2+
3+
final class SomeTests: XCTestCase {
4+
func testSomething() {}
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fatalError("Should not use the contents of LinuxMain.swift")

Sources/Commands/SwiftBuildCommand.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ package struct SwiftBuildCommand: AsyncSwiftCommand {
154154
buildParameters.testingParameters = .init(
155155
configuration: buildParameters.configuration,
156156
targetTriple: buildParameters.triple,
157+
enableCodeCoverage: buildParameters.testingParameters.enableCodeCoverage,
158+
enableTestability: buildParameters.testingParameters.enableTestability,
159+
experimentalTestOutput: buildParameters.testingParameters.experimentalTestOutput,
160+
forceTestDiscovery: globalOptions.build.enableTestDiscovery,
161+
testEntryPointPath: globalOptions.build.testEntryPointPath,
157162
library: library
158163
)
159164
try build(swiftCommandState, subset: subset, buildParameters: buildParameters)

Tests/CommandsTests/BuildCommandTests.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ final class BuildCommandTests: CommandsTestCase {
4242
try SwiftPM.Build.execute(args, packagePath: packagePath, env: environment)
4343
}
4444

45-
func build(_ args: [String], packagePath: AbsolutePath? = nil, isRelease: Bool = false) throws -> BuildResult {
45+
func build(_ args: [String], packagePath: AbsolutePath? = nil, isRelease: Bool = false, cleanAfterward: Bool = true) throws -> BuildResult {
4646
let buildConfigurationArguments = isRelease ? ["-c", "release"] : []
4747
let (stdout, stderr) = try execute(args + buildConfigurationArguments, packagePath: packagePath)
48-
defer { try! SwiftPM.Package.execute(["clean"], packagePath: packagePath) }
48+
defer {
49+
if cleanAfterward {
50+
try! SwiftPM.Package.execute(["clean"], packagePath: packagePath)
51+
}
52+
}
4953
let (binPathOutput, _) = try execute(
5054
["--show-bin-path"] + buildConfigurationArguments,
5155
packagePath: packagePath
@@ -644,4 +648,20 @@ final class BuildCommandTests: CommandsTestCase {
644648
XCTAssertNoMatch(buildResult.stdout, .contains("codesign --force --sign - --entitlements"))
645649
}
646650
}
651+
652+
#if !canImport(Darwin)
653+
func testIgnoresLinuxMain() throws {
654+
try fixture(name: "Miscellaneous/TestDiscovery/IgnoresLinuxMain") { fixturePath in
655+
let buildResult = try self.build(["-v", "--build-tests", "--enable-test-discovery"], packagePath: fixturePath, cleanAfterward: false)
656+
let testBinaryPath = buildResult.binPath.appending("IgnoresLinuxMainPackageTests.xctest")
657+
658+
let processTerminated = expectation(description: "Process terminated")
659+
_ = try Process.run(testBinaryPath.asURL, arguments: [] ) { process in
660+
XCTAssertEqual(process.terminationStatus, EXIT_SUCCESS)
661+
processTerminated.fulfill()
662+
}
663+
wait(for: [processTerminated], timeout: .infinity)
664+
}
665+
}
666+
#endif
647667
}

0 commit comments

Comments
 (0)