This commit is contained in:
@@ -58,6 +58,10 @@ $Config = @{
|
||||
MaxDiscordEmbeds = 10
|
||||
RetryCount = 3
|
||||
RetryDelaySeconds = 2
|
||||
AuthoringTestsEnabled = $true
|
||||
AuthoringTestsPattern = 'test_authoring_server.py'
|
||||
AuthoringTestsDirectory = Join-Path $PSScriptRoot 'tests'
|
||||
AuthoringTestTimeoutSec = 120
|
||||
|
||||
# Tune these patterns to your build tooling.
|
||||
ErrorPatterns = @(
|
||||
@@ -613,6 +617,148 @@ function Send-DiscordNotification {
|
||||
}
|
||||
}
|
||||
|
||||
function Get-PythonCommand {
|
||||
$linuxVenvPython = Join-Path $PSScriptRoot '.venv/bin/python'
|
||||
$windowsVenvPython = Join-Path $PSScriptRoot '.venv/Scripts/python.exe'
|
||||
|
||||
if (Test-Path -Path $linuxVenvPython) {
|
||||
return $linuxVenvPython
|
||||
}
|
||||
if (Test-Path -Path $windowsVenvPython) {
|
||||
return $windowsVenvPython
|
||||
}
|
||||
|
||||
return 'python3'
|
||||
}
|
||||
|
||||
function Invoke-AuthoringServerTests {
|
||||
if (-not $Config.AuthoringTestsEnabled) {
|
||||
return $null
|
||||
}
|
||||
|
||||
if (-not (Test-Path -Path $Config.AuthoringTestsDirectory)) {
|
||||
throw "Authoring test directory does not exist: $($Config.AuthoringTestsDirectory)"
|
||||
}
|
||||
|
||||
$python = Get-PythonCommand
|
||||
$arguments = @(
|
||||
'-m',
|
||||
'unittest',
|
||||
'discover',
|
||||
'-s',
|
||||
$Config.AuthoringTestsDirectory,
|
||||
'-p',
|
||||
$Config.AuthoringTestsPattern,
|
||||
'-v'
|
||||
)
|
||||
|
||||
Write-Log -Message "Running authoring server tests with $python."
|
||||
|
||||
$process = New-Object System.Diagnostics.Process
|
||||
$process.StartInfo.FileName = $python
|
||||
foreach ($argument in $arguments) {
|
||||
$process.StartInfo.ArgumentList.Add([string]$argument)
|
||||
}
|
||||
$process.StartInfo.WorkingDirectory = $PSScriptRoot
|
||||
$process.StartInfo.UseShellExecute = $false
|
||||
$process.StartInfo.RedirectStandardOutput = $true
|
||||
$process.StartInfo.RedirectStandardError = $true
|
||||
|
||||
[void]$process.Start()
|
||||
$stdoutTask = $process.StandardOutput.ReadToEndAsync()
|
||||
$stderrTask = $process.StandardError.ReadToEndAsync()
|
||||
$completed = $process.WaitForExit([int]$Config.AuthoringTestTimeoutSec * 1000)
|
||||
if (-not $completed) {
|
||||
$process.Kill()
|
||||
$process.WaitForExit()
|
||||
}
|
||||
else {
|
||||
$process.WaitForExit()
|
||||
}
|
||||
|
||||
$stdout = $stdoutTask.Result
|
||||
$stderr = $stderrTask.Result
|
||||
$output = (($stdout, $stderr) -join "`n").Trim()
|
||||
$exitCode = if ($completed) { $process.ExitCode } else { 124 }
|
||||
$ran = 0
|
||||
$failures = 0
|
||||
$errors = 0
|
||||
$skipped = 0
|
||||
|
||||
if ($output -match 'Ran\s+(\d+)\s+tests?') {
|
||||
$ran = [int]$Matches[1]
|
||||
}
|
||||
if ($output -match 'failures=(\d+)') {
|
||||
$failures = [int]$Matches[1]
|
||||
}
|
||||
if ($output -match 'errors=(\d+)') {
|
||||
$errors = [int]$Matches[1]
|
||||
}
|
||||
if ($output -match 'skipped=(\d+)') {
|
||||
$skipped = [int]$Matches[1]
|
||||
}
|
||||
|
||||
$status = if ($completed -and $exitCode -eq 0) { 'passed' } elseif (-not $completed) { 'timed out' } else { 'failed' }
|
||||
$tailLines = @($output -split "`r?`n" | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } | Select-Object -Last 12)
|
||||
$summary = if ($tailLines.Count -gt 0) { $tailLines -join "`n" } else { 'No test output captured.' }
|
||||
if ($summary.Length -gt 950) {
|
||||
$summary = $summary.Substring(0, 947) + '...'
|
||||
}
|
||||
|
||||
$result = [pscustomobject]@{
|
||||
Name = 'authoring_server.py tests'
|
||||
Status = $status
|
||||
Success = ($completed -and $exitCode -eq 0)
|
||||
ExitCode = $exitCode
|
||||
Ran = $ran
|
||||
Failures = $failures
|
||||
Errors = $errors
|
||||
Skipped = $skipped
|
||||
Summary = $summary
|
||||
Completed = $completed
|
||||
Timestamp = (Get-Date).ToUniversalTime().ToString('o')
|
||||
}
|
||||
|
||||
Write-Log -Message ("Authoring server tests {0}: ran={1}, failures={2}, errors={3}, skipped={4}, exit={5}" -f $result.Status, $result.Ran, $result.Failures, $result.Errors, $result.Skipped, $result.ExitCode)
|
||||
return $result
|
||||
}
|
||||
|
||||
function Send-AuthoringTestNotification {
|
||||
param([Parameter(Mandatory)][object]$Result)
|
||||
|
||||
$emoji = if ($Result.Success) { '✅' } else { '❌' }
|
||||
$color = if ($Result.Success) { 3066993 } else { 15158332 }
|
||||
$payload = @{
|
||||
username = 'Gitea Build Monitor'
|
||||
content = "$emoji Authoring server test result: $($Result.Status)."
|
||||
embeds = @(
|
||||
@{
|
||||
title = "$emoji $($Result.Name)"
|
||||
color = $color
|
||||
description = "Unit test results from gitea-build-monitor.ps1"
|
||||
fields = @(
|
||||
@{ name = 'Status'; value = $Result.Status; inline = $true },
|
||||
@{ name = 'Tests'; value = [string]$Result.Ran; inline = $true },
|
||||
@{ name = 'Exit code'; value = [string]$Result.ExitCode; inline = $true },
|
||||
@{ name = 'Failures'; value = [string]$Result.Failures; inline = $true },
|
||||
@{ name = 'Errors'; value = [string]$Result.Errors; inline = $true },
|
||||
@{ name = 'Skipped'; value = [string]$Result.Skipped; inline = $true },
|
||||
@{ name = 'Summary'; value = $Result.Summary; inline = $false }
|
||||
)
|
||||
timestamp = $Result.Timestamp
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
Invoke-DiscordWebhook -Payload $payload
|
||||
if ($DryRun) {
|
||||
Write-Log -Message 'Prepared authoring server test notification in dry-run mode.'
|
||||
}
|
||||
else {
|
||||
Write-Log -Message 'Sent authoring server test notification.'
|
||||
}
|
||||
}
|
||||
|
||||
function Test-IsRecentBuild {
|
||||
param([Parameter(Mandatory)][object]$Job)
|
||||
|
||||
@@ -657,6 +803,11 @@ function Start-BuildMonitor {
|
||||
return
|
||||
}
|
||||
|
||||
$authoringTestResult = Invoke-AuthoringServerTests
|
||||
if ($null -ne $authoringTestResult) {
|
||||
Send-AuthoringTestNotification -Result $authoringTestResult
|
||||
}
|
||||
|
||||
$cache = if ($DryRun) { @{} } else { Read-ProcessedCache }
|
||||
$analyses = New-Object System.Collections.Generic.List[object]
|
||||
|
||||
@@ -705,6 +856,10 @@ function Start-BuildMonitor {
|
||||
if (-not $DryRun) {
|
||||
Save-ProcessedCache -Cache $cache
|
||||
}
|
||||
|
||||
if ($null -ne $authoringTestResult -and -not $authoringTestResult.Success) {
|
||||
throw 'Authoring server tests failed.'
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user