Skip to content

Commit

Permalink
Modify pester.ps1 to recurse through all test dirs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnybottles committed Dec 4, 2024
1 parent 13feedd commit e3f0a6b
Showing 1 changed file with 69 additions and 78 deletions.
147 changes: 69 additions & 78 deletions Hawk/tests/pester.ps1
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
param (
$TestGeneral = $true,

$TestFunctions = $true,

[ValidateSet('None', 'Normal', 'Detailed', 'Diagnostic')]
[Alias('Show')]
$Output = "None",

$Include = "*",

$Exclude = ""
$TestGeneral = $true,
$TestFunctions = $true,
[ValidateSet('None', 'Normal', 'Detailed', 'Diagnostic')]
[Alias('Show')]
$Output = "None",
$Include = "*",
$Exclude = "",
$TestRoot = $PSScriptRoot # Pass test root as a parameter
)

Write-PSFMessage -Level Important -Message "Starting Tests"

Write-PSFMessage -Level Important -Message "Importing Module"

$global:testroot = $PSScriptRoot
$global:__pester_data = @{ }
# Set local variable for script execution context
$testroot = $TestRoot

Remove-Module Hawk -ErrorAction Ignore
Import-Module "$PSScriptRoot\..\Hawk.psd1"
Import-Module "$PSScriptRoot\..\Hawk.psm1" -Force

# Need to import explicitly so we can use the configuration class
# Import explicitly to use the configuration class
Import-Module Pester

Write-PSFMessage -Level Important -Message "Creating test result folder"
Expand All @@ -37,77 +34,71 @@ $config = [PesterConfiguration]::Default
$config.TestResult.Enabled = $true

#region Run General Tests
if ($TestGeneral)
{
Write-PSFMessage -Level Important -Message "Modules imported, proceeding with general tests"
foreach ($file in (Get-ChildItem "$PSScriptRoot\general" | Where-Object Name -like "*.Tests.ps1"))
{
if ($file.Name -notlike $Include) { continue }
if ($file.Name -like $Exclude) { continue }
if ($TestGeneral) {
Write-PSFMessage -Level Important -Message "Modules imported, proceeding with general tests"
foreach ($file in (Get-ChildItem "$testroot\general" | Where-Object Name -like "*.Tests.ps1")) {
if ($file.Name -notlike $Include) { continue }
if ($file.Name -like $Exclude) { continue }

Write-PSFMessage -Level Significant -Message " Executing <c='em'>$($file.Name)</c>"
$config.TestResult.OutputPath = Join-Path "$PSScriptRoot\..\..\TestResults" "TEST-$($file.BaseName).xml"
$config.Run.Path = $file.FullName
$config.Run.PassThru = $true
$config.Output.Verbosity = $Output
$results = Invoke-Pester -Configuration $config
foreach ($result in $results)
{
$totalRun += $result.TotalCount
$totalFailed += $result.FailedCount
$result.Tests | Where-Object Result -ne 'Passed' | ForEach-Object {
$testresults += [pscustomobject]@{
Block = $_.Block
Name = "It $($_.Name)"
Result = $_.Result
Message = $_.ErrorRecord.DisplayErrorMessage
}
}
}
}
Write-PSFMessage -Level Significant -Message " Executing <c='em'>$($file.Name)</c>"
$config.TestResult.OutputPath = Join-Path "$PSScriptRoot\..\..\TestResults" "TEST-$($file.BaseName).xml"
$config.Run.Path = $file.FullName
$config.Run.PassThru = $true
$config.Output.Verbosity = $Output
$results = Invoke-Pester -Configuration $config
foreach ($result in $results) {
$totalRun += $result.TotalCount
$totalFailed += $result.FailedCount
$result.Tests | Where-Object Result -ne 'Passed' | ForEach-Object {
$testresults += [pscustomobject]@{
Block = $_.Block
Name = "It $($_.Name)"
Result = $_.Result
Message = $_.ErrorRecord.DisplayErrorMessage
}
}
}
}
}
#endregion Run General Tests

$global:__pester_data.ScriptAnalyzer | Out-Host
#region Run Function Tests
if ($TestFunctions) {
Write-PSFMessage -Level Important -Message "Proceeding with individual tests"
foreach ($file in (Get-ChildItem "$testroot\functions" -Recurse -File | Where-Object Name -like "*Tests.ps1")) {
if ($file.Name -notlike $Include) { continue }
if ($file.Name -like $Exclude) { continue }

#region Test Commands
if ($TestFunctions)
{
Write-PSFMessage -Level Important -Message "Proceeding with individual tests"
foreach ($file in (Get-ChildItem "$PSScriptRoot\functions" -Recurse -File | Where-Object Name -like "*Tests.ps1"))
{
if ($file.Name -notlike $Include) { continue }
if ($file.Name -like $Exclude) { continue }

Write-PSFMessage -Level Significant -Message " Executing $($file.Name)"
$config.TestResult.OutputPath = Join-Path "$PSScriptRoot\..\..\TestResults" "TEST-$($file.BaseName).xml"
$config.Run.Path = $file.FullName
$config.Run.PassThru = $true
$config.Output.Verbosity = $Output
$results = Invoke-Pester -Configuration $config
foreach ($result in $results)
{
$totalRun += $result.TotalCount
$totalFailed += $result.FailedCount
$result.Tests | Where-Object Result -ne 'Passed' | ForEach-Object {
$testresults += [pscustomobject]@{
Block = $_.Block
Name = "It $($_.Name)"
Result = $_.Result
Message = $_.ErrorRecord.DisplayErrorMessage
}
}
}
}
Write-PSFMessage -Level Significant -Message " Executing $($file.Name)"
$config.TestResult.OutputPath = Join-Path "$PSScriptRoot\..\..\TestResults" "TEST-$($file.BaseName).xml"
$config.Run.Path = $file.FullName
$config.Run.PassThru = $true
$config.Output.Verbosity = $Output
$results = Invoke-Pester -Configuration $config
foreach ($result in $results) {
$totalRun += $result.TotalCount
$totalFailed += $result.FailedCount
$result.Tests | Where-Object Result -ne 'Passed' | ForEach-Object {
$testresults += [pscustomobject]@{
Block = $_.Block
Name = "It $($_.Name)"
Result = $_.Result
Message = $_.ErrorRecord.DisplayErrorMessage
}
}
}
}
}
#endregion Test Commands
#endregion Run Function Tests

$testresults | Sort-Object Describe, Context, Name, Result, Message | Format-List

if ($totalFailed -eq 0) { Write-PSFMessage -Level Critical -Message "All <c='em'>$totalRun</c> tests executed without a single failure!" }
else { Write-PSFMessage -Level Critical -Message "<c='em'>$totalFailed tests</c> out of <c='sub'>$totalRun</c> tests failed!" }
if ($totalFailed -eq 0) {
Write-PSFMessage -Level Critical -Message "All <c='em'>$totalRun</c> tests executed without a single failure!"
} else {
Write-PSFMessage -Level Critical -Message "<c='em'>$totalFailed tests</c> out of <c='sub'>$totalRun</c> tests failed!"
}

if ($totalFailed -gt 0)
{
throw "$totalFailed / $totalRun tests failed!"
}
if ($totalFailed -gt 0) {
throw "$totalFailed / $totalRun tests failed!"
}

0 comments on commit e3f0a6b

Please sign in to comment.