tasks/test.tasks.ps1
|
# <copyright file="test.tasks.ps1" company="Endjin Limited"> # Copyright (c) Endjin Limited. All rights reserved. # </copyright> . $PSScriptRoot/test.properties.ps1 # Synopsis: Installs the required version of Pester task InstallPester { [array]$existingModule = Get-Module -ListAvailable Pester $existingPesterVersions = $existingModule | Select-Object -ExpandProperty Version | ForEach-Object { $_.ToString() } if (!$existingModule -or $existingPesterVersions -notcontains $PesterVersion) { Install-Module Pester -RequiredVersion $PesterVersion -Force -Scope CurrentUser -SkipPublisherCheck } Get-Module Pester | Remove-Module Import-Module Pester -RequiredVersion $PesterVersion } # Synopsis: Runs all the available Pester tests using modern v5 configuration approach with support for code coverage, filtering, and multiple output formats task RunPesterTests ` -If {!$SkipPesterTests -and $PesterTestsDir} ` -After TestCore ` InstallPester,{ # Create Pester configuration object using modern v5 approach $config = New-PesterConfiguration # Configure test discovery and execution $config.Run.Path = $PesterTestsDir $config.Run.PassThru = $true # Configure test result output $config.TestResult.Enabled = $true $config.TestResult.OutputFormat = $PesterOutputFormat $config.TestResult.OutputPath = $PesterOutputFilePath # Configure output verbosity - prefer explicit $PesterVerbosity over legacy $PesterShowOptions if ($PesterVerbosity) { $config.Output.Verbosity = $PesterVerbosity } elseif ($PesterShowOptions -contains "All" -or $PesterShowOptions -contains "Describe" -or $PesterShowOptions -contains "Context") { Write-Build Yellow "Mapping deprecated PesterShowOptions to 'Detailed' verbosity - considering replacing `$PesterShowOptions with the `$PesterVerbosity property" $config.Output.Verbosity = 'Detailed' } elseif ($PesterShowOptions -contains "Summary" -and $PesterShowOptions.Count -eq 1) { Write-Build Yellow "Mapping deprecated PesterShowOptions to 'Minimal' verbosity - considering replacing `$PesterShowOptions with the `$PesterVerbosity property" $config.Output.Verbosity = 'Minimal' } elseif ($PesterShowOptions -contains "None") { Write-Build Yellow "Mapping deprecated PesterShowOptions to 'None' verbosity - considering replacing `$PesterShowOptions with the `$PesterVerbosity property" $config.Output.Verbosity = 'None' } else { $config.Output.Verbosity = 'Normal' } # Configure test filtering by tags if ($PesterTagFilter.Count -gt 0) { $config.Filter.Tag = $PesterTagFilter } if ($PesterExcludeTagFilter.Count -gt 0) { $config.Filter.ExcludeTag = $PesterExcludeTagFilter } # Configure code coverage if enabled if ($PesterCodeCoverageEnabled) { $config.CodeCoverage.Enabled = $true $config.CodeCoverage.OutputFormat = $PesterCodeCoverageOutputFormat $config.CodeCoverage.OutputPath = $PesterCodeCoverageOutputPath # Set coverage path - default to build directory if not specified if ($PesterCodeCoveragePaths.Count -gt 0) { $config.CodeCoverage.Path = $PesterCodeCoveragePaths } else { $config.CodeCoverage.Path = $PWD } } $results = Invoke-Pester -Configuration $config # Check code coverage threshold if enabled if ($PesterCodeCoverageEnabled -and $PesterCodeCoverageThreshold -gt 0 -and $results.CodeCoverage) { $coveragePercent = [math]::Round(($results.CodeCoverage.CoveragePercent), 2) if ($results.CodeCoverage.CommandsAnalyzedCount -eq 0) { Write-Host ("No commands were found for coverage analysis - skipping threshold rule") -ForegroundColor Yellow } elseif ($coveragePercent -lt $PesterCodeCoverageThreshold) { throw ("Code coverage of {0}% is below the required threshold of {1}%" -f $coveragePercent, $PesterCodeCoverageThreshold) } else { Write-Host ("Code coverage: {0}% (meets threshold of {1}%)" -f $coveragePercent, $PesterCodeCoverageThreshold) -ForegroundColor Green } } if ($results.FailedCount -gt 0) { throw ("{0} out of {1} tests failed - check previous logging for more details" -f $results.FailedCount, $results.TotalCount) } } |