diff --git a/build.psake.ps1 b/build.psake.ps1 index 67f184e..e7cb8ea 100644 --- a/build.psake.ps1 +++ b/build.psake.ps1 @@ -14,19 +14,21 @@ properties { $PSBPreference.Test.OutputFile = [IO.Path]::Combine($PSScriptRoot, 'out', 'testResults.xml') $PSBPreference.Test.OutputFormat = 'NUnitXml' - # Code coverage configuration - # Coverage files must point to Output directory where tests actually execute - # (Tests import module from Output/, not source directory) - $moduleName = 'ReScenePS' - $sourceManifest = Join-Path $PSScriptRoot "$moduleName/$moduleName.psd1" - $moduleVersion = (Import-PowerShellDataFile -Path $sourceManifest).ModuleVersion - $moduleOutDir = Join-Path $PSScriptRoot "Output/$moduleName/$moduleVersion" - $PSBPreference.Test.CodeCoverage.Enabled = $true + # Coverage must target the staged build output, not the source tree — tests + # Import-Module from Output//, so Pester only records hits + # against those paths. $Env:BHBuildOutput points at /BuildOutput at + # properties-evaluation time (PowerShellBuild rewrites it later inside its + # tasks), so we compute the staged path from the manifest version here. + if (-not $Env:BHPSModuleManifest -or -not $Env:BHProjectName) { + throw 'Coverage configuration requires BuildHelpers env vars. Run via ./build.ps1 or call Set-BuildEnvironment first.' + } + $moduleVersion = (Import-PowerShellDataFile -Path $Env:BHPSModuleManifest).ModuleVersion + $stagedOutput = [IO.Path]::Combine($PSScriptRoot, 'Output', $Env:BHProjectName, $moduleVersion) $PSBPreference.Test.CodeCoverage.Files = @( - "$moduleOutDir/Public/*.ps1" - "$moduleOutDir/Private/*.ps1" - "$moduleOutDir/Classes/*.ps1" + "$stagedOutput/Public/*.ps1" + "$stagedOutput/Private/*.ps1" + "$stagedOutput/Classes/*.ps1" ) $PSBPreference.Test.CodeCoverage.Threshold = 0 # Threshold enforced by Codecov $PSBPreference.Test.CodeCoverage.OutputFile = [IO.Path]::Combine($PSScriptRoot, 'out', 'coverage.xml') diff --git a/tests/Help.tests.ps1 b/tests/Help.tests.ps1 index 4437b86..859d5c9 100644 --- a/tests/Help.tests.ps1 +++ b/tests/Help.tests.ps1 @@ -68,6 +68,11 @@ BeforeDiscovery { If the BHBuildOutput environment variable exists, it is running in a psake build, so do not build the module. #> if ($null -eq $Env:BHBuildOutput) { + # Populate BuildHelpers env vars so build.psake.ps1's properties block has + # the values it needs (BHPSModuleManifest, BHProjectName) — when running + # via ./build.ps1 this happens before psake; running tests in isolation + # bypasses that, so we do it here. + Set-BuildEnvironment -Path (Split-Path -Parent $PSScriptRoot) -Force $buildFilePath = Join-Path -Path $PSScriptRoot -ChildPath '..\build.psake.ps1' $invokePsakeParameters = @{ TaskList = 'Build' @@ -121,6 +126,11 @@ BeforeAll { If the BHBuildOutput environment variable exists, it is running in a psake build, so do not build the module. #> if ($null -eq $Env:BHBuildOutput) { + # Populate BuildHelpers env vars so build.psake.ps1's properties block has + # the values it needs (BHPSModuleManifest, BHProjectName) — when running + # via ./build.ps1 this happens before psake; running tests in isolation + # bypasses that, so we do it here. + Set-BuildEnvironment -Path (Split-Path -Parent $PSScriptRoot) -Force $buildFilePath = Join-Path -Path $PSScriptRoot -ChildPath '..\build.psake.ps1' $invokePsakeParameters = @{ TaskList = 'Build' diff --git a/tests/Manifest.tests.ps1 b/tests/Manifest.tests.ps1 index b27b8ab..45813e8 100644 --- a/tests/Manifest.tests.ps1 +++ b/tests/Manifest.tests.ps1 @@ -26,6 +26,11 @@ BeforeDiscovery { If the BHBuildOutput environment variable exists, it is running in a psake build, so do not build the module. #> if ($null -eq $Env:BHBuildOutput) { + # Populate BuildHelpers env vars so build.psake.ps1's properties block has + # the values it needs (BHPSModuleManifest, BHProjectName) — when running + # via ./build.ps1 this happens before psake; running tests in isolation + # bypasses that, so we do it here. + Set-BuildEnvironment -Path (Split-Path -Parent $PSScriptRoot) -Force $buildFilePath = Join-Path -Path $PSScriptRoot -ChildPath '..\build.psake.ps1' $invokePsakeParameters = @{ TaskList = 'Build' @@ -59,6 +64,11 @@ BeforeAll { If the BHBuildOutput environment variable exists, it is running in a psake build, so do not build the module. #> if ($null -eq $Env:BHBuildOutput) { + # Populate BuildHelpers env vars so build.psake.ps1's properties block has + # the values it needs (BHPSModuleManifest, BHProjectName) — when running + # via ./build.ps1 this happens before psake; running tests in isolation + # bypasses that, so we do it here. + Set-BuildEnvironment -Path (Split-Path -Parent $PSScriptRoot) -Force $buildFilePath = Join-Path -Path $PSScriptRoot -ChildPath '..\build.psake.ps1' $invokePsakeParameters = @{ TaskList = 'Build'