ci: replace OpenCppCoverage with Microsoft native coverage (#220)

This commit is contained in:
Dave Lane authored and GitHub committed 2026-11-04 16:26:25 -05:00
1 parent 9f4cac3fff
commit 4847ea017f
4 files changed
+105 -44

No files matched your search

+66 -42
View File
@@ -22,7 +22,6 @@ env:
COMMIT: ${{ inputs.release_commit }}
FREEBSD_CLANG_VERSION: 19
FREEBSD_VERSION: '15.1'
OPENCPPCOVERAGE_VERSION: '0.9.9.0'
PYTHON_VERSION: ${{ inputs.python_version }}
jobs:
@@ -225,20 +224,18 @@ jobs:
mingw-w64-${{ matrix.toolchain }}-ninja
mingw-w64-${{ matrix.toolchain }}-toolchain
- name: Setup Dependencies Windows MSVC
- name: Locate MSVC coverage collector
if: matrix.kind == 'msvc'
run: |
choco install opencppcoverage --version=${{ env.OPENCPPCOVERAGE_VERSION }} --yes --no-progress
$openCppCoverageDir = "${env:ProgramFiles}\OpenCppCoverage"
if (!(Test-Path (Join-Path $openCppCoverageDir "OpenCppCoverage.exe"))) {
$openCppCoverageDir = "${env:ProgramFiles(x86)}\OpenCppCoverage"
}
if (!(Test-Path (Join-Path $openCppCoverageDir "OpenCppCoverage.exe"))) {
throw "OpenCppCoverage.exe was not found after Chocolatey install."
$vswhere = Join-Path ${env:ProgramFiles(x86)} "Microsoft Visual Studio\Installer\vswhere.exe"
$collector = & $vswhere -latest -products '*' `
-find 'Common7\IDE\Extensions\Microsoft\CodeCoverage.Console\Microsoft.CodeCoverage.Console.exe' |
Select-Object -First 1
if (!$collector) {
throw "Microsoft.CodeCoverage.Console.exe was not found in the latest Visual Studio installation."
}
$openCppCoverageDir | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
"MSVC_COVERAGE_COLLECTOR=$collector" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
- name: Setup Windows coverage inspection tools
if: matrix.kind == 'msvc'
@@ -434,30 +431,48 @@ jobs:
id: test_msvc
if: matrix.kind == 'msvc'
run: |
$openCppCoverage = (Get-Command OpenCppCoverage.exe -ErrorAction SilentlyContinue).Source
if (!$openCppCoverage) {
$candidates = @(
"${env:ProgramFiles}\OpenCppCoverage\OpenCppCoverage.exe",
"${env:ProgramFiles(x86)}\OpenCppCoverage\OpenCppCoverage.exe"
)
$openCppCoverage = $candidates | Where-Object { Test-Path $_ } | Select-Object -First 1
}
if (!$openCppCoverage) {
throw "OpenCppCoverage.exe was not found."
}
$testBinary = Join-Path $env:GITHUB_WORKSPACE `
"cmake-build-ci\tests\$env:CMAKE_BUILD_CONFIG\test_libvirtualhid.exe"
$reportRoot = Join-Path $env:GITHUB_WORKSPACE "cmake-build-ci\reports"
$coveragePath = Join-Path $reportRoot "coverage.xml"
$junitPath = Join-Path $reportRoot "junit.xml"
$settingsPath = Join-Path $reportRoot "coverage.config"
[xml] $settings = Get-Content -LiteralPath tests/msvc-coverage.config.in -Raw
$settings.Configuration.CodeCoverage.ModulePaths.IncludeDirectories.Directory.InnerText =
Split-Path $testBinary
$settings.Configuration.CodeCoverage.ModulePaths.Include.ModulePath =
'^' + [regex]::Escape($testBinary) + '$'
$sourcePattern = [regex]::Escape($env:GITHUB_WORKSPACE).Replace('\\', '[\\/]')
$settings.Configuration.CodeCoverage.Sources.Include.Source = "$sourcePattern[\\/](examples|src|tools)[\\/].*"
# The broker test hook compiles a private copy only for failure injection.
& $openCppCoverage `
--sources "$env:GITHUB_WORKSPACE\examples" `
--sources "$env:GITHUB_WORKSPACE\src" `
--sources "$env:GITHUB_WORKSPACE\tools" `
--excluded_sources "$env:GITHUB_WORKSPACE\src\platform\windows\broker" `
"--export_type=cobertura:$env:GITHUB_WORKSPACE\cmake-build-ci\reports\coverage.xml" `
--working_dir "$env:GITHUB_WORKSPACE\cmake-build-ci\tests" `
-- `
"$env:GITHUB_WORKSPACE\cmake-build-ci\tests\$env:CMAKE_BUILD_CONFIG\test_libvirtualhid.exe" `
--gtest_color=yes `
"--gtest_output=xml:$env:GITHUB_WORKSPACE\cmake-build-ci\reports\junit.xml"
$settings.Configuration.CodeCoverage.Sources.Exclude.Source =
"$sourcePattern[\\/]src[\\/]platform[\\/]windows[\\/]broker[\\/].*"
$settings.Save($settingsPath)
Remove-Item -LiteralPath $coveragePath, $junitPath -Force -ErrorAction SilentlyContinue
Push-Location -LiteralPath (Join-Path $env:GITHUB_WORKSPACE "cmake-build-ci\tests")
try {
& $env:MSVC_COVERAGE_COLLECTOR collect `
--settings $settingsPath `
--output $coveragePath `
--output-format cobertura `
$testBinary `
--gtest_color=yes `
"--gtest_output=xml:$junitPath"
$collectorExitCode = $LASTEXITCODE
} finally {
Pop-Location
}
if ($collectorExitCode -ne 0) {
throw "Microsoft coverage collector exited with code $collectorExitCode."
}
if (!(Test-Path -LiteralPath $junitPath)) {
throw "GoogleTest did not produce junit.xml."
}
[xml] $junit = Get-Content -LiteralPath $junitPath -Raw
if ([int] $junit.testsuites.tests -le 0 -or
[int] $junit.testsuites.failures -gt 0 -or [int] $junit.testsuites.errors -gt 0) {
throw "GoogleTest reported failures, errors, or no tests."
}
- name: Normalize MSVC coverage paths
if: >-
@@ -466,27 +481,36 @@ jobs:
(steps.test_msvc.outcome == 'success' || steps.test_msvc.outcome == 'failure')
run: |
$coveragePath = Join-Path $env:GITHUB_WORKSPACE "cmake-build-ci\reports\coverage.xml"
if (!(Test-Path $coveragePath)) {
return
if (!(Test-Path -LiteralPath $coveragePath)) {
throw "Microsoft coverage collector did not produce native source coverage."
}
[xml] $coverage = Get-Content $coveragePath
[xml] $coverage = Get-Content -LiteralPath $coveragePath -Raw
$workspace = $env:GITHUB_WORKSPACE.Replace('\', '/')
foreach ($node in $coverage.SelectNodes('//*[@filename]')) {
$filename = $node.GetAttribute('filename').Replace('\', '/')
if ($filename.StartsWith("${workspace}/")) {
if (![IO.Path]::IsPathRooted($filename)) {
foreach ($source in $coverage.SelectNodes('//source')) {
$candidate = $source.InnerText.Replace('\', '/').TrimEnd('/') + '/' + $filename
if ($candidate.StartsWith("${workspace}/", [StringComparison]::OrdinalIgnoreCase)) {
$filename = $candidate
break
}
}
}
if ($filename.StartsWith("${workspace}/", [StringComparison]::OrdinalIgnoreCase)) {
$filename = $filename.Substring($workspace.Length + 1)
}
$node.SetAttribute('filename', $filename)
}
foreach ($source in $coverage.SelectNodes('//source')) {
$source.InnerText = '.'
}
$coverage.Save($coveragePath)
if (!($coverage.SelectSingleNode('/coverage/packages/package/classes/class/lines/line'))) {
throw "Microsoft coverage collector did not produce native source coverage."
}
- name: Generate gcov report
id: test_report
if: >-