# Installs, runs, upgrades and removes the packed installers on a clean Windows runner (BRIEF ยง18): silent mode, # folder with a space, Add/Remove Programs entry, shortcuts, the .titano file type, upgrade in place (files of the # previous version removed), user data kept, the uninstaller removing itself, and the documented exit codes. param( [Parameter(Mandatory)] [string] $Setup1, [Parameter(Mandatory)] [string] $Setup2, [Parameter(Mandatory)] [string] $UploaderSetup, [Parameter(Mandatory)] [string] $Version1, [Parameter(Mandatory)] [string] $Version2, [Parameter(Mandatory)] [string] $Screenshots ) $ErrorActionPreference = 'Stop' Set-StrictMode -Version Latest # Relative paths are taken from the PowerShell location: .NET resolves them from the process directory, which a # Set-Location does not move, and the programs get the screenshot folder as an argument. $Setup1 = (Resolve-Path -LiteralPath $Setup1).Path $Setup2 = (Resolve-Path -LiteralPath $Setup2).Path $UploaderSetup = (Resolve-Path -LiteralPath $UploaderSetup).Path $Screenshots = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Screenshots) function Invoke-Program([string] $Exe, [string[]] $Arguments) { $start = [System.Diagnostics.ProcessStartInfo]::new($Exe) foreach ($argument in $Arguments) { $start.ArgumentList.Add($argument) } $start.UseShellExecute = $false $process = [System.Diagnostics.Process]::Start($start) if (-not $process.WaitForExit(600000)) { $process.Kill($true) throw "$Exe $Arguments did not finish in 10 minutes" } Write-Host "$(Split-Path $Exe -Leaf) $Arguments -> exit $($process.ExitCode)" return $process.ExitCode } function Assert-True([bool] $Condition, [string] $Message) { if (-not $Condition) { throw "FAILED: $Message" } Write-Host "ok: $Message" } function Wait-Removed([string] $Path) { for ($i = 0; $i -lt 120 -and (Test-Path -LiteralPath $Path); $i++) { Start-Sleep -Milliseconds 500 } return -not (Test-Path -LiteralPath $Path) } function Get-DefaultValue([string] $Key) { if (-not (Test-Path -LiteralPath $Key)) { return $null } return (Get-Item -LiteralPath $Key).GetValue('') } $dir = Join-Path $env:RUNNER_TEMP 'Titano Test' $entryKey = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\Titano' $programs = [Environment]::GetFolderPath('Programs') $desktop = [Environment]::GetFolderPath('DesktopDirectory') $links = @( (Join-Path $programs 'Titano.lnk'), (Join-Path $programs 'Titano Uploader.lnk'), (Join-Path $desktop 'Titano.lnk'), (Join-Path $desktop 'Titano Uploader.lnk')) $userData = Join-Path $env:LOCALAPPDATA 'Titano' $userFile = Join-Path $userData 'kept-by-setup.txt' New-Item -ItemType Directory -Force -Path $userData | Out-Null Set-Content -LiteralPath $userFile -Value 'user data' Write-Host '--- Arguments' Assert-True ((Invoke-Program $Setup1 @('/BOGUS')) -eq 2) 'an unknown switch exits with 2 (invalid arguments)' Assert-True ((Invoke-Program $Setup1 @('/S', '/uninstall')) -eq 6) 'removing a product that is not installed exits with 6' Write-Host "--- Install $Version1 into '$dir'" Assert-True ((Invoke-Program $Setup1 @('/S', "/DIR=$dir")) -eq 0) 'silent install exits with 0' foreach ($file in 'Titano.exe', 'Titano.Uploader.exe', 'Uninstall Titano.exe', 'install-log.json', 'legacy.txt') { Assert-True (Test-Path -LiteralPath (Join-Path $dir $file)) "$file installed" } $entry = Get-ItemProperty -LiteralPath $entryKey Assert-True ($entry.DisplayVersion -eq $Version1) "Add/Remove Programs lists version $Version1" Assert-True ($entry.InstallLocation -eq $dir) 'Add/Remove Programs lists the chosen folder' Assert-True ($entry.QuietUninstallString -eq "`"$(Join-Path $dir 'Uninstall Titano.exe')`" /uninstall /S") 'quiet uninstall command points at the uninstaller' foreach ($link in $links) { Assert-True (Test-Path -LiteralPath $link) "shortcut '$link' created" } Assert-True ((Get-DefaultValue 'HKCU:\Software\Classes\.titano') -eq 'Titano.Project') '.titano opens with Titano' Assert-True ((Get-DefaultValue 'HKCU:\Software\Classes\Titano.Project\shell\open\command') -eq "`"$(Join-Path $dir 'Titano.exe')`" `"%1`"") 'Titano.Project opens the installed Titano.exe' Write-Host '--- Run the installed programs' foreach ($program in 'Titano.exe', 'Titano.Uploader.exe') { $before = @(Get-ChildItem -Path $Screenshots -Filter *.png -ErrorAction SilentlyContinue).Count $code = Invoke-Program (Join-Path $dir $program) @('--capture', $Screenshots) $count = @(Get-ChildItem -Path $Screenshots -Filter *.png -ErrorAction SilentlyContinue).Count - $before Assert-True ($code -eq 0 -and $count -gt 0) "installed $program starts and draws its screens ($count screenshot(s))" } Write-Host "--- Upgrade in place to $Version2" Assert-True ((Invoke-Program $Setup2 @('/S')) -eq 0) 'silent upgrade exits with 0' Assert-True ((Get-ItemProperty -LiteralPath $entryKey).DisplayVersion -eq $Version2) "Add/Remove Programs lists version $Version2" Assert-True ((Get-Content -Raw -LiteralPath (Join-Path $dir 'install-log.json') | ConvertFrom-Json).version -eq $Version2) "install log records version $Version2" Assert-True (Test-Path -LiteralPath (Join-Path $dir 'Titano.exe')) 'Titano.exe still installed' Assert-True (-not (Test-Path -LiteralPath (Join-Path $dir 'legacy.txt'))) 'a file only the previous version had is removed' foreach ($link in $links) { Assert-True (Test-Path -LiteralPath $link) "shortcut '$link' kept" } Assert-True (Test-Path -LiteralPath $userFile) 'user data kept by the upgrade' Write-Host '--- Uninstall with the installed uninstaller' Assert-True ((Invoke-Program (Join-Path $dir 'Uninstall Titano.exe') @('/uninstall', '/S')) -eq 0) 'silent uninstall exits with 0' Assert-True (Wait-Removed $dir) 'install folder removed, uninstaller included' Assert-True (-not (Test-Path -LiteralPath $entryKey)) 'Add/Remove Programs entry removed' foreach ($link in $links) { Assert-True (-not (Test-Path -LiteralPath $link)) "shortcut '$link' removed" } Assert-True (-not (Test-Path -LiteralPath 'HKCU:\Software\Classes\Titano.Project')) 'Titano.Project file type removed' Assert-True ($null -eq (Get-DefaultValue 'HKCU:\Software\Classes\.titano')) '.titano no longer opens with Titano' Assert-True (Test-Path -LiteralPath $userFile) 'user data kept by the uninstall' Write-Host '--- Uploader installer: default folder, removal through the installer itself' $uploaderDir = Join-Path $env:LOCALAPPDATA 'Programs\Titano Uploader' $uploaderKey = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\TitanoUploader' Assert-True ((Invoke-Program $UploaderSetup @('/S')) -eq 0) 'silent Uploader install exits with 0' Assert-True (Test-Path -LiteralPath (Join-Path $uploaderDir 'Titano.Uploader.exe')) 'Uploader installed in %LOCALAPPDATA%\Programs\Titano Uploader' Assert-True (Test-Path -LiteralPath $uploaderKey) 'Uploader listed in Add/Remove Programs' Assert-True ((Invoke-Program $UploaderSetup @('/uninstall', '/S')) -eq 0) 'silent Uploader removal exits with 0' Assert-True (Wait-Removed $uploaderDir) 'Uploader folder removed' Assert-True (-not (Test-Path -LiteralPath $uploaderKey)) 'Uploader entry removed' Assert-True (Test-Path -LiteralPath $userFile) 'user data kept' Write-Host 'Installer test passed.'