|
| 1 | +[Diagnostics.CodeAnalysis.SuppressMessageAttribute( |
| 2 | + 'PSUseDeclaredVarsMoreThanAssignments', '', |
| 3 | + Justification = 'Variables are assigned in BeforeAll and used inside It blocks.' |
| 4 | +)] |
| 5 | +[CmdletBinding()] |
| 6 | +param() |
| 7 | + |
| 8 | +BeforeAll { |
| 9 | + Import-Module -Name PSModule -Force |
| 10 | + $script:cleanupScriptPath = Join-Path -Path $PSScriptRoot -ChildPath '../src/cleanup.ps1' |
| 11 | + $script:environmentNames = @( |
| 12 | + 'GITHUB_REPOSITORY' |
| 13 | + 'GITHUB_EVENT_PATH' |
| 14 | + 'PSMODULE_CLEANUP_PSMODULEPRERELEASES_INPUT_PullRequest' |
| 15 | + 'PSMODULE_CLEANUP_PSMODULEPRERELEASES_INPUT_WhatIf' |
| 16 | + 'PSMODULE_CLEANUP_PSMODULEPRERELEASES_CONTEXT_ReleaseTag' |
| 17 | + ) |
| 18 | + $script:originalEnvironment = @{} |
| 19 | + foreach ($name in $script:environmentNames) { |
| 20 | + $script:originalEnvironment[$name] = [Environment]::GetEnvironmentVariable($name) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +AfterAll { |
| 25 | + foreach ($name in $script:environmentNames) { |
| 26 | + [Environment]::SetEnvironmentVariable($name, $script:originalEnvironment[$name]) |
| 27 | + } |
| 28 | + Remove-Item -Path 'Function:\gh' -ErrorAction SilentlyContinue |
| 29 | +} |
| 30 | + |
| 31 | +Describe 'Cleanup-PSModulePrereleases ownership' { |
| 32 | + BeforeEach { |
| 33 | + $script:state = @{ |
| 34 | + Calls = [System.Collections.Generic.List[string]]::new() |
| 35 | + Releases = @( |
| 36 | + [pscustomobject]@{ |
| 37 | + tagName = 'v1.2.4-featurea001'; isPrerelease = $true; targetCommitish = 'feature/a' |
| 38 | + } |
| 39 | + [pscustomobject]@{ |
| 40 | + tagName = 'v1.2.4-featureab001'; isPrerelease = $true; targetCommitish = 'feature/ab' |
| 41 | + } |
| 42 | + [pscustomobject]@{ |
| 43 | + tagName = 'v1.2.4-featurea002'; isPrerelease = $true; targetCommitish = 'feature-a' |
| 44 | + } |
| 45 | + [pscustomobject]@{ |
| 46 | + tagName = 'v1.2.4-featurea003'; isPrerelease = $true; targetCommitish = 'feature/a' |
| 47 | + } |
| 48 | + [pscustomobject]@{ |
| 49 | + tagName = 'v1.2.4'; isPrerelease = $false; targetCommitish = 'feature/a' |
| 50 | + } |
| 51 | + ) |
| 52 | + } |
| 53 | + $state = $script:state |
| 54 | + Set-Item -Path 'Function:global:gh' -Value { |
| 55 | + [CmdletBinding()] |
| 56 | + param( |
| 57 | + [Parameter(ValueFromRemainingArguments)] |
| 58 | + [string[]] $Arguments |
| 59 | + ) |
| 60 | + |
| 61 | + $state.Calls.Add($Arguments -join ' ') |
| 62 | + $global:LASTEXITCODE = 0 |
| 63 | + if ($Arguments[0] -eq 'api' -or ($Arguments[0] -eq 'release' -and $Arguments[1] -eq 'list')) { |
| 64 | + $state.Releases | ConvertTo-Json -Compress |
| 65 | + } |
| 66 | + }.GetNewClosure() |
| 67 | + $eventPath = Join-Path -Path $TestDrive -ChildPath 'dispatch.json' |
| 68 | + @{} | ConvertTo-Json | Set-Content -LiteralPath $eventPath |
| 69 | + $env:GITHUB_EVENT_PATH = $eventPath |
| 70 | + $env:GITHUB_REPOSITORY = 'PSModule/CleanupFixture' |
| 71 | + $env:PSMODULE_CLEANUP_PSMODULEPRERELEASES_INPUT_PullRequest = @{ |
| 72 | + Number = 42; HeadRef = 'feature/a' |
| 73 | + } | ConvertTo-Json |
| 74 | + $env:PSMODULE_CLEANUP_PSMODULEPRERELEASES_INPUT_WhatIf = 'true' |
| 75 | + $env:PSMODULE_CLEANUP_PSMODULEPRERELEASES_CONTEXT_ReleaseTag = 'v1.2.4-featurea003' |
| 76 | + } |
| 77 | + |
| 78 | + It 'does not preview deleting another branch or a sanitized-name collision during a dispatch' { |
| 79 | + $output = & $script:cleanupScriptPath 6>&1 | Out-String |
| 80 | + |
| 81 | + $output | Should -Match 'release delete.*v1\.2\.4-featurea001' |
| 82 | + $output | Should -Not -Match 'release delete.*v1\.2\.4-featureab001' |
| 83 | + $output | Should -Not -Match 'release delete.*v1\.2\.4-featurea002' |
| 84 | + $output | Should -Not -Match 'release delete.*v1\.2\.4-featurea003' |
| 85 | + @($script:state.Calls | Where-Object { $_ -like 'release delete*' }).Count | Should -Be 0 |
| 86 | + } |
| 87 | + |
| 88 | + It 'deletes only exact branch-owned prereleases and retains the published tag' { |
| 89 | + $env:PSMODULE_CLEANUP_PSMODULEPRERELEASES_INPUT_WhatIf = 'false' |
| 90 | + |
| 91 | + & $script:cleanupScriptPath |
| 92 | + |
| 93 | + $deletions = @($script:state.Calls | Where-Object { $_ -like 'release delete*' }) |
| 94 | + $deletions.Count | Should -Be 1 |
| 95 | + $deletions[0] | Should -Match 'v1\.2\.4-featurea001' |
| 96 | + $deletions[0] | Should -Not -Match 'featureab|featurea002|featurea003' |
| 97 | + } |
| 98 | + |
| 99 | + It 'keeps numeric branch identifiers distinct from prerelease counters' { |
| 100 | + $script:state.Releases = @( |
| 101 | + [pscustomobject]@{ |
| 102 | + tagName = 'v1.2.4-feature1001'; isPrerelease = $true; targetCommitish = 'feature/1' |
| 103 | + } |
| 104 | + [pscustomobject]@{ |
| 105 | + tagName = 'v1.2.4-feature10001'; isPrerelease = $true; targetCommitish = 'feature/10' |
| 106 | + } |
| 107 | + ) |
| 108 | + $env:PSMODULE_CLEANUP_PSMODULEPRERELEASES_INPUT_PullRequest = @{ |
| 109 | + Number = 42; HeadRef = 'feature/1' |
| 110 | + } | ConvertTo-Json |
| 111 | + |
| 112 | + $output = & $script:cleanupScriptPath 6>&1 | Out-String |
| 113 | + |
| 114 | + $output | Should -Match 'release delete.*v1\.2\.4-feature1001' |
| 115 | + $output | Should -Not -Match 'release delete.*v1\.2\.4-feature10001' |
| 116 | + } |
| 117 | +} |
0 commit comments