Skip to content

Commit 6d07b8c

Browse files
committed
tighten up Invoke-SharedPesterTest.ps1
1 parent 60887fd commit 6d07b8c

1 file changed

Lines changed: 18 additions & 63 deletions

File tree

tools/Invoke-SharedPesterTests.ps1

Lines changed: 18 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ $repoRoot = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot ".."))
1515
$originalSystemRoot = $env:SystemRoot
1616
$originalTemp = $env:TEMP
1717

18+
function Assert-PesterEnvironment {
19+
if ($PSVersionTable.PSEdition -eq "Core" -and -not $IsWindows) {
20+
$referenceAssembliesPath = Join-Path $PSHOME "ref"
21+
if (-not (Test-Path -Path $referenceAssembliesPath)) {
22+
throw "Pester 3.4.3 on macOS requires a compatible pwsh installation with reference assemblies under '$referenceAssembliesPath'. This runner is intentionally lean and does not patch Pester at runtime."
23+
}
24+
}
25+
}
26+
1827
function Get-PesterModulePath {
1928
$packagesFolder = Join-Path $repoRoot "packages"
2029
$localPesterPaths = @(
@@ -24,80 +33,26 @@ function Get-PesterModulePath {
2433

2534
foreach ($localPesterPath in $localPesterPaths) {
2635
if (Test-Path -Path $localPesterPath) {
36+
$localManifestPath = Join-Path $localPesterPath "Pester.psd1"
37+
if (Test-Path -Path $localManifestPath) {
38+
return $localManifestPath
39+
}
40+
2741
return $localPesterPath
2842
}
2943
}
3044

3145
$globalPester = Get-Module -ListAvailable Pester | Where-Object { $_.Version -eq [version]"3.4.3" } | Select-Object -First 1
3246
if ($globalPester) {
33-
return $globalPester.ModuleBase
47+
return $globalPester.Path
3448
}
3549

3650
throw "Pester 3.4.3 was not found in the repository packages folder or installed modules."
3751
}
3852

39-
function Import-PatchedPester {
40-
param(
41-
[Parameter(Mandatory = $true)]
42-
[string] $PesterModulePath
43-
)
44-
45-
$patchedPesterPath = Join-Path ([System.IO.Path]::GetTempPath()) ("Pester.3.4.3-pwsh-compatible-{0}" -f ([guid]::NewGuid().ToString("N")))
46-
47-
Copy-Item -Path $PesterModulePath -Destination $patchedPesterPath -Recurse -Force
48-
49-
$setupTeardownPath = Join-Path $patchedPesterPath "Functions" "SetupTeardown.ps1"
50-
$setupTeardown = [System.IO.File]::ReadAllText($setupTeardownPath)
51-
52-
$addTypePattern = '& \$SafeCommands\[''Add-Type''\] -TypeDefinition @''[\s\S]*?''@\r?\n\r?\n'
53-
$setupTeardown = [System.Text.RegularExpressions.Regex]::Replace($setupTeardown, $addTypePattern, "", 1)
54-
55-
$setupTeardown = $setupTeardown.Replace(
56-
'$closeIndex = [Pester.ClosingBraceFinder]::GetClosingBraceIndex($Tokens, $GroupStartTokenIndex)',
57-
@'
58-
$groupLevel = 1
59-
$closeIndex = -1
60-
61-
for ($i = $GroupStartTokenIndex + 1; $i -lt $Tokens.Length; $i++)
62-
{
63-
$type = $Tokens[$i].Type
64-
65-
if ($type -eq [System.Management.Automation.PSTokenType]::GroupStart)
66-
{
67-
$groupLevel++
68-
}
69-
elseif ($type -eq [System.Management.Automation.PSTokenType]::GroupEnd)
70-
{
71-
$groupLevel--
72-
73-
if ($groupLevel -le 0)
74-
{
75-
$closeIndex = $i
76-
break
77-
}
78-
}
79-
}
80-
'@)
81-
82-
[System.IO.File]::WriteAllText($setupTeardownPath, $setupTeardown)
83-
84-
Remove-Module Pester -ErrorAction SilentlyContinue
85-
Import-Module -Name (Join-Path $patchedPesterPath "Pester.psd1") -Force -ErrorAction Stop
86-
}
87-
8853
function Import-Pester {
8954
$pesterModulePath = Get-PesterModulePath
90-
91-
try {
92-
Import-Module -Name $pesterModulePath -RequiredVersion 3.4.3 -ErrorAction Stop
93-
} catch {
94-
if ($PSVersionTable.PSEdition -eq "Core" -and -not $IsWindows) {
95-
Write-Host "Importing a patched temporary copy of Pester 3.4.3 for pwsh compatibility."
96-
Import-PatchedPester -PesterModulePath $pesterModulePath
97-
} else {
98-
throw
99-
}
100-
}
55+
Import-Module -Name $pesterModulePath -RequiredVersion 3.4.3 -ErrorAction Stop
10156
}
10257

10358
function Get-TestFiles {
@@ -144,8 +99,6 @@ try {
14499
Import-Module -Name $modulePath -ErrorAction Stop
145100
}
146101

147-
Import-Pester
148-
149102
if ($BeforeRun) {
150103
& $BeforeRun
151104
}
@@ -156,6 +109,8 @@ try {
156109
return
157110
}
158111

112+
Assert-PesterEnvironment
113+
Import-Pester
159114
Invoke-SelectedTests -TestFiles $testFiles
160115
} finally {
161116
$env:SystemRoot = $originalSystemRoot

0 commit comments

Comments
 (0)