|
# WARNING: If this doesn't work inside of VS Code, run it from a command prompt. |
|
|
|
[CmdletBinding()] |
|
param ( |
|
[Parameter(Mandatory = $false)][ValidateNotNull()][String] $RunPath = 'C:\git\PerfAndMon-Tools\Scripts\Utilities\DeployScript', |
|
[Parameter(Mandatory = $false)][String[]] $RunExcludePath = $null, |
|
|
|
[Parameter(Mandatory = $false)][nullable[bool]] $CodeCoverageEnabled = $False, |
|
|
|
[Parameter(Mandatory = $false)][nullable[bool]] $TestResultEnabled = $true, |
|
|
|
[Parameter(Mandatory = $false)][String] $OutputVerbosity = 'Normal', |
|
[Parameter(Mandatory = $false)][String] $OutputRenderMode = 'PlainText' |
|
) |
|
|
|
begin { |
|
# ---------- Configuration ---------- |
|
|
|
#Requires -Version 7.3 |
|
Set-StrictMode -Version 3.0 |
|
$ErrorActionPreference = 'Stop' # Normal |
|
#$ErrorActionPreference = 'Inquire' # For debugging |
|
#$ErrorActionPreference = 'break' # For debugging |
|
|
|
# ---------- Module Imports ---------- |
|
Import-Module "$PsScriptRoot\PNMTests.psm1" |
|
} |
|
|
|
process { |
|
|
|
Write-Output 'Starting a test run...' |
|
#$scriptPath = $MyInvocation.MyCommand.Path | Split-Path |
|
|
|
Push-Location $RunPath |
|
|
|
try { |
|
|
|
$container = New-PesterContainer -Path $RunPath #-Data @() |
|
|
|
# Reference: https://pester-docs.netlify.app/docs/commands/New-PesterConfiguration |
|
# Reference: https://pester.dev/docs/usage/Configuration |
|
$configuration = New-PesterConfiguration |
|
$configuration.Run.Container = $container |
|
|
|
# if (-not [string]::IsNullOrWhiteSpace($RunPath)) { |
|
# #$configuration.Run.Path = "." # Default |
|
# $configuration.Run.Path = $RunPath |
|
# } |
|
if ($null -ne $RunExcludePath) { |
|
#$configuration.Run.ExcludePath = @() # Default |
|
$configuration.Run.ExcludePath = $RunExcludePath #@("c\PerfandMon-Tools\Scripts\Tests", "c\PerfandMon-Tools\Scripts\Prototypes") |
|
} |
|
|
|
if ($null -ne $CodeCoverageEnabled) { |
|
$configuration.CodeCoverage.Enabled = $CodeCoverageEnabled |
|
} |
|
|
|
if ($null -ne $TestResultEnabled) { |
|
$configuration.TestResult.Enabled = $TestResultEnabled |
|
} |
|
|
|
|
|
if (-not [string]::IsNullOrWhiteSpace($OutputVerbosity)) { |
|
#$configuration.Output.Verbosity = "Normal" # Default |
|
$configuration.Output.Verbosity = $OutputVerbosity |
|
} |
|
if (-not [string]::IsNullOrWhiteSpace($OutputRenderMode)) { |
|
#$Configuration.Output.RenderMode = "Auto" # Default |
|
$Configuration.Output.RenderMode = $OutputRenderMode |
|
} |
|
|
|
|
|
# Write configuration to JSON file |
|
$pesterConfigPath = "$($Env:UserProfile)\AppData\Local\PesterConfig.json" |
|
Write-Output "Running Pester with the settings in '$($pesterConfigPath)'" |
|
#$JsonContent = ConvertTo-Json -InputObject $Configuration -Depth 100 |
|
#Set-Content -Path $pesterConfigPath -Value $JsonContent |
|
#Invoke-Item $pesterConfigPath |
|
|
|
|
|
if ($configuration.TestResult.Enabled) { |
|
$testResultsFolder = Join-Path -Path (ResolvePathForce -Path $RunPath) -ChildPath 'TestResults' |
|
$configuration.TestResult.OutputPath = Join-Path -Path $TestResultsFolder -ChildPath 'testResults.xml' |
|
|
|
if (-not (Test-Path -Path $testResultsFolder -PathType Container -ErrorAction SilentlyContinue)) { |
|
New-Item -Path $testResultsFolder -ItemType Directory | Out-Null |
|
} |
|
else { |
|
Write-Output 'Removing old test result files' |
|
Remove-Item -Path "$testResultsFolder\*.*" -Recurse |
|
} |
|
} |
|
|
|
if ($configuration.CodeCoverage.Enabled) { |
|
$coverageFolder = Join-Path -Path (ResolvePathForce -Path $RunPath) -ChildPath 'Coverage' |
|
$configuration.CodeCoverage.OutputPath = Join-Path -Path $coverageFolder -ChildPath 'coverage.xml' |
|
|
|
if (-not (Test-Path -Path $coverageFolder -PathType Container)) { |
|
New-Item -Path $coverageFolder -ItemType Directory | Out-Null |
|
} |
|
else { |
|
Write-Output 'Removing old coverage files' |
|
Remove-Item -Path "$coverageFolder\*.*" -Recurse |
|
} |
|
} |
|
|
|
|
|
|
|
try { |
|
Write-Output 'Invoking Pester...' |
|
|
|
Invoke-Pester -Configuration $Configuration #-verbose -debug |
|
|
|
Write-Output 'Pester finished.' |
|
} |
|
catch { |
|
Write-Warning $PSitem |
|
$exception = [System.Exception]::new('Running Pester failed.', $PSItem.Exception) |
|
throw $exception |
|
} |
|
|
|
|
|
|
|
|
|
|
|
# if ($configuration.TestResult.Enabled) { |
|
# if (Test-Path $configuration.TestResult.OutputPath.Value -ErrorAction SilentlyContinue ) { |
|
# Invoke-Item (ResolvePathForce -Path $configuration.TestResult.OutputPath.Value) |
|
# } |
|
# } |
|
|
|
|
|
|
|
|
|
if ($configuration.CodeCoverage.Enabled) { |
|
Write-Output 'Generating the Coverage Report...' |
|
& C:\bin\ReportGenerator\net7.0\ReportGenerator.exe "-reports:$($configuration.CodeCoverage.OutputPath.Value)" "-SourceDirs:$($configuration.Run.Path.Value)" "-TargetDir:$($coverageFolder)" '-License:ewogICJMaWNlbnNlIjogewogICAgIklkIjogImVjYjNhYzVkLTM2YWMtNDQ2Mi05MjlkLWEyNjU1NjA1OTc3ZiIsCiAgICAiTG9naW4iOiAicm9iYnJhdHRvbiIsCiAgICAiTmFtZSI6ICJSb2JlcnQgRS4gQnJhdHRvbiIsCiAgICAiRW1haWwiOiBudWxsLAogICAgIkxpY2Vuc2VUeXBlIjogIlBybyIsCiAgICAiSXNzdWVkQXQiOiAiMjAyMy0wNC0wN1QxMzozODoyMy42Nzc3Njk0WiIKICB9LAogICJTaWduYXR1cmUiOiAiQ2tsTTh1RUFwdlx1MDAyQlhcdTAwMkJLVXJXZi9QQ3pMM0NmTGFYNEtEZ0V1Q1BTUlo2bmJrNW9HcFQ5VTFUXHUwMDJCUUdaamIyQ1JtbGlrNk00Skg5Y1VxaXNsY2svbEx3amIvZVlKVUNRUFJucEdxbjdCUDFpNjA3OUVPSksvaWZReXNQT2Rlc2JMT1x1MDAyQnhhME9hS2x0RmxJbkR6ZUsxTlhwei91SkJQXHUwMDJCRFx1MDAyQnd0ekVlaXkwMVpuL0p5dFh0MFpoengvNm82QzJUcFx1MDAyQko2ancxa0Q4Yy83dHlZWUtKWVdtaWhcdTAwMkJcdTAwMkIxU3Z1cE05dDFPV1x1MDAyQkx6VWpZNzZIRU9pa1pvQTlJcURXMUlPQlFBMGpzQ21SQVZnVEhaQ0I5TnF4S1FRVElmS3NQbWN5MGk2NEtEZGVaUXVJR2lCSWg5UWNcdTAwMkI5RDZwL1VPQ0liZTBnTk5scFhSVUZaMEkzMHAvQ0xMa2dMYXJ1aFJsdz09Igp9' |
|
|
|
$coverageFile = "$($coverageFolder)\Index.html" |
|
if (-not (Test-Path $coverageFile -PathType Leaf -ErrorAction SilentlyContinue )) { |
|
Throw 'Coverage HTML file does not exist.' |
|
} |
|
|
|
Write-Output 'Launching the Coverage Report...' |
|
Invoke-Item $CoverageFile |
|
} |
|
} |
|
catch { |
|
Write-Error "The script failed: $($PSItem)" |
|
} |
|
Finally { |
|
Pop-Location |
|
} |
|
} |
|
|
|
end { |
|
Write-Output 'Script is finished.' |
|
} |