rimworld-blax-slaves/Tests/run-tests.ps1

103 lines
5.2 KiB
PowerShell

<#
Runs the in-game test suite. RimWorld 1.7 starts headless (no window, no focus change) with its
own save data folder, so your mod list, saves and settings stay untouched. Needs Steam running and
the Harmony mod from the Workshop. Exit code: 0 all passed, 1 failures, 2 the run didn't finish.
.\Tests\run-tests.ps1 Harmony, the installed DLCs, Blax Slaves
.\Tests\run-tests.ps1 -Compat also the installed mods Blax Slaves has compat code for
.\Tests\run-tests.ps1 -NoBuild skip dotnet build
While it runs, the test mod is linked into the game's Mods folder; the link is removed afterwards.
#>
param(
[string]$RimWorldDir = 'E:\SteamLibrary\steamapps\common\RimWorld',
[string]$DataDir = (Join-Path $env:TEMP 'BlaxSlavesTest'),
[switch]$Compat,
[switch]$NoBuild,
[int]$TimeoutSeconds = 900
)
$ErrorActionPreference = 'Stop'
$repo = Split-Path -Parent $PSScriptRoot
$testMod = Join-Path $PSScriptRoot 'BlaxSlavesTests'
$link = Join-Path $RimWorldDir 'Mods\BlaxSlavesTests'
$workshop = Join-Path $RimWorldDir '..\..\workshop\content\294100'
if (Get-Process -Name RimWorld -ErrorAction SilentlyContinue) { throw 'RimWorld is running. Close it first.' }
if (-not $NoBuild) {
foreach ($proj in (Join-Path $repo 'Source\BlaxSlaves\BlaxSlaves.csproj'), (Join-Path $testMod 'Source\BlaxSlavesTests.csproj')) {
& dotnet build $proj -c Release -nologo -v q "-p:RimWorldDir=$RimWorldDir"
if ($LASTEXITCODE -ne 0) { throw "build failed: $proj" }
}
}
# packageId (lower case) -> About.xml, for every installed local and Workshop mod
$installed = @{}
foreach ($about in @(Get-ChildItem (Join-Path $RimWorldDir 'Mods\*\About\About.xml') -ErrorAction SilentlyContinue) +
@(Get-ChildItem (Join-Path $workshop '*\About\About.xml') -ErrorAction SilentlyContinue)) {
$x = [xml](Get-Content -LiteralPath $about.FullName -Raw -Encoding UTF8)
if ($x.ModMetaData.packageId) { $installed[$x.ModMetaData.packageId.Trim().ToLower()] = $x }
}
function Add-WithDependencies([string]$id, [System.Collections.ArrayList]$list) {
$id = $id.ToLower()
if ($list -contains $id -or $id -like 'ludeon.*') { return }
$x = $installed[$id]
if (-not $x) { Write-Warning "not installed, skipped: $id"; return }
foreach ($dep in @($x.ModMetaData.modDependencies.li)) { if ($dep.packageId) { Add-WithDependencies $dep.packageId $list } }
[void]$list.Add($id)
}
$mods = New-Object System.Collections.ArrayList
$expansions = @()
[void]$mods.Add('brrainz.harmony')
[void]$mods.Add('ludeon.rimworld')
foreach ($dlc in 'Royalty', 'Ideology', 'Biotech', 'Anomaly', 'Odyssey') {
if (Test-Path (Join-Path $RimWorldDir "Data\$dlc")) { $id = 'ludeon.rimworld.' + $dlc.ToLower(); [void]$mods.Add($id); $expansions += $id }
}
if ($Compat) {
foreach ($id in 'unlimitedhugs.hugslib', 'orion.hospitality', 'smuffle.harvestorganspostmortem',
'mersid.wce2updated.core', 'mlie.syrmoreslaves', 'bblkepling.everybodybuysslaves') {
Add-WithDependencies $id $mods
}
}
[void]$mods.Add('blax.slaves')
[void]$mods.Add('blax.slaves.tests')
# fresh test data folder: mod list only, no old save, results or mod settings
New-Item -ItemType Directory -Force (Join-Path $DataDir 'Config') | Out-Null
Remove-Item -Force -ErrorAction SilentlyContinue (Join-Path $DataDir 'Saves\BlaxSlavesTest.rws'), (Join-Path $DataDir 'BlaxSlavesTestResults.txt'), (Join-Path $DataDir 'Config\Mod_blax.slaves_*.xml')
$li = { param($ids) ($ids | ForEach-Object { " <li>$_</li>" }) -join "`n" }
$modsConfig = "<?xml version=`"1.0`" encoding=`"utf-8`"?>`n<ModsConfigData>`n <version>1.7</version>`n <activeMods>`n" +
(& $li $mods) + "`n </activeMods>`n <knownExpansions>`n" + (& $li $expansions) + "`n </knownExpansions>`n</ModsConfigData>`n"
[IO.File]::WriteAllText((Join-Path $DataDir 'Config\ModsConfig.xml'), $modsConfig)
Write-Host ("Mods: " + ($mods -join ', '))
$createdLink = $false
try {
if (-not (Test-Path $link)) {
New-Item -ItemType Junction -Path $link -Target $testMod | Out-Null
$createdLink = $true
}
$gameArgs = "-headless -quicktest -blaxslavestests -NoFileLog -NoCrashDialog `"-savedatafolder=$DataDir`""
$sw = [Diagnostics.Stopwatch]::StartNew()
$p = Start-Process -FilePath (Join-Path $RimWorldDir 'RimWorld.exe') -WorkingDirectory $RimWorldDir -ArgumentList $gameArgs `
-RedirectStandardOutput (Join-Path $DataDir 'game.log') -RedirectStandardError (Join-Path $DataDir 'game.err.log') `
-WindowStyle Hidden -PassThru
if (-not $p.WaitForExit($TimeoutSeconds * 1000)) {
$p.Kill()
Write-Warning "timed out after $TimeoutSeconds s, game killed"
}
Write-Host ("Game ran {0:N0} s. Log: {1}" -f $sw.Elapsed.TotalSeconds, (Join-Path $DataDir 'game.log'))
}
finally {
if ($createdLink -and (Test-Path $link)) { cmd /c rmdir "$link" | Out-Null }
}
$results = Join-Path $DataDir 'BlaxSlavesTestResults.txt'
if (-not (Test-Path $results)) { Write-Host 'No results file: the suite never started. See game.log.'; exit 2 }
$lines = Get-Content -LiteralPath $results -Encoding UTF8
$lines | ForEach-Object { Write-Host $_ }
if (-not ($lines -match '^END$')) { Write-Host 'The run did not finish.'; exit 2 }
if ($lines -match '^FAIL ') { exit 1 }
exit 0