返回 CodeWhale
installer-path-regression.tests.ps1
根目录 / scripts / installer / installer-path-regression.tests.ps1
1 [CmdletBinding()]
2 param(
3 [switch] $AllowUserPathMutation
4 )
5
6 $ErrorActionPreference = 'Stop'
7 Set-StrictMode -Version Latest
8
9 if ([System.Environment]::OSVersion.Platform -ne [System.PlatformID]::Win32NT) {
10 throw 'The installer PATH regression only supports Windows.'
11 }
12 if (-not $AllowUserPathMutation) {
13 throw 'Pass -AllowUserPathMutation to confirm this test may temporarily replace the current-user PATH.'
14 }
15
16 function Get-RawUserPath {
17 param(
18 [Parameter(Mandatory = $true)]
19 [Microsoft.Win32.RegistryKey] $EnvironmentKey
20 )
21
22 if (-not (@($EnvironmentKey.GetValueNames()) -contains 'Path')) {
23 return $null
24 }
25
26 return [string] $EnvironmentKey.GetValue(
27 'Path',
28 $null,
29 [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames
30 )
31 }
32
33 $repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..\..')).Path
34 $testRoot = Join-Path `
35 ([System.IO.Path]::GetTempPath()) `
36 "codewhale-installer-path-$PID-$([guid]::NewGuid().ToString('N'))"
37 $stageRoot = Join-Path $testRoot 'source'
38 $stageInstallerDir = Join-Path $stageRoot 'scripts\installer'
39 $installDir = Join-Path $testRoot 'installed'
40 $programsDir = [Environment]::GetFolderPath([Environment+SpecialFolder]::Programs)
41 $shortcutDir = Join-Path $programsDir 'CodeWhale'
42 $shortcut = Join-Path $shortcutDir 'CodeWhale.lnk'
43 $environmentKey = [Microsoft.Win32.Registry]::CurrentUser.CreateSubKey('Environment')
44 if ($null -eq $environmentKey) {
45 throw 'Could not open or create the current-user Environment registry key.'
46 }
47
48 $originalPathExists = @($environmentKey.GetValueNames()) -contains 'Path'
49 if ($originalPathExists) {
50 $originalPath = Get-RawUserPath -EnvironmentKey $environmentKey
51 $originalPathKind = $environmentKey.GetValueKind('Path')
52 }
53 else {
54 $originalPath = $null
55 $originalPathKind = [Microsoft.Win32.RegistryValueKind]::ExpandString
56 }
57
58 try {
59 [void] (New-Item -ItemType Directory -Path $stageInstallerDir -Force)
60 $noticeFiles = @('LICENSE', 'THIRD_PARTY_NOTICES.md', 'docs/THIRD_PARTY_NOTICES.md')
61 [void] (New-Item -ItemType Directory -Path (Join-Path $stageRoot 'docs') -Force)
62 foreach ($file in $noticeFiles) {
63 Copy-Item -LiteralPath (Join-Path $repoRoot $file) -Destination (Join-Path $stageRoot $file)
64 }
65 Copy-Item `
66 -LiteralPath (Join-Path $PSScriptRoot 'codewhale.nsi') `
67 -Destination (Join-Path $stageInstallerDir 'codewhale.nsi')
68 Copy-Item `
69 -LiteralPath (Join-Path $PSScriptRoot 'update-user-path.ps1') `
70 -Destination (Join-Path $stageInstallerDir 'update-user-path.ps1')
71 Copy-Item `
72 -LiteralPath (Join-Path $PSScriptRoot 'codewhale.bat') `
73 -Destination (Join-Path $stageInstallerDir 'codewhale.bat')
74
75 foreach ($binary in @('codewhale.exe', 'codew.exe', 'codewhale-tui.exe')) {
76 [System.IO.File]::WriteAllBytes(
77 (Join-Path $stageInstallerDir $binary),
78 [byte[]] @(0x4d, 0x5a)
79 )
80 }
81
82 $makensisCandidates = @(
83 "${env:ProgramFiles(x86)}\NSIS\makensis.exe",
84 "$env:ProgramFiles\NSIS\makensis.exe"
85 )
86 $makensis = $makensisCandidates |
87 Where-Object { Test-Path -LiteralPath $_ -PathType Leaf } |
88 Select-Object -First 1
89 if ([string]::IsNullOrWhiteSpace($makensis)) {
90 throw 'makensis.exe was not found after installing NSIS.'
91 }
92
93 Push-Location $stageInstallerDir
94 try {
95 & $makensis '/DVERSION=0.0.0-test' 'codewhale.nsi'
96 if ($LASTEXITCODE -ne 0) {
97 throw "makensis.exe exited with code $LASTEXITCODE."
98 }
99 }
100 finally {
101 Pop-Location
102 }
103
104 $installer = Join-Path $stageInstallerDir 'CodeWhaleSetup.exe'
105 if (-not (Test-Path -LiteralPath $installer -PathType Leaf)) {
106 throw 'CodeWhaleSetup.exe was not produced.'
107 }
108
109 $canaries = [System.Collections.Generic.List[string]]::new()
110 $index = 1
111 while ((($canaries -join ';').Length) -lt 1800) {
112 $canaries.Add(('C:\CWCanary\DevelopmentTool{0:D4}\bin' -f $index))
113 $index++
114 }
115 $seedPath = $canaries -join ';'
116 $environmentKey.SetValue(
117 'Path',
118 $seedPath,
119 [Microsoft.Win32.RegistryValueKind]::ExpandString
120 )
121
122 $installArguments = [System.Collections.Generic.List[string]]::new()
123 $installArguments.Add('/S')
124 $installArguments.Add("/D=$installDir")
125 $collisionDir = Join-Path $testRoot 'collision'
126 $collisionDocs = Join-Path $collisionDir 'codewhale-notices'
127 [void] (New-Item -ItemType Directory -Path $collisionDocs -Force)
128 $collisionFile = Join-Path $collisionDocs 'LICENSE'
129 [System.IO.File]::WriteAllText($collisionFile, 'belongs to another project')
130 $refused = Start-Process -FilePath $installer -ArgumentList @('/S', "/D=$collisionDir") -Wait -PassThru
131 if ($refused.ExitCode -eq 0 -or [System.IO.File]::ReadAllText($collisionFile) -cne 'belongs to another project' -or
132 (Test-Path -LiteralPath (Join-Path $collisionDir '.codewhale-install'))) {
133 throw 'Installer did not preserve an existing release notice destination.'
134 }
135 $installProcess = Start-Process `
136 -FilePath $installer `
137 -ArgumentList @($installArguments) `
138 -Wait `
139 -PassThru
140 if ($installProcess.ExitCode -ne 0) {
141 throw "CodeWhaleSetup.exe exited with code $($installProcess.ExitCode)."
142 }
143
144 $rootMarker = Join-Path $installDir '.codewhale-install'
145 $staleLicense = Join-Path $installDir 'codewhale-notices/LICENSE'
146 Move-Item -LiteralPath $rootMarker -Destination "$rootMarker.saved"
147 try {
148 [System.IO.File]::WriteAllText($staleLicense, 'belongs to the replacement directory')
149 $refused = Start-Process -FilePath $installer -ArgumentList @($installArguments) -Wait -PassThru
150 if ($refused.ExitCode -eq 0 -or [System.IO.File]::ReadAllText($staleLicense) -cne 'belongs to the replacement directory' -or
151 (Test-Path -LiteralPath $rootMarker)) {
152 throw 'Stale registry ownership allowed the installer to overwrite a replacement directory.'
153 }
154 } finally {
155 Move-Item -LiteralPath "$rootMarker.saved" -Destination $rootMarker
156 }
157 $upgrade = Start-Process -FilePath $installer -ArgumentList @($installArguments) -Wait -PassThru
158 if ($upgrade.ExitCode -ne 0) { throw 'An upgrade to the owned directory failed.' }
159 foreach ($file in $noticeFiles) {
160 $installed = Join-Path (Join-Path $installDir 'codewhale-notices') $file
161 if ((Get-FileHash -LiteralPath $installed).Hash -ne (Get-FileHash -LiteralPath (Join-Path $repoRoot $file)).Hash) {
162 throw "Installed notice differs from the release source: $file"
163 }
164 }
165 $expectedBin = Join-Path $installDir 'bin'
166 $afterInstall = Get-RawUserPath -EnvironmentKey $environmentKey
167 if (([string] $afterInstall) -cne "$seedPath;$expectedBin") {
168 throw "The installer did not preserve the seeded long PATH exactly. Before=$($seedPath.Length), after=$(([string] $afterInstall).Length)."
169 }
170 if ($environmentKey.GetValueKind('Path') -ne [Microsoft.Win32.RegistryValueKind]::ExpandString) {
171 throw 'The installer changed the user PATH registry value kind.'
172 }
173
174 $launcher = Join-Path $expectedBin 'codewhale.bat'
175 if (-not (Test-Path -LiteralPath $launcher -PathType Leaf)) {
176 throw 'The installer did not install codewhale.bat.'
177 }
178 $launcherText = [System.IO.File]::ReadAllText($launcher)
179 if ($launcherText -notmatch 'where wt') {
180 throw 'Installed codewhale.bat does not prefer Windows Terminal.'
181 }
182 if ($launcherText -notmatch 'codewhale\.exe') {
183 throw 'Installed codewhale.bat does not launch codewhale.exe.'
184 }
185 if (-not (Test-Path -LiteralPath $shortcut -PathType Leaf)) {
186 throw 'The installer did not create the Start Menu shortcut.'
187 }
188 $shell = New-Object -ComObject WScript.Shell
189 try {
190 $lnk = $shell.CreateShortcut($shortcut)
191 $expectedTarget = [System.IO.Path]::GetFullPath($launcher)
192 $actualTarget = [System.IO.Path]::GetFullPath($lnk.TargetPath)
193 if ($actualTarget -cne $expectedTarget) {
194 throw "Start Menu shortcut target was '$($lnk.TargetPath)', expected '$launcher'."
195 }
196 }
197 finally {
198 [void] [System.Runtime.InteropServices.Marshal]::ReleaseComObject($shell)
199 }
200
201 $uninstaller = Join-Path $installDir 'Uninstall.exe'
202 if (-not (Test-Path -LiteralPath $uninstaller -PathType Leaf)) {
203 throw 'Uninstall.exe was not produced.'
204 }
205 $uninstallProcess = Start-Process `
206 -FilePath $uninstaller `
207 -ArgumentList '/S' `
208 -Wait `
209 -PassThru
210 if ($uninstallProcess.ExitCode -ne 0) {
211 throw "Uninstall.exe exited with code $($uninstallProcess.ExitCode)."
212 }
213
214 $afterUninstall = Get-RawUserPath -EnvironmentKey $environmentKey
215 if (([string] $afterUninstall) -cne $seedPath) {
216 throw 'Install followed by uninstall did not restore the seeded long PATH exactly.'
217 }
218 if (Test-Path -LiteralPath (Join-Path $installDir 'codewhale-notices')) {
219 throw 'The uninstaller left release notices behind.'
220 }
221 if (Test-Path -LiteralPath $launcher -PathType Leaf) {
222 throw 'Uninstall did not remove codewhale.bat.'
223 }
224 if (Test-Path -LiteralPath $shortcut -PathType Leaf) {
225 throw 'Uninstall did not remove the Start Menu shortcut.'
226 }
227 if (Test-Path -LiteralPath $shortcutDir) {
228 throw 'Uninstall did not remove the Start Menu folder.'
229 }
230
231 Write-Host "Full NSIS installer PATH regression passed with $($seedPath.Length) characters."
232 }
233 finally {
234 $uninstaller = Join-Path $installDir 'Uninstall.exe'
235 if (Test-Path -LiteralPath $uninstaller -PathType Leaf) {
236 try {
237 [void] (Start-Process -FilePath $uninstaller -ArgumentList '/S' -Wait -PassThru)
238 }
239 catch {
240 Write-Warning "Best-effort test uninstallation failed: $_"
241 }
242 }
243
244 if ($originalPathExists) {
245 $environmentKey.SetValue('Path', $originalPath, $originalPathKind)
246 }
247 else {
248 $environmentKey.DeleteValue('Path', $false)
249 }
250 $environmentKey.Close()
251
252 if (Test-Path -LiteralPath $shortcut -PathType Leaf) {
253 Remove-Item -LiteralPath $shortcut -Force -ErrorAction SilentlyContinue
254 }
255 if (Test-Path -LiteralPath $shortcutDir) {
256 Remove-Item -LiteralPath $shortcutDir -Recurse -Force -ErrorAction SilentlyContinue
257 }
258
259 if (Test-Path -LiteralPath $testRoot) {
260 Remove-Item -LiteralPath $testRoot -Recurse -Force -ErrorAction SilentlyContinue
261 }
262 }
263
263 lines Plain Text