Skip to content

Commit 5f1eb80

Browse files
committed
wip
1 parent ab0b423 commit 5f1eb80

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

eng/scripts/tests/test-get-aspire-cli.ps1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ if (`$newPath.Contains(`$InstallPath) -and -not `$originalPath.Contains(`$Instal
282282

283283
Set-Content -Path $testScriptPath -Value $testScript
284284

285-
$process = Start-Process -FilePath "pwsh" -ArgumentList @("-File", $testScriptPath, $testInstallPath) -Wait -PassThru -RedirectStandardOutput $pathTestOut -RedirectStandardError $pathTestErr -NoNewWindow
285+
$pwsh = if (Get-Command Get-PwshPath -ErrorAction SilentlyContinue) { Get-PwshPath } else { 'pwsh' }
286+
$process = Start-Process -FilePath $pwsh -ArgumentList @("-File", $testScriptPath, $testInstallPath) -Wait -PassThru -RedirectStandardOutput $pathTestOut -RedirectStandardError $pathTestErr -NoNewWindow
286287

287288
$output = if (Test-Path $pathTestOut) { Get-Content $pathTestOut -Raw } else { "" }
288289
$error_output = if (Test-Path $pathTestErr) { Get-Content $pathTestErr -Raw } else { "" }
@@ -347,7 +348,8 @@ finally {
347348

348349
Set-Content -Path $testScriptPath -Value $testScript
349350

350-
$process = Start-Process -FilePath "pwsh" -ArgumentList @("-File", $testScriptPath, $testOutputGa) -Wait -PassThru -RedirectStandardOutput $gaTestOut -RedirectStandardError $gaTestErr -NoNewWindow
351+
$pwsh = if (Get-Command Get-PwshPath -ErrorAction SilentlyContinue) { Get-PwshPath } else { 'pwsh' }
352+
$process = Start-Process -FilePath $pwsh -ArgumentList @("-File", $testScriptPath, $testOutputGa) -Wait -PassThru -RedirectStandardOutput $gaTestOut -RedirectStandardError $gaTestErr -NoNewWindow
351353

352354
$output = if (Test-Path $gaTestOut) { Get-Content $gaTestOut -Raw } else { "" }
353355
$error_output = if (Test-Path $gaTestErr) { Get-Content $gaTestErr -Raw } else { "" }
@@ -393,7 +395,8 @@ function Test-URLConstruction {
393395
Invoke-Test "URL construction $($test.Name) quality" {
394396
try {
395397
$testPath = Join-Path $testBaseDir "test-url-$($test.Name)"
396-
$result = & pwsh -Command "& '$Script:ScriptPath' -Quality '$($test.Quality)' -InstallPath '$testPath' -WhatIf -Verbose" 2>&1
398+
$pwsh = if (Get-Command Get-PwshPath -ErrorAction SilentlyContinue) { Get-PwshPath } else { 'pwsh' }
399+
$result = & $pwsh -Command "& '$Script:ScriptPath' -Quality '$($test.Quality)' -InstallPath '$testPath' -WhatIf -Verbose" 2>&1
397400
$output = $result -join "`n"
398401

399402
if ($output -like $test.Expected) {

eng/scripts/tests/test-utils.ps1

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ $Script:TotalTests = 0
2525
$Script:PassedTests = 0
2626
$Script:FailedTests = 0
2727
$Script:TestResults = @()
28+
$Script:PwshPath = "pwsh" # Will be resolved to a working executable path
2829

2930
# Helper function for cross-platform Windows detection
3031
function Test-IsWindows {
@@ -82,6 +83,15 @@ function Initialize-TestFramework {
8283
$Script:TestResults = @()
8384
$Script:VerboseTests = $VerboseOutput
8485

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+
8595
# Create a top-level temporary directory for all test operations
8696
$tempPath = [System.IO.Path]::GetTempPath()
8797
$testId = [System.Guid]::NewGuid().ToString('N').Substring(0, 8)
@@ -98,6 +108,43 @@ function Initialize-TestFramework {
98108
}
99109
}
100110

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+
101148
<#
102149
.SYNOPSIS
103150
Writes colored output text
@@ -245,7 +292,7 @@ function Invoke-TestCommand {
245292
$allArgs += $Arguments
246293

247294
# Run the PowerShell command in a separate process
248-
$process = Start-Process -FilePath "pwsh" `
295+
$process = Start-Process -FilePath $Script:PwshPath `
249296
-ArgumentList $allArgs `
250297
-Wait -PassThru `
251298
-RedirectStandardOutput $tempOut `
@@ -660,7 +707,8 @@ function Test-HelpFunctionality {
660707

661708
Invoke-Test $TestName {
662709
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
664712
if ($helpOutput -and $helpOutput.Length -gt 0 -and ($helpOutput -match "SYNOPSIS" -or $helpOutput -match "DESCRIPTION" -or $helpOutput -match "NAME")) {
665713
Write-Output "Help functionality works: $helpOutput"
666714
return $helpOutput

0 commit comments

Comments
 (0)