@@ -25,6 +25,7 @@ $Script:TotalTests = 0
25
25
$Script :PassedTests = 0
26
26
$Script :FailedTests = 0
27
27
$Script :TestResults = @ ()
28
+ $Script :PwshPath = " pwsh" # Will be resolved to a working executable path
28
29
29
30
# Helper function for cross-platform Windows detection
30
31
function Test-IsWindows {
@@ -82,6 +83,15 @@ function Initialize-TestFramework {
82
83
$Script :TestResults = @ ()
83
84
$Script :VerboseTests = $VerboseOutput
84
85
86
+ # Resolve a working PowerShell executable path (handles scenarios where default pwsh path is not directly startable)
87
+ $Script :PwshPath = Resolve-PwshPath
88
+
89
+ # Harden environment to prevent help/browser tools from launching external viewers (macOS/Linux)
90
+ $env: PAGER = ' cat'
91
+ $env: MANPAGER = ' cat'
92
+ $env: BROWSER = ' true'
93
+ $env: POWERSHELL_TELEMETRY_OPTOUT = ' 1'
94
+
85
95
# Create a top-level temporary directory for all test operations
86
96
$tempPath = [System.IO.Path ]::GetTempPath()
87
97
$testId = [System.Guid ]::NewGuid().ToString(' N' ).Substring(0 , 8 )
@@ -98,6 +108,43 @@ function Initialize-TestFramework {
98
108
}
99
109
}
100
110
111
+ function Resolve-PwshPath {
112
+ [CmdletBinding ()]
113
+ [OutputType ([string ])]
114
+ param ()
115
+
116
+ $candidatePaths = @ ()
117
+ # Prefer well-known system locations first to avoid store-wrapper paths that can trigger mailcap/browser logic
118
+ $candidatePaths += @ (
119
+ " /usr/bin/pwsh" ,
120
+ " /opt/microsoft/powershell/7/pwsh" ,
121
+ " /snap/bin/pwsh"
122
+ )
123
+
124
+ try {
125
+ $cmd = Get-Command pwsh - ErrorAction Stop
126
+ if ($cmd -and $cmd.Path ) { $candidatePaths += $cmd.Path }
127
+ } catch { }
128
+
129
+ $candidatePaths = $candidatePaths | Where-Object { $_ -and (Test-Path $_ ) } | Select-Object - Unique
130
+
131
+ foreach ($path in $candidatePaths ) {
132
+ try {
133
+ & $path - NoLogo - NoProfile - Command " exit 0" * > $null 2>&1
134
+ if ($LASTEXITCODE -eq 0 ) { return $path }
135
+ } catch { }
136
+ }
137
+
138
+ # Fallback
139
+ return " pwsh"
140
+ }
141
+
142
+ function Get-PwshPath {
143
+ [CmdletBinding ()]
144
+ param ()
145
+ return $Script :PwshPath
146
+ }
147
+
101
148
<#
102
149
. SYNOPSIS
103
150
Writes colored output text
@@ -245,7 +292,7 @@ function Invoke-TestCommand {
245
292
$allArgs += $Arguments
246
293
247
294
# Run the PowerShell command in a separate process
248
- $process = Start-Process - FilePath " pwsh " `
295
+ $process = Start-Process - FilePath $ Script :PwshPath `
249
296
- ArgumentList $allArgs `
250
297
- Wait - PassThru `
251
298
- RedirectStandardOutput $tempOut `
@@ -660,7 +707,8 @@ function Test-HelpFunctionality {
660
707
661
708
Invoke-Test $TestName {
662
709
try {
663
- $helpOutput = & pwsh - Command " Get-Help '$ScriptPath '" 2>&1 | Out-String
710
+ $pwsh = Get-PwshPath
711
+ $helpOutput = & $pwsh - Command " Get-Help '$ScriptPath '" 2>&1 | Out-String
664
712
if ($helpOutput -and $helpOutput.Length -gt 0 -and ($helpOutput -match " SYNOPSIS" -or $helpOutput -match " DESCRIPTION" -or $helpOutput -match " NAME" )) {
665
713
Write-Output " Help functionality works: $helpOutput "
666
714
return $helpOutput
0 commit comments