Skip to content

Commit 40331d1

Browse files
committed
add flag to allow disabling building with testablity when building tests
motivation: allow building tests without testability (testable imports), this can increase build / test cycles when tests do not require the testable imports feature since more is cachable changes: * add a --disable-testable-imports flag to "swift test" with which tests are build without the testablity feature * add tests rdar://82448144
1 parent 7a12c5e commit 40331d1

File tree

3 files changed

+53
-15
lines changed

3 files changed

+53
-15
lines changed

Sources/Commands/SwiftTestTool.swift

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ struct TestToolOptions: ParsableArguments {
138138
@Option(help: "Test the specified product.")
139139
var testProduct: String?
140140

141+
/// Generate LinuxMain entries and exit.
142+
@Flag(name: .customLong("testable-imports"), inversion: .prefixedEnableDisable, help: "Enable or disable testable imports. Enabled by default.")
143+
var enableTestableImports: Bool = true
144+
141145
/// Returns the test case specifier if overridden in the env.
142146
private func testCaseSkipOverride() -> TestCaseSpecifier? {
143147
guard let override = ProcessEnv.vars["_SWIFTPM_SKIP_TESTS_LIST"] else {
@@ -235,7 +239,7 @@ public struct SwiftTestTool: SwiftCommand {
235239
guard let rootManifest = rootManifests.values.first else {
236240
throw StringError("invalid manifests at \(root.packages)")
237241
}
238-
let buildParameters = try swiftTool.buildParametersForTest()
242+
let buildParameters = try swiftTool.buildParametersForTest(options: self.options)
239243
print(codeCovAsJSONPath(buildParameters: buildParameters, packageName: rootManifest.displayName))
240244

241245
case .generateLinuxMain:
@@ -256,7 +260,7 @@ public struct SwiftTestTool: SwiftCommand {
256260
case .runSerial:
257261
let toolchain = try swiftTool.getToolchain()
258262
let testProducts = try buildTestsIfNeeded(swiftTool: swiftTool)
259-
let buildParameters = try swiftTool.buildParametersForTest()
263+
let buildParameters = try swiftTool.buildParametersForTest(options: self.options)
260264

261265
// Clean out the code coverage directory that may contain stale
262266
// profraw files from a previous run of the code coverage tool.
@@ -324,7 +328,7 @@ public struct SwiftTestTool: SwiftCommand {
324328
let tests = try testSuites
325329
.filteredTests(specifier: options.testCaseSpecifier)
326330
.skippedTests(specifier: options.testCaseSkip)
327-
let buildParameters = try swiftTool.buildParametersForTest()
331+
let buildParameters = try swiftTool.buildParametersForTest(options: self.options)
328332

329333
// If there were no matches, emit a warning and exit.
330334
if tests.isEmpty {
@@ -380,7 +384,7 @@ public struct SwiftTestTool: SwiftCommand {
380384
// Merge all the profraw files to produce a single profdata file.
381385
try mergeCodeCovRawDataFiles(swiftTool: swiftTool)
382386

383-
let buildParameters = try swiftTool.buildParametersForTest()
387+
let buildParameters = try swiftTool.buildParametersForTest(options: self.options)
384388
for product in testProducts {
385389
// Export the codecov data as JSON.
386390
let jsonPath = codeCovAsJSONPath(
@@ -396,7 +400,7 @@ public struct SwiftTestTool: SwiftCommand {
396400
let llvmProf = try swiftTool.getToolchain().getLLVMProf()
397401

398402
// Get the profraw files.
399-
let buildParameters = try swiftTool.buildParametersForTest()
403+
let buildParameters = try swiftTool.buildParametersForTest(options: self.options)
400404
let codeCovFiles = try localFileSystem.getDirectoryContents(buildParameters.codeCovPath)
401405

402406
// Construct arguments for invoking the llvm-prof tool.
@@ -420,7 +424,7 @@ public struct SwiftTestTool: SwiftCommand {
420424
private func exportCodeCovAsJSON(to path: AbsolutePath, testBinary: AbsolutePath, swiftTool: SwiftTool) throws {
421425
// Export using the llvm-cov tool.
422426
let llvmCov = try swiftTool.getToolchain().getLLVMCov()
423-
let buildParameters = try swiftTool.buildParametersForTest()
427+
let buildParameters = try swiftTool.buildParametersForTest(options: self.options)
424428
let args = [
425429
llvmCov.pathString,
426430
"export",
@@ -440,7 +444,8 @@ public struct SwiftTestTool: SwiftCommand {
440444
///
441445
/// - Returns: The paths to the build test products.
442446
private func buildTestsIfNeeded(swiftTool: SwiftTool) throws -> [BuiltTestProduct] {
443-
let buildSystem = try swiftTool.createBuildSystem(buildParameters: swiftTool.buildParametersForTest())
447+
let buildParameters = try swiftTool.buildParametersForTest(options: self.options)
448+
let buildSystem = try swiftTool.createBuildSystem(buildParameters: buildParameters)
444449

445450
if options.shouldBuildTests {
446451
let subset = options.testProduct.map(BuildSubset.product) ?? .allIncludingTests
@@ -1016,8 +1021,17 @@ final class XUnitGenerator {
10161021
}
10171022
}
10181023

1024+
extension SwiftTool {
1025+
func buildParametersForTest(options: TestToolOptions) throws -> BuildParameters {
1026+
try self.buildParametersForTest(
1027+
enableTestability: options.enableTestableImports
1028+
)
1029+
}
1030+
}
1031+
10191032
private extension Basics.Diagnostic {
10201033
static var noMatchingTests: Self {
10211034
.warning("No matching test cases were run")
10221035
}
10231036
}
1037+

Sources/Commands/TestingSupport.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,12 @@ enum TestingSupport {
134134
}
135135

136136
extension SwiftTool {
137-
func buildParametersForTest() throws -> BuildParameters {
137+
func buildParametersForTest(
138+
enableTestability: Bool? = nil
139+
) throws -> BuildParameters {
138140
var parameters = try self.buildParameters()
139-
// for test commands, alway enable building with testability enabled
140-
parameters.enableTestability = true
141+
// for test commands, we normally enable building with testability
142+
parameters.enableTestability = enableTestability ?? true
141143
return parameters
142144
}
143145
}

Tests/CommandsTests/TestToolTests.swift

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11-
import XCTest
12-
13-
import SPMTestSupport
1411
import Commands
12+
import SPMTestSupport
13+
import TSCBasic
14+
import XCTest
1515

1616
final class TestToolTests: CommandsTestCase {
1717

18-
private func execute(_ args: [String]) throws -> (stdout: String, stderr: String) {
19-
return try SwiftPMProduct.SwiftTest.execute(args)
18+
private func execute(_ args: [String], packagePath: AbsolutePath? = nil) throws -> (stdout: String, stderr: String) {
19+
return try SwiftPMProduct.SwiftTest.execute(args, packagePath: packagePath)
2020
}
2121

2222
func testUsage() throws {
@@ -58,4 +58,26 @@ final class TestToolTests: CommandsTestCase {
5858
}
5959
#endif
6060
}
61+
62+
func testEnableDisableTestability() {
63+
fixture(name: "Miscellaneous/TestableExe") { path in
64+
do {
65+
let result = try execute(["--vv"], packagePath: path)
66+
// default should run with testability
67+
XCTAssertMatch(result.stdout, .contains("-enable-testing"))
68+
}
69+
70+
do {
71+
// disable
72+
let result = try execute(["--disable-testable-imports", "--vv"], packagePath: path)
73+
XCTAssertNoMatch(result.stdout, .contains("-enable-testing"))
74+
}
75+
76+
do {
77+
// enable
78+
let result = try execute(["--enable-testable-imports", "--vv"], packagePath: path)
79+
XCTAssertMatch(result.stdout, .contains("-enable-testing"))
80+
}
81+
}
82+
}
6183
}

0 commit comments

Comments
 (0)