Skip to content

Commit e39fd3d

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 b4c5874 commit e39fd3d

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 {
@@ -238,7 +242,7 @@ public struct SwiftTestTool: SwiftCommand {
238242
guard let rootManifest = rootManifests.values.first else {
239243
throw StringError("invalid manifests at \(root.packages)")
240244
}
241-
let buildParameters = try swiftTool.buildParametersForTest()
245+
let buildParameters = try swiftTool.buildParametersForTest(options: self.options)
242246
print(codeCovAsJSONPath(buildParameters: buildParameters, packageName: rootManifest.displayName))
243247

244248
case .generateLinuxMain:
@@ -259,7 +263,7 @@ public struct SwiftTestTool: SwiftCommand {
259263
case .runSerial:
260264
let toolchain = try swiftTool.getToolchain()
261265
let testProducts = try buildTestsIfNeeded(swiftTool: swiftTool)
262-
let buildParameters = try swiftTool.buildParametersForTest()
266+
let buildParameters = try swiftTool.buildParametersForTest(options: self.options)
263267

264268
// Clean out the code coverage directory that may contain stale
265269
// profraw files from a previous run of the code coverage tool.
@@ -327,7 +331,7 @@ public struct SwiftTestTool: SwiftCommand {
327331
let tests = try testSuites
328332
.filteredTests(specifier: options.testCaseSpecifier)
329333
.skippedTests(specifier: options.testCaseSkip)
330-
let buildParameters = try swiftTool.buildParametersForTest()
334+
let buildParameters = try swiftTool.buildParametersForTest(options: self.options)
331335

332336
// If there were no matches, emit a warning and exit.
333337
if tests.isEmpty {
@@ -383,7 +387,7 @@ public struct SwiftTestTool: SwiftCommand {
383387
// Merge all the profraw files to produce a single profdata file.
384388
try mergeCodeCovRawDataFiles(swiftTool: swiftTool)
385389

386-
let buildParameters = try swiftTool.buildParametersForTest()
390+
let buildParameters = try swiftTool.buildParametersForTest(options: self.options)
387391
for product in testProducts {
388392
// Export the codecov data as JSON.
389393
let jsonPath = codeCovAsJSONPath(
@@ -399,7 +403,7 @@ public struct SwiftTestTool: SwiftCommand {
399403
let llvmProf = try swiftTool.getToolchain().getLLVMProf()
400404

401405
// Get the profraw files.
402-
let buildParameters = try swiftTool.buildParametersForTest()
406+
let buildParameters = try swiftTool.buildParametersForTest(options: self.options)
403407
let codeCovFiles = try localFileSystem.getDirectoryContents(buildParameters.codeCovPath)
404408

405409
// Construct arguments for invoking the llvm-prof tool.
@@ -423,7 +427,7 @@ public struct SwiftTestTool: SwiftCommand {
423427
private func exportCodeCovAsJSON(to path: AbsolutePath, testBinary: AbsolutePath, swiftTool: SwiftTool) throws {
424428
// Export using the llvm-cov tool.
425429
let llvmCov = try swiftTool.getToolchain().getLLVMCov()
426-
let buildParameters = try swiftTool.buildParametersForTest()
430+
let buildParameters = try swiftTool.buildParametersForTest(options: self.options)
427431
let args = [
428432
llvmCov.pathString,
429433
"export",
@@ -443,7 +447,8 @@ public struct SwiftTestTool: SwiftCommand {
443447
///
444448
/// - Returns: The paths to the build test products.
445449
private func buildTestsIfNeeded(swiftTool: SwiftTool) throws -> [BuiltTestProduct] {
446-
let buildSystem = try swiftTool.createBuildSystem(buildParameters: swiftTool.buildParametersForTest())
450+
let buildParameters = try swiftTool.buildParametersForTest(options: self.options)
451+
let buildSystem = try swiftTool.createBuildSystem(buildParameters: buildParameters)
447452

448453
if options.shouldBuildTests {
449454
let subset = options.testProduct.map(BuildSubset.product) ?? .allIncludingTests
@@ -1019,8 +1024,17 @@ final class XUnitGenerator {
10191024
}
10201025
}
10211026

1027+
extension SwiftTool {
1028+
func buildParametersForTest(options: TestToolOptions) throws -> BuildParameters {
1029+
try self.buildParametersForTest(
1030+
enableTestability: options.enableTestableImports
1031+
)
1032+
}
1033+
}
1034+
10221035
private extension Basics.Diagnostic {
10231036
static var noMatchingTests: Self {
10241037
.warning("No matching test cases were run")
10251038
}
10261039
}
1040+

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)