Skip to content

fix: Report real code coverage percentages in Test-PSBuildPester - #165

Open
tablackburn wants to merge 2 commits into
mainfrom
bugfix/138-coverage-threshold-math
Open

fix: Report real code coverage percentages in Test-PSBuildPester#165
tablackburn wants to merge 2 commits into
mainfrom
bugfix/138-coverage-threshold-math

Conversation

@tablackburn

Copy link
Copy Markdown
Contributor

Summary

Test-PSBuildPester computed every JaCoCo counter percentage as
[Math]::Truncate([int]$_.covered / $total). [Math]::Truncate collapses any fraction to
0, so the value was 0 for every coverage level below exactly 100%.

One wrong number, two user-facing consequences:

  • The printed coverage report always read 0.00% (or 100.00%)
  • The CodeCoverageThreshold comparison failed the build for any nonzero threshold
    unless coverage was exactly 100%, so $PSBPreference.Test.CodeCoverage.Threshold could
    not be used at all

The ratio is now kept as a fraction between 0 and 1, which is what the {2:p} format string
and the documented .90 = 90% threshold already expected.

Not rounded, on purpose.#138 offered rounding as an alternative. Rounding 0.7996 up to
0.80 would let a threshold of 0.80 be met by coverage that does not actually meet it, and
a coverage gate that reports "met" when it isn't is the same class of bug being fixed here.

Test Plan

Two tests bracket the reported value from both sides, against the existing coverage fixture
(the scenario exercises only Get-Widget while measuring both public fixture functions, so
coverage is always partial):

ThresholdExpectedCatches
0.01passesthe truncated 0 — this is the failing test for #138
0.99failsan over-correction to a 0-to-100 scale, where 95 -lt 0.99 is false

Together they pin the value to a fraction strictly between 0 and 1 without hardcoding the
fixture's exact coverage. Both run against every installed Pester major (5.7.1 and 6.1.0).

  • New tests fail on the unfixed source (4 failures: the 0.01 case on both Pester
    majors) and pass after the fix
  • Full suite on this branch — 429 passed, 12 failed
  • Full suite on unmodified main425 passed, 12 failed

The same 12 failures occur on both, all from one container (tests/Manifest.tests.ps1). They
are the local psake 5.0.4 break-escape failure diagnosed in #155 and fixed in #162, which is
still unmerged. The +4 on this branch is the two new tests × two Pester majors.

PSScriptAnalyzer reports no new findings; the four warnings on the branch are all present on
main.

Breaking Changes

None. The comparison is strictly more permissive than before: the old code only ever passed a
nonzero threshold at exactly 100% coverage, which the new code also passes. A build that
passed with a coverage threshold set still passes; builds that were failing incorrectly now
pass.

Notes for the reviewer

  • The subprocess test helper now captures the command's output into an Output property
    rather than letting it fall through to the job's output stream, where the report lines were
    interleaving with the result object. Existing assertions were reading through that array by
    member enumeration and happened to work.
  • Adjacent, not fixed here: $total is 0 if a counter ever reports missed="0" covered="0", which would throw a divide-by-zero mid-build. I have not seen Pester emit such
    a counter and did not want to widen this fix on speculation. Happy to open a separate issue
    if you think it is worth guarding.
  • CHANGELOG.md appends to the same ### Fixed block as fix: Settle the severity threshold contract and retry analyzer crashes #163, so expect a trivial conflict
    depending on merge order.

Closes#138

Each JaCoCo counter percentage was computed as
[Math]::Truncate([int]$_.covered / $total)
and [Math]::Truncate collapses any fraction to 0, so the value was 0 for
every coverage level below exactly 100%. Two consequences fell out of the
same number: the printed coverage report always read 0.00% (or 100.00%),
and the CodeCoverageThreshold comparison failed the build for any nonzero
threshold unless coverage was exactly 100% -- coverage gating could not be
used at all.
The ratio is now kept as a fraction between 0 and 1, matching what the
{2:p} format string and the documented ".90 = 90%" threshold expect. It is
deliberately not rounded, so a threshold is never met by a value that only
rounds up to it.
The change is strictly more permissive: a build that passed with a coverage
threshold set still passes.
Tests bracket the reported value from both sides against a fixture with
partial coverage -- a threshold below it must pass, one above it must fail
-- which pins the value to a fraction and catches both a truncated 0 and a
0-to-100 scale. The subprocess helper now captures the command's output
instead of letting the report lines interleave with the job result object.
Closes#138
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018TJfFJGtUJY5CFu8MRRMYt
CopilotAI lite review requested due to automatic review settings August 22, 2026 16:05

CopilotAI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes Test-PSBuildPester coverage calculations so JaCoCo percentages and thresholds work correctly.

Changes:

  • Preserves fractional coverage ratios.
  • Adds threshold regression tests.
  • Documents the fix.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

FileSummary
tests/Test-PSBuildPester.tests.ps1Adds coverage threshold tests and captures subprocess output.
PowerShellBuild/Public/Test-PSBuildPester.ps1Corrects fractional coverage calculation.
CHANGELOG.mdDocuments the coverage fix.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-actions

github-actionsBot commented Aug 22, 2026

Copy link
Copy Markdown

Test Results

4 files 696 suites 5m 0s ⏱️
440 tests 438 ✅ 2 💤 0 ❌
1 764 runs 1 717 ✅ 47 💤 0 ❌

Results for commit 7817e5a.

♻️ This comment has been updated with latest results.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test-PSBuildPester coverage threshold math truncates every percentage to zero

2 participants

@tablackburn