Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions tools/Invoke-IdlePesterTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ Coverage output format supported by Pester.
One or more paths to include for coverage (e.g. 'src'). Defaults to 'src'
relative to the repository root.

.PARAMETER MinimumPesterVersion
Minimum supported Pester version. The script will install Pester in CurrentUser scope
when missing or below the minimum version.
.PARAMETER PesterVersion
Pinned Pester version to use. Defaults to 5.7.1.

.EXAMPLE
pwsh -NoProfile -File ./tools/Invoke-IdlePesterTests.ps1
Expand Down Expand Up @@ -85,7 +84,7 @@ param(

[Parameter()]
[ValidateNotNullOrEmpty()]
[version] $MinimumPesterVersion = '5.0.0'
[version] $PesterVersion = '5.7.1'
)

Set-StrictMode -Version Latest
Expand Down Expand Up @@ -131,7 +130,7 @@ function Get-IdleFullPath {
return [System.IO.Path]::GetFullPath((Join-Path -Path $RepoRootPath -ChildPath $Path))
}

function Ensure-Directory {
function Initialize-Directory {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
Expand All @@ -143,35 +142,39 @@ function Ensure-Directory {
}
}

function Ensure-Pester {
function Initialize-Pester {
<#
.SYNOPSIS
Ensures a compatible Pester module is installed and imported.
Initializes Pester by ensuring it is installed (pinned version) and imported.

.DESCRIPTION
CI runners are ephemeral. If Pester is missing, the script installs it
in CurrentUser scope. Local users can also benefit from auto-install.
CI runners are ephemeral. When missing, we install Pester in CurrentUser scope.
We explicitly pin versions for determinism.

IMPORTANT:
- We keep this logic self-contained and consistent across local + CI runs.
- We avoid auto-upgrading to newer versions unless the pinned version is changed in code.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[version] $MinimumVersion
[version] $RequiredVersion
)

$pester = Get-Module -ListAvailable -Name Pester |
Sort-Object Version -Descending |
$installed = Get-Module -ListAvailable -Name Pester |
Where-Object { $_.Version -eq $RequiredVersion } |
Select-Object -First 1

if (-not $pester -or $pester.Version -lt $MinimumVersion) {
if (-not $installed) {
if (-not (Get-Command -Name Install-Module -ErrorAction SilentlyContinue)) {
throw "Pester >= $MinimumVersion is required, but Install-Module is not available. Install Pester manually and retry."
throw "Pester ($RequiredVersion) is required, but Install-Module is not available. Install Pester manually and retry."
}

Write-Host "Installing Pester >= $MinimumVersion (CurrentUser scope)..."
Install-Module -Name Pester -Scope CurrentUser -Force -MinimumVersion $MinimumVersion | Out-Null
Write-Host "Installing Pester ($RequiredVersion) in CurrentUser scope..."
Install-Module -Name Pester -Scope CurrentUser -Force -RequiredVersion $RequiredVersion -AllowClobber | Out-Null
}

Import-Module -Name Pester -MinimumVersion $MinimumVersion -Force
Import-Module -Name Pester -RequiredVersion $RequiredVersion -Force
}

$repoRoot = Resolve-IdleRepoRoot
Expand All @@ -187,22 +190,22 @@ $coverageEnabled = $CI.IsPresent -or $EnableCoverage.IsPresent
$resolvedTestResultsPath = $null
if ($emitTestResults) {
$resolvedTestResultsPath = Get-IdleFullPath -RepoRootPath $repoRoot -Path $TestResultsPath
Ensure-Directory -Path (Split-Path -Path $resolvedTestResultsPath -Parent)
Initialize-Directory -Path (Split-Path -Path $resolvedTestResultsPath -Parent)
}

$resolvedCoverageOutputPath = $null
$resolvedCoveragePaths = @()

if ($coverageEnabled) {
$resolvedCoverageOutputPath = Get-IdleFullPath -RepoRootPath $repoRoot -Path $CoverageOutputPath
Ensure-Directory -Path (Split-Path -Path $resolvedCoverageOutputPath -Parent)
Initialize-Directory -Path (Split-Path -Path $resolvedCoverageOutputPath -Parent)

foreach ($p in $CoveragePath) {
$resolvedCoveragePaths += (Get-IdleFullPath -RepoRootPath $repoRoot -Path $p)
}
}

Ensure-Pester -MinimumVersion $MinimumPesterVersion
Initialize-Pester -RequiredVersion $PesterVersion

$config = New-PesterConfiguration
$config.Run.Path = $resolvedTestPath
Expand Down
14 changes: 7 additions & 7 deletions tools/Invoke-IdleScriptAnalyzer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function Get-IdleFullPath {
return [System.IO.Path]::GetFullPath((Join-Path -Path $RepoRootPath -ChildPath $Path))
}

function Ensure-Directory {
function Initialize-Directory {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
Expand All @@ -148,10 +148,10 @@ function Ensure-Directory {
}
}

function Ensure-Module {
function Initialize-Module {
<#
.SYNOPSIS
Ensures a module is installed (pinned version) and imported.
Initializes a module by ensuring it is installed (pinned version) and imported.

.DESCRIPTION
CI runners are ephemeral. When missing, we install the module in CurrentUser scope.
Expand Down Expand Up @@ -232,7 +232,7 @@ foreach ($p in $Paths) {
$resolvedJsonOutputPath = $null
if ($CI) {
$resolvedJsonOutputPath = Get-IdleFullPath -RepoRootPath $repoRoot -Path $JsonOutputPath
Ensure-Directory -Path (Split-Path -Path $resolvedJsonOutputPath -Parent)
Initialize-Directory -Path (Split-Path -Path $resolvedJsonOutputPath -Parent)
}

# SARIF is optional: only generate when CI is on (or user explicitly wants it later)
Expand All @@ -241,12 +241,12 @@ $resolvedSarifOutputPath = $null
$emitSarif = $false
if ($CI -and $SarifOutputPath) {
$resolvedSarifOutputPath = Get-IdleFullPath -RepoRootPath $repoRoot -Path $SarifOutputPath
Ensure-Directory -Path (Split-Path -Path $resolvedSarifOutputPath -Parent)
Initialize-Directory -Path (Split-Path -Path $resolvedSarifOutputPath -Parent)
$emitSarif = $true
}

# Ensure analyzer module is present (pinned).
Ensure-Module -Name 'PSScriptAnalyzer' -RequiredVersion $PSScriptAnalyzerVersion
Initialize-Module -Name 'PSScriptAnalyzer' -RequiredVersion $PSScriptAnalyzerVersion

# Run analysis using the repo settings file.
# We rely on the settings file for rule selection and severities.
Expand Down Expand Up @@ -281,7 +281,7 @@ if ($CI -and $resolvedJsonOutputPath) {
if ($emitSarif -and $resolvedSarifOutputPath) {
# ConvertToSARIF provides the ConvertTo-SARIF cmdlet which accepts -FilePath.
# We install it only when SARIF output is requested.
Ensure-Module -Name 'ConvertToSARIF' -RequiredVersion $ConvertToSarifVersion
Initialize-Module -Name 'ConvertToSARIF' -RequiredVersion $ConvertToSarifVersion

$convertCommand = Get-Command -Name 'ConvertTo-SARIF' -ErrorAction SilentlyContinue
if (-not $convertCommand) {
Expand Down