Skip to content

Commit 0aba9c8

Browse files
nohwndCopilot
andcommitted
Avoid evaluating $IsWindows under Strict mode in PS 5.1
PS 5.1 does not define the $IsWindows automatic variable, and the hidden-folders tests run under Strict mode, so reading it directly threw 'The variable $IsWindows cannot be retrieved because it has not been set' in the PS_5_1_Windows_Server2022 leg. Reorder the check so the version comparison short-circuits before $IsWindows is touched: in PS 5.1 the first operand is $true and $IsWindows is never evaluated; in PS 6+ the variable is always set so reading it is safe. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 28a50b7 commit 0aba9c8

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

tst/Pester.RSpec.ts.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2980,7 +2980,9 @@ i -PassThru:$PassThru {
29802980
$null = New-Item -ItemType Directory -Path $hiddenDir -Force
29812981
# On Windows the dot-prefix alone is not enough — mark it Hidden so
29822982
# Get-ChildItem actually treats it as hidden (matches Linux behavior).
2983-
if ($IsWindows -or $env:OS -eq 'Windows_NT') {
2983+
# Use a short-circuit check so $IsWindows is not evaluated under Strict
2984+
# mode in PowerShell 5.1 (where the automatic variable does not exist).
2985+
if (($PSVersionTable.PSVersion.Major -lt 6) -or $IsWindows) {
29842986
$di = Get-Item -LiteralPath $hiddenDir -Force
29852987
$di.Attributes = $di.Attributes -bor [System.IO.FileAttributes]::Hidden
29862988
}
@@ -3001,7 +3003,7 @@ i -PassThru:$PassThru {
30013003
$tempDir = Join-Path ([IO.Path]::GetTempPath()) "PesterHidden2_$([IO.Path]::GetRandomFileName().Substring(0,8))"
30023004
$nestedHidden = Join-Path $tempDir '.config/tests'
30033005
$null = New-Item -ItemType Directory -Path $nestedHidden -Force
3004-
if ($IsWindows -or $env:OS -eq 'Windows_NT') {
3006+
if (($PSVersionTable.PSVersion.Major -lt 6) -or $IsWindows) {
30053007
$dotConfig = Get-Item -LiteralPath (Join-Path $tempDir '.config') -Force
30063008
$dotConfig.Attributes = $dotConfig.Attributes -bor [System.IO.FileAttributes]::Hidden
30073009
}

0 commit comments

Comments
 (0)