Skip to content

Commit 52c6038

Browse files
committed
feat(#208): enhance CodeScene analysis and coverage reporting
- skip branch-coverage upload if no BRANCH counters are present in JaCoCo report - update tests to validate branch-coverage upload behavior
1 parent 18f68e8 commit 52c6038

3 files changed

Lines changed: 47 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
5454
- `Test-NovaBuild` and `% nova test` no longer emit `Remove-Item -Recurse` progress bars during the Pester run; `$global:ProgressPreference` is saved and restored around the test execution so CI logs stay clean.
5555
- Code coverage in `Test-NovaBuild` now runs against the source files, instead of the built `dist/<ModuleName>/<ModuleName>.psm1` file so measured coverage percentages and line numbers align with the deployed module.
5656
- `Invoke-CodeSceneAnalysis.ps1` now normalizes Pester's JaCoCo output before upload so the `<class sourcefilename>` and `<sourcefile name>` entries are bare filenames and `package + sourcefile` resolves to real repository paths. This fixes the CodeScene `cs-coverage upload` failure "The coverage data does not contains any records related to the current repo" that appeared after the source-mirrored coverage migration.
57+
- `Invoke-CodeSceneAnalysis.ps1` now only invokes the `cs-coverage upload … --metric branch-coverage` step when the JaCoCo report actually contains `<counter type="BRANCH">` entries, so Pester reports that only emit line counters no longer fail the CodeScene step with "Requested metric is not present in coverage data".
5758

5859

5960
### Security

scripts/build/ci/Invoke-CodeSceneAnalysis.ps1

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ function Repair-PesterJaCoCoCoveragePath {
133133
}
134134
}
135135

136+
function Test-JaCoCoBranchCoverageAvailable {
137+
param([Parameter(Mandatory)][string]$Path)
138+
139+
[xml]$document = Get-Content -LiteralPath $Path -Raw
140+
return $null -ne $document.SelectSingleNode('//counter[@type="BRANCH"]')
141+
}
142+
136143
$shouldUploadCoverage = Test-CodeSceneCoverageUploadRequested -CoveragePath $CoveragePath -UploadCoverage:$UploadCoverage
137144
$shouldRunAnalysis = $TriggerAnalysis.IsPresent
138145

@@ -158,9 +165,14 @@ if ($shouldUploadCoverage) {
158165
throw "CodeScene line-coverage upload failed with exit code $LASTEXITCODE."
159166
}
160167

161-
& cs-coverage upload --format 'jacoco' --metric 'branch-coverage' $resolvedCoveragePath
162-
if ($LASTEXITCODE -ne 0) {
163-
throw "CodeScene branch-coverage upload failed with exit code $LASTEXITCODE."
168+
if (Test-JaCoCoBranchCoverageAvailable -Path $resolvedCoveragePath) {
169+
& cs-coverage upload --format 'jacoco' --metric 'branch-coverage' $resolvedCoveragePath
170+
if ($LASTEXITCODE -ne 0) {
171+
throw "CodeScene branch-coverage upload failed with exit code $LASTEXITCODE."
172+
}
173+
}
174+
else {
175+
Write-Host "Skipping branch-coverage upload: '$resolvedCoveragePath' does not contain <counter type=`"BRANCH`"> entries."
164176
}
165177
}
166178

tests/CodeSceneAnalysis.Tests.ps1

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function Invoke-WebRequest {
8484
It 'still uploads coverage when CoveragePath is provided' {
8585
$coveragePath = Join-Path $TestDrive 'coverage.xml'
8686
$uploadLogPath = Join-Path $TestDrive 'cs-coverage-upload.txt'
87-
Set-Content -LiteralPath $coveragePath -Value '<report />' -Encoding utf8
87+
Set-Content -LiteralPath $coveragePath -Value '<report><counter type="BRANCH" missed="1" covered="1" /></report>' -Encoding utf8
8888

8989
$runnerContent = @"
9090
function cs-coverage {
@@ -114,7 +114,7 @@ function cs-coverage {
114114
$coveragePath = Join-Path $artifactsDir 'coverage.xml'
115115
$uploadLogPath = Join-Path $TestDrive 'cs-coverage-upload-discovered.txt'
116116
New-Item -ItemType Directory -Path $artifactsDir -Force | Out-Null
117-
Set-Content -LiteralPath $coveragePath -Value '<report />' -Encoding utf8
117+
Set-Content -LiteralPath $coveragePath -Value '<report><counter type="BRANCH" missed="1" covered="1" /></report>' -Encoding utf8
118118

119119
$runnerContent = @"
120120
function cs-coverage {
@@ -200,4 +200,33 @@ function cs-coverage {
200200
$sourcefile = $rewritten.SelectSingleNode('//sourcefile[@name]')
201201
$sourcefile.GetAttribute('name') | Should -Be 'Foo.ps1'
202202
}
203+
204+
It 'skips branch-coverage upload when the JaCoCo report has no BRANCH counters' {
205+
$coveragePath = Join-Path $TestDrive 'coverage-no-branch.xml'
206+
$uploadLogPath = Join-Path $TestDrive 'cs-coverage-upload-no-branch.txt'
207+
Set-Content -LiteralPath $coveragePath -Value '<report><counter type="LINE" missed="0" covered="1" /></report>' -Encoding utf8
208+
209+
$runnerContent = @"
210+
function cs-coverage {
211+
param([Parameter(ValueFromRemainingArguments = `$true)][string[]]`$ArgumentList)
212+
213+
Add-Content -LiteralPath '$uploadLogPath' -Value (`$ArgumentList -join ' ') -Encoding utf8
214+
`$global:LASTEXITCODE = 0
215+
}
216+
217+
[Environment]::SetEnvironmentVariable('CS_URL', 'https://codescene.example.test')
218+
[Environment]::SetEnvironmentVariable('CS_PROJECT_ID', '123')
219+
[Environment]::SetEnvironmentVariable('CS_ACCESS_TOKEN', 'token')
220+
221+
& '$codeSceneAnalysisScriptPath' -CoveragePath '$coveragePath'
222+
"@
223+
224+
$result = Invoke-CodeSceneAnalysisTestScript -RunnerContent $runnerContent
225+
226+
$result.ExitCode | Should -Be 0 -Because ($result.Output -join [Environment]::NewLine)
227+
$uploadLog = Get-Content -LiteralPath $uploadLogPath -Raw
228+
$uploadLog | Should -Match 'upload --format jacoco --metric line-coverage'
229+
$uploadLog | Should -Not -Match 'branch-coverage'
230+
($result.Output -join [Environment]::NewLine) | Should -Match 'Skipping branch-coverage upload'
231+
}
203232
}

0 commit comments

Comments
 (0)