|
| 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 | + Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '../src/Publish-PSModule.Helpers.psm1') -Force |
| 11 | +} |
| 12 | + |
| 13 | +Describe 'Publish-PSModule.Helpers' { |
| 14 | + Describe 'Get-ModuleVersionString' { |
| 15 | + Context 'Get-ModuleVersionString - SemVer only, never prefixed' { |
| 16 | + It 'Get-ModuleVersionString - returns the module version for a stable release' { |
| 17 | + Get-ModuleVersionString -ModuleVersion '1.1.10' | Should -Be '1.1.10' |
| 18 | + } |
| 19 | + |
| 20 | + It 'Get-ModuleVersionString - appends the prerelease label' { |
| 21 | + Get-ModuleVersionString -ModuleVersion '1.1.10' -Prerelease 'mybranch001' | |
| 22 | + Should -Be '1.1.10-mybranch001' |
| 23 | + } |
| 24 | + |
| 25 | + It 'Get-ModuleVersionString - treats an empty prerelease label as a stable release' { |
| 26 | + Get-ModuleVersionString -ModuleVersion '1.1.10' -Prerelease '' | Should -Be '1.1.10' |
| 27 | + } |
| 28 | + |
| 29 | + It 'Get-ModuleVersionString - treats a whitespace-only prerelease label as a stable release' { |
| 30 | + Get-ModuleVersionString -ModuleVersion '1.1.10' -Prerelease ' ' | Should -Be '1.1.10' |
| 31 | + } |
| 32 | + |
| 33 | + It 'Get-ModuleVersionString - trims whitespace around the prerelease label' { |
| 34 | + Get-ModuleVersionString -ModuleVersion '1.1.10' -Prerelease ' mybranch001 ' | |
| 35 | + Should -Be '1.1.10-mybranch001' |
| 36 | + } |
| 37 | + |
| 38 | + It 'Get-ModuleVersionString - takes no version prefix parameter at all' { |
| 39 | + (Get-Command Get-ModuleVersionString).Parameters.Keys | Should -Not -Contain 'VersionPrefix' |
| 40 | + } |
| 41 | + |
| 42 | + It 'Get-ModuleVersionString - requires a module version' { |
| 43 | + { Get-ModuleVersionString -ModuleVersion '' } | Should -Throw |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + Describe 'Get-ReleaseTag' { |
| 49 | + Context 'Get-ReleaseTag - repository with a version prefix' { |
| 50 | + It 'Get-ReleaseTag - prefixes a stable release tag with the configured prefix' { |
| 51 | + Get-ReleaseTag -VersionPrefix 'v' -ModuleVersion '1.1.10' | Should -Be 'v1.1.10' |
| 52 | + } |
| 53 | + |
| 54 | + It 'Get-ReleaseTag - prefixes a stable release tag when the prerelease label is empty' { |
| 55 | + Get-ReleaseTag -VersionPrefix 'v' -ModuleVersion '1.1.10' -Prerelease '' | Should -Be 'v1.1.10' |
| 56 | + } |
| 57 | + |
| 58 | + It 'Get-ReleaseTag - prefixes a prerelease tag with the configured prefix' { |
| 59 | + Get-ReleaseTag -VersionPrefix 'v' -ModuleVersion '1.1.10' -Prerelease 'mybranch001' | |
| 60 | + Should -Be 'v1.1.10-mybranch001' |
| 61 | + } |
| 62 | + |
| 63 | + It 'Get-ReleaseTag - supports a multi-character prefix' { |
| 64 | + Get-ReleaseTag -VersionPrefix 'release-v' -ModuleVersion '2.0.0' | Should -Be 'release-v2.0.0' |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + Context 'Get-ReleaseTag - repository without a version prefix' { |
| 69 | + It 'Get-ReleaseTag - leaves a stable release tag unprefixed' { |
| 70 | + Get-ReleaseTag -VersionPrefix '' -ModuleVersion '1.1.10' | Should -Be '1.1.10' |
| 71 | + } |
| 72 | + |
| 73 | + It 'Get-ReleaseTag - leaves a prerelease tag unprefixed' { |
| 74 | + Get-ReleaseTag -VersionPrefix '' -ModuleVersion '1.1.10' -Prerelease 'mybranch001' | |
| 75 | + Should -Be '1.1.10-mybranch001' |
| 76 | + } |
| 77 | + |
| 78 | + It 'Get-ReleaseTag - treats an absent prefix as no prefix' { |
| 79 | + Get-ReleaseTag -ModuleVersion '1.1.10' | Should -Be '1.1.10' |
| 80 | + } |
| 81 | + |
| 82 | + It 'Get-ReleaseTag - treats a null prefix as no prefix' { |
| 83 | + Get-ReleaseTag -VersionPrefix $null -ModuleVersion '1.1.10' | Should -Be '1.1.10' |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + Context 'Get-ReleaseTag - input normalization' { |
| 88 | + It 'Get-ReleaseTag - trims whitespace around the prefix' { |
| 89 | + Get-ReleaseTag -VersionPrefix ' v ' -ModuleVersion '1.1.10' | Should -Be 'v1.1.10' |
| 90 | + } |
| 91 | + |
| 92 | + It 'Get-ReleaseTag - trims whitespace around the prerelease label' { |
| 93 | + Get-ReleaseTag -VersionPrefix 'v' -ModuleVersion '1.1.10' -Prerelease ' mybranch001 ' | |
| 94 | + Should -Be 'v1.1.10-mybranch001' |
| 95 | + } |
| 96 | + |
| 97 | + It 'Get-ReleaseTag - treats a whitespace-only prerelease label as a stable release' { |
| 98 | + Get-ReleaseTag -VersionPrefix 'v' -ModuleVersion '1.1.10' -Prerelease ' ' | Should -Be 'v1.1.10' |
| 99 | + } |
| 100 | + |
| 101 | + It 'Get-ReleaseTag - requires a module version' { |
| 102 | + { Get-ReleaseTag -VersionPrefix 'v' -ModuleVersion '' } | Should -Throw |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + # Cleanup-PSModulePrereleases selects the releases to delete with |
| 107 | + # `tagName -like "*$prereleaseName*" -and tagName -ne $publishedReleaseTag`, where the published tag is |
| 108 | + # the value publish.ps1 exports as PSMODULE_PUBLISH_PSMODULE_CONTEXT_ReleaseTag. Both halves of that |
| 109 | + # filter have to keep working once the tag carries a prefix. |
| 110 | + Context 'Get-ReleaseTag - AutoCleanup tag matching contract' { |
| 111 | + It 'Get-ReleaseTag - keeps the prerelease name inside a prefixed tag so cleanup still matches it' { |
| 112 | + Get-ReleaseTag -VersionPrefix 'v' -ModuleVersion '1.1.10' -Prerelease 'mybranch001' | |
| 113 | + Should -BeLike '*mybranch*' |
| 114 | + } |
| 115 | + |
| 116 | + It 'Get-ReleaseTag - keeps the prerelease name inside an unprefixed tag so cleanup still matches it' { |
| 117 | + Get-ReleaseTag -VersionPrefix '' -ModuleVersion '1.1.10' -Prerelease 'mybranch001' | |
| 118 | + Should -BeLike '*mybranch*' |
| 119 | + } |
| 120 | + |
| 121 | + It 'Get-ReleaseTag - produces the same tag twice so cleanup can exclude the published release' { |
| 122 | + $first = Get-ReleaseTag -VersionPrefix 'v' -ModuleVersion '1.1.10' -Prerelease 'mybranch001' |
| 123 | + $second = Get-ReleaseTag -VersionPrefix 'v' -ModuleVersion '1.1.10' -Prerelease 'mybranch001' |
| 124 | + $first | Should -Be $second |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + # The PowerShell Gallery and the module manifest only accept plain SemVer. The prefix therefore |
| 129 | + # belongs to the GitHub release tag and to nothing else, and the two strings must differ by exactly |
| 130 | + # the prefix - never by anything else, and never in the other direction. |
| 131 | + Context 'Get-ReleaseTag - the prefix reaches the release tag and nothing else' { |
| 132 | + It 'Get-ReleaseTag - the tag is the prefix followed by the module version string' -ForEach @( |
| 133 | + @{ Prefix = 'v'; Version = '1.1.10'; Label = '' } |
| 134 | + @{ Prefix = 'v'; Version = '1.1.10'; Label = 'mybranch001' } |
| 135 | + @{ Prefix = ''; Version = '1.1.10'; Label = '' } |
| 136 | + @{ Prefix = ''; Version = '1.1.10'; Label = 'mybranch001' } |
| 137 | + @{ Prefix = 'release-v'; Version = '2.0.0'; Label = 'mybranch001' } |
| 138 | + ) { |
| 139 | + $moduleVersion = Get-ModuleVersionString -ModuleVersion $Version -Prerelease $Label |
| 140 | + $tag = Get-ReleaseTag -VersionPrefix $Prefix -ModuleVersion $Version -Prerelease $Label |
| 141 | + $tag | Should -Be "$Prefix$moduleVersion" |
| 142 | + } |
| 143 | + |
| 144 | + It 'Get-ReleaseTag - the module version string never gains the prefix' -ForEach @( |
| 145 | + @{ Prefix = 'v'; Version = '1.1.10'; Label = '' } |
| 146 | + @{ Prefix = 'v'; Version = '1.1.10'; Label = 'mybranch001' } |
| 147 | + @{ Prefix = 'release-v'; Version = '2.0.0'; Label = 'mybranch001' } |
| 148 | + ) { |
| 149 | + $moduleVersion = Get-ModuleVersionString -ModuleVersion $Version -Prerelease $Label |
| 150 | + $moduleVersion | Should -Not -BeLike "$Prefix*" |
| 151 | + $moduleVersion | Should -Match '^\d+\.\d+\.\d+(-[0-9A-Za-z\-.]+)?$' |
| 152 | + } |
| 153 | + |
| 154 | + It 'Get-ReleaseTag - an unprefixed repository gets identical strings' { |
| 155 | + $moduleVersion = Get-ModuleVersionString -ModuleVersion '1.1.10' -Prerelease 'mybranch001' |
| 156 | + $tag = Get-ReleaseTag -VersionPrefix '' -ModuleVersion '1.1.10' -Prerelease 'mybranch001' |
| 157 | + $tag | Should -Be $moduleVersion |
| 158 | + } |
| 159 | + |
| 160 | + It 'Get-ReleaseTag - stripping the prefix from the tag yields the module version string' { |
| 161 | + $moduleVersion = Get-ModuleVersionString -ModuleVersion '1.1.10' -Prerelease 'mybranch001' |
| 162 | + $tag = Get-ReleaseTag -VersionPrefix 'v' -ModuleVersion '1.1.10' -Prerelease 'mybranch001' |
| 163 | + $tag -replace '^v' | Should -Be $moduleVersion |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments