-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-tests.ps1
More file actions
147 lines (122 loc) Β· 5.55 KB
/
run-tests.ps1
File metadata and controls
147 lines (122 loc) Β· 5.55 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
# PowerShell script to run Java tests and return structured results
# This script compiles and runs the Java test suite, then parses the output
param(
[string]$TestClass = "Unit5CompletionTest",
[string]$BranchName = "5-6-solution"
)
# Function to create a PowerShell custom object with test results
function New-TestResult {
param(
[bool]$AllTestsPassed,
[int]$TotalTests,
[int]$PassedTests,
[int]$FailedTests,
[array]$FailedTestNames = @(),
[string]$BranchName,
[string]$ErrorMessage = ""
)
return [PSCustomObject]@{
BranchName = $BranchName
AllTestsPassed = $AllTestsPassed
TotalTests = $TotalTests
PassedTests = $PassedTests
FailedTests = $FailedTests
FailedTestNames = $FailedTestNames
ErrorMessage = $ErrorMessage
Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
}
}
# Function to run Java tests and parse output
function Invoke-JavaTests {
param(
[string]$TestClass,
[string]$BranchName
)
try {
Write-Host "Running tests for branch: $BranchName" -ForegroundColor Cyan
Write-Host "Test class: $TestClass" -ForegroundColor Cyan
Write-Host "=" * 50 -ForegroundColor Cyan
# Check if main source files exist
$mainFiles = Get-ChildItem -Path "src/main/java" -Filter "*.java" -ErrorAction SilentlyContinue
$testFiles = Get-ChildItem -Path "src/test/java" -Filter "*.java" -ErrorAction SilentlyContinue
if ($testFiles.Count -eq 0) {
return New-TestResult -AllTestsPassed $false -TotalTests 0 -PassedTests 0 -FailedTests 0 -BranchName $BranchName -ErrorMessage "No test files found"
}
# Compile Java files
Write-Host "Compiling Java files..." -ForegroundColor Yellow
# Build classpath
$classpath = "src/main/java:src/test/java"
# Compile main source files
if ($mainFiles.Count -gt 0) {
$mainCompileResult = javac -cp $classpath $mainFiles.FullName 2>&1
if ($LASTEXITCODE -ne 0) {
return New-TestResult -AllTestsPassed $false -TotalTests 0 -PassedTests 0 -FailedTests 0 -BranchName $BranchName -ErrorMessage "Main compilation failed: $mainCompileResult"
}
}
# Compile test files
$testCompileResult = javac -cp $classpath $testFiles.FullName 2>&1
if ($LASTEXITCODE -ne 0) {
return New-TestResult -AllTestsPassed $false -TotalTests 0 -PassedTests 0 -FailedTests 0 -BranchName $BranchName -ErrorMessage "Test compilation failed: $testCompileResult"
}
# Run the test class
Write-Host "Running tests..." -ForegroundColor Yellow
$testOutput = java -cp $classpath $TestClass 2>&1
# Parse the test output
$totalTests = 0
$passedTests = 0
$failedTests = 0
$failedTestNames = @()
# Look for test summary patterns
if ($testOutput -match "Total Tests: (\d+)") {
$totalTests = [int]$matches[1]
}
if ($testOutput -match "Passed: (\d+)") {
$passedTests = [int]$matches[1]
}
if ($testOutput -match "Failed: (\d+)") {
$failedTests = [int]$matches[1]
}
# Also check for "Tests run" pattern
if ($totalTests -eq 0 -and $testOutput -match "Tests run: (\d+)") {
$totalTests = [int]$matches[1]
}
# Special handling for celebration tests
if ($totalTests -eq 0 -and $passedTests -eq 0 -and $failedTests -eq 0) {
# Check if this is a celebration test
if ($testOutput -match "(CONGRATULATIONS|π|β
|Successfully completed|All tests passed|Unit 5.*completed)") {
$totalTests = 1
$passedTests = 1
$failedTests = 0
$allTestsPassed = $true
} else {
$allTestsPassed = $false
}
} else {
# Extract failed test names
$failedTestNames = ($testOutput | Select-String "β FAIL:|β FAIL:|FAIL:" | ForEach-Object { $_.Matches[0].Value })
# Determine if all tests passed
$allTestsPassed = $failedTests -eq 0 -and $totalTests -gt 0
}
# Display results
Write-Host "`nTest Results:" -ForegroundColor Green
Write-Host "Total Tests: $totalTests" -ForegroundColor White
Write-Host "Passed: $passedTests" -ForegroundColor Green
Write-Host "Failed: $failedTests" -ForegroundColor Red
if ($allTestsPassed) {
Write-Host "π ALL TESTS PASSED!" -ForegroundColor Green
} else {
Write-Host "β Some tests failed" -ForegroundColor Red
if ($failedTestNames.Count -gt 0) {
Write-Host "Failed tests:" -ForegroundColor Red
$failedTestNames | ForEach-Object { Write-Host " - $_" -ForegroundColor Red }
}
}
return New-TestResult -AllTestsPassed $allTestsPassed -TotalTests $totalTests -PassedTests $passedTests -FailedTests $failedTests -FailedTestNames $failedTestNames -BranchName $BranchName
} catch {
return New-TestResult -AllTestsPassed $false -TotalTests 0 -PassedTests 0 -FailedTests 0 -BranchName $BranchName -ErrorMessage $_.Exception.Message
}
}
# Main execution
$result = Invoke-JavaTests -TestClass $TestClass -BranchName $BranchName
# Return the result object
return $result