|
| 1 | +[Diagnostics.CodeAnalysis.SuppressMessageAttribute( |
| 2 | + 'PSUseDeclaredVarsMoreThanAssignments', '', |
| 3 | + Justification = 'Pester setup state is used in test blocks and command replacements.' |
| 4 | +)] |
| 5 | +[CmdletBinding()] |
| 6 | +param() |
| 7 | + |
| 8 | +$ErrorActionPreference = 'Stop' |
| 9 | + |
| 10 | +BeforeAll { |
| 11 | + Import-Module -Name PSModule -Force |
| 12 | + Import-Module -Name ([IO.Path]::Combine( |
| 13 | + $PSScriptRoot, '..', 'src', 'Resolve-PSModuleVersion.Helpers.psm1' |
| 14 | + )) -Force |
| 15 | + $script:originalFind = (Get-Item -LiteralPath 'Function:\Find-PSResource' -ErrorAction SilentlyContinue).ScriptBlock |
| 16 | + $script:originalSleep = (Get-Item -LiteralPath 'Function:\Start-Sleep' -ErrorAction SilentlyContinue).ScriptBlock |
| 17 | +} |
| 18 | + |
| 19 | +AfterAll { |
| 20 | + foreach ($name in @('Find-PSResource', 'Start-Sleep')) { |
| 21 | + Remove-Item -LiteralPath "Function:\$name" -ErrorAction SilentlyContinue |
| 22 | + } |
| 23 | + if ($null -ne $script:originalFind) { |
| 24 | + Set-Item -Path function:global:Find-PSResource -Value $script:originalFind |
| 25 | + } |
| 26 | + if ($null -ne $script:originalSleep) { |
| 27 | + Set-Item -Path function:global:Start-Sleep -Value $script:originalSleep |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +Describe 'Version lookup errors are not version baselines' { |
| 32 | + BeforeEach { |
| 33 | + $state = @{ Message = ''; ErrorId = ''; Package = $null } |
| 34 | + $script:state = $state |
| 35 | + Set-Item -Path function:global:Start-Sleep -Value {} |
| 36 | + Set-Item -Path function:global:Find-PSResource -Value { |
| 37 | + [CmdletBinding()] |
| 38 | + param( |
| 39 | + [string] $Name, |
| 40 | + [string] $Repository, |
| 41 | + [string] $Version, |
| 42 | + [switch] $Prerelease |
| 43 | + ) |
| 44 | + if ($Name -cne 'TestModule' -or $Repository -cne 'PSGallery' -or |
| 45 | + ($Version -and ($Version -cne '*' -or -not $Prerelease))) { |
| 46 | + throw 'Unexpected Gallery boundary in the version-reader fixture.' |
| 47 | + } |
| 48 | + if ($state.Message) { |
| 49 | + Write-Error -Message $state.Message -ErrorId $state.ErrorId -Category ResourceUnavailable |
| 50 | + return |
| 51 | + } |
| 52 | + $state.Package |
| 53 | + }.GetNewClosure() |
| 54 | + } |
| 55 | + |
| 56 | + It 'retains verified Gallery package absence as the first-release baseline' { |
| 57 | + $script:state.Message = 'The exact module is absent.' |
| 58 | + $script:state.ErrorId = 'PackageNotFound' |
| 59 | + |
| 60 | + (Get-LatestPSGalleryVersion -ModuleName TestModule).ToString() | Should -BeExactly '0.0.0' |
| 61 | + } |
| 62 | + |
| 63 | + It 'surfaces Gallery authentication and API errors before resolving a publishable version' -ForEach @( |
| 64 | + @{ Message = 'Gallery authentication failed (HTTP 401).' } |
| 65 | + @{ Message = 'Gallery Service Unavailable (HTTP 503).' } |
| 66 | + ) { |
| 67 | + $script:state.Message = $Message |
| 68 | + $script:state.ErrorId = 'HttpRequestCallFailure' |
| 69 | + |
| 70 | + { Get-LatestPSGalleryVersion -ModuleName TestModule } | Should -Throw |
| 71 | + } |
| 72 | + |
| 73 | + It 'rejects a successful lookup with no version metadata rather than returning 0.0.0' { |
| 74 | + $script:state.Package = [pscustomobject]@{ Name = 'TestModule'; Version = $null } |
| 75 | + |
| 76 | + { Get-LatestPSGalleryVersion -ModuleName TestModule } | Should -Throw '*version*' |
| 77 | + } |
| 78 | + |
| 79 | + It 'retains the actual stable version without an extra bump policy' { |
| 80 | + $script:state.Package = [pscustomobject]@{ Name = 'TestModule'; Version = [version]'1.2.3' } |
| 81 | + |
| 82 | + (Get-LatestPSGalleryVersion -ModuleName TestModule).ToString() | Should -BeExactly '1.2.3' |
| 83 | + } |
| 84 | + |
| 85 | + It 'does not interpret a prerelease enumeration outage as counter zero' { |
| 86 | + $script:state.Message = 'Gallery enumeration failed (HTTP 503).' |
| 87 | + $script:state.ErrorId = 'HttpRequestCallFailure' |
| 88 | + $parameters = @{ ModuleName = 'TestModule'; BaseVersion = '1.2.4'; PrereleaseName = 'preview'; Releases = @() } |
| 89 | + |
| 90 | + { Get-NextPrereleaseNumber @parameters } | Should -Throw |
| 91 | + } |
| 92 | +} |
0 commit comments