[CmdletBinding()] param([string]$LauncherPath, [string]$RepositoryRoot) $ErrorActionPreference = 'Stop' $repoRoot = if ($RepositoryRoot) { $RepositoryRoot } else { Split-Path $PSScriptRoot -Parent } $sourcePath = Join-Path $repoRoot 'Plain Craft Launcher 2\Modules\Base\ModLauncherUpdate.vb' $testRoot = Join-Path $repoRoot ('artifacts\update-tests\' + [Guid]::NewGuid().ToString('N')) [IO.Directory]::CreateDirectory($testRoot) | Out-Null $framework = Join-Path $env:WINDIR 'Microsoft.NET\Framework\v4.0.30319' $testLibrary = Join-Path $testRoot 'LauncherUpdateTests.dll' & (Join-Path $framework 'vbc.exe') /nologo /target:library "/out:$testLibrary" $sourcePath if ($LASTEXITCODE -ne 0) { throw 'Update module compilation failed' } $updateType = [Reflection.Assembly]::LoadFile($testLibrary).GetType('ModLauncherUpdate') $parse = $updateType.GetMethod('ParseLauncherVersion') $replace = $updateType.GetMethod('ReplaceLauncherFile') $testState = @{ Passed = 0 } function Assert-True([bool]$Condition, [string]$Description) { if (-not $Condition) { throw "FAIL: $Description" } $testState.Passed++ Write-Host "PASS: $Description" } function Replace-File([int]$PreviousId, [string]$Target, [string]$Source, [int]$Timeout = 30000) { $replace.Invoke($null, @($PreviousId, $Target, $Source, $Timeout)) } foreach ($inputVersion in @('0.1.1', 'v0.1.1', '0.1.1-dev', '0.1.1+build')) { Assert-True ($parse.Invoke($null, @($inputVersion)) -eq [Version]'0.1.1.0') "parse $inputVersion" } foreach ($inputVersion in @('', '1', '1.2', '1.2.3.4', '-1.2.3', '1.two.3', '2147483648.0.0')) { Assert-True ($null -eq $parse.Invoke($null, @($inputVersion))) "reject invalid version [$inputVersion]" } Assert-True ($parse.Invoke($null, @('0.1.1')) -gt $parse.Invoke($null, @('0.1.0'))) '0.1.1 updates 0.1.0' $target = Join-Path $testRoot 'PCL-ISE.exe' $download = Join-Path $testRoot 'download.exe' [IO.File]::WriteAllText($target, 'old version') [IO.File]::WriteAllText($download, 'new version') Replace-File 0 $target $download Assert-True ([IO.File]::ReadAllText($target) -eq 'new version') 'replace target' Assert-True ([IO.File]::ReadAllText($target + '.bak') -eq 'old version') 'retain backup' Assert-True ([IO.File]::ReadAllText($download) -eq 'new version') 'retain helper/download' $missingRejected = $false try { Replace-File 0 $target (Join-Path $testRoot 'missing.exe') } catch { $missingRejected = $true } Assert-True ($missingRejected -and [IO.File]::ReadAllText($target) -eq 'new version') 'missing download leaves target intact' # An isolated test process holds the old executable open during a normal update. $oldProgram = Join-Path $testRoot 'OldLauncher.cs' [IO.File]::WriteAllText($oldProgram, 'using System.Threading; class OldLauncher { static void Main() { Thread.Sleep(1500); } }') $oldExe = Join-Path $testRoot 'OldLauncher.exe' $csc = Join-Path $framework 'csc.exe' & $csc /nologo /target:winexe "/out:$oldExe" $oldProgram if ($LASTEXITCODE -ne 0) { throw 'Test launcher compilation failed' } $oldProcess = Start-Process -FilePath $oldExe -WindowStyle Hidden -PassThru $timeoutRejected = $false try { Replace-File $oldProcess.Id $target $download 20 } catch { $timeoutRejected = $_.Exception.InnerException -is [TimeoutException] } Assert-True ($timeoutRejected -and [IO.File]::ReadAllText($target) -eq 'new version') 'wait timeout leaves target intact' $oldProcess.WaitForExit() $newTarget = Join-Path $testRoot 'new-target.exe' Replace-File 0 $newTarget $download Assert-True ([IO.File]::ReadAllText($newTarget) -eq 'new version') 'install into absent target' Assert-True (@(Get-ChildItem $testRoot -Filter '*.new').Count -eq 0) 'no staged files left behind' if ($LauncherPath) { $LauncherPath = (Resolve-Path $LauncherPath).Path $liveTarget = Join-Path $testRoot 'RunningLauncher.exe' Copy-Item -LiteralPath $oldExe -Destination $liveTarget $previous = Start-Process -FilePath $liveTarget -WindowStyle Hidden -PassThru $arguments = '--update {0} "{1}" "{2}" false' -f $previous.Id, $liveTarget, $LauncherPath $helper = Start-Process -FilePath $LauncherPath -ArgumentList $arguments -WorkingDirectory $testRoot -WindowStyle Hidden -PassThru if (-not $helper.WaitForExit(20000)) { throw 'Update helper did not finish within 20 seconds' } $helper.Refresh() # The launcher uses ProcessReturnValues.TaskDone (5) for command-line tasks. Assert-True ($helper.ExitCode -eq 5) 'real launcher update helper exits successfully' Assert-True ((Get-FileHash $liveTarget).Hash -eq (Get-FileHash $LauncherPath).Hash) 'real helper replaces running old launcher after it exits' Assert-True ((Get-FileHash ($liveTarget + '.bak')).Hash -eq (Get-FileHash $oldExe).Hash) 'real helper preserves previous launcher' } Write-Host "$($testState.Passed) checks passed. Artifacts: $testRoot"