|
| 1 | +function Write-ZtTestProgress { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Appends a progress entry to the overall test execution progress log. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Appends a single line to _progress.log in the logs folder, recording when |
| 8 | + each test starts, completes, or fails. This append-only log provides an |
| 9 | + at-a-glance timeline of all test executions and makes it easy to identify |
| 10 | + hanging tests (STARTED without a matching COMPLETED/FAILED line). |
| 11 | +
|
| 12 | + Uses a per-file named mutex plus [System.IO.File]::AppendAllText to |
| 13 | + serialize writes from parallel runspaces/processes and keep each entry |
| 14 | + on a single line. |
| 15 | +
|
| 16 | + .PARAMETER TestID |
| 17 | + The test ID to log progress for. |
| 18 | +
|
| 19 | + .PARAMETER LogsPath |
| 20 | + Path to the logs folder. If empty or null, the function is a no-op. |
| 21 | +
|
| 22 | + .PARAMETER Action |
| 23 | + The progress action: Started, Completed, or Failed. |
| 24 | +
|
| 25 | + .PARAMETER Duration |
| 26 | + The test duration (for Completed/Failed actions). |
| 27 | +
|
| 28 | + .PARAMETER ErrorMessage |
| 29 | + The error message (for Failed actions). |
| 30 | +
|
| 31 | + .EXAMPLE |
| 32 | + PS C:\> Write-ZtTestProgress -TestID 25384 -LogsPath $logsPath -Action Started |
| 33 | +
|
| 34 | + Appends a STARTED line for test 25384 to the progress log. |
| 35 | +
|
| 36 | + .EXAMPLE |
| 37 | + PS C:\> Write-ZtTestProgress -TestID 25384 -LogsPath $logsPath -Action Completed -Duration $result.Duration |
| 38 | +
|
| 39 | + Appends a COMPLETED line for test 25384 to the progress log. |
| 40 | + #> |
| 41 | + [CmdletBinding()] |
| 42 | + param ( |
| 43 | + [Parameter(Mandatory = $true)] |
| 44 | + $TestID, |
| 45 | + |
| 46 | + [string] |
| 47 | + $LogsPath, |
| 48 | + |
| 49 | + [Parameter(Mandatory = $true)] |
| 50 | + [ValidateSet('Started', 'Completed', 'Failed')] |
| 51 | + [string] |
| 52 | + $Action, |
| 53 | + |
| 54 | + [timespan] |
| 55 | + $Duration, |
| 56 | + |
| 57 | + $ErrorMessage |
| 58 | + ) |
| 59 | + process { |
| 60 | + if (-not $LogsPath) { return } |
| 61 | + |
| 62 | + try { |
| 63 | + [void][System.IO.Directory]::CreateDirectory($LogsPath) |
| 64 | + |
| 65 | + $timestamp = (Get-Date).ToString('yyyy-MM-dd HH:mm:ss.fff') |
| 66 | + $actionPadded = $Action.ToUpper().PadRight(10) |
| 67 | + |
| 68 | + $line = "$timestamp $actionPadded $TestID" |
| 69 | + if ($null -ne $Duration) { |
| 70 | + $line += " $($Duration.ToString('hh\:mm\:ss\.fff'))" |
| 71 | + } |
| 72 | + if ($Action -eq 'Completed') { |
| 73 | + $line += ' Pass' |
| 74 | + } |
| 75 | + if ($Action -eq 'Failed' -and $ErrorMessage) { |
| 76 | + $errorText = "$ErrorMessage" |
| 77 | + $errorText = $errorText -replace '[\r\n\t]+', ' ' |
| 78 | + if ($errorText.Length -gt 1000) { |
| 79 | + $errorText = $errorText.Substring(0, 1000) + '...' |
| 80 | + } |
| 81 | + $line += " $errorText" |
| 82 | + } |
| 83 | + $line += [System.Environment]::NewLine |
| 84 | + |
| 85 | + $progressFilePath = Join-Path $LogsPath '_progress.log' |
| 86 | + $fullPath = [System.IO.Path]::GetFullPath($progressFilePath) |
| 87 | + $normalizedPath = if ($IsWindows) { $fullPath.ToLowerInvariant() } else { $fullPath } |
| 88 | + |
| 89 | + # Cache the mutex name per resolved path to avoid repeated SHA256 hashing |
| 90 | + if (-not $script:ZtProgressMutexCache) { |
| 91 | + $script:ZtProgressMutexCache = @{} |
| 92 | + } |
| 93 | + if ($script:ZtProgressMutexCache.ContainsKey($normalizedPath)) { |
| 94 | + $mutexName = $script:ZtProgressMutexCache[$normalizedPath] |
| 95 | + } |
| 96 | + else { |
| 97 | + $pathBytes = [System.Text.Encoding]::UTF8.GetBytes($normalizedPath) |
| 98 | + $pathHashBytes = [System.Security.Cryptography.SHA256]::HashData($pathBytes) |
| 99 | + $pathHash = [System.BitConverter]::ToString($pathHashBytes).Replace('-', '') |
| 100 | + $mutexName = "Local\ZtProgress_$pathHash" |
| 101 | + $script:ZtProgressMutexCache[$normalizedPath] = $mutexName |
| 102 | + } |
| 103 | + |
| 104 | + $mutex = $null |
| 105 | + $lockAcquired = $false |
| 106 | + try { |
| 107 | + $mutex = [System.Threading.Mutex]::new($false, $mutexName) |
| 108 | + $lockAcquired = $mutex.WaitOne([TimeSpan]::FromSeconds(5)) |
| 109 | + if (-not $lockAcquired) { |
| 110 | + throw "Timed out waiting for progress log mutex '$mutexName'." |
| 111 | + } |
| 112 | + |
| 113 | + [System.IO.File]::AppendAllText($fullPath, $line) |
| 114 | + } |
| 115 | + finally { |
| 116 | + if ($lockAcquired -and $null -ne $mutex) { |
| 117 | + $null = $mutex.ReleaseMutex() |
| 118 | + } |
| 119 | + if ($null -ne $mutex) { |
| 120 | + $mutex.Dispose() |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + catch { |
| 125 | + Write-PSFMessage -Level Warning -Message "Failed to write progress log for test '{0}': {1}" -StringValues $TestID, $_.Exception.Message -Tag log |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments