#requires -Version 7.0 $ErrorActionPreference = 'Stop' $root = Split-Path $PSScriptRoot Set-Location $root [xml]$props = Get-Content Directory.Build.props $version = [string]$props.Project.PropertyGroup.Version $fixture = Join-Path $root ('artifacts/release-automation-tests/' + [guid]::NewGuid()) $state = @{ Draft=$true; Created=$false; Published=$false; FailName=''; TagCommit=('a'*40); Dirty=$false; Calls=0; Assets=[Collections.Generic.List[object]]::new(); CorruptExisting=$false } $savedToken = $env:ASTROFRAME_RELEASE_TOKEN $env:ASTROFRAME_RELEASE_TOKEN = 'fixture-token' $passed = 0 function Check($condition, [string]$label) { if (!$condition) { throw $label }; $script:passed++; Write-Output "PASS $label" } function Reject([scriptblock]$operation, [string]$message) { try { & $operation | Out-Null } catch { if ($_.Exception.Message -like "*$message*") { return }; throw } throw "Expected failure: $message" } # Mock only remote operations and Git state; asset validation uses real files and hashes. function git { $global:LASTEXITCODE = 0 if ($args[0] -eq 'rev-parse') { return ('a'*40) } if ($args[0] -eq 'status' -and $state.Dirty) { return ' M source.cs' } } function Invoke-RestMethod { param([string]$Uri, [string]$Method='Get', $Headers, [string]$ContentType, [string]$Body, $Form) $state.Calls++ if ($Uri -match '/commits/') { return @{ sha=('a'*40) } } if ($Uri -match '/tags/' -and $Uri -notmatch '/releases/tags/') { return @{ commit=@{sha=$state.TagCommit} } } if ($Uri -match '/releases/tags/') { if (!$state.Created) { throw [Microsoft.PowerShell.Commands.HttpResponseException]::new('Not found', [Net.Http.HttpResponseMessage]::new([Net.HttpStatusCode]::NotFound)) } return @{ id=1; draft=$state.Draft; html_url='https://git.elijahkuntz.com/Elijah/AstroFrame/releases' } } if ($Method -eq 'Post' -and $Uri.EndsWith('/releases')) { $data = $Body | ConvertFrom-Json if (!$data.draft) { throw 'Release must start as a draft.' } $state.Created = $true return @{ id=1; draft=$true; html_url='https://git.elijahkuntz.com/Elijah/AstroFrame/releases' } } if ($Method -eq 'Get' -and $Uri.EndsWith('/assets')) { return $state.Assets.ToArray() } if ($Method -eq 'Post' -and $Uri -match '/assets\?') { $file = $Form.attachment if ($file.Name -eq $state.FailName) { throw 'Simulated upload failure.' } $asset = @{ name=$file.Name; size=$file.Length; browser_download_url=('https://git.elijahkuntz.com/test-assets/' + [uri]::EscapeDataString($file.Name)); LocalPath=$file.FullName } $state.Assets.Add($asset) return $asset } if ($Method -eq 'Patch') { $state.Published=$true; return @{} } throw "Unexpected mocked request: $Method $Uri" } function Invoke-WebRequest { param([uri]$Uri, $Headers, [string]$OutFile) $name = [uri]::UnescapeDataString($Uri.Segments[-1]) $asset = $state.Assets | Where-Object name -EQ $name | Select-Object -First 1 if ($state.CorruptExisting) { [IO.File]::WriteAllText($OutFile, 'different bytes') } else { Copy-Item -LiteralPath $asset.LocalPath -Destination $OutFile } } try { foreach ($channel in @('win-x64','linux-x64')) { $folder = Join-Path $fixture $channel New-Item -ItemType Directory -Force $folder | Out-Null $package = "AstroFrame-$version-$channel-full.nupkg" [IO.File]::WriteAllText((Join-Path $folder $package), 'test-package-bytes') $asset = @{ PackageId='AstroFrame';Version=$version;Type='Full';FileName=$package;SHA256=(Get-FileHash (Join-Path $folder $package)).Hash;Size=(Get-Item (Join-Path $folder $package)).Length } @{Assets=@($asset)} | ConvertTo-Json -Depth 4 | Set-Content (Join-Path $folder "releases.$channel.json") $names = @("RELEASES-$channel", "assets.$channel.json") if ($channel -eq 'win-x64') { $names += 'AstroFrame-win-x64-Setup.exe' } else { $names += @('AstroFrame-linux-x64.AppImage','install-linux.sh','astroframe.png',"AstroFrame-$version-linux-x64.zip",'AstroFrame-0.0.1-linux-x64.zip') } foreach ($name in $names) { [IO.File]::WriteAllText((Join-Path $folder $name), "fixture-$name") } } Reject { & ./packaging/Build-ServerRelease.ps1 -Ref refs/tags/v999.0.0 } 'does not match' Check ($state.Calls -eq 0) 'mismatched version tag fails before build or release requests' $state.FailName='AstroFrame-win-x64-Setup.exe' Reject { & ./packaging/Publish.ps1 -Directory $fixture -Publish -RequireTag } 'Simulated upload failure' Check ($state.Created -and !$state.Published) 'upload failure leaves the release unpublished as a draft' $uploadedBeforeRetry = $state.Assets.Count $state.FailName='' & ./packaging/Publish.ps1 -Directory $fixture -Publish -RequireTag | Out-Null Check ($state.Published -and $uploadedBeforeRetry -gt 0) 'interrupted draft resumes and publishes after all uploads succeed' Check (($state.Assets.name | Select-Object -Unique).Count -eq $state.Assets.Count) 'resuming identical files does not create duplicate attachments' Check ($state.Assets.name -notcontains 'AstroFrame-0.0.1-linux-x64.zip') 'older build archives are excluded from a new release' $state.Published=$false; $state.CorruptExisting=$true Reject { & ./packaging/Publish.ps1 -Directory $fixture -Publish -RequireTag } 'different bytes' Check (!$state.Published) 'conflicting draft assets prevent publication' $state.CorruptExisting=$false; $state.Draft=$false Reject { & ./packaging/Publish.ps1 -Directory $fixture -Publish -RequireTag } 'already published' Check (!$state.Published) 'published releases cannot be overwritten' $state.Draft=$true; $state.TagCommit=('b'*40) Reject { & ./packaging/Publish.ps1 -Directory $fixture -Publish -RequireTag } 'another commit' Check (!$state.Published) 'a tag pointing to different source cannot be released' $state.TagCommit=('a'*40); $state.Dirty=$true Reject { & ./packaging/Publish.ps1 -Directory $fixture -Publish -RequireTag } 'Commit the release source' Check (!$state.Published) 'uncommitted source cannot be released' $state.Dirty=$false; $state.Calls=0 [IO.File]::WriteAllText((Join-Path $fixture "win-x64/AstroFrame-$version-win-x64-full.nupkg"), 'corrupt') Reject { & ./packaging/Publish.ps1 -Directory $fixture -Publish -RequireTag } 'checksum' Check ($state.Calls -eq 0) 'corrupt packages fail before any remote mutation' Write-Output "$passed release automation checks passed. No real release API calls were made." } finally { $env:ASTROFRAME_RELEASE_TOKEN = $savedToken }