Skip to content

Commit f501644

Browse files
tablackburnclaude
andcommitted
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>
1 parent 1ae2cd9 commit f501644

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

build.psake.ps1

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@ 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+
$_moduleVersion = (Import-PowerShellDataFile -Path $Env:BHPSModuleManifest).ModuleVersion
23+
$_stagedOutput = Join-Path $PSScriptRoot "Output/$Env:BHProjectName/$_moduleVersion"
1724
$PSBPreference.Test.CodeCoverage.Files = @(
18-
"$PSScriptRoot/{{ModuleName}}/Public/*.ps1"
19-
"$PSScriptRoot/{{ModuleName}}/Private/*.ps1"
25+
"$_stagedOutput/Public/*.ps1"
26+
"$_stagedOutput/Private/*.ps1"
2027
)
2128
$PSBPreference.Test.CodeCoverage.Threshold = 0 # Threshold enforced by Codecov
2229
$PSBPreference.Test.CodeCoverage.OutputFile = [IO.Path]::Combine($PSScriptRoot, 'out', 'codeCoverage.xml')

0 commit comments

Comments
 (0)