|
| 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 | + |
| 11 | + $script:publishScriptPath = Join-Path -Path $PSScriptRoot -ChildPath '../src/publish.ps1' |
| 12 | + $script:environmentVariableNames = @( |
| 13 | + 'GITHUB_EVENT_PATH' |
| 14 | + 'GITHUB_REPOSITORY' |
| 15 | + 'GITHUB_WORKSPACE' |
| 16 | + 'PSMODULE_PUBLISH_PSMODULE_INPUT_Name' |
| 17 | + 'PSMODULE_PUBLISH_PSMODULE_INPUT_ModulePath' |
| 18 | + 'PSMODULE_PUBLISH_PSMODULE_INPUT_PSGALLERY_API_KEY' |
| 19 | + 'PSMODULE_PUBLISH_PSMODULE_INPUT_PullRequest' |
| 20 | + 'PSMODULE_PUBLISH_PSMODULE_INPUT_WhatIf' |
| 21 | + ) |
| 22 | + $script:originalEnvironment = @{} |
| 23 | + foreach ($name in $script:environmentVariableNames) { |
| 24 | + $script:originalEnvironment[$name] = [System.Environment]::GetEnvironmentVariable($name) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +AfterAll { |
| 29 | + foreach ($name in $script:environmentVariableNames) { |
| 30 | + [System.Environment]::SetEnvironmentVariable($name, $script:originalEnvironment[$name]) |
| 31 | + } |
| 32 | + Remove-Item -Path function:global:Find-PSResource -ErrorAction SilentlyContinue |
| 33 | + Remove-Item -Path function:global:Publish-PSResource -ErrorAction SilentlyContinue |
| 34 | + Remove-Item -Path function:global:Resolve-PSModuleDependency -ErrorAction SilentlyContinue |
| 35 | +} |
| 36 | + |
| 37 | +Describe 'Publish-PSModule recovery' { |
| 38 | + BeforeEach { |
| 39 | + $script:moduleName = 'TestModule' |
| 40 | + $script:workspacePath = Join-Path -Path $TestDrive -ChildPath 'workspace' |
| 41 | + $script:modulePath = Join-Path -Path $script:workspacePath -ChildPath "outputs/module/$script:moduleName" |
| 42 | + $manifestPath = Join-Path -Path $script:modulePath -ChildPath "$script:moduleName.psd1" |
| 43 | + $eventPath = Join-Path -Path $TestDrive -ChildPath 'event.json' |
| 44 | + $null = New-Item -Path $script:modulePath -ItemType Directory -Force |
| 45 | + Set-Content -Path (Join-Path -Path $script:modulePath -ChildPath "$script:moduleName.psm1") -Value '' |
| 46 | + Set-Content -Path $manifestPath -Value @" |
| 47 | +@{ |
| 48 | + RootModule = '$script:moduleName.psm1' |
| 49 | + ModuleVersion = '1.2.4' |
| 50 | + GUID = '3c0f8d73-60b7-4f38-b3cf-9386db4982a4' |
| 51 | + Author = 'PSModule' |
| 52 | + Description = 'Test module' |
| 53 | + PowerShellVersion = '5.1' |
| 54 | + PrivateData = @{ |
| 55 | + PSData = @{ |
| 56 | + Prerelease = '' |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | +"@ |
| 61 | + @{} | ConvertTo-Json | Set-Content -Path $eventPath |
| 62 | + |
| 63 | + $env:GITHUB_EVENT_PATH = $eventPath |
| 64 | + $env:GITHUB_REPOSITORY = "PSModule/$script:moduleName" |
| 65 | + $env:GITHUB_WORKSPACE = $script:workspacePath |
| 66 | + $env:PSMODULE_PUBLISH_PSMODULE_INPUT_Name = $script:moduleName |
| 67 | + $env:PSMODULE_PUBLISH_PSMODULE_INPUT_ModulePath = 'outputs/module' |
| 68 | + $env:PSMODULE_PUBLISH_PSMODULE_INPUT_PSGALLERY_API_KEY = 'test-key' |
| 69 | + $env:PSMODULE_PUBLISH_PSMODULE_INPUT_PullRequest = '' |
| 70 | + $env:PSMODULE_PUBLISH_PSMODULE_INPUT_WhatIf = 'false' |
| 71 | + $script:publishInvoked = $false |
| 72 | + |
| 73 | + Set-Item -Path function:global:Resolve-PSModuleDependency -Value {} |
| 74 | + Set-Item -Path function:global:Find-PSResource -Value { |
| 75 | + [PSCustomObject]@{ Name = 'TestModule'; Version = '1.2.4' } |
| 76 | + } |
| 77 | + Set-Item -Path function:global:Publish-PSResource -Value { |
| 78 | + $script:publishInvoked = $true |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + It 'skips Gallery publication when the resolved version already exists' { |
| 83 | + { & $script:publishScriptPath } | Should -Not -Throw |
| 84 | + |
| 85 | + $script:publishInvoked | Should -BeFalse |
| 86 | + } |
| 87 | +} |
0 commit comments