Skip to content

Commit 49a6dd6

Browse files
tablackburnclaude
andcommitted
refactor(tests): bootstrap remaining standalone tests via build.ps1
Apply the delegate-to-build.ps1 bootstrap from #38 to the rest of the test scaffolds: tests/Manifest.tests.ps1 (both BeforeDiscovery and BeforeAll) and the tests/Unit/ Private and Public templates. These previously called Invoke-psake against build.psake.ps1 directly without populating the BuildHelpers env vars its properties block needs, so the standalone path was broken the same way Help.tests.ps1 was. Each guard now calls build.ps1 -Task 'Build' -Bootstrap via the call operator (&), so dependency bootstrap and BuildHelpers environment setup run through the canonical entry point and the script's terminating exit is contained to the script boundary instead of ending the Pester run. Project root is resolved with Split-Path -Parent (one level for Manifest.tests.ps1, three for the tests/Unit/ files, matching each file's existing root computation) and a single two-argument Join-Path, keeping it valid on PowerShell 5.1. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent fc04e67 commit 49a6dd6

4 files changed

Lines changed: 37 additions & 24 deletions

File tree

CHANGELOG.md

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

2121
- Renamed required PowerShell Gallery publish secret `PS_GALLERY_KEY``PSGALLERY_API_KEY` so the secret name matches the env var name PowerShellBuild reads (eliminating the previous mapping caveat). New modules created from the template after this change pick up the new name automatically. **Migration for existing modules:** create a new `PSGALLERY_API_KEY` repo secret with the same value, update `.github/workflows/PublishModuleToPowerShellGallery.yaml` to reference `secrets.PSGALLERY_API_KEY`, then delete the old `PS_GALLERY_KEY` secret.
2222
- Test scaffolding (`tests/Help.tests.ps1`, `tests/Manifest.tests.ps1`, `tests/Meta.tests.ps1`, `tests/MetaFixers.psm1`, and the `tests/Unit/` templates) no longer names parameters on single-argument calls (e.g. `Test-Path $path`, `Get-Module $name`, `Get-Help $command`), matching the scoped named-parameter rule — name parameters only when a call passes two or more arguments. A trailing switch counts as an argument, so `Split-Path -Path $x -Parent`, `Get-Content -Path $x -Raw`, and `Get-ChildItem -Path $x -Recurse` keep their names, as do genuinely multi-value calls (`Join-Path`, `Get-Command -Name … -CommandType …`) and `Test-Path -LiteralPath`.
23+
- `tests/Manifest.tests.ps1` and the `tests/Unit/` templates now use the same standalone build-bootstrap approach introduced for `tests/Help.tests.ps1` (#38): their `BHBuildOutput`-missing guards delegate to `build.ps1 -Task 'Build' -Bootstrap` (invoked with `&`, not dot-sourced, so its terminating `exit` is contained) instead of calling `Invoke-psake` against `build.psake.ps1` directly. This routes dependency bootstrap and BuildHelpers environment setup through the canonical entry point, so these tests can be run standalone (e.g. from an editor or by an agent) without first running `./build.ps1`. No effect on CI or `./build.ps1` runs — the guard only fires when `BHBuildOutput` is unset.
2324

2425
## [2026.04.29] - 2026-04-29
2526

tests/Manifest.tests.ps1

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ BeforeDiscovery {
6060
# Check if the BHBuildOutput environment variable exists to determine if this test is running in a psake
6161
# build or not. If it does not exist, it is not running in a psake build, so build the module.
6262
if ($null -eq $Env:BHBuildOutput) {
63-
$buildFilePath = Join-Path -Path $PSScriptRoot -ChildPath '..\build.psake.ps1'
64-
$invokePsakeParameters = @{
65-
TaskList = 'Build'
66-
BuildFile = $buildFilePath
67-
}
68-
Invoke-psake @invokePsakeParameters
63+
# Standalone run (e.g. Invoke-Pester on this file directly, or an agent
64+
# running one test): the module isn't built and the BuildHelpers env vars
65+
# aren't set. Defer to build.ps1 -- the canonical entry point -- to bootstrap
66+
# dependencies, set the BuildHelpers environment, and stage the module.
67+
# Invoke with & (not dot-sourcing): build.ps1 ends in an exit statement, and
68+
# the call operator contains it to the script boundary instead of ending the
69+
# whole Pester run.
70+
$buildScript = Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath 'build.ps1'
71+
& $buildScript -Task 'Build' -Bootstrap
6972
}
7073

7174
# 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
# PowerShellBuild outputs to Output/<ModuleName>/<Version>/, override BHBuildOutput

tests/Unit/Private/Invoke-{{Prefix}}Helper.tests.ps1

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ param()
88
BeforeDiscovery {
99
# Build module if not running in psake build
1010
if ($null -eq $Env:BHBuildOutput) {
11-
$buildFilePath = Join-Path -Path $PSScriptRoot -ChildPath '..\..\..\build.psake.ps1'
12-
$invokePsakeParameters = @{
13-
TaskList = 'Build'
14-
BuildFile = $buildFilePath
15-
}
16-
Invoke-psake @invokePsakeParameters
11+
# Standalone run (e.g. Invoke-Pester on this file directly, or an agent
12+
# running one test): the module isn't built and the BuildHelpers env vars
13+
# aren't set. Defer to build.ps1 -- the canonical entry point -- to bootstrap
14+
# dependencies, set the BuildHelpers environment, and stage the module.
15+
# Invoke with & (not dot-sourcing): build.ps1 ends in an exit statement, and
16+
# the call operator contains it to the script boundary instead of ending the
17+
# whole Pester run.
18+
$buildScript = Join-Path -Path (Split-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -Parent) -ChildPath 'build.ps1'
19+
& $buildScript -Task 'Build' -Bootstrap
1720
}
1821

1922
# PowerShellBuild outputs to Output/<ModuleName>/<Version>/

tests/Unit/Public/Get-{{Prefix}}Example.tests.ps1

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ param()
88
BeforeDiscovery {
99
# Build module if not running in psake build
1010
if ($null -eq $Env:BHBuildOutput) {
11-
$buildFilePath = Join-Path -Path $PSScriptRoot -ChildPath '..\..\..\build.psake.ps1'
12-
$invokePsakeParameters = @{
13-
TaskList = 'Build'
14-
BuildFile = $buildFilePath
15-
}
16-
Invoke-psake @invokePsakeParameters
11+
# Standalone run (e.g. Invoke-Pester on this file directly, or an agent
12+
# running one test): the module isn't built and the BuildHelpers env vars
13+
# aren't set. Defer to build.ps1 -- the canonical entry point -- to bootstrap
14+
# dependencies, set the BuildHelpers environment, and stage the module.
15+
# Invoke with & (not dot-sourcing): build.ps1 ends in an exit statement, and
16+
# the call operator contains it to the script boundary instead of ending the
17+
# whole Pester run.
18+
$buildScript = Join-Path -Path (Split-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -Parent) -ChildPath 'build.ps1'
19+
& $buildScript -Task 'Build' -Bootstrap
1720
}
1821

1922
# PowerShellBuild outputs to Output/<ModuleName>/<Version>/

0 commit comments

Comments
 (0)