[CmdletBinding()] param([switch]$Summary) # Checks that Python resolution skips a PATH entry that cannot run Python (such as the Windows # Store app execution alias) and that check-capabilities reports the resolved interpreter. $ErrorActionPreference = 'Stop' $root = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent $stubs = Join-Path ([System.IO.Path]::GetTempPath()) ('capabilities-' + [Guid]::NewGuid().ToString('N')) New-Item $stubs -ItemType Directory | Out-Null $previousPath = $env:PATH try { $onWindows = [Environment]::OSVersion.Platform -eq [PlatformID]::Win32NT # python imitates the Store alias: it is on PATH but only prints an install hint. if ($onWindows) { Set-Content (Join-Path $stubs 'python.cmd') "@echo Python was not found; run without arguments to install from the Microsoft Store 1>&2`r`n@exit /b 9009" -Encoding ASCII Set-Content (Join-Path $stubs 'python3.cmd') "@if `"%~1`"==`"-c`" echo 3.12.0`r`n@exit /b 0" -Encoding ASCII } else { Set-Content (Join-Path $stubs 'python') "#!/bin/sh`necho 'Python was not found' >&2`nexit 9009`n" -NoNewline Set-Content (Join-Path $stubs 'python3') "#!/bin/sh`nfor arg in `"`$@`"; do [ `"`$arg`" != -c ] || { echo 3.12.0; exit 0; }; done`nexit 0`n" -NoNewline & chmod +x (Join-Path $stubs 'python') (Join-Path $stubs 'python3') } $env:PATH = $stubs + [System.IO.Path]::PathSeparator + $previousPath $hostPath = (Get-Process -Id $PID).Path $resolved = & $hostPath -NoProfile -ExecutionPolicy Bypass -Command ". '$root/shared/hooks/scripts/Resolve-Python.ps1'; (Get-PythonCommand).Display" if ("$resolved".Trim() -ne 'python3') { throw "Resolver chose '$resolved' instead of skipping the broken python." } $output = (& $hostPath -NoProfile -ExecutionPolicy Bypass -File (Join-Path $root 'scripts/commands/check-capabilities.ps1') 2>&1 | Out-String) if ($output -notmatch 'PASS Python 3\.12\.0') { throw "check-capabilities did not use the resolved interpreter:`n$output" } } finally { $env:PATH = $previousPath Remove-Item -LiteralPath $stubs -Recurse -Force -ErrorAction SilentlyContinue } if ($Summary) { Write-Output 'Tests: PASS | capabilities' } else { Write-Output 'PASS capabilities: broken Python aliases are skipped' }