Skip to content

Commit 03b9864

Browse files
tablackburnclaude
andauthored
fix(build): target staged output for code coverage (#19)
* fix(build): target staged output for code coverage Pester reports 0% coverage on every module scaffolded from this template because the coverage globs point at the source tree (`{{ModuleName}}/{Public,Private}/*.ps1`) but the test files Import-Module from the staged build output (`Output/<Name>/<Version>/`). Pester treats the two paths as different files, so every executed line counts as "missed." The natural fix — using `$Env:BHBuildOutput` directly — does not work: PowerShellBuild only rewrites that variable to the staged version path later, inside its own tasks. At psake `properties`-evaluation time, `$Env:BHBuildOutput` is still BuildHelpers' default `<root>/BuildOutput`, which doesn't exist yet and would cause Pester to bail with "Could not resolve coverage path." Compute the staged path explicitly from the manifest version (read via `$Env:BHPSModuleManifest`, which BuildHelpers populates before psake runs). This also drops the `{{ModuleName}}` placeholder from the coverage section entirely, since the path is now derived from the already-substituted BuildHelpers env vars. Verified downstream in JsmOperations: Pester goes from 0% / 75% to 100% / 75% on the same test suite, with no test changes. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(build): guard coverage env vars and normalize path style Address PR #19 review feedback: - Guard against missing $Env:BHPSModuleManifest / $Env:BHProjectName so bypassing build.ps1 (and Set-BuildEnvironment) fails fast with an actionable message instead of an obscure Import-PowerShellDataFile null-binding error. (Copilot review) - Switch the staged-output assembly from Join-Path with embedded forward slashes to [IO.Path]::Combine to match the rest of the properties block and avoid mixed separators on Windows. (CodeRabbit review) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 1ae2cd9 commit 03b9864

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

build.psake.ps1

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,19 @@ properties {
1414
$PSBPreference.Test.OutputFile = [IO.Path]::Combine($PSScriptRoot, 'out', 'testResults.xml')
1515
$PSBPreference.Test.OutputFormat = 'NUnitXml'
1616
$PSBPreference.Test.CodeCoverage.Enabled = $true
17+
# Coverage must target the staged build output, not the source tree — tests
18+
# Import-Module from Output/<Name>/<Version>, so Pester only records hits
19+
# against those paths. $Env:BHBuildOutput points at <root>/BuildOutput at
20+
# properties-evaluation time (PowerShellBuild rewrites it later inside its
21+
# tasks), so we compute the staged path from the manifest version here.
22+
if (-not $Env:BHPSModuleManifest -or -not $Env:BHProjectName) {
23+
throw 'Coverage configuration requires BuildHelpers env vars. Run via ./build.ps1 or call Set-BuildEnvironment first.'
24+
}
25+
$_moduleVersion = (Import-PowerShellDataFile -Path $Env:BHPSModuleManifest).ModuleVersion
26+
$_stagedOutput = [IO.Path]::Combine($PSScriptRoot, 'Output', $Env:BHProjectName, $_moduleVersion)
1727
$PSBPreference.Test.CodeCoverage.Files = @(
18-
"$PSScriptRoot/{{ModuleName}}/Public/*.ps1"
19-
"$PSScriptRoot/{{ModuleName}}/Private/*.ps1"
28+
"$_stagedOutput/Public/*.ps1"
29+
"$_stagedOutput/Private/*.ps1"
2030
)
2131
$PSBPreference.Test.CodeCoverage.Threshold = 0 # Threshold enforced by Codecov
2232
$PSBPreference.Test.CodeCoverage.OutputFile = [IO.Path]::Combine($PSScriptRoot, 'out', 'codeCoverage.xml')

0 commit comments

Comments
 (0)