11[CmdletBinding ()]
22param (
3- [string ]$TestsPath = ' '
3+ [string ]$TestsPath = ' ' ,
4+ [switch ]$CurrentShellOnly ,
5+ [string []]$Shells = @ (' powershell' , ' pwsh' )
46)
57
68$ErrorActionPreference = ' Stop'
79Set-StrictMode - Version Latest
810
9- if (-not (Get-Module - ListAvailable - Name Pester)) {
10- throw " Pester is not installed. Run: Install-Module Pester -Scope CurrentUser -Force"
11+ function Get-UserModulePath {
12+ if ($PSVersionTable.PSEdition -eq ' Desktop' ) {
13+ return (Join-Path $HOME ' Documents\WindowsPowerShell\Modules' )
14+ } else {
15+ return (Join-Path $HOME ' Documents\PowerShell\Modules' )
16+ }
1117}
1218
13- Import-Module Pester - MinimumVersion 5.0 - ErrorAction Stop
19+ $userModules = Get-UserModulePath
20+ if ($env: PSModulePath -notlike " *$userModules *" ) {
21+ $env: PSModulePath = " $userModules ;$env: PSModulePath "
22+ }
1423
1524if (-not $TestsPath ) {
1625 $TestsPath = Resolve-Path - LiteralPath (Join-Path $PSScriptRoot ' ..\tests' ) |
@@ -21,9 +30,46 @@ if (-not (Test-Path -LiteralPath $TestsPath)) {
2130 throw " Tests folder not found: $TestsPath "
2231}
2332
24- $result = Invoke-Pester - Path $TestsPath - CI - PassThru
33+ $runAllShells = -not $CurrentShellOnly
34+ if ($env: GITHUB_ACTIONS -eq ' true' -or $env: CI -eq ' true' ) {
35+ $runAllShells = $false
36+ }
37+
38+ if ($runAllShells ) {
39+ $uniqueShells = @ ($Shells | Where-Object { -not [string ]::IsNullOrWhiteSpace($_ ) } | Select-Object - Unique)
40+ if ($uniqueShells.Count -eq 0 ) {
41+ throw ' No shells specified for test execution.'
42+ }
43+
44+ $missingShells = @ ($uniqueShells | Where-Object { -not (Get-Command $_ - ErrorAction SilentlyContinue) })
45+ if ($missingShells.Count -gt 0 ) {
46+ throw (" Missing required shell executable(s): {0}. Install both Windows PowerShell 5.1 ('powershell') and PowerShell 7+ ('pwsh'), or run -CurrentShellOnly." -f ($missingShells -join ' , ' ))
47+ }
48+
49+ foreach ($shellName in $uniqueShells ) {
50+ Write-Host (" Running tests in {0}..." -f $shellName )
51+ & $shellName - NoProfile - ExecutionPolicy Bypass - File $PSCommandPath - TestsPath $TestsPath - CurrentShellOnly
52+ if ($LASTEXITCODE -ne 0 ) {
53+ throw (" Tests failed in shell: {0}" -f $shellName )
54+ }
55+ }
56+
57+ Write-Host (" Pester: all tests passed in shells: {0}." -f ($uniqueShells -join ' , ' ))
58+ return
59+ }
60+
61+ if (-not (Get-Module - ListAvailable - Name Pester)) {
62+ throw " Pester is not installed in this shell. Run: Install-Module Pester -Scope CurrentUser -Force"
63+ }
64+
65+ Import-Module Pester - MinimumVersion 5.0 - ErrorAction Stop
66+
67+ $result = Invoke-Pester - Path $TestsPath - CI - PassThru - ErrorAction Stop
68+ if ($null -eq $result ) {
69+ throw ' Invoke-Pester returned no result.'
70+ }
2571if ($result.FailedCount -gt 0 ) {
2672 throw " Pester failed: $ ( $result.FailedCount ) test(s) failed."
2773}
2874
29- Write-Host " Pester: all tests passed ($ ( $ result.PassedCount ) passed). "
75+ Write-Host ( " Pester: all tests passed ({0} passed) in {1} {2}. " -f $ result.PassedCount , $PSVersionTable .PSEdition , $PSVersionTable .PSVersion )
0 commit comments