Skip to content

Commit 409bbda

Browse files
tablackburnclaude
andcommitted
refactor(tests): compute project root once in bootstrap
Address review feedback (Copilot) on the standalone bootstrap: the project root was computed twice -- once inline for $buildScript and again a few lines later for $projectRoot -- which is exactly the kind of duplication that can drift. Hoist the single $projectRoot assignment above the BHBuildOutput guard and derive $buildScript from it via Join-Path, in Manifest.tests.ps1 (both blocks) and the tests/Unit/ Private and Public templates. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 49a6dd6 commit 409bbda

3 files changed

Lines changed: 20 additions & 8 deletions

File tree

tests/Manifest.tests.ps1

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
param()
5858

5959
BeforeDiscovery {
60+
# Resolve the project root once (tests/ -> repo root); used both to locate
61+
# build.ps1 below and to compute the staged build output path.
62+
$projectRoot = Split-Path -Path $PSScriptRoot -Parent
63+
6064
# Check if the BHBuildOutput environment variable exists to determine if this test is running in a psake
6165
# build or not. If it does not exist, it is not running in a psake build, so build the module.
6266
if ($null -eq $Env:BHBuildOutput) {
@@ -67,12 +71,11 @@ BeforeDiscovery {
6771
# Invoke with & (not dot-sourcing): build.ps1 ends in an exit statement, and
6872
# the call operator contains it to the script boundary instead of ending the
6973
# whole Pester run.
70-
$buildScript = Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath 'build.ps1'
74+
$buildScript = Join-Path -Path $projectRoot -ChildPath 'build.ps1'
7175
& $buildScript -Task 'Build' -Bootstrap
7276
}
7377

7478
# PowerShellBuild outputs to Output/<ModuleName>/<Version>/, override BHBuildOutput
75-
$projectRoot = Split-Path -Path $PSScriptRoot -Parent
7679
$sourceManifest = Join-Path -Path $projectRoot -ChildPath "$Env:BHProjectName/$Env:BHProjectName.psd1"
7780
$moduleVersion = (Import-PowerShellDataFile $sourceManifest).ModuleVersion
7881
$Env:BHBuildOutput = Join-Path -Path $projectRoot -ChildPath "Output/$Env:BHProjectName/$moduleVersion"
@@ -99,6 +102,10 @@ BeforeDiscovery {
99102
$isTemplate = Test-Path -LiteralPath (Join-Path -Path $Env:BHProjectPath -ChildPath 'CHANGELOG.template.md')
100103
}
101104
BeforeAll {
105+
# Resolve the project root once (tests/ -> repo root); used both to locate
106+
# build.ps1 below and to compute the staged build output path.
107+
$projectRoot = Split-Path -Path $PSScriptRoot -Parent
108+
102109
# Check if the BHBuildOutput environment variable exists to determine if this test is running in a psake
103110
# build or not. If it does not exist, it is not running in a psake build, so build the module.
104111
if ($null -eq $Env:BHBuildOutput) {
@@ -109,12 +116,11 @@ BeforeAll {
109116
# Invoke with & (not dot-sourcing): build.ps1 ends in an exit statement, and
110117
# the call operator contains it to the script boundary instead of ending the
111118
# whole Pester run.
112-
$buildScript = Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath 'build.ps1'
119+
$buildScript = Join-Path -Path $projectRoot -ChildPath 'build.ps1'
113120
& $buildScript -Task 'Build' -Bootstrap
114121
}
115122

116123
# PowerShellBuild outputs to Output/<ModuleName>/<Version>/, override BHBuildOutput
117-
$projectRoot = Split-Path -Path $PSScriptRoot -Parent
118124
$sourceManifest = Join-Path -Path $projectRoot -ChildPath "$Env:BHProjectName/$Env:BHProjectName.psd1"
119125
$moduleVersion = (Import-PowerShellDataFile $sourceManifest).ModuleVersion
120126
$Env:BHBuildOutput = Join-Path -Path $projectRoot -ChildPath "Output/$Env:BHProjectName/$moduleVersion"

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
param()
77

88
BeforeDiscovery {
9+
# Resolve the project root once (tests/Unit/<Layer>/ -> repo root, three levels up);
10+
# used both to locate build.ps1 below and to compute the staged build output path.
11+
$projectRoot = Split-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -Parent
12+
913
# Build module if not running in psake build
1014
if ($null -eq $Env:BHBuildOutput) {
1115
# Standalone run (e.g. Invoke-Pester on this file directly, or an agent
@@ -15,12 +19,11 @@ BeforeDiscovery {
1519
# Invoke with & (not dot-sourcing): build.ps1 ends in an exit statement, and
1620
# the call operator contains it to the script boundary instead of ending the
1721
# whole Pester run.
18-
$buildScript = Join-Path -Path (Split-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -Parent) -ChildPath 'build.ps1'
22+
$buildScript = Join-Path -Path $projectRoot -ChildPath 'build.ps1'
1923
& $buildScript -Task 'Build' -Bootstrap
2024
}
2125

2226
# PowerShellBuild outputs to Output/<ModuleName>/<Version>/
23-
$projectRoot = Split-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -Parent
2427
$sourceManifest = Join-Path -Path $projectRoot -ChildPath "$Env:BHProjectName/$Env:BHProjectName.psd1"
2528
$moduleVersion = (Import-PowerShellDataFile $sourceManifest).ModuleVersion
2629
$Env:BHBuildOutput = Join-Path -Path $projectRoot -ChildPath "Output/$Env:BHProjectName/$moduleVersion"

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
param()
77

88
BeforeDiscovery {
9+
# Resolve the project root once (tests/Unit/<Layer>/ -> repo root, three levels up);
10+
# used both to locate build.ps1 below and to compute the staged build output path.
11+
$projectRoot = Split-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -Parent
12+
913
# Build module if not running in psake build
1014
if ($null -eq $Env:BHBuildOutput) {
1115
# Standalone run (e.g. Invoke-Pester on this file directly, or an agent
@@ -15,12 +19,11 @@ BeforeDiscovery {
1519
# Invoke with & (not dot-sourcing): build.ps1 ends in an exit statement, and
1620
# the call operator contains it to the script boundary instead of ending the
1721
# whole Pester run.
18-
$buildScript = Join-Path -Path (Split-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -Parent) -ChildPath 'build.ps1'
22+
$buildScript = Join-Path -Path $projectRoot -ChildPath 'build.ps1'
1923
& $buildScript -Task 'Build' -Bootstrap
2024
}
2125

2226
# PowerShellBuild outputs to Output/<ModuleName>/<Version>/
23-
$projectRoot = Split-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -Parent
2427
$sourceManifest = Join-Path -Path $projectRoot -ChildPath "$Env:BHProjectName/$Env:BHProjectName.psd1"
2528
$moduleVersion = (Import-PowerShellDataFile $sourceManifest).ModuleVersion
2629
$Env:BHBuildOutput = Join-Path -Path $projectRoot -ChildPath "Output/$Env:BHProjectName/$moduleVersion"

0 commit comments

Comments
 (0)