-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke-Tests.ps1
More file actions
185 lines (151 loc) · 5.91 KB
/
Copy pathInvoke-Tests.ps1
File metadata and controls
185 lines (151 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#Requires -Version 5.1
#Requires -Modules Pester
<#
.SYNOPSIS
Runs all Pester tests for Daily Motivation Brain Helper
.DESCRIPTION
Executes unit and integration tests using Pester 5.x with code coverage analysis.
Supports filtering by tags, generating reports, and CI/CD integration.
.PARAMETER Tag
Run only tests with specified tags (e.g., 'Unit', 'Integration', 'Initialization')
.PARAMETER ExcludeTag
Exclude tests with specified tags
.PARAMETER CI
Run in CI mode: exits with error code on test failures, generates reports
.PARAMETER Coverage
Enable code coverage analysis (default: $true)
.EXAMPLE
.\Invoke-Tests.ps1
Runs all tests with code coverage
.EXAMPLE
.\Invoke-Tests.ps1 -Tag Unit
Runs only unit tests
.EXAMPLE
.\Invoke-Tests.ps1 -CI
Runs all tests in CI mode with reports
.EXAMPLE
.\Invoke-Tests.ps1 -Tag Integration -ExcludeTag Slow
Runs integration tests excluding slow tests
#>
[CmdletBinding()]
param(
[Parameter()]
[string[]]$Tag,
[Parameter()]
[string[]]$ExcludeTag,
[Parameter()]
[switch]$CI,
[Parameter()]
[bool]$Coverage = $true
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# Ensure we're in the repo root
$RepoRoot = $PSScriptRoot
Push-Location $RepoRoot
try {
Write-Host "=====================================================================" -ForegroundColor Cyan
Write-Host " Daily Motivation Brain Helper - Test Suite" -ForegroundColor Cyan
Write-Host "=====================================================================" -ForegroundColor Cyan
Write-Host ""
# Check Pester version
$pesterModule = Get-Module -Name Pester -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1
if (-not $pesterModule) {
throw "Pester module not found. Install with: Install-Module -Name Pester -Force -SkipPublisherCheck"
}
Write-Host "Using Pester version: $($pesterModule.Version)" -ForegroundColor Green
if ($pesterModule.Version.Major -lt 5) {
Write-Warning "Pester 5.x or higher is recommended. Current version: $($pesterModule.Version)"
}
# Import Pester
Import-Module Pester -MinimumVersion 5.0 -ErrorAction Stop
# Load configuration
$configPath = Join-Path $RepoRoot 'PesterConfiguration.psd1'
if (Test-Path $configPath) {
$configData = Import-PowerShellDataFile -Path $configPath
} else {
Write-Warning "PesterConfiguration.psd1 not found, using defaults"
$configData = @{}
}
# Build Pester configuration
$config = New-PesterConfiguration
$config.Run.Path = Join-Path $RepoRoot 'Tests'
$config.Run.PassThru = $true
$config.Output.Verbosity = 'Detailed'
# Apply tag filters
if ($Tag) {
$config.Filter.Tag = $Tag
Write-Host "Filtering by tags: $($Tag -join ', ')" -ForegroundColor Yellow
}
if ($ExcludeTag) {
$config.Filter.ExcludeTag = $ExcludeTag
Write-Host "Excluding tags: $($ExcludeTag -join ', ')" -ForegroundColor Yellow
}
# CI mode configuration
if ($CI) {
Write-Host "Running in CI mode" -ForegroundColor Yellow
$config.Run.Exit = $true
$config.TestResult.Enabled = $true
$config.TestResult.OutputFormat = 'NUnitXml'
$config.TestResult.OutputPath = Join-Path $RepoRoot 'TestResults.xml'
}
# Code coverage configuration
if ($Coverage) {
Write-Host "Code coverage analysis enabled" -ForegroundColor Yellow
$config.CodeCoverage.Enabled = $true
$config.CodeCoverage.Path = @(
(Join-Path $RepoRoot 'src\Modules\*.psm1')
)
$config.CodeCoverage.OutputFormat = 'JaCoCo'
$config.CodeCoverage.OutputPath = Join-Path $RepoRoot 'coverage.xml'
}
Write-Host ""
Write-Host "Starting test execution..." -ForegroundColor Cyan
Write-Host ""
# Run tests
$result = Invoke-Pester -Configuration $config
# Display results
Write-Host ""
Write-Host "=====================================================================" -ForegroundColor Cyan
Write-Host " Test Results Summary" -ForegroundColor Cyan
Write-Host "=====================================================================" -ForegroundColor Cyan
Write-Host "Total Tests: $($result.TotalCount)" -ForegroundColor White
Write-Host "Passed: $($result.PassedCount)" -ForegroundColor Green
Write-Host "Failed: $($result.FailedCount)" -ForegroundColor $(if ($result.FailedCount -gt 0) { 'Red' } else { 'Green' })
Write-Host "Skipped: $($result.SkippedCount)" -ForegroundColor Yellow
Write-Host "Duration: $($result.Duration)" -ForegroundColor White
if ($Coverage -and $result.CodeCoverage) {
$coveragePercent = [math]::Round(($result.CodeCoverage.CoveredPercent), 2)
$coverageColor = switch ($coveragePercent) {
{ $_ -ge 80 } { 'Green' }
{ $_ -ge 60 } { 'Yellow' }
default { 'Red' }
}
Write-Host ""
Write-Host "Code Coverage: $coveragePercent%" -ForegroundColor $coverageColor
Write-Host " Lines Covered: $($result.CodeCoverage.CoveredCommands) / $($result.CodeCoverage.CommandCount)" -ForegroundColor White
}
Write-Host "=====================================================================" -ForegroundColor Cyan
Write-Host ""
# Output report locations
if ($CI) {
Write-Host "Test results saved to: TestResults.xml" -ForegroundColor Green
if ($Coverage) {
Write-Host "Coverage report saved to: coverage.xml" -ForegroundColor Green
}
}
# Exit with error code if tests failed
if ($result.FailedCount -gt 0) {
Write-Host "❌ TESTS FAILED" -ForegroundColor Red
if ($CI) {
exit 1
}
} else {
Write-Host "✅ ALL TESTS PASSED" -ForegroundColor Green
if ($CI) {
exit 0
}
}
} finally {
Pop-Location
}