Skip to content

Commit fc04e67

Browse files
tablackburnclaude
andauthored
feat: make Help tests runnable standalone via build.ps1 (#38)
* feat: make Help tests runnable standalone via Set-BuildEnvironment tests/Help.tests.ps1 bootstraps the build via Invoke-psake when $Env:BHBuildOutput is unset, but build.psake.ps1's properties block needs BuildHelpers vars (BHProjectName, BHPSModuleManifest) that are only populated by ./build.ps1 before psake runs. Running the Help tests in isolation (e.g. Invoke-Pester tests/Help.tests.ps1 from an editor) bypasses that, leaving the vars empty and the standalone build broken. Call Set-BuildEnvironment inside the existing bootstrap guard in both BeforeDiscovery and BeforeAll so the vars are populated before psake is invoked. The guard only fires when BHBuildOutput is unset, so there is no effect when tests run via ./build.ps1 or in CI; Set-BuildEnvironment -Force is idempotent. Surfaced by a cross-repo audit against a consumer module (tablackburn/PlexAutomationToolkit) that already carried this fix locally. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * refactor(tests): bootstrap standalone Help tests via build.ps1 Replace the inline Set-BuildEnvironment + Invoke-psake bootstrap in Help.tests.ps1 (both BeforeDiscovery and BeforeAll) with a single call to build.ps1 -- the canonical entry point -- so dependency bootstrap, BuildHelpers environment setup, and module staging all run through the real build path instead of a partial reimplementation that could drift. build.ps1 is invoked with the call operator (&), not dot-sourced, so its terminating exit is contained to the script boundary and does not end the Pester run (verified on Windows PowerShell 5.1 and PowerShell 7). Path is built with a single two-argument Join-Path over Split-Path -Parent so it stays valid on PowerShell 5.1 (which lacks Join-Path -AdditionalChildPath). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 329ca92 commit fc04e67

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ own `CHANGELOG.md` (generated from `CHANGELOG.template.md` during init).
1414

1515
- "Repository secrets" section in `README.md` documenting the GitHub Actions secrets the bundled workflows expect (`PSGALLERY_API_KEY`, `CODECOV_TOKEN`, `GITGUARDIAN_API_KEY`) — required vs. optional, source, and failure mode when missing.
1616
- `Initialize-Template.ps1` now mentions configuring GitHub repository secrets in its post-init "Next steps" output, between the build-test step and the first push.
17+
- `tests/Help.tests.ps1` can now run standalone — e.g. `Invoke-Pester tests/Help.tests.ps1` directly from an editor (or an agent running a single test) — without first running `./build.ps1`. Its build-bootstrap guard (in both `BeforeDiscovery` and `BeforeAll`) now delegates to `build.ps1 -Task 'Build' -Bootstrap`, the canonical entry point, so dependency bootstrap, BuildHelpers environment setup, and module staging all happen through the real build path instead of a partial reimplementation. The guard only fires when `BHBuildOutput` is unset, so there is no effect on CI or `./build.ps1` runs. `build.ps1` is invoked with the call operator (`&`), not dot-sourced, so its terminating `exit` is contained to the script boundary and does not end the Pester run.
1718

1819
### Changed
1920

tests/Help.tests.ps1

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,15 @@ BeforeDiscovery {
4848
# Check if the BHBuildOutput environment variable exists to determine if this test is running in a psake
4949
# build or not. If it does not exist, it is not running in a psake build, so build the module.
5050
if ($null -eq $Env:BHBuildOutput) {
51-
$buildFilePath = Join-Path -Path $PSScriptRoot -ChildPath '..\build.psake.ps1'
52-
$invokePsakeParameters = @{
53-
TaskList = 'Build'
54-
BuildFile = $buildFilePath
55-
}
56-
Invoke-psake @invokePsakeParameters
51+
# Standalone run (e.g. Invoke-Pester on this file directly, or an agent
52+
# running one test): the module isn't built and the BuildHelpers env vars
53+
# aren't set. Defer to build.ps1 -- the canonical entry point -- to bootstrap
54+
# dependencies, set the BuildHelpers environment, and stage the module.
55+
# Invoke with & (not dot-sourcing): build.ps1 ends in an exit statement, and
56+
# the call operator contains it to the script boundary instead of ending the
57+
# whole Pester run.
58+
$buildScript = Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath 'build.ps1'
59+
& $buildScript -Task 'Build' -Bootstrap
5760
}
5861

5962
# PowerShellBuild outputs to Output/<ModuleName>/<Version>/, override BHBuildOutput
@@ -99,12 +102,15 @@ BeforeAll {
99102
# Check if the BHBuildOutput environment variable exists to determine if this test is running in a psake
100103
# build or not. If it does not exist, it is not running in a psake build, so build the module.
101104
if ($null -eq $Env:BHBuildOutput) {
102-
$buildFilePath = Join-Path -Path $PSScriptRoot -ChildPath '..\build.psake.ps1'
103-
$invokePsakeParameters = @{
104-
TaskList = 'Build'
105-
BuildFile = $buildFilePath
106-
}
107-
Invoke-psake @invokePsakeParameters
105+
# Standalone run (e.g. Invoke-Pester on this file directly, or an agent
106+
# running one test): the module isn't built and the BuildHelpers env vars
107+
# aren't set. Defer to build.ps1 -- the canonical entry point -- to bootstrap
108+
# dependencies, set the BuildHelpers environment, and stage the module.
109+
# Invoke with & (not dot-sourcing): build.ps1 ends in an exit statement, and
110+
# the call operator contains it to the script boundary instead of ending the
111+
# whole Pester run.
112+
$buildScript = Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath 'build.ps1'
113+
& $buildScript -Task 'Build' -Bootstrap
108114
}
109115
}
110116

0 commit comments

Comments
 (0)