Skip to content

Commit 63d75f9

Browse files
SDGGiesbrechtaciidgh
authored andcommitted
[SwiftTestTool] Add flag to display test coverage path
1 parent 32e144d commit 63d75f9

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

Sources/Commands/SwiftTestTool.swift

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ public class TestToolOptions: ToolOptions {
118118
return .generateLinuxMain
119119
}
120120

121+
if shouldPrintCodeCovPath {
122+
return .codeCovPath
123+
}
124+
121125
return .runSerial
122126
}
123127

@@ -136,6 +140,9 @@ public class TestToolOptions: ToolOptions {
136140
/// Generate LinuxMain entries and exit.
137141
var shouldGenerateLinuxMain = false
138142

143+
/// If the path of the exported code coverage JSON should be printed.
144+
var shouldPrintCodeCovPath = false
145+
139146
var testCaseSpecifier: TestCaseSpecifier {
140147
testCaseSpecifierOverride() ?? _testCaseSpecifier
141148
}
@@ -186,6 +193,7 @@ public enum TestCaseSpecifier {
186193
public enum TestMode {
187194
case version
188195
case listTests
196+
case codeCovPath
189197
case generateLinuxMain
190198
case runSerial
191199
case runParallel
@@ -225,6 +233,13 @@ public class SwiftTestTool: SwiftTool<TestToolOptions> {
225233
print(test.specifier)
226234
}
227235

236+
case .codeCovPath:
237+
let workspace = try getActiveWorkspace()
238+
let root = try getWorkspaceRoot()
239+
let rootManifest = workspace.loadRootManifests(packages: root.packages, diagnostics: diagnostics)[0]
240+
let buildParameters = try self.buildParameters()
241+
print(codeCovAsJSONPath(buildParameters: buildParameters, packageName: rootManifest.name))
242+
228243
case .generateLinuxMain:
229244
#if os(Linux)
230245
diagnostics.emit(data: LinuxTestDiscoveryDiagnostic())
@@ -356,7 +371,10 @@ public class SwiftTestTool: SwiftTool<TestToolOptions> {
356371
}
357372

358373
// Export the codecov data as JSON.
359-
try exportCodeCovAsJSON(filename: buildPlan.graph.rootPackages[0].name, testBinary: testProduct.binary)
374+
let jsonPath = codeCovAsJSONPath(
375+
buildParameters: buildPlan.buildParameters,
376+
packageName: buildPlan.graph.rootPackages[0].name)
377+
try exportCodeCovAsJSON(to: jsonPath, testBinary: testProduct.binary)
360378
}
361379

362380
/// Merges all profraw profiles in codecoverage directory into default.profdata file.
@@ -381,8 +399,12 @@ public class SwiftTestTool: SwiftTool<TestToolOptions> {
381399
try Process.checkNonZeroExit(arguments: args)
382400
}
383401

402+
private func codeCovAsJSONPath(buildParameters: BuildParameters, packageName: String) -> AbsolutePath {
403+
return buildParameters.codeCovPath.appending(component: packageName + ".json")
404+
}
405+
384406
/// Exports profdata as a JSON file.
385-
private func exportCodeCovAsJSON(filename: String, testBinary: AbsolutePath) throws {
407+
private func exportCodeCovAsJSON(to path: AbsolutePath, testBinary: AbsolutePath) throws {
386408
// Export using the llvm-cov tool.
387409
let llvmCov = try getToolchain().getLLVMCov()
388410
let buildParameters = try self.buildParameters()
@@ -393,10 +415,7 @@ public class SwiftTestTool: SwiftTool<TestToolOptions> {
393415
testBinary.pathString
394416
]
395417
let result = try Process.popen(arguments: args)
396-
397-
// Write to a file.
398-
let jsonPath = buildParameters.codeCovPath.appending(component: filename + ".json")
399-
try localFileSystem.writeFileContents(jsonPath, bytes: ByteString(result.output.dematerialize()))
418+
try localFileSystem.writeFileContents(path, bytes: ByteString(result.output.dematerialize()))
400419
}
401420

402421
/// Builds the "test" target if enabled in options.
@@ -464,6 +483,11 @@ public class SwiftTestTool: SwiftTool<TestToolOptions> {
464483
option: parser.add(option: "--enable-code-coverage", kind: Bool.self,
465484
usage: "Test with code coverage enabled"),
466485
to: { $0.shouldEnableCodeCoverage = $1 })
486+
487+
binder.bind(
488+
option: parser.add(option: "--show-codecov-path", kind: Bool.self,
489+
usage: "Print the path of the exported code coverage JSON file"),
490+
to: { $0.shouldPrintCodeCovPath = $1 })
467491
}
468492

469493
/// Locates XCTestHelper tool inside the libexec directory and bin directory.

0 commit comments

Comments
 (0)