-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ps1
More file actions
588 lines (489 loc) · 18.5 KB
/
test.ps1
File metadata and controls
588 lines (489 loc) · 18.5 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#Requires -Version 7.0
<#
.SYNOPSIS
Runs all tests for DevPossible.Ton libraries
.DESCRIPTION
This script executes tests for C#/.NET, JavaScript/TypeScript, and Python implementations
of the DevPossible.Ton library with detailed reporting and coverage options.
.PARAMETER Language
Specific language to test (CSharp, JavaScript, Python, or All). Default is All.
.PARAMETER Configuration
Build configuration for C# tests (Debug or Release). Default is Debug.
.PARAMETER Coverage
If specified, generates code coverage reports.
.PARAMETER Verbose
If specified, shows detailed test output.
.PARAMETER Filter
Test filter pattern for selective test execution.
.PARAMETER FailFast
If specified, stops on first test failure.
.EXAMPLE
.\test.ps1
Runs all tests for all languages
.EXAMPLE
.\test.ps1 -Language CSharp -Coverage
Runs C# tests with code coverage
.EXAMPLE
.\test.ps1 -Filter "Parser" -Verbose
Runs tests matching "Parser" with detailed output
#>
param(
[ValidateSet("CSharp", "JavaScript", "Python", "All")]
[string]$Language = "All",
[ValidateSet("Debug", "Release")]
[string]$Configuration = "Debug",
[switch]$Coverage,
[switch]$Verbose,
[string]$Filter = "",
[switch]$FailFast
)
$ErrorActionPreference = "Stop"
$ProgressPreference = "Continue"
# Define colors for output
$SuccessColor = "Green"
$InfoColor = "Cyan"
$WarningColor = "Yellow"
$ErrorColor = "Red"
function Write-TestHeader {
param([string]$Message)
Write-Host "`n===================================================" -ForegroundColor $InfoColor
Write-Host " $Message" -ForegroundColor $InfoColor
Write-Host "===================================================" -ForegroundColor $InfoColor
}
function Write-TestInfo {
param([string]$Message)
Write-Host "► $Message" -ForegroundColor $InfoColor
}
function Write-TestSuccess {
param([string]$Message)
Write-Host "✓ $Message" -ForegroundColor $SuccessColor
}
function Write-TestError {
param([string]$Message)
Write-Host "✗ $Message" -ForegroundColor $ErrorColor
}
function Write-TestWarning {
param([string]$Message)
Write-Host "⚠ $Message" -ForegroundColor $WarningColor
}
function Write-TestResult {
param(
[string]$Name,
[int]$Passed,
[int]$Failed,
[int]$Skipped,
[int]$Total,
[double]$Duration
)
Write-Host "`n Test Results for $Name :" -ForegroundColor White
Write-Host " ─────────────────────────────────────" -ForegroundColor DarkGray
if ($Failed -eq 0) {
Write-Host " ✓ Passed: $Passed/$Total" -ForegroundColor $SuccessColor
} else {
Write-Host " ✓ Passed: $Passed/$Total" -ForegroundColor $SuccessColor
Write-Host " ✗ Failed: $Failed/$Total" -ForegroundColor $ErrorColor
}
if ($Skipped -gt 0) {
Write-Host " ○ Skipped: $Skipped" -ForegroundColor $WarningColor
}
Write-Host " ⏱ Duration: $($Duration)s" -ForegroundColor Gray
}
# Start test process
Write-TestHeader "DevPossible.Ton Test Runner"
Write-TestInfo "Language: $Language"
Write-TestInfo "Configuration: $Configuration"
if ($Coverage) {
Write-TestInfo "Coverage: Enabled"
}
if ($Verbose) {
Write-TestInfo "Verbose: Enabled"
}
if ($Filter) {
Write-TestInfo "Filter: $Filter"
}
if ($FailFast) {
Write-TestInfo "Fail Fast: Enabled"
}
# Track test results
$testResults = @{
CSharp = @{ Passed = 0; Failed = 0; Skipped = 0; Total = 0; Duration = 0 }
JavaScript = @{ Passed = 0; Failed = 0; Skipped = 0; Total = 0; Duration = 0 }
Python = @{ Passed = 0; Failed = 0; Skipped = 0; Total = 0; Duration = 0 }
}
$overallSuccess = $true
# Run C# Tests
if ($Language -eq "All" -or $Language -eq "CSharp") {
Write-TestHeader "Running C#/.NET Tests"
try {
Push-Location "src\CSharp"
# Check if test project exists
if (-not (Test-Path "DevPossible.Ton.Tests\DevPossible.Ton.Tests.csproj")) {
throw "C# test project not found"
}
# Build test arguments
$testArgs = @("test", "DevPossible.Ton.Tests\DevPossible.Ton.Tests.csproj")
$testArgs += "-c", $Configuration
$testArgs += "--logger", "console;verbosity=normal"
if ($Verbose) {
$testArgs += "--verbosity", "detailed"
} else {
$testArgs += "--verbosity", "minimal"
}
if ($Filter) {
$testArgs += "--filter", $Filter
}
if ($Coverage) {
$testArgs += "--collect:""XPlat Code Coverage"""
$testArgs += "--settings", "coverlet.runsettings"
}
if ($FailFast) {
$testArgs += "--"
$testArgs += "RunConfiguration.StopOnError=true"
}
Write-TestInfo "Executing: dotnet $($testArgs -join ' ')"
$startTime = Get-Date
# Run tests and capture output
$testOutput = & dotnet $testArgs 2>&1
$exitCode = $LASTEXITCODE
$duration = ((Get-Date) - $startTime).TotalSeconds
# Parse test results from output
$testOutput | ForEach-Object {
if ($Verbose) {
Write-Host $_
}
if ($_ -match "Passed:\s+(\d+)") {
$testResults.CSharp.Passed = [int]$Matches[1]
}
if ($_ -match "Failed:\s+(\d+)") {
$testResults.CSharp.Failed = [int]$Matches[1]
}
if ($_ -match "Skipped:\s+(\d+)") {
$testResults.CSharp.Skipped = [int]$Matches[1]
}
if ($_ -match "Total:\s+(\d+)") {
$testResults.CSharp.Total = [int]$Matches[1]
}
}
# If Total wasn't captured, calculate it from parsed values
if ($testResults.CSharp.Total -eq 0 -and $testResults.CSharp.Passed -gt 0) {
$testResults.CSharp.Total = $testResults.CSharp.Passed + $testResults.CSharp.Failed + $testResults.CSharp.Skipped
}
$testResults.CSharp.Duration = [math]::Round($duration, 2)
if ($exitCode -ne 0) {
$overallSuccess = $false
Write-TestError "C# tests failed with exit code $exitCode"
} else {
Write-TestSuccess "C# tests completed successfully"
}
# Generate coverage report if requested
if ($Coverage) {
$coverageFile = Get-ChildItem -Path "DevPossible.Ton.Tests\TestResults" -Filter "coverage.cobertura.xml" -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1
if ($coverageFile) {
Write-TestInfo "Coverage report generated: $($coverageFile.FullName)"
# Try to generate HTML report if ReportGenerator is available
$reportGeneratorTool = & dotnet tool list -g | Where-Object { $_ -match "reportgenerator" }
if ($reportGeneratorTool) {
Write-TestInfo "Generating HTML coverage report..."
& reportgenerator "-reports:$($coverageFile.FullName)" "-targetdir:TestResults\CoverageReport" "-reporttypes:Html" 2>&1 | Out-Null
Write-TestSuccess "HTML coverage report generated in TestResults\CoverageReport"
}
}
}
Write-TestResult -Name "C#/.NET" `
-Passed $testResults.CSharp.Passed `
-Failed $testResults.CSharp.Failed `
-Skipped $testResults.CSharp.Skipped `
-Total $testResults.CSharp.Total `
-Duration $testResults.CSharp.Duration
Pop-Location
if ($FailFast -and $testResults.CSharp.Failed -gt 0) {
Write-TestError "Stopping due to test failures (FailFast enabled)"
exit 1
}
}
catch {
Pop-Location
Write-TestError "C# test execution failed: $_"
$overallSuccess = $false
if ($FailFast) {
exit 1
}
}
}
# Run JavaScript Tests
if ($Language -eq "All" -or $Language -eq "JavaScript") {
Write-TestHeader "Running JavaScript/TypeScript Tests"
try {
Push-Location "src\JavaScript\devpossible-ton"
# Check if npm is available
$npmVersion = & npm --version 2>&1
if ($LASTEXITCODE -ne 0) {
throw "npm is not installed or not in PATH"
}
# Check if package.json exists
if (-not (Test-Path "package.json")) {
throw "JavaScript package.json not found"
}
# Install dependencies if needed
if (-not (Test-Path "node_modules")) {
Write-TestInfo "Installing dependencies..."
$result = & npm ci 2>&1
if ($LASTEXITCODE -ne 0) {
$result = & npm install 2>&1
if ($LASTEXITCODE -ne 0) {
throw "Failed to install dependencies"
}
}
}
# Build test command
$testCommand = "test"
if ($Coverage) {
$testCommand = "test:coverage"
}
$env:JEST_VERBOSE = if ($Verbose) { "true" } else { "false" }
if ($Filter) {
$env:JEST_TEST_NAME_PATTERN = $Filter
}
if ($FailFast) {
$env:JEST_BAIL = "1"
}
# Build Jest arguments
$jestArgs = @()
if ($Verbose) {
$jestArgs += "--verbose"
}
if ($Filter) {
$jestArgs += "--testNamePattern=$Filter"
}
if ($FailFast) {
$jestArgs += "--bail"
}
if ($Coverage) {
$jestArgs += "--coverage"
}
# Find jest executable
$jestPath = ".\node_modules\.bin\jest.cmd"
if (-not (Test-Path $jestPath)) {
$jestPath = ".\node_modules\.bin\jest"
}
Write-TestInfo "Executing: jest $($jestArgs -join ' ')"
$startTime = Get-Date
# Run tests - call batch file directly
$testOutput = & $jestPath $jestArgs 2>&1
$exitCode = $LASTEXITCODE
$duration = ((Get-Date) - $startTime).TotalSeconds
# Parse test results from output
$testOutput | ForEach-Object {
if ($Verbose) {
Write-Host $_
}
# Match Jest summary output patterns
if ($_ -match "Test Suites:\s+(\d+)\s+passed") {
# Test suites passed count
}
if ($_ -match "Tests:\s+(\d+)\s+passed,\s+(\d+)\s+total") {
$testResults.JavaScript.Passed = [int]$Matches[1]
$testResults.JavaScript.Total = [int]$Matches[2]
}
elseif ($_ -match "Tests:\s+(\d+)\s+failed.*?(\d+)\s+passed,\s+(\d+)\s+total") {
$testResults.JavaScript.Failed = [int]$Matches[1]
$testResults.JavaScript.Passed = [int]$Matches[2]
$testResults.JavaScript.Total = [int]$Matches[3]
}
elseif ($_ -match "Tests:\s+(\d+)\s+passed") {
# Only passed count without total
$testResults.JavaScript.Passed = [int]$Matches[1]
}
}
# If Total wasn't captured, calculate it
if ($testResults.JavaScript.Total -eq 0 -and $testResults.JavaScript.Passed -gt 0) {
$testResults.JavaScript.Total = $testResults.JavaScript.Passed + $testResults.JavaScript.Failed
}
$testResults.JavaScript.Duration = [math]::Round($duration, 2)
if ($exitCode -ne 0) {
$overallSuccess = $false
Write-TestError "JavaScript tests failed with exit code $exitCode"
} else {
Write-TestSuccess "JavaScript tests completed successfully"
}
# Check for coverage report
if ($Coverage -and (Test-Path "coverage")) {
Write-TestInfo "Coverage report generated in coverage/"
if (Test-Path "coverage\lcov-report\index.html") {
Write-TestSuccess "HTML coverage report: coverage\lcov-report\index.html"
}
}
Write-TestResult -Name "JavaScript" `
-Passed $testResults.JavaScript.Passed `
-Failed $testResults.JavaScript.Failed `
-Skipped $testResults.JavaScript.Skipped `
-Total $testResults.JavaScript.Total `
-Duration $testResults.JavaScript.Duration
Pop-Location
if ($FailFast -and $testResults.JavaScript.Failed -gt 0) {
Write-TestError "Stopping due to test failures (FailFast enabled)"
exit 1
}
}
catch {
Pop-Location
Write-TestError "JavaScript test execution failed: $_"
$overallSuccess = $false
if ($FailFast) {
exit 1
}
}
}
# Run Python Tests
if ($Language -eq "All" -or $Language -eq "Python") {
Write-TestHeader "Running Python Tests"
try {
Push-Location "src\Python\devpossible_ton"
# Check if Python is available
$pythonVersion = & python --version 2>&1
if ($LASTEXITCODE -ne 0) {
throw "Python is not installed or not in PATH"
}
# Check if tests directory exists
if (-not (Test-Path "tests")) {
throw "Python tests directory not found"
}
# Install pytest if needed
$pytestVersion = & python -m pytest --version 2>&1
if ($LASTEXITCODE -ne 0) {
Write-TestInfo "Installing pytest..."
& python -m pip install pytest pytest-cov 2>&1 | Out-Null
}
# Build pytest arguments
$pytestArgs = @("-m", "pytest", "tests")
if ($Verbose) {
$pytestArgs += "-vv"
} else {
$pytestArgs += "-v"
}
if ($Filter) {
$pytestArgs += "-k", $Filter
}
if ($Coverage) {
$pytestArgs += "--cov=devpossible_ton"
$pytestArgs += "--cov-report=html"
$pytestArgs += "--cov-report=term"
}
if ($FailFast) {
$pytestArgs += "-x"
}
$pytestArgs += "--tb=short"
$pytestArgs += "--color=yes"
Write-TestInfo "Executing: python $($pytestArgs -join ' ')"
$startTime = Get-Date
# Run tests and capture output
$testOutput = & python $pytestArgs 2>&1
$exitCode = $LASTEXITCODE
$duration = ((Get-Date) - $startTime).TotalSeconds
# Parse test results from output
$testOutput | ForEach-Object {
if ($Verbose) {
Write-Host $_
}
if ($_ -match "(\d+) passed") {
$testResults.Python.Passed = [int]$Matches[1]
}
if ($_ -match "(\d+) failed") {
$testResults.Python.Failed = [int]$Matches[1]
}
if ($_ -match "(\d+) skipped") {
$testResults.Python.Skipped = [int]$Matches[1]
}
if ($_ -match "(\d+) error") {
$testResults.Python.Failed += [int]$Matches[1]
}
}
$testResults.Python.Total = $testResults.Python.Passed + $testResults.Python.Failed + $testResults.Python.Skipped
$testResults.Python.Duration = [math]::Round($duration, 2)
if ($exitCode -ne 0) {
$overallSuccess = $false
Write-TestError "Python tests failed with exit code $exitCode"
} else {
Write-TestSuccess "Python tests completed successfully"
}
# Check for coverage report
if ($Coverage -and (Test-Path "htmlcov")) {
Write-TestInfo "Coverage report generated in htmlcov/"
if (Test-Path "htmlcov\index.html") {
Write-TestSuccess "HTML coverage report: htmlcov\index.html"
}
}
Write-TestResult -Name "Python" `
-Passed $testResults.Python.Passed `
-Failed $testResults.Python.Failed `
-Skipped $testResults.Python.Skipped `
-Total $testResults.Python.Total `
-Duration $testResults.Python.Duration
Pop-Location
if ($FailFast -and $testResults.Python.Failed -gt 0) {
Write-TestError "Stopping due to test failures (FailFast enabled)"
exit 1
}
}
catch {
Pop-Location
Write-TestError "Python test execution failed: $_"
$overallSuccess = $false
if ($FailFast) {
exit 1
}
}
}
# Summary
Write-TestHeader "Test Summary"
$totalPassed = 0
$totalFailed = 0
$totalSkipped = 0
$totalTests = 0
$totalDuration = 0
foreach ($lang in $testResults.Keys) {
if ($Language -eq "All" -or $Language -eq $lang) {
$totalPassed += $testResults[$lang].Passed
$totalFailed += $testResults[$lang].Failed
$totalSkipped += $testResults[$lang].Skipped
$totalTests += $testResults[$lang].Total
$totalDuration += $testResults[$lang].Duration
}
}
Write-Host "`n Overall Results:" -ForegroundColor White
Write-Host " ─────────────────────────────────────" -ForegroundColor DarkGray
if ($overallSuccess -and $totalFailed -eq 0 -and $totalTests -gt 0) {
Write-Host " ✓ ALL TESTS PASSED!" -ForegroundColor $SuccessColor
Write-Host " Total: $totalPassed tests" -ForegroundColor $SuccessColor
} elseif ($totalTests -eq 0) {
Write-TestWarning " No tests were executed"
} else {
# Recalculate total to ensure accuracy
$calculatedTotal = $totalPassed + $totalFailed
$displayTotal = if ($totalTests -gt 0 -and $totalTests -eq $calculatedTotal) { $totalTests } else { $calculatedTotal }
Write-Host " ✓ Passed: $totalPassed/$displayTotal" -ForegroundColor $SuccessColor
Write-Host " ✗ Failed: $totalFailed/$displayTotal" -ForegroundColor $ErrorColor
}
if ($totalSkipped -gt 0) {
Write-Host " ○ Skipped: $totalSkipped" -ForegroundColor $WarningColor
}
Write-Host " ⏱ Total Duration: $($totalDuration)s" -ForegroundColor Gray
if ($Coverage) {
Write-Host "`n Coverage Reports:" -ForegroundColor White
Write-Host " ─────────────────────────────────────" -ForegroundColor DarkGray
if ($Language -eq "All" -or $Language -eq "CSharp") {
Write-Host " C#: src\CSharp\TestResults\CoverageReport" -ForegroundColor Gray
}
if ($Language -eq "All" -or $Language -eq "JavaScript") {
Write-Host " JS: src\JavaScript\devpossible-ton\coverage" -ForegroundColor Gray
}
if ($Language -eq "All" -or $Language -eq "Python") {
Write-Host " PY: src\Python\devpossible_ton\htmlcov" -ForegroundColor Gray
}
}
# Exit with appropriate code
if ($overallSuccess) {
exit 0
} else {
exit 1
}