|
| 1 | +param( |
| 2 | + [Parameter(Mandatory = $true)] |
| 3 | + [string] $TestRoot, |
| 4 | + [string] $Filter = "*", |
| 5 | + [scriptblock] $BeforeRun, |
| 6 | + [string[]] $ImportModules = @(), |
| 7 | + [switch] $UsePassThruFailureCheck, |
| 8 | + [string] $PreferredPesterVersion, |
| 9 | + [string] $SuiteName = "tests" |
| 10 | +) |
| 11 | + |
| 12 | +$ErrorActionPreference = "Stop"; |
| 13 | +Set-StrictMode -Version "Latest"; |
| 14 | + |
| 15 | +$testRootPath = [System.IO.Path]::GetFullPath($TestRoot) |
| 16 | +$repoRoot = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot "..")) |
| 17 | +$originalSystemRoot = $env:SystemRoot |
| 18 | +$originalTemp = $env:TEMP |
| 19 | + |
| 20 | +function Get-PesterModuleSpec { |
| 21 | + $packagesFolder = Join-Path $repoRoot "packages" |
| 22 | + $attempts = @() |
| 23 | + $localPesterPaths = @() |
| 24 | + |
| 25 | + if ($PreferredPesterVersion) { |
| 26 | + $localPesterPaths += (Join-Path $packagesFolder ("Pester.{0}" -f $PreferredPesterVersion) "tools" "Pester") |
| 27 | + } |
| 28 | + $localPesterPaths += (Join-Path $packagesFolder "Pester" "tools" "Pester") |
| 29 | + |
| 30 | + foreach ($localPesterPath in $localPesterPaths | Select-Object -Unique) { |
| 31 | + $attempts += $localPesterPath |
| 32 | + if (Test-Path -Path $localPesterPath) { |
| 33 | + $localManifestPath = Join-Path $localPesterPath "Pester.psd1" |
| 34 | + $modulePath = $localPesterPath |
| 35 | + if (Test-Path -Path $localManifestPath) { |
| 36 | + $modulePath = $localManifestPath |
| 37 | + } |
| 38 | + |
| 39 | + $module = Test-ModuleManifest -Path $modulePath -ErrorAction Stop |
| 40 | + if ($module.Version.Major -eq 3 -and ((-not $PreferredPesterVersion) -or $module.Version -eq [version]$PreferredPesterVersion)) { |
| 41 | + return [pscustomobject]@{ |
| 42 | + ModulePath = $module.Path |
| 43 | + Version = $module.Version.ToString() |
| 44 | + Source = "repository packages" |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + $availablePesterModules = @(Get-Module -ListAvailable Pester | Sort-Object Version -Descending) |
| 51 | + $globalPester = $null |
| 52 | + |
| 53 | + if ($PreferredPesterVersion) { |
| 54 | + $globalPester = $availablePesterModules | Where-Object { $_.Version -eq [version]$PreferredPesterVersion } | Select-Object -First 1 |
| 55 | + } |
| 56 | + |
| 57 | + if (-not $globalPester) { |
| 58 | + $globalPester = $availablePesterModules | Where-Object { $_.Version.Major -eq 3 } | Select-Object -First 1 |
| 59 | + } |
| 60 | + |
| 61 | + if ($globalPester) { |
| 62 | + return [pscustomobject]@{ |
| 63 | + ModulePath = $globalPester.Path |
| 64 | + Version = $globalPester.Version.ToString() |
| 65 | + Source = "installed modules" |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + $preferredVersionMessage = if ($PreferredPesterVersion) { "preferred version $PreferredPesterVersion" } else { "a Pester 3.x version" } |
| 70 | + $attemptedPathsMessage = if ($attempts.Count -gt 0) { " Tried package paths: $($attempts -join ', ')." } else { "" } |
| 71 | + throw "Pester $preferredVersionMessage for suite '$SuiteName' was not found in the repository packages folder or installed modules.$attemptedPathsMessage" |
| 72 | +} |
| 73 | + |
| 74 | +function Invoke-SelectedTests { |
| 75 | + param( |
| 76 | + [Parameter(Mandatory = $true)] |
| 77 | + [System.IO.FileInfo[]] $TestFiles |
| 78 | + ) |
| 79 | + |
| 80 | + foreach ($testFile in $TestFiles) { |
| 81 | + Write-Host "Running tests in: $($testFile.FullName)" |
| 82 | + if ($UsePassThruFailureCheck) { |
| 83 | + $result = Invoke-Pester -Path $testFile.FullName -PassThru |
| 84 | + if ($result.FailedCount -gt 0) { |
| 85 | + throw "Tests failed in $($testFile.FullName)." |
| 86 | + } |
| 87 | + } else { |
| 88 | + Invoke-Pester -Path $testFile.FullName |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +try { |
| 94 | + if (-not $env:SystemRoot) { |
| 95 | + $env:SystemRoot = "C:\Windows" |
| 96 | + } |
| 97 | + if (-not $env:TEMP) { |
| 98 | + $env:TEMP = [System.IO.Path]::GetTempPath() |
| 99 | + } |
| 100 | + |
| 101 | + foreach ($modulePath in $ImportModules) { |
| 102 | + Import-Module -Name $modulePath -ErrorAction Stop |
| 103 | + } |
| 104 | + |
| 105 | + if ($BeforeRun) { |
| 106 | + & $BeforeRun |
| 107 | + } |
| 108 | + |
| 109 | + $testFiles = @(Get-ChildItem -Path $testRootPath -Filter "*.tests.ps1" -Recurse) |
| 110 | + if (-not [string]::IsNullOrWhiteSpace($Filter) -and $Filter -ne "*") { |
| 111 | + $testFiles = @($testFiles | Where-Object { $_.Name -like $Filter -or $_.FullName -like $Filter }) |
| 112 | + } |
| 113 | + |
| 114 | + if ($testFiles.Count -eq 0) { |
| 115 | + Write-Host "No matching test files found under $testRootPath for filter '$Filter'." |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + if ($PSVersionTable.PSEdition -eq "Core" -and -not $IsWindows) { |
| 120 | + $referenceAssembliesPath = Join-Path $PSHOME "ref" |
| 121 | + if (-not (Test-Path -Path $referenceAssembliesPath)) { |
| 122 | + 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." |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + $pesterModule = Get-PesterModuleSpec |
| 127 | + Write-Host "Using Pester module version $($pesterModule.Version) from $($pesterModule.Source)." |
| 128 | + Import-Module -Name $pesterModule.ModulePath -RequiredVersion $pesterModule.Version -ErrorAction Stop |
| 129 | + Invoke-SelectedTests -TestFiles $testFiles |
| 130 | +} finally { |
| 131 | + $env:SystemRoot = $originalSystemRoot |
| 132 | + $env:TEMP = $originalTemp |
| 133 | +} |
0 commit comments