|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Ensures the requested PSScriptAnalyzer version is the only one installed and loaded. |
| 4 | +
|
| 5 | + .DESCRIPTION |
| 6 | + Runs as the Invoke-Pester prescript, in the same process as the analyzer test run. It installs the |
| 7 | + PSScriptAnalyzer version selected through the action's Version/Prerelease inputs (retrying transient |
| 8 | + PSGallery failures), removes any other PSScriptAnalyzer version from the session, and imports the chosen |
| 9 | + version into the global session state. This guarantees the tests use the selected version instead of |
| 10 | + PowerShell auto-loading whatever copy happens to be preinstalled on the runner. |
| 11 | +#> |
| 12 | + |
| 13 | +[CmdletBinding()] |
| 14 | +param() |
| 15 | + |
| 16 | +$moduleName = 'PSScriptAnalyzer' |
| 17 | +$version = $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_Version |
| 18 | +$prerelease = $env:PSMODULE_INVOKE_SCRIPTANALYZER_INPUT_Prerelease -eq 'true' |
| 19 | + |
| 20 | +$installParams = @{ |
| 21 | + Name = $moduleName |
| 22 | + Repository = 'PSGallery' |
| 23 | + TrustRepository = $true |
| 24 | + PassThru = $true |
| 25 | + WarningAction = 'SilentlyContinue' |
| 26 | +} |
| 27 | +if (-not [string]::IsNullOrWhiteSpace($version)) { |
| 28 | + $installParams['Version'] = $version |
| 29 | +} |
| 30 | +if ($prerelease) { |
| 31 | + $installParams['Prerelease'] = $true |
| 32 | +} |
| 33 | + |
| 34 | +$label = [string]::IsNullOrWhiteSpace($version) ? $moduleName : "$moduleName $version" |
| 35 | +Write-Host "Installing module: $label" |
| 36 | + |
| 37 | +$installed = $null |
| 38 | +$retryCount = 5 |
| 39 | +$retryDelay = 10 |
| 40 | +for ($i = 0; $i -lt $retryCount; $i++) { |
| 41 | + try { |
| 42 | + $installed = Install-PSResource @installParams |
| 43 | + break |
| 44 | + } catch { |
| 45 | + Write-Warning "Installation of $moduleName failed with error: $_" |
| 46 | + if ($i -eq $retryCount - 1) { |
| 47 | + throw |
| 48 | + } |
| 49 | + Write-Warning "Retrying in $retryDelay seconds..." |
| 50 | + Start-Sleep -Seconds $retryDelay |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +# Resolve the exact version to load. Prefer what was just installed; if the resource was already |
| 55 | +# present, Install-PSResource returns nothing, so fall back to the newest installed version that |
| 56 | +# satisfies the requested constraint. |
| 57 | +$resolved = $installed | Where-Object { $_.Name -eq $moduleName } | Sort-Object Version -Descending | Select-Object -First 1 |
| 58 | +if (-not $resolved) { |
| 59 | + $getParams = @{ Name = $moduleName; Verbose = $false; ErrorAction = 'SilentlyContinue' } |
| 60 | + if (-not [string]::IsNullOrWhiteSpace($version)) { |
| 61 | + $getParams['Version'] = $version |
| 62 | + } |
| 63 | + $resolved = Get-InstalledPSResource @getParams | Sort-Object Version -Descending | Select-Object -First 1 |
| 64 | +} |
| 65 | +if (-not $resolved) { |
| 66 | + throw "No installed '$moduleName' version satisfies constraint '$version'." |
| 67 | +} |
| 68 | + |
| 69 | +# Remove any already-loaded versions so only the chosen one remains, then import that exact version |
| 70 | +# into the global session state used by the Pester run. |
| 71 | +Write-Host "Removing any loaded '$moduleName' module from the session" |
| 72 | +Remove-Module -Name $moduleName -Force -ErrorAction SilentlyContinue |
| 73 | + |
| 74 | +Write-Host "Importing module: $moduleName $($resolved.Version)" |
| 75 | +Import-Module -Name $moduleName -RequiredVersion $resolved.Version -Force -Global |
0 commit comments