Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions Sources/Commands/SwiftTestTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ public class TestToolOptions: ToolOptions {
return .generateLinuxMain
}

if shouldPrintCodeCovPath {
return .codeCovPath
}

return .runSerial
}

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

/// If the path of the exported code coverage JSON should be printed.
var shouldPrintCodeCovPath = false

var testCaseSpecifier: TestCaseSpecifier {
testCaseSpecifierOverride() ?? _testCaseSpecifier
}
Expand Down Expand Up @@ -186,6 +193,7 @@ public enum TestCaseSpecifier {
public enum TestMode {
case version
case listTests
case codeCovPath
case generateLinuxMain
case runSerial
case runParallel
Expand Down Expand Up @@ -225,6 +233,13 @@ public class SwiftTestTool: SwiftTool<TestToolOptions> {
print(test.specifier)
}

case .codeCovPath:
let workspace = try getActiveWorkspace()
let root = try getWorkspaceRoot()
let rootManifest = workspace.loadRootManifests(packages: root.packages, diagnostics: diagnostics)[0]
let buildParameters = try self.buildParameters()
print(codeCovAsJSONPath(buildParameters: buildParameters, packageName: rootManifest.name))

case .generateLinuxMain:
#if os(Linux)
diagnostics.emit(data: LinuxTestDiscoveryDiagnostic())
Expand Down Expand Up @@ -356,7 +371,7 @@ public class SwiftTestTool: SwiftTool<TestToolOptions> {
}

// Export the codecov data as JSON.
try exportCodeCovAsJSON(filename: buildPlan.graph.rootPackages[0].name, testBinary: testProduct.binary)
try exportCodeCovAsJSON(buildPlan: buildPlan, testBinary: testProduct.binary)
}

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

private func codeCovAsJSONPath(buildParameters: BuildParameters, packageName: String) -> AbsolutePath {
return buildParameters.codeCovPath.appending(component: packageName + ".json")
}

/// Exports profdata as a JSON file.
private func exportCodeCovAsJSON(filename: String, testBinary: AbsolutePath) throws {
private func exportCodeCovAsJSON(buildPlan: BuildPlan, testBinary: AbsolutePath) throws {
// Export using the llvm-cov tool.
let llvmCov = try getToolchain().getLLVMCov()
let buildParameters = try self.buildParameters()
Expand All @@ -395,7 +414,9 @@ public class SwiftTestTool: SwiftTool<TestToolOptions> {
let result = try Process.popen(arguments: args)

// Write to a file.
let jsonPath = buildParameters.codeCovPath.appending(component: filename + ".json")
let jsonPath = codeCovAsJSONPath(
buildParameters: buildParameters,
packageName: buildPlan.graph.rootPackages[0].name)
try localFileSystem.writeFileContents(jsonPath, bytes: ByteString(result.output.dematerialize()))
}

Expand Down Expand Up @@ -464,6 +485,11 @@ public class SwiftTestTool: SwiftTool<TestToolOptions> {
option: parser.add(option: "--enable-code-coverage", kind: Bool.self,
usage: "Test with code coverage enabled"),
to: { $0.shouldEnableCodeCoverage = $1 })

binder.bind(
option: parser.add(option: "--show-codecov-path", kind: Bool.self,
usage: "Print the path of the exported code coverage JSON"),
to: { $0.shouldPrintCodeCovPath = $1 })
}

/// Locates XCTestHelper tool inside the libexec directory and bin directory.
Expand Down