|
| 1 | +# Run locally on Windows with ./.github/scripts/test-install-bootstrap.ps1; no registry or installed vp needed. |
| 2 | +$ErrorActionPreference = 'Stop' |
| 3 | +$source = Get-Content -LiteralPath (Join-Path $PSScriptRoot '../../packages/cli/install.ps1') -Raw |
| 4 | +. ([scriptblock]::Create(($source -replace '(?m)^ Main\r?$', ''))) |
| 5 | +function Exit-Installer { param([int]$Code = 1); $script:ExitCode = $Code; throw $script:InstallStopSignal } |
| 6 | +function Assert($Condition, [string]$Message) { |
| 7 | + if (-not $Condition) { throw $Message } |
| 8 | +} |
| 9 | + |
| 10 | +$testRoot = Join-Path $env:TEMP "vite-bootstrap-test-$(Get-Random)" |
| 11 | +$originalTemp = $env:TEMP |
| 12 | +$originalCheck = $env:VP_SELF_SETUP_SUPPORT_CHECK |
| 13 | +$originalPath = $env:Path |
| 14 | +$originalRegistry = $env:NPM_CONFIG_REGISTRY |
| 15 | +$fixtureSha = '0123456789012345678901234567890123456789' |
| 16 | +New-Item -ItemType Directory -Path "$testRoot/package", "$testRoot/tmp", "$testRoot/scripts" | Out-Null |
| 17 | +Set-Content -LiteralPath "$testRoot/package/vp.exe" -Value 'Payload fixture' |
| 18 | +@' |
| 19 | +if ($args.Count -eq 0) { |
| 20 | + if (Test-Path Env:VP_SELF_SETUP_SUPPORT_CHECK) { exit 99 } |
| 21 | + New-Item -ItemType File -Path "$testRoot/binary-invoked" | Out-Null |
| 22 | + if ($scenario -eq 'failure') { exit 42 } |
| 23 | + if ($env:VP_SELF_SETUP_SHELL -ne 'powershell') { exit 98 } |
| 24 | + if ($scenario -eq 'supported-pr' -and $env:NPM_CONFIG_REGISTRY -ne 'https://registry-bridge.viteplus.dev/') { exit 97 } |
| 25 | + Write-Output ("`$script:InstallDir = '{0}'" -f "$testRoot/data") |
| 26 | + Write-Output ("`$script:ShimDir = '{0}'" -f "$testRoot/installed bin") |
| 27 | + Write-Output ("`$script:CacheDir = '{0}'" -f "$testRoot/cache") |
| 28 | + Write-Output ("`$script:ConfigDir = '{0}'" -f "$testRoot/config") |
| 29 | + Write-Output ("`$script:StateDir = '{0}'" -f "$testRoot/state") |
| 30 | + exit 0 |
| 31 | +} |
| 32 | +if ($env:VP_SELF_SETUP_SUPPORT_CHECK -ne '1') { exit 99 } |
| 33 | +if ($scenario -in @('legacy', 'legacy-failure', 'pr')) { Write-Output 'Usage: vp [COMMAND]' } |
| 34 | +else { Write-Output 'vite-plus-self-setup-v1' } |
| 35 | +exit 0 |
| 36 | +'@ | Set-Content -LiteralPath "$testRoot/package/binary.ps1" |
| 37 | +@' |
| 38 | +param($BinarySource, $ResolvedVersion, $PreviewRef) |
| 39 | +$script:InstallDir = "$testRoot/data" |
| 40 | +$script:ShimDir = "$testRoot/installed bin" |
| 41 | +$script:CacheDir = "$testRoot/cache" |
| 42 | +$script:ConfigDir = "$testRoot/config" |
| 43 | +$script:StateDir = "$testRoot/state" |
| 44 | +if (-not [System.IO.Path]::IsPathRooted($BinarySource) -or -not (Test-Path -LiteralPath $BinarySource)) { throw 'Invalid payload path' } |
| 45 | +@($ResolvedVersion, $PreviewRef) | Set-Content -LiteralPath "$testRoot/legacy" |
| 46 | +if ($scenario -eq 'legacy-failure') { exit 42 } |
| 47 | +'@ | Set-Content -LiteralPath "$testRoot/scripts/install-legacy.ps1" |
| 48 | +& "$env:SystemRoot\System32\tar.exe" -czf "$testRoot/payload.tgz" -C $testRoot package |
| 49 | +Assert ($LASTEXITCODE -eq 0) 'Could not create fixture' |
| 50 | +$env:TEMP = "$testRoot/tmp" |
| 51 | + |
| 52 | +function Invoke-RestMethod { |
| 53 | + param($Uri) |
| 54 | + $script:Requests.Add("GET $Uri") |
| 55 | + return @{ version = '0.2.9' } |
| 56 | +} |
| 57 | +function Invoke-WebRequest { |
| 58 | + param($Uri, $Method, $OutFile, [switch]$UseBasicParsing, $ErrorAction) |
| 59 | + $script:Requests.Add("$Method $Uri") |
| 60 | + if ($Method -eq 'Head') { |
| 61 | + return @{ Headers = @{ 'x-commit-key' = "voidzero-dev:vite-plus:$fixtureSha" } } |
| 62 | + } |
| 63 | + Copy-Item -LiteralPath "$testRoot/payload.tgz" -Destination $OutFile |
| 64 | +} |
| 65 | + |
| 66 | +# Use an executable script fixture so these checks need no native compiler. |
| 67 | +$probe = ${function:Test-SelfSetupSupport} |
| 68 | +function Test-SelfSetupSupport { |
| 69 | + param($BinarySource) |
| 70 | + & $probe -BinarySource (Join-Path (Split-Path $BinarySource) 'binary.ps1') |
| 71 | +} |
| 72 | +$handoff = ${function:Invoke-InstallHandoff} |
| 73 | +function Invoke-InstallHandoff { |
| 74 | + param($BinarySource) |
| 75 | + & $handoff -BinarySource (Join-Path (Split-Path $BinarySource) 'binary.ps1') |
| 76 | +} |
| 77 | + |
| 78 | +try { |
| 79 | + foreach ($scenario in @('supported', 'legacy', 'legacy-failure', 'failure', 'pr', 'supported-pr')) { |
| 80 | + $env:Path = $originalPath |
| 81 | + $env:NPM_CONFIG_REGISTRY = 'https://custom.example' |
| 82 | + $script:Requests = New-Object 'System.Collections.Generic.List[string]' |
| 83 | + $script:ExitCode = 0 |
| 84 | + $script:PackageMetadata = $null |
| 85 | + Remove-Item -LiteralPath "$testRoot/legacy", "$testRoot/binary-invoked" -ErrorAction SilentlyContinue |
| 86 | + $env:VP_SELF_SETUP_SUPPORT_CHECK = if ($scenario -eq 'supported') { 'original' } else { $null } |
| 87 | + try { |
| 88 | + & { |
| 89 | + $ViteVersion = 'latest' |
| 90 | + $LocalTgz = $LocalBinary = $PrVersion = $PrCommitVersion = $null |
| 91 | + $NpmRegistry = 'https://custom.example' |
| 92 | + $InstallerDirectory = "$testRoot/scripts" |
| 93 | + if ($scenario -in @('pr', 'supported-pr')) { $PrVersion = '2406' } |
| 94 | + Main |
| 95 | + Assert ($env:NPM_CONFIG_REGISTRY -eq 'https://custom.example') 'Setup changed the caller registry' |
| 96 | + Assert ($script:InstallDir -eq "$testRoot/data") 'InstallDir was lost' |
| 97 | + Assert ($script:ShimDir -eq "$testRoot/installed bin") 'ShimDir was lost' |
| 98 | + Assert ($script:CacheDir -eq "$testRoot/cache") 'CacheDir was lost' |
| 99 | + Assert ($script:ConfigDir -eq "$testRoot/config") 'ConfigDir was lost' |
| 100 | + Assert ($script:StateDir -eq "$testRoot/state") 'StateDir was lost' |
| 101 | + } |
| 102 | + } catch { |
| 103 | + if ($scenario -notin @('failure', 'legacy-failure') -or -not (Test-IsInstallStopException $_)) { throw } |
| 104 | + } |
| 105 | + $expectedExit = if ($scenario -in @('failure', 'legacy-failure')) { 42 } else { 0 } |
| 106 | + Assert ($script:ExitCode -eq $expectedExit) 'Binary exit code was lost' |
| 107 | + if ($scenario -eq 'supported') { |
| 108 | + Assert (($env:Path -split ';')[0] -eq "$testRoot/installed bin") 'Installed bin directory was not added to the current PATH' |
| 109 | + } elseif ($scenario -eq 'failure') { |
| 110 | + Assert ($env:Path -eq $originalPath) 'Failed setup changed the current PATH' |
| 111 | + } |
| 112 | + $usesBinary = $scenario -in @('supported', 'supported-pr', 'failure') |
| 113 | + Assert ((Test-Path -LiteralPath "$testRoot/binary-invoked") -eq $usesBinary) 'Incorrect binary invocation' |
| 114 | + Assert ((Test-Path -LiteralPath "$testRoot/legacy") -eq (-not $usesBinary)) 'Incorrect legacy invocation' |
| 115 | + if ($scenario -eq 'pr') { |
| 116 | + $record = @(Get-Content -LiteralPath "$testRoot/legacy") |
| 117 | + Assert ($record[0] -eq "0.0.0-commit.$fixtureSha" -and $record[1] -eq '2406') 'Resolved preview identity was lost' |
| 118 | + Assert ($script:Requests[-1].EndsWith("@$fixtureSha")) 'Payload used a mutable ref' |
| 119 | + Assert ($script:Requests.Count -eq 2) 'Preview was resolved or downloaded more than once' |
| 120 | + } |
| 121 | + Assert (@(Get-ChildItem -LiteralPath "$testRoot/tmp" -Force).Count -eq 0) 'Temporary payload was not cleaned up' |
| 122 | + Write-Host "PASS: $scenario" |
| 123 | + } |
| 124 | +} finally { |
| 125 | + $env:VP_SELF_SETUP_SUPPORT_CHECK = $originalCheck |
| 126 | + $env:Path = $originalPath |
| 127 | + $env:NPM_CONFIG_REGISTRY = $originalRegistry |
| 128 | + $env:TEMP = $originalTemp |
| 129 | + Remove-Item -LiteralPath $testRoot -Recurse -Force |
| 130 | + $global:LASTEXITCODE = 0 |
| 131 | +} |
0 commit comments