|
| 1 | +function Test-MaesterSuite { |
| 2 | +<# |
| 3 | + .SYNOPSIS |
| 4 | + Update the Maester module and tests to the latest version and then import the module. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Update the Maester module and tests to the latest version and then import the module. |
| 8 | +
|
| 9 | + .PARAMETER UpdateModule |
| 10 | + Update the Maester module to the latest prerelease version from the PowerShell Gallery. |
| 11 | +
|
| 12 | + .PARAMETER UpdateTests |
| 13 | + Update the Maester tests to the latest version from the module's install location. |
| 14 | +
|
| 15 | + .PARAMETER TestsPath |
| 16 | + Path to install or update the Maester tests in when -UpdateTests is used. Defaults to "$HOME\maester-tests". |
| 17 | +
|
| 18 | + .PARAMETER ModuleSource |
| 19 | + Specifies the source for the Maester module. Valid choices are the 'Installed' version (default) and 'LocalDevelopment'. |
| 20 | + 'Installed' imports the installed Maester module. 'LocalDevelopment' imports the local development version. |
| 21 | +
|
| 22 | + .PARAMETER DevPath |
| 23 | + Path to the local development version of Maester and its tests. Defaults to "$HOME\Code\Personal\Maester". |
| 24 | +
|
| 25 | + .PARAMETER DevBranch |
| 26 | + Branch to use when importing the local development version of Maester instead of the installed version. Defaults to "main". |
| 27 | +
|
| 28 | + .PARAMETER DevTests |
| 29 | + Import the local development version of Maester tests instead of the installed tests. |
| 30 | +
|
| 31 | + .PARAMETER OutputPath |
| 32 | + Path to save the test output files to. Defaults "$HOME\maester-results\". |
| 33 | +
|
| 34 | + .PARAMETER NoInvoke |
| 35 | + Do not execute Maester tests. |
| 36 | +
|
| 37 | + .PARAMETER IncludeLongRunning |
| 38 | + Include tests that are marked as long-running. |
| 39 | +
|
| 40 | + .PARAMETER IncludePreview |
| 41 | + Include tests that are marked as preview. |
| 42 | +
|
| 43 | + .EXAMPLE |
| 44 | + Test-MaesterSuite -UpdateModule -UpdateTests |
| 45 | +
|
| 46 | + .EXAMPLE |
| 47 | + Test-MaesterSuite -ModuleSource LocalDevelopment -UpdateTests |
| 48 | +#> |
| 49 | + [CmdletBinding(SupportsShouldProcess)] |
| 50 | + param( |
| 51 | + # Update the Maester module to the latest prerelease version from the PowerShell Gallery. |
| 52 | + [Parameter()] |
| 53 | + [switch]$UpdateModule, |
| 54 | + |
| 55 | + # Path to install or update the Maester tests to when not using -DevTests. |
| 56 | + [Parameter()] |
| 57 | + [string]$TestsPath = ( Join-Path -Path $HOME -ChildPath "Maester-Tests"), |
| 58 | + |
| 59 | + # Update the Maester tests to the latest version from the module's install location. |
| 60 | + [Parameter()] |
| 61 | + [switch]$UpdateTests, |
| 62 | + |
| 63 | + # Specifies the source for the Maester module. Valid values are 'Installed' (default) and 'LocalDevelopment'. |
| 64 | + [Parameter()] |
| 65 | + [ValidateSet('Installed', 'LocalDevelopment')] |
| 66 | + [string]$ModuleSource = 'Installed', |
| 67 | + |
| 68 | + # Path to the local development version of Maester and its tests. Defaults to "$HOME\Code\Personal\Maester". |
| 69 | + [Parameter()] |
| 70 | + [string]$DevPath = ( Join-Path -Path $HOME -ChildPath "Code\Personal\Maester" ), |
| 71 | + |
| 72 | + # Branch to use when importing the local development version of Maester instead of the installed version. Defaults to "main". |
| 73 | + [Parameter()] |
| 74 | + [string]$DevBranch = "main", |
| 75 | + |
| 76 | + # Import the local development version of Maester tests instead of the installed tests. |
| 77 | + [Parameter()] |
| 78 | + [switch]$DevTests, |
| 79 | + |
| 80 | + # Path to save the test output files to. Defaults "$HOME\maester-results\". |
| 81 | + [Parameter()] |
| 82 | + [string]$OutputPath = ( Join-Path -Path $HOME -ChildPath "maester-results" ), |
| 83 | + |
| 84 | + # Do not execute Maester tests. |
| 85 | + [Parameter()] |
| 86 | + [switch]$NoInvoke, |
| 87 | + |
| 88 | + # Include tests that are marked as long-running. |
| 89 | + [Parameter()] |
| 90 | + [switch]$IncludeLongRunning, |
| 91 | + |
| 92 | + # Include tests that are marked as preview. |
| 93 | + [Parameter()] |
| 94 | + [switch]$IncludePreview |
| 95 | + ) |
| 96 | + |
| 97 | + Remove-Module -Name Maester -Force -ErrorAction SilentlyContinue |
| 98 | + |
| 99 | + if ($UpdateModule) { |
| 100 | + Write-Verbose "Updating Maester module to the latest prerelease version from the PowerShell Gallery." |
| 101 | + try { |
| 102 | + Update-PSResource -Name 'Maester' -Prerelease -Force |
| 103 | + } catch { |
| 104 | + Write-Warning "Failed to update Maester module: $($_.Exception.Message)" |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if ($ModuleSource -eq 'LocalDevelopment') { |
| 109 | + Write-Verbose "Importing the local development version of Maester from $DevPath instead of the installed version." |
| 110 | + if (-Not (Test-Path -Path $DevPath)) { |
| 111 | + Write-Error "The specified DevPath '$DevPath' does not exist. Please create it or specify a different path." |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + try { |
| 116 | + Write-Verbose "Switching to branch '$DevBranch' and pulling the latest changes." |
| 117 | + Set-Location -Path $DevPath |
| 118 | + git switch $DevBranch |
| 119 | + git pull origin $DevBranch |
| 120 | + } catch { |
| 121 | + Write-Warning "Failed to switch to branch '$DevBranch' or pull the latest changes. Please ensure the branch exists and the Git CLI is installed. $($_.Exception.Message)" |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + $ModulePath = Join-Path -Path $DevPath -ChildPath "powershell/Maester.psm1" |
| 126 | + if (-Not (Test-Path -Path $ModulePath)) { |
| 127 | + Write-Error "The Maester module manifest was not found at '$ModulePath'. Please check the DevPath and ensure the module exists." |
| 128 | + return |
| 129 | + } |
| 130 | + Remove-Module -Name Maester -Force -ErrorAction SilentlyContinue |
| 131 | + try { |
| 132 | + Import-Module $ModulePath -Force |
| 133 | + } catch { |
| 134 | + Write-Error "Failed to import the Maester module from '$ModulePath'. $($_.Exception.Message)" |
| 135 | + return |
| 136 | + } |
| 137 | + } else { |
| 138 | + # Import the installed Maester module |
| 139 | + Import-Module -Name Maester -Force |
| 140 | + } |
| 141 | + |
| 142 | + if ($UpdateTests) { |
| 143 | + Write-Verbose "Updating Maester tests in path: '$TestsPath'" |
| 144 | + try { |
| 145 | + Update-MaesterTests -Path $TestsPath |
| 146 | + } catch { |
| 147 | + Write-Warning "Failed to update Maester tests: $($_.Exception.Message)" |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + if ($DevTests) { |
| 152 | + $UsingTestsPath = Join-Path -Path $DevPath -ChildPath "tests" |
| 153 | + } else { |
| 154 | + $UsingTestsPath = $TestsPath |
| 155 | + } |
| 156 | + |
| 157 | + if (-not $NoInvoke) { |
| 158 | + Write-Verbose "Running Maester tests from '$UsingTestsPath' and saving results to '$OutputPath'" |
| 159 | + Invoke-Maester -Path $UsingTestsPath -OutputFolder $OutputPath -IncludeLongRunning:$IncludeLongRunning -IncludePreview:$IncludePreview |
| 160 | + } else { |
| 161 | + Write-Verbose "Skipping test invocation as -NoInvoke was specified." |
| 162 | + } |
| 163 | +} # End of function Test-MaesterSuite |
0 commit comments