-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-runner.ps1
89 lines (66 loc) · 3.42 KB
/
test-runner.ps1
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
$projectPath = Get-Location
$dockerImageName = "integration-test-image"
$dockerContainerName = "integration-test-container"
function Write-ProgressBar($progress) {
$progress = [int]$progress
if ($progress -lt 0) { $progress = 0 }
if ($progress -gt 100) { $progress = 100 }
$barLength = 20 # Length of the bar (number of blocks to fill)
$filledLength = [math]::Floor($progress / 5) # 5% per block
$emptyLength = $barLength - $filledLength
$filledBar = "#" * $filledLength
$emptyBar = " " * $emptyLength
Write-Host " [$filledBar$emptyBar] $progress%"
Write-Host ""
}
function Get-TimeStamp() {
Get-Date -Format "HH:mm:ss"
}
Write-ProgressBar 0
Write-Host "[$(Get-TimeStamp) INFO] Cleaning up Docker resources..." -ForegroundColor Green
docker rm -f $dockerContainerName > $null 2>&1
docker rmi $dockerImageName > $null 2>&1
Write-ProgressBar 20
Write-Host "[$(Get-TimeStamp) INFO] Running unit tests..." -ForegroundColor Green
$unitTestOutput = & dotnet test "$projectPath/tests/UnitTests" -v n
if ($LASTEXITCODE -ne 0) {
Write-Host "[$(Get-TimeStamp) ERROR] Unit tests encountered errors." -ForegroundColor Red
} else {
Write-Host "[$(Get-TimeStamp) INFO] Unit tests completed." -ForegroundColor Green
}
$unitTestPassedCount = ($unitTestOutput | Select-String -Pattern "Passed: (\d+)" | ForEach-Object { $_.Matches.Groups[1].Value }) -join ''
$unitTestFailedCount = ($unitTestOutput | Select-String -Pattern "Failed: (\d+)" | ForEach-Object { $_.Matches.Groups[1].Value }) -join ''
if (-not $unitTestPassedCount) { $unitTestPassedCount = 0 }
if (-not $unitTestFailedCount) { $unitTestFailedCount = 0 }
Write-ProgressBar 40
Write-Host "[$(Get-TimeStamp) INFO] Building Docker image..." -ForegroundColor Green
docker build -t $dockerImageName $projectPath > $null 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host "[$(Get-TimeStamp) ERROR] Docker build failed." -ForegroundColor Red
exit 1
} else {
Write-Host "[$(Get-TimeStamp) INFO] Docker image built successfully." -ForegroundColor Green
}
Write-ProgressBar 60
Write-Host "[$(Get-TimeStamp) INFO] Running Docker container for integration tests..." -ForegroundColor Green
$integrationTestOutput = docker run --name $dockerContainerName $dockerImageName
$integrationTestPassedCount = ($integrationTestOutput | Select-String -Pattern "Passed: (\d+)" | ForEach-Object { $_.Matches.Groups[1].Value }) -join ''
$integrationTestFailedCount = ($integrationTestOutput | Select-String -Pattern "Failed: (\d+)" | ForEach-Object { $_.Matches.Groups[1].Value }) -join ''
if (-not $integrationTestPassedCount) { $integrationTestPassedCount = 0 }
if (-not $integrationTestFailedCount) { $integrationTestFailedCount = 0 }
Write-ProgressBar 80
Write-Host "[$(Get-TimeStamp) INFO] Cleaning up Docker resources..." -ForegroundColor Green
docker rm -f $dockerContainerName > $null 2>&1
docker rmi $dockerImageName > $null 2>&1
Write-ProgressBar 100
Write-Host "------------------------"
Write-Host "Test Summary:"
Write-Host "Unit Tests - Passed: " -NoNewline
Write-Host "$unitTestPassedCount" -ForegroundColor Green -NoNewline
Write-Host ", Failed: " -NoNewline
Write-Host "$unitTestFailedCount" -ForegroundColor Red
Write-Host "Integration Tests - Passed: " -NoNewline
Write-Host "$integrationTestPassedCount" -ForegroundColor Green -NoNewline
Write-Host ", Failed: " -NoNewline
Write-Host "$integrationTestFailedCount" -ForegroundColor Red
Write-Host "------------------------"