-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInvoke-Tests.ps1
More file actions
286 lines (228 loc) · 8.79 KB
/
Copy pathInvoke-Tests.ps1
File metadata and controls
286 lines (228 loc) · 8.79 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<#
.SYNOPSIS
Run tests for oh-my-pwsh
.DESCRIPTION
Orchestrates Pester test execution with support for different test types,
coverage reporting, and various output formats.
.PARAMETER Type
Type of tests to run: Unit, Integration, E2E, All (default: All)
.PARAMETER Coverage
Generate code coverage report (HTML + XML). Default: `
.PARAMETER Fast
Fast mode - parallel execution, no coverage (for git hooks)
.PARAMETER Watch
Watch mode - re-run tests when files change
.PARAMETER Filter
Run only tests matching this filter pattern
.EXAMPLE
.\scripts\Invoke-Tests.ps1
Run all tests
.EXAMPLE
.\scripts\Invoke-Tests.ps1 -Type Unit -Coverage
Run unit tests with coverage report
.EXAMPLE
.\scripts\Invoke-Tests.ps1 -Type Unit -Fast
Run unit tests in fast mode (for pre-commit hook)
.EXAMPLE
.\scripts\Invoke-Tests.ps1 -Filter "Icon*"
Run only tests matching "Icon*"
.NOTES
Requires Pester 5.5.0+
Run Install-TestDeps.ps1 first if Pester not installed
#>
[CmdletBinding()]
param(
[Parameter()]
[ValidateSet('Unit', 'Integration', 'E2E', 'All')]
[string]$Type = 'All',
[Parameter()]
[switch]$Coverage,
[Parameter()]
[switch]$Fast,
[Parameter()]
[switch]$Watch,
[Parameter()]
[string]$Filter = "*"
)
$ErrorActionPreference = 'Stop'
# Check Pester installation
$pester = Get-Module -ListAvailable -Name Pester |
Where-Object { $_.Version -ge [Version]"5.5.0" } |
Sort-Object Version -Descending |
Select-Object -First 1
if (-not $pester) {
Write-Host "✗ Pester 5.5.0+ not found" -ForegroundColor Red
Write-Host " Run: ./scripts/Install-TestDeps.ps1" -ForegroundColor Yellow
exit 1
}
# Import Pester
Import-Module Pester -MinimumVersion 5.5.0
# Project root
$projectRoot = Split-Path $PSScriptRoot -Parent
# Configure Pester
$config = [PesterConfiguration]::Default
# The power/profile tests do not use Pester's registry drive. Pester 6 enables
# TestRegistry by default on Windows, which requires write access to HKCU and
# makes otherwise isolated tests fail before their assertions run. Keep the
# opt-in registry fixture available for dedicated tests, but disable it for
# this repository's normal suite.
if ($config.PSObject.Properties.Name -contains 'TestRegistry') {
$config.TestRegistry.Enabled = $false
}
# Set test path based on type
$testRoot = Join-Path $projectRoot "tests"
switch ($Type) {
'Unit' { $testRoot = Join-Path $projectRoot "tests/Unit" }
'Integration' { $testRoot = Join-Path $projectRoot "tests/Integration" }
'E2E' { $testRoot = Join-Path $projectRoot "tests/E2E" }
}
$config.Run.Path = $testRoot
# Filter
if ($Filter -ne "*") {
# A file-name filter is more useful for this runner than Pester's full
# test-name filter when the caller asks for a focused module suite.
$matchingTestFiles = @(
Get-ChildItem -Path $testRoot -Recurse -Filter "*.Tests.ps1" |
Where-Object { -not $_.PSIsContainer -and $_.BaseName -like "*$Filter*" }
)
if ($matchingTestFiles.Count -gt 0) {
$config.Run.Path = @($matchingTestFiles.FullName)
} else {
$config.Filter.FullName = "*$Filter*"
}
}
# Output configuration
$config.Output.Verbosity = 'Detailed'
$config.Run.Exit = $false
$config.Run.PassThru = $true
# Fast mode optimizations
if ($Fast) {
Write-Host "⚡ Fast mode enabled (no coverage)" -ForegroundColor Cyan
$config.CodeCoverage.Enabled = $false
$config.Output.Verbosity = 'Normal'
}
# Coverage configuration
if ($Coverage -and -not $Fast) {
$config.CodeCoverage.Enabled = $true
$config.CodeCoverage.Path = @(
(Join-Path $projectRoot "settings/*.ps1"),
(Join-Path $projectRoot "modules/*.ps1"),
(Join-Path $projectRoot "profile.ps1")
)
$coverageDir = Join-Path $projectRoot "tests/Coverage"
if (-not (Test-Path $coverageDir)) {
New-Item -ItemType Directory -Path $coverageDir | Out-Null
}
$config.CodeCoverage.OutputPath = Join-Path $coverageDir "coverage.xml"
$config.CodeCoverage.OutputFormat = 'JaCoCo'
Write-Host "📊 Coverage report will be generated" -ForegroundColor Cyan
}
# Watch mode
if ($Watch) {
Write-Host "👀 Watch mode enabled - watching for file changes..." -ForegroundColor Cyan
Write-Host " Press Ctrl+C to stop`n" -ForegroundColor DarkGray
# Function to run tests
$runTests = {
Clear-Host
Write-Host "🔄 Running tests... ($(Get-Date -Format 'HH:mm:ss'))`n" -ForegroundColor Cyan
$result = Invoke-Pester -Configuration $config
Write-Host "`n" -NoNewline
if ($result.FailedCount -eq 0) {
Write-Host "✅ All tests passed!" -ForegroundColor Green
} else {
Write-Host "❌ $($result.FailedCount) test(s) failed" -ForegroundColor Red
}
Write-Host " Total: $($result.TotalCount) | " -NoNewline -ForegroundColor Gray
Write-Host "Passed: $($result.PassedCount) | " -NoNewline -ForegroundColor Green
if ($result.FailedCount -gt 0) {
Write-Host "Failed: $($result.FailedCount) | " -NoNewline -ForegroundColor Red
}
Write-Host "Skipped: $($result.SkippedCount)`n" -ForegroundColor Yellow
Write-Host "👀 Watching for changes... (Ctrl+C to stop)" -ForegroundColor Cyan
}
# Run tests initially
& $runTests
# Setup file watcher
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $projectRoot
$watcher.Filter = "*.ps1"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
# Debounce mechanism
$lastRun = [DateTime]::MinValue
$debounceMs = 2000
$action = {
$path = $Event.SourceEventArgs.FullPath
# Ignore coverage and .git files
if ($path -like "*\tests\Coverage\*" -or $path -like "*\.git\*") {
return
}
# Debounce - only run if enough time has passed
$now = Get-Date
if (($now - $script:lastRun).TotalMilliseconds -lt $script:debounceMs) {
return
}
$script:lastRun = $now
Write-Host "`n📝 File changed: $(Split-Path $path -Leaf)" -ForegroundColor Yellow
Start-Sleep -Milliseconds 500 # Wait for file to be written
& $script:runTests
}
# Register events
Register-ObjectEvent -InputObject $watcher -EventName Changed -Action $action | Out-Null
Register-ObjectEvent -InputObject $watcher -EventName Created -Action $action | Out-Null
Register-ObjectEvent -InputObject $watcher -EventName Deleted -Action $action | Out-Null
try {
# Keep script running
while ($true) {
Start-Sleep -Seconds 1
}
} finally {
# Cleanup
$watcher.Dispose()
Get-EventSubscriber | Unregister-Event
}
exit 0
}
# Run tests
Write-Host "`n🧪 Running $Type tests...`n" -ForegroundColor Cyan
$result = Invoke-Pester -Configuration $config
# Display results
Write-Host "`n" -NoNewline
if ($result.FailedCount -eq 0) {
Write-Host "✅ All tests passed!" -ForegroundColor Green
} else {
Write-Host "❌ $($result.FailedCount) test(s) failed" -ForegroundColor Red
}
Write-Host " Total: $($result.TotalCount) | " -NoNewline -ForegroundColor Gray
Write-Host "Passed: $($result.PassedCount) | " -NoNewline -ForegroundColor Green
if ($result.FailedCount -gt 0) {
Write-Host "Failed: $($result.FailedCount) | " -NoNewline -ForegroundColor Red
}
Write-Host "Skipped: $($result.SkippedCount)" -ForegroundColor Yellow
# Coverage summary
if ($Coverage -and -not $Fast) {
if ($result.CodeCoverage) {
$coverageReport = $result.CodeCoverage
$coveredCommands = $coverageReport.CommandsExecutedCount
$totalCommands = $coverageReport.CommandsAnalyzedCount
if ($totalCommands -gt 0) {
$coveragePercent = [math]::Round(($coveredCommands / $totalCommands) * 100, 2)
Write-Host "`n📊 Code Coverage: " -NoNewline -ForegroundColor Cyan
if ($coveragePercent -ge 75) {
Write-Host "$coveragePercent%" -ForegroundColor Green
} elseif ($coveragePercent -ge 50) {
Write-Host "$coveragePercent%" -ForegroundColor Yellow
} else {
Write-Host "$coveragePercent%" -ForegroundColor Red
}
Write-Host " Covered: $coveredCommands / $totalCommands commands" -ForegroundColor Gray
Write-Host " Report: tests/Coverage/coverage.xml" -ForegroundColor Gray
}
}
}
Write-Host ""
# Exit with appropriate code
if ($result.FailedCount -gt 0) {
exit 1
}
exit 0