Skip to content

Commit bede618

Browse files
Plugin: Stop building with -enable-testing even with release config
1 parent 23fa281 commit bede618

File tree

12 files changed

+73
-48
lines changed

12 files changed

+73
-48
lines changed

Sources/Build/BuildDescription/ProductBuildDescription.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
205205
return []
206206
case .test:
207207
// Test products are bundle when using Objective-C, executable when using test entry point.
208-
switch self.buildParameters.testingParameters.testProductStyle {
208+
switch self.buildParameters.testProductStyle {
209209
case .loadableBundle:
210210
args += ["-Xlinker", "-bundle"]
211211
case .entryPointExecutable:

Sources/Build/BuildDescription/SwiftModuleBuildDescription.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ public final class SwiftModuleBuildDescription {
955955
// test targets must be built with -enable-testing
956956
// since its required for test discovery (the non objective-c reflection kind)
957957
return ["-enable-testing"]
958-
} else if self.buildParameters.testingParameters.enableTestability {
958+
} else if self.buildParameters.enableTestability {
959959
return ["-enable-testing"]
960960
} else {
961961
return []

Sources/Build/BuildPlan/BuildPlan+Test.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extension BuildPlan {
4343
) throws -> [(product: ResolvedProduct, discoveryTargetBuildDescription: SwiftModuleBuildDescription?, entryPointTargetBuildDescription: SwiftModuleBuildDescription)] {
4444
var explicitlyEnabledDiscovery = false
4545
var explicitlySpecifiedPath: AbsolutePath?
46-
if case let .entryPointExecutable(caseExplicitlyEnabledDiscovery, caseExplicitlySpecifiedPath) = destinationBuildParameters.testingParameters.testProductStyle {
46+
if case let .entryPointExecutable(caseExplicitlyEnabledDiscovery, caseExplicitlySpecifiedPath) = destinationBuildParameters.testProductStyle {
4747
explicitlyEnabledDiscovery = caseExplicitlyEnabledDiscovery
4848
explicitlySpecifiedPath = caseExplicitlySpecifiedPath
4949
}

Sources/Build/LLBuildCommands.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ final class TestDiscoveryCommand: CustomLLBuildCommand, TestBuildCommand {
103103
private func execute(fileSystem: Basics.FileSystem, tool: TestDiscoveryTool) throws {
104104
let outputs = tool.outputs.compactMap { try? AbsolutePath(validating: $0.name) }
105105

106-
if case .loadableBundle = context.productsBuildParameters.testingParameters.testProductStyle {
106+
if case .loadableBundle = context.productsBuildParameters.testProductStyle {
107107
// When building an XCTest bundle, test discovery is handled by the
108108
// test harness process (i.e. this is the Darwin path.)
109109
for file in outputs {
@@ -222,7 +222,7 @@ final class TestEntryPointCommand: CustomLLBuildCommand, TestBuildCommand {
222222
testObservabilitySetup = ""
223223
}
224224

225-
let isXCTMainAvailable: String = switch buildParameters.testingParameters.testProductStyle {
225+
let isXCTMainAvailable: String = switch buildParameters.testProductStyle {
226226
case .entryPointExecutable:
227227
"canImport(XCTest)"
228228
case .loadableBundle:

Sources/Commands/Utilities/PluginDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ final class PluginDelegate: PluginInvocationDelegate {
227227
// which ones they are until we've built them and can examine the binaries.
228228
let toolchain = try swiftCommandState.getHostToolchain()
229229
var toolsBuildParameters = try swiftCommandState.toolsBuildParameters
230-
toolsBuildParameters.testingParameters.enableTestability = true
230+
toolsBuildParameters.testingParameters.explicitlyEnabledTestability = true
231231
toolsBuildParameters.testingParameters.enableCodeCoverage = parameters.enableCodeCoverage
232232
let buildSystem = try await swiftCommandState.createBuildSystem(
233233
traitConfiguration: .init(),

Sources/Commands/Utilities/TestingSupport.swift

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -250,27 +250,10 @@ extension SwiftCommandState {
250250
experimentalTestOutput: Bool
251251
) -> BuildParameters {
252252
var parameters = parameters
253-
254-
var explicitlyEnabledDiscovery = false
255-
var explicitlySpecifiedPath: AbsolutePath?
256-
if case let .entryPointExecutable(
257-
explicitlyEnabledDiscoveryValue,
258-
explicitlySpecifiedPathValue
259-
) = parameters.testingParameters.testProductStyle {
260-
explicitlyEnabledDiscovery = explicitlyEnabledDiscoveryValue
261-
explicitlySpecifiedPath = explicitlySpecifiedPathValue
262-
}
263-
parameters.testingParameters = .init(
264-
configuration: parameters.configuration,
265-
targetTriple: parameters.triple,
266-
forceTestDiscovery: explicitlyEnabledDiscovery,
267-
testEntryPointPath: explicitlySpecifiedPath
268-
)
269-
270253
parameters.testingParameters.enableCodeCoverage = enableCodeCoverage
271254
// for test commands, we normally enable building with testability
272255
// but we let users override this with a flag
273-
parameters.testingParameters.enableTestability = enableTestability ?? true
256+
parameters.testingParameters.explicitlyEnabledTestability = enableTestability ?? true
274257
parameters.shouldSkipBuilding = shouldSkipBuilding
275258
parameters.testingParameters.experimentalTestOutput = experimentalTestOutput
276259
return parameters

Sources/CoreCommands/BuildSystemSupport.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private struct NativeBuildSystemFactory: BuildSystemFactory {
3636
observabilityScope: ObservabilityScope?
3737
) async throws -> any BuildSystem {
3838
_ = try await swiftCommandState.getRootPackageInformation()
39-
let testEntryPointPath = productsBuildParameters?.testingParameters.testProductStyle.explicitlySpecifiedEntryPointPath
39+
let testEntryPointPath = productsBuildParameters?.testProductStyle.explicitlySpecifiedEntryPointPath
4040
return try BuildOperation(
4141
productsBuildParameters: try productsBuildParameters ?? self.swiftCommandState.productsBuildParameters,
4242
toolsBuildParameters: try toolsBuildParameters ?? self.swiftCommandState.toolsBuildParameters,

Sources/CoreCommands/SwiftCommandState.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,6 @@ public final class SwiftCommandState {
826826
isVerbose: self.logLevel <= .info
827827
),
828828
testingParameters: .init(
829-
configuration: options.build.configuration ?? self.preferredBuildConfiguration,
830-
targetTriple: triple,
831829
forceTestDiscovery: options.build.enableTestDiscovery, // backwards compatibility, remove with --enable-test-discovery
832830
testEntryPointPath: options.build.testEntryPointPath
833831
)

Sources/SPMBuildCore/BuildParameters/BuildParameters+Testing.swift

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,20 @@ extension BuildParameters {
7777
/// Whether to enable code coverage.
7878
public var enableCodeCoverage: Bool
7979

80-
/// Whether building for testability is enabled.
81-
public var enableTestability: Bool
80+
/// Whether building for testability is explicitly enabled or disabled.
81+
package var explicitlyEnabledTestability: Bool?
8282

8383
/// Whether or not to enable the experimental test output mode.
8484
public var experimentalTestOutput: Bool
8585

86-
/// The style of test product to produce.
87-
public var testProductStyle: TestProductStyle
86+
/// Whether to force test discovery.
87+
fileprivate var explicitlyEnabledDiscovery: Bool
88+
89+
/// The path to the test entry point file, if one was specified explicitly
90+
/// via `--experimental-test-entry-point-path <file>`.
91+
fileprivate var explicitlySpecifiedPath: AbsolutePath?
8892

8993
public init(
90-
configuration: BuildConfiguration,
91-
targetTriple: Triple,
9294
enableCodeCoverage: Bool = false,
9395
enableTestability: Bool? = nil,
9496
experimentalTestOutput: Bool = false,
@@ -97,18 +99,29 @@ extension BuildParameters {
9799
) {
98100
self.enableCodeCoverage = enableCodeCoverage
99101
self.experimentalTestOutput = experimentalTestOutput
100-
// decide on testability based on debug/release config
101-
// the goals of this being based on the build configuration is
102-
// that `swift build` followed by a `swift test` will need to do minimal rebuilding
103-
// given that the default configuration for `swift build` is debug
104-
// and that `swift test` normally requires building with testable enabled.
105-
// when building and testing in release mode, one can use the '--disable-testable-imports' flag
106-
// to disable testability in `swift test`, but that requires that the tests do not use the testable imports feature
107-
self.enableTestability = enableTestability ?? (.debug == configuration)
108-
self.testProductStyle = targetTriple.isDarwin() ? .loadableBundle : .entryPointExecutable(
109-
explicitlyEnabledDiscovery: forceTestDiscovery,
110-
explicitlySpecifiedPath: testEntryPointPath
111-
)
102+
self.explicitlyEnabledTestability = enableTestability
103+
self.explicitlyEnabledDiscovery = forceTestDiscovery
104+
self.explicitlySpecifiedPath = testEntryPointPath
112105
}
113106
}
107+
108+
/// Whether building for testability is enabled.
109+
public var enableTestability: Bool {
110+
// decide on testability based on debug/release config
111+
// the goals of this being based on the build configuration is
112+
// that `swift build` followed by a `swift test` will need to do minimal rebuilding
113+
// given that the default configuration for `swift build` is debug
114+
// and that `swift test` normally requires building with testable enabled.
115+
// when building and testing in release mode, one can use the '--disable-testable-imports' flag
116+
// to disable testability in `swift test`, but that requires that the tests do not use the testable imports feature
117+
self.testingParameters.explicitlyEnabledTestability ?? (self.configuration == .debug)
118+
}
119+
120+
/// The style of test product to produce.
121+
public var testProductStyle: TestProductStyle {
122+
return triple.isDarwin() ? .loadableBundle : .entryPointExecutable(
123+
explicitlyEnabledDiscovery: testingParameters.explicitlyEnabledDiscovery,
124+
explicitlySpecifiedPath: testingParameters.explicitlySpecifiedPath
125+
)
126+
}
114127
}

Sources/SPMBuildCore/BuildParameters/BuildParameters.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public struct BuildParameters: Encodable {
163163
driverParameters: Driver = .init(),
164164
linkingParameters: Linking = .init(),
165165
outputParameters: Output = .init(),
166-
testingParameters: Testing? = nil
166+
testingParameters: Testing = .init()
167167
) throws {
168168
let triple = try triple ?? .getHostTriple(usingSwiftCompiler: toolchain.swiftCompilerPath)
169169
self.debuggingParameters = debuggingParameters ?? .init(
@@ -218,7 +218,7 @@ public struct BuildParameters: Encodable {
218218
self.driverParameters = driverParameters
219219
self.linkingParameters = linkingParameters
220220
self.outputParameters = outputParameters
221-
self.testingParameters = testingParameters ?? .init(configuration: configuration, targetTriple: triple)
221+
self.testingParameters = testingParameters
222222
}
223223

224224
/// The path to the build directory (inside the data directory).

0 commit comments

Comments
 (0)