Skip to content

Commit

Permalink
Validate plugin configuration early
Browse files Browse the repository at this point in the history
  • Loading branch information
fflaten committed May 14, 2023
1 parent 21aceb8 commit 7520096
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 32 deletions.
14 changes: 10 additions & 4 deletions src/functions/Coverage.Plugin.ps1
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
function Get-CoveragePlugin {
# Validate configuration
Resolve-CodeCoverageConfiguration

$p = @{
Name = 'Coverage'
}

$p.Start = {
param($Context)

if ($PesterPreference.CodeCoverage.OutputFormat.Value -notin 'JaCoCo', 'CoverageGutters') {
throw "CodeCoverage.CoverageFormat must be 'JaCoCo' or 'CoverageGutters', but it was $($PesterPreference.CodeCoverage.OutputFormat.Value), please review your configuration."
}

$paths = @(if (0 -lt $PesterPreference.CodeCoverage.Path.Value.Count) {
$PesterPreference.CodeCoverage.Path.Value
}
Expand Down Expand Up @@ -215,3 +214,10 @@

New-PluginObject @p
}

function Resolve-CodeCoverageConfiguration {
$supportedFormats = 'JaCoCo', 'CoverageGutters'
if ($PesterPreference.CodeCoverage.OutputFormat.Value -notin $supportedFormats) {
throw (Get-StringOptionErrorMessage -OptionPath 'CodeCoverage.OutputFormat' -SupportedValues $supportedFormats -Value $PesterPreference.CodeCoverage.OutputFormat.Value)
}
}
18 changes: 13 additions & 5 deletions src/functions/Get-SkipRemainingOnFailurePlugin.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,26 @@ function New-SkippedTestMessage {
"Skipped due to previous failure at '$($Test.ExpandedPath)' and Run.SkipRemainingOnFailure set to '$($PesterPreference.Run.SkipRemainingOnFailure.Value)'"
}

function Resolve-SkipRemainingOnFailureConfiguration {
$supportedValues = 'None', 'Block', 'Container', 'Run'
if ($PesterPreference.Run.SkipRemainingOnFailure.Value -notin $supportedValues) {
throw (Get-StringOptionErrorMessage -OptionPath 'Run.SkipRemainingOnFailure' -SupportedValues $supportedValues -Value $PesterPreference.Run.SkipRemainingOnFailure.Value)
}
}


function Get-SkipRemainingOnFailurePlugin {
# Validate configuration
Resolve-SkipRemainingOnFailureConfiguration

# Create plugin
$p = @{
Name = "SkipRemainingOnFailure"
Name = 'SkipRemainingOnFailure'
}

$p.Start = {
param ($Context)

if ($PesterPreference.Run.SkipRemainingOnFailure.Value -notin 'None', 'Block', 'Container', 'Run') {
throw "Unsupported Run.SkipRemainingOnFailure option '$($PesterPreference.Run.SkipRemainingOnFailure.Value)'"
}

$Context.Configuration.SkipRemainingOnFailureCount = 0
}

Expand Down
25 changes: 15 additions & 10 deletions src/functions/Output.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1168,12 +1168,14 @@ function Write-BlockToScreen {
# This is not a plugin-step due to Output-features being dependencies in Invoke-PluginStep etc for error/debug
# Output-options are also used for Write-PesterDebugMessage which is independent of WriteScreenPlugin
function Resolve-OutputConfiguration ([PesterConfiguration]$PesterPreference) {
if ($PesterPreference.Output.Verbosity.Value -notin 'None', 'Normal', 'Detailed', 'Diagnostic') {
throw "Unsupported level of output '$($PesterPreference.Output.Verbosity.Value)'"
$supportedVerbosity = 'None', 'Normal', 'Detailed', 'Diagnostic'
if ($PesterPreference.Output.Verbosity.Value -notin $supportedVerbosity) {
throw (Get-StringOptionErrorMessage -OptionPath 'Output.Verbosity' -SupportedValues $supportedVerbosity -Value $PesterPreference.Output.Verbosity.Value)
}

if ($PesterPreference.Output.RenderMode.Value -notin 'Auto', 'Ansi', 'ConsoleColor', 'Plaintext') {
throw "Unsupported Output.RenderMode option '$($PesterPreference.Output.RenderMode.Value)'"
$supportedRenderModes = 'Auto', 'Ansi', 'ConsoleColor', 'Plaintext'
if ($PesterPreference.Output.RenderMode.Value -notin $supportedRenderModes) {
throw (Get-StringOptionErrorMessage -OptionPath 'Output.RenderMode' -SupportedValues $supportedRenderModes -Value $PesterPreference.Output.RenderMode.Value)
}
elseif ($PesterPreference.Output.RenderMode.Value -eq 'Auto') {
if ($null -ne $env:NO_COLOR) {
Expand All @@ -1188,8 +1190,9 @@ function Resolve-OutputConfiguration ([PesterConfiguration]$PesterPreference) {
}
}

if ($PesterPreference.Output.CIFormat.Value -notin 'None', 'Auto', 'AzureDevops', 'GithubActions') {
throw "Unsupported CI format '$($PesterPreference.Output.CIFormat.Value)'"
$supportedCIFormats = 'None', 'Auto', 'AzureDevops', 'GithubActions'
if ($PesterPreference.Output.CIFormat.Value -notin $supportedCIFormats) {
throw (Get-StringOptionErrorMessage -OptionPath 'Output.CIFormat' -SupportedValues $supportedCIFormats -Value $PesterPreference.Output.CIFormat.Value)
}
elseif ($PesterPreference.Output.CIFormat.Value -eq 'Auto') {
# Variable is set to 'True' if the script is being run by a Azure Devops build task. https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml
Expand All @@ -1208,8 +1211,9 @@ function Resolve-OutputConfiguration ([PesterConfiguration]$PesterPreference) {
}
}

if ($PesterPreference.Output.CILogLevel.Value -notin 'Error', 'Warning') {
throw "Unsupported CI log level '$($PesterPreference.Output.CILogLevel.Value)'"
$supportedCILogLevels = 'Error', 'Warning'
if ($PesterPreference.Output.CILogLevel.Value -notin $supportedCILogLevels) {
throw (Get-StringOptionErrorMessage -OptionPath 'Output.CILogLevel' -SupportedValues $supportedCILogLevels -Value $PesterPreference.Output.CILogLevel.Value)
}

if ('Diagnostic' -eq $PesterPreference.Output.Verbosity.Value) {
Expand All @@ -1227,7 +1231,8 @@ function Resolve-OutputConfiguration ([PesterConfiguration]$PesterPreference) {
$PesterPreference.Output.StackTraceVerbosity = 'Full'
}

if ($PesterPreference.Output.StackTraceVerbosity.Value -notin 'None', 'FirstLine', 'Filtered', 'Full') {
throw "Unsupported level of stacktrace output '$($PesterPreference.Output.StackTraceVerbosity.Value)'"
$supportedStackTraceLevels = 'None', 'FirstLine', 'Filtered', 'Full'
if ($PesterPreference.Output.StackTraceVerbosity.Value -notin $supportedStackTraceLevels) {
throw (Get-StringOptionErrorMessage -OptionPath 'Output.StackTraceVerbosity' -SupportedValues $supportedStackTraceLevels -Value $PesterPreference.Output.StackTraceVerbosity.Value)
}
}
18 changes: 10 additions & 8 deletions src/functions/TestResults.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1044,18 +1044,13 @@ function Get-GroupResult ($InputObject) {
}

function Get-TestResultPlugin {
# Validate configuration
Resolve-TestResultConfiguration

$p = @{
Name = 'TestResult'
}

$p.Start = {
param($Context)

if ($PesterPreference.TestResult.OutputFormat.Value -notin 'NUnitXml', 'NUnit2.5', 'JUnitXml') {
throw "Unsupported TestResult.OutputFormat option '$($PesterPreference.TestResult.OutputFormat.Value)'"
}
}

$p.End = {
param($Context)

Expand All @@ -1066,3 +1061,10 @@ function Get-TestResultPlugin {

New-PluginObject @p
}

function Resolve-TestResultConfiguration {
$supportedFormats = 'NUnitXml', 'NUnit2.5', 'NUnit3', 'JUnitXml'
if ($PesterPreference.TestResult.OutputFormat.Value -notin $supportedFormats) {
throw (Get-StringOptionErrorMessage -OptionPath 'TestResult.OutputFormat' -SupportedValues $supportedFormats -Value $PesterPreference.TestResult.OutputFormat.Value)
}
}
17 changes: 12 additions & 5 deletions tst/Pester.RSpec.Configuration.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ i -PassThru:$PassThru {
$c = [PesterConfiguration] @{
Run = @{
ScriptBlock = $sb
PassThru = $true
Throw = $true
}
Debug = @{
ShowFullErrors = $false
Expand All @@ -862,8 +862,10 @@ i -PassThru:$PassThru {
Invoke-Pester -Configuration $c
}
catch {
$_.Exception.Message -match "Unsupported level of stacktrace output 'Something'" | Verify-True
$_.Exception.Message -match "Output.StackTraceVerbosity must be .* it was 'Something'" | Verify-True
$failed = $true
}
$failed | Verify-True
}
}

Expand Down Expand Up @@ -1098,7 +1100,7 @@ i -PassThru:$PassThru {
$c = [PesterConfiguration] @{
Run = @{
ScriptBlock = $sb
PassThru = $true
Throw = $true
}
Output = @{
CIFormat = "Something"
Expand All @@ -1109,8 +1111,10 @@ i -PassThru:$PassThru {
Invoke-Pester -Configuration $c
}
catch {
$_.Exception.Message -match "Unsupported CI format 'Something'" | Verify-True
$_.Exception.Message -match "Output.CIFormat must be .* it was 'Something'" | Verify-True
$failed = $true
}
$failed | Verify-True
}

t "Output.CIFormat is None when set" {
Expand Down Expand Up @@ -1296,9 +1300,12 @@ i -PassThru:$PassThru {

try {
Invoke-Pester -Configuration $c
$true | Verify-False # Should not get here
} catch {
$_.Exception.Message -match "Unsupported Output.RenderMode option 'Something'" | Verify-True
$_.Exception.Message -match "Output.RenderMode must be .* it was 'Something'" | Verify-True
$failed = $true
}
$failed | Verify-True
}
}

Expand Down

0 comments on commit 7520096

Please sign in to comment.