<# .SYNOPSIS Checks scripts/Get-Version.ps1 and scripts/Get-NextRelease.ps1 (README, Versioning). Plain asserts, no Pester needed; exits 1 on the first failure. .DESCRIPTION The version rules run against canned `git describe` output. Unless -SkipGit, a throwaway repository (SHA-256, like this one) with a bare "origin" is built in the temp folder and walked through the seed and the first releases: calendar-era tags are ignored, no release tag is refused, v0.1.0 plus a breaking change releases 1.0.0, a re-run of the tagged commit publishes 1.0.0 again, builds after it are 1.0.0.N, a fix releases 1.0.1, docs alone release nothing, a feat releases 1.1.0. Needs git and node (build/release is installed with npm ci when missing). Never touches this repository's tags. .EXAMPLE ./scripts/tests/Test-Versioning.ps1 ./scripts/tests/Test-Versioning.ps1 -SkipGit #> [CmdletBinding()] param( [switch]$SkipGit ) $ErrorActionPreference = 'Stop' Set-StrictMode -Version 3.0 $scripts = Split-Path -Parent $PSScriptRoot $getVersion = Join-Path $scripts 'Get-Version.ps1' $getNext = Join-Path $scripts 'Get-NextRelease.ps1' $failures = 0 # The version overrides, and the CI variables semantic-release reads the branch from (in CI they name the # pipeline's branch, not the scratch repository's), are cleared while the checks run. $cleared = 'UNIFIED_VERSION', 'GIT_COMMIT_SHORT', 'CI', 'GITHUB_ACTIONS', 'GITEA_ACTIONS', 'GITHUB_REF', 'GITHUB_HEAD_REF', 'GITHUB_BASE_REF', 'GITHUB_EVENT_NAME', 'GITHUB_EVENT_PATH', 'GITHUB_SHA', 'GITHUB_REPOSITORY' $saved = @{} foreach ($name in $cleared) { $saved[$name] = [Environment]::GetEnvironmentVariable($name) [Environment]::SetEnvironmentVariable($name, $null) } function Assert-Equal($Expected, $Actual, [string]$What) { if ("$Expected" -ceq "$Actual") { Write-Host " ok $What = $Actual" } else { Write-Host " FAIL $What = '$Actual', expected '$Expected'" -ForegroundColor Red; $script:failures++ } } function Assert-Throws([scriptblock]$Action, [string]$Pattern, [string]$What) { try { & $Action | Out-Null; Write-Host " FAIL $What did not throw" -ForegroundColor Red; $script:failures++ } catch { if ("$_" -match $Pattern) { Write-Host " ok $What refused: $_" } else { Write-Host " FAIL $What threw '$_', expected /$Pattern/" -ForegroundColor Red; $script:failures++ } } } try { Write-Host 'Version rules' Assert-Equal '1.4.0.7' (& $getVersion -Describe 'v1.4.0-7-g1a2b3c4') 'seven commits after v1.4.0' Assert-Equal '1.4.0' (& $getVersion -Describe 'v1.4.0-0-g1a2b3c4') 'the tagged commit' Assert-Equal '1.4.0.0' (& $getVersion -Describe 'v1.4.0-0-g1a2b3c4' -Format FileVersion) 'file version of a release' Assert-Equal '1.4.0.7' (& $getVersion -Describe 'v1.4.0-7-g1a2b3c4' -Format FileVersion) 'file version of a build' Assert-Equal '0.0.0.253' (& $getVersion -Describe '' -Commits 253) 'no release tag' Assert-Equal '0.0.0' (& $getVersion -Describe '' -Commits 0) 'no git' Assert-Equal '255.255.65535.65535' (& $getVersion -Version '255.255.65535.65535') 'the largest version' Assert-Equal '1.5.0' (& $getVersion -Version '1.5.0') 'explicit release' $env:UNIFIED_VERSION = '2.0.0.3' Assert-Equal '2.0.0.3' (& $getVersion) 'UNIFIED_VERSION' Assert-Equal '2.0.0.3' (& (Join-Path $scripts 'Get-UnifiedVersion.ps1')) 'the old entry point' $env:GIT_COMMIT_SHORT = 'abc1234' Assert-Equal '2.0.0.3+abc1234' (& $getVersion -Format Informational) 'informational version' $env:UNIFIED_VERSION = $null $env:GIT_COMMIT_SHORT = $null Assert-Throws { & $getVersion -Version '2026.10.10.0140' } 'calendar-era' 'a calendar version' Assert-Throws { & $getVersion -Version '256.0.0' } 'does not fit' 'major 256' Assert-Throws { & $getVersion -Version '1.256.0' } 'does not fit' 'minor 256' Assert-Throws { & $getVersion -Version '1.0.65536' } 'does not fit' 'patch 65536' Assert-Throws { & $getVersion -Version '1.0.0.65536' } 'does not fit' 'build 65536' Assert-Throws { & $getVersion -Version '1.0.0-rc.1' } 'no -label' 'a prerelease label' Assert-Throws { & $getVersion -Version '1.0' } 'no -label' 'two fields' Assert-Throws { & $getVersion -Describe 'v1.0.0-rc1-3-gabc' } 'not v' 'an odd describe' if (-not $SkipGit) { Write-Host 'Releases in a scratch repository' $temp = Join-Path ([System.IO.Path]::GetTempPath()) "executer-versioning-$([guid]::NewGuid().ToString('N'))" $origin = Join-Path $temp 'origin.git' $work = Join-Path $temp 'work' New-Item -ItemType Directory -Force $temp | Out-Null try { function Invoke-Git { & git -C $work @args 2>&1 | Out-Null; if ($LASTEXITCODE -ne 0) { throw "git $args failed ($LASTEXITCODE)" } } function Add-Commit([string]$Message) { Add-Content -LiteralPath (Join-Path $work 'history.txt') -Value $Message Invoke-Git add history.txt Invoke-Git -c user.name=test -c user.email=test@example.invalid commit -q -m $Message Invoke-Git push -q origin main } function Next { $r = & $getNext -Root $work 6>$null; if ($r) { "$($r.Version) $($r.Type)" } else { 'none' } } & git init -q --bare --object-format=sha256 --initial-branch=main $origin & git init -q --object-format=sha256 --initial-branch=main $work Invoke-Git remote add origin $origin Invoke-Git config commit.gpgsign false Invoke-Git config tag.gpgsign false Add-Commit 'feat: the calendar era' Invoke-Git tag v2026.10.10.0140 Add-Commit 'fix: still calendar' Invoke-Git push -q origin --tags Assert-Equal '0.0.0.2' (& $getVersion -Root $work) 'calendar tags only' Assert-Throws { & $getNext -Root $work 6>$null } 'No release tag' 'a release without the seed tag' Invoke-Git tag v0.1.0 Invoke-Git push -q origin v0.1.0 Assert-Equal '0.1.0' (& $getVersion -Root $work) 'the seed commit' Add-Commit "build(version)!: semantic versions`n`nBREAKING CHANGE: versions are X.Y.Z from release tags." Assert-Equal '0.1.0.1' (& $getVersion -Root $work) 'a build after the seed' Assert-Equal '1.0.0 major' (Next) 'the first release after the seed' Invoke-Git tag v1.0.0 Invoke-Git push -q origin v1.0.0 Assert-Equal '1.0.0' (& $getVersion -Root $work) 'the released commit' Assert-Equal '1.0.0 rerun' (Next) 'a re-run of the released commit' Add-Commit 'docs: a note' Assert-Equal '1.0.0.1' (& $getVersion -Root $work) 'docs after 1.0.0' Assert-Equal 'none' (Next) 'docs alone' Add-Commit 'fix: a bug' Assert-Equal '1.0.0.2' (& $getVersion -Root $work) 'a fix after 1.0.0' Assert-Equal '1.0.1 patch' (Next) 'a fix' Add-Commit 'feat: a feature' Assert-Equal '1.1.0 minor' (Next) 'a feat and a fix' Invoke-Git checkout -q -b topic Assert-Throws { & $getNext -Root $work 6>$null } 'did not analyze' 'a branch other than main' } finally { Remove-Item -LiteralPath $temp -Recurse -Force -ErrorAction SilentlyContinue } } } finally { foreach ($name in $cleared) { [Environment]::SetEnvironmentVariable($name, $saved[$name]) } } if ($failures -gt 0) { Write-Host "$failures check(s) failed." -ForegroundColor Red exit 1 } Write-Host 'All versioning checks passed.' # The refusals above leave a non-zero $LASTEXITCODE behind; the result is this. exit 0