Files
2026-09-22 19:15:35 +08:00

107 lines
4.2 KiB
PowerShell

$ErrorActionPreference = "Stop"
# Do not treat nonzero exit codes from native commands as terminating errors.
if (Test-Path variable:PSNativeCommandUseErrorActionPreference) {
$PSNativeCommandUseErrorActionPreference = $false
}
# Isolate user resources, credentials, temporary files, and tool configuration.
$tempParent = $env:TEMP
if ([string]::IsNullOrEmpty($tempParent)) {
$tempParent = [System.IO.Path]::GetTempPath()
}
$tempParent = $tempParent.TrimEnd('\', '/')
$testRoot = Join-Path $tempParent ("pi-test." + [guid]::NewGuid().ToString("N").Substring(0, 6))
$homeDir = Join-Path $testRoot "home"
$tmpDir = Join-Path $testRoot "tmp"
$cacheDir = Join-Path $testRoot "cache"
New-Item -ItemType Directory -Path $testRoot -Force | Out-Null
New-Item -ItemType Directory -Path (Join-Path $homeDir ".config"), $tmpDir, (Join-Path $cacheDir "npm") -Force | Out-Null
# Mark the generated root so cleanup can verify ownership before deleting it.
New-Item -ItemType File -Path (Join-Path $testRoot ".pi-test-owned"), (Join-Path $testRoot "npm-userconfig"), (Join-Path $testRoot "npm-globalconfig"), (Join-Path $testRoot "gitconfig") -Force | Out-Null
# Git runs these helpers instead of a real askpass program and editor.
Set-Content -LiteralPath (Join-Path $testRoot "git-askpass.cmd") -Value "@exit /b 1" -Encoding ascii
Set-Content -LiteralPath (Join-Path $testRoot "git-editor.cmd") -Value "@exit /b 0" -Encoding ascii
# Only remove the marked directory created above, never an unverified path.
function Remove-TestRoot {
$expectedName = [System.IO.Path]::GetFileName($testRoot) -match '^pi-test\.[0-9a-f]{6}$'
$expectedParent = [System.IO.Path]::GetDirectoryName([System.IO.Path]::GetFullPath($testRoot)) -eq $tempParent
$owned = Test-Path -LiteralPath (Join-Path $testRoot ".pi-test-owned")
if ($expectedName -and $expectedParent -and $owned) {
Remove-Item -LiteralPath $testRoot -Recurse -Force
} else {
Write-Warning "Refusing to remove unverified test directory: $testRoot"
}
}
$savedEnv = @{}
foreach ($entry in Get-ChildItem Env:) {
$savedEnv[$entry.Name] = $entry.Value
}
$exitCode = 1
try {
# Start from an empty environment and allow only required platform and test settings.
Get-ChildItem Env: | Remove-Item -Force -ErrorAction SilentlyContinue
$testEnv = [ordered]@{
"PATH" = $savedEnv["PATH"]
"HOME" = $homeDir
"USERPROFILE" = $homeDir
"TMPDIR" = $tmpDir
"TMP" = $tmpDir
"TEMP" = $tmpDir
"XDG_CONFIG_HOME" = Join-Path $homeDir ".config"
"XDG_CACHE_HOME" = $cacheDir
"LANG" = "C"
"LC_ALL" = "C"
"TZ" = "UTC"
"GIT_CONFIG_NOSYSTEM" = "1"
"GIT_CONFIG_GLOBAL" = Join-Path $testRoot "gitconfig"
"GIT_TERMINAL_PROMPT" = "0"
"GIT_ASKPASS" = Join-Path $testRoot "git-askpass.cmd"
"GIT_EDITOR" = Join-Path $testRoot "git-editor.cmd"
"GIT_SEQUENCE_EDITOR" = Join-Path $testRoot "git-editor.cmd"
"NPM_CONFIG_USERCONFIG" = Join-Path $testRoot "npm-userconfig"
"NPM_CONFIG_GLOBALCONFIG" = Join-Path $testRoot "npm-globalconfig"
"NPM_CONFIG_CACHE" = Join-Path $cacheDir "npm"
"PI_NO_LOCAL_LLM" = "1"
"AWS_EC2_METADATA_DISABLED" = "true"
}
# Native Windows needs these inherited values to launch child processes.
foreach ($name in @("SystemRoot", "SYSTEMROOT", "WINDIR", "COMSPEC", "PATHEXT")) {
if ($savedEnv.ContainsKey($name)) {
$testEnv[$name] = $savedEnv[$name]
}
}
# Preserve CI detection only for runner behavior and test reporting.
foreach ($name in @("CI", "GITHUB_ACTIONS")) {
if ($savedEnv.ContainsKey($name)) {
$testEnv[$name] = $savedEnv[$name]
}
}
foreach ($name in $testEnv.Keys) {
Set-Item -Path "Env:$name" -Value $testEnv[$name]
}
Write-Host "Running tests without API keys in isolated home: $homeDir"
& npm test
if ($null -ne $LASTEXITCODE) {
$exitCode = $LASTEXITCODE
}
} finally {
Remove-TestRoot
Get-ChildItem Env: | Remove-Item -Force -ErrorAction SilentlyContinue
foreach ($name in $savedEnv.Keys) {
Set-Item -Path "Env:$name" -Value $savedEnv[$name]
}
}
exit $exitCode