Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 61 additions & 29 deletions tools/ci/Convert-CarayaReport.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ Markdown table with one row per test and its asserts.

TestStatus is the exit code of tools\ci\Invoke-CliTest.ps1 for the suite;
with 2 (LabVIEW not reachable) the report says that the tests did not run
instead of showing a failure. ProtocolReportPath and ProtocolStatus add a
section for tests\Run_Protocol_Tests_CLI.vi (tools\test\Test-McpServer.ps1
against a running server).
instead of showing a failure, and with "not-run" (the workflow skipped the
step because finding LabVIEW or installing the packages failed) it says
that. ProtocolReportPath and ProtocolStatus (the same values) add a section
for tests\Run_Protocol_Tests_CLI.vi (tools\test\Test-McpServer.ps1 against a
running server).

Labels and VI names are escaped for the Markdown table, and the protocol
tester's output is put in a code fence that no line of it can close.

Exit code is always 0; whether the run passed is decided by the test step.
#>
Expand All @@ -24,6 +29,7 @@ param(
)

$ErrorActionPreference = 'Stop'
Import-Module (Join-Path $PSScriptRoot 'LabVIEWCi.psm1') -Force
$utf8 = New-Object System.Text.UTF8Encoding $false
$md = New-Object System.Text.StringBuilder

Expand All @@ -36,18 +42,17 @@ function Add-ProtocolSection {
'0' { [void]$md.AppendLine(':white_check_mark: `Test-McpServer.ps1` passed against the test server.') }
'2' { [void]$md.AppendLine(':warning: Not run: LabVIEW could not be reached (LabVIEW CLI error -350000).') }
'3' { [void]$md.AppendLine('Not run: `tests\Run_Protocol_Tests_CLI.vi` does not exist on this branch.') }
'not-run' { [void]$md.AppendLine(':warning: Not run: an earlier step (finding LabVIEW, installing the packages) failed, or LabVIEW was not reachable; see the job log.') }
default { [void]$md.AppendLine(':x: `Test-McpServer.ps1` reported failed required checks against the test server.') }
}
[void]$md.AppendLine()
if ($ProtocolReportPath -and (Test-Path $ProtocolReportPath)) {
$lines = @(Get-Content $ProtocolReportPath)
if ($ProtocolStatus -ne 'not-run' -and $ProtocolReportPath -and (Test-Path -LiteralPath $ProtocolReportPath)) {
$lines = @(Get-Content -LiteralPath $ProtocolReportPath -Encoding UTF8)
$fails = @($lines | Where-Object { $_ -match '^\[FAIL\] \(required\)' })
$open = if ($ProtocolStatus -ne '0') { ' open' } else { '' }
[void]$md.AppendLine("<details$open><summary>Protocol tester output ($($fails.Count) failed required checks)</summary>")
[void]$md.AppendLine()
[void]$md.AppendLine('```')
foreach ($line in $lines) { [void]$md.AppendLine($line) }
[void]$md.AppendLine('```')
[void]$md.AppendLine((Format-MarkdownCodeBlock ($lines -join "`n")))
[void]$md.AppendLine()
[void]$md.AppendLine('</details>')
[void]$md.AppendLine()
Expand All @@ -69,7 +74,14 @@ if ($TestStatus -eq '2') {
return
}

if (-not (Test-Path $ReportPath)) {
if ($TestStatus -eq 'not-run') {
[void]$md.AppendLine(':warning: The tests did not run: finding LabVIEW or installing the packages failed before them. See the job log.')
[void]$md.AppendLine()
Save-Report
return
}

if (-not (Test-Path -LiteralPath $ReportPath)) {
[void]$md.AppendLine('No per-test report was produced. `tests\Run_Tests_CLI.vi` on this branch does not write `tests\results\caraya-report.txt` yet (update the branch from main), or the run stopped before the suite finished.')
Save-Report
return
Expand All @@ -80,35 +92,53 @@ function Format-Result([string]$Result) {
return ":x: $($Result.ToLower())"
}

# Caraya indents by call depth, and that depth depends on how the suite was
# started (LabVIEWCLI adds its own callers on top). The deepest lines are the
# asserts; the lines one step (two spaces) above them are the tests.
# The report, one line per entry: "<time> - <RESULT> - <indent><text>",
# indented by call depth, two spaces per level. Its structure:
# * first the callers (LabVIEW CLI's call chain, then Run_Tests_CLI and
# the suite VI), each line "<name> - <calling VI>", each at its own depth;
# * then one line per test, "<test label> - <library>:<test VI>.vi", all at
# the same depth;
# * after each test its asserts, one level (two spaces) deeper, with no VI
# part; an assert's label may itself contain " - ".
# How deep the tree starts depends on how the suite was started. The assert
# level is the depth most lines share (each test has at least one assert,
# each caller line has a depth of its own; on a tie the deeper one), and the
# tests are one level above it. A line deeper than that belongs to the
# current test as well, so one deeper line cannot shift the levels.
$entries = @()
foreach ($line in (Get-Content $ReportPath)) {
foreach ($line in (Get-Content -LiteralPath $ReportPath -Encoding UTF8)) {
$parts = $line -split ' - ', 3
if ($parts.Count -lt 3) { continue }
$rest = $parts[2]
$label = $rest.Trim()
$vi = ''
$cut = $label.LastIndexOf(' - ')
if ($cut -ge 0) { $vi = $label.Substring($cut + 3).Trim(); $label = $label.Substring(0, $cut).Trim() }
$entries += [pscustomobject]@{
Indent = $rest.Length - $rest.TrimStart().Length
Label = $label.TrimEnd(' ', '-')
Vi = ($vi -replace '^.*:', '')
Text = $rest.Trim()
Result = $parts[1].Trim()
}
}

$assertIndent = ($entries | Measure-Object -Property Indent -Maximum).Maximum
$tests = @()
$current = $null
foreach ($entry in $entries) {
if ($entry.Indent -eq $assertIndent) {
if ($current) { $current.Asserts += $entry }
} elseif ($entry.Indent -eq $assertIndent - 2) {
$current = [pscustomobject]@{ Label = $entry.Label; Vi = $entry.Vi; Result = $entry.Result; Asserts = @() }
$tests += $current
if ($entries.Count -gt 0) {
$assertIndent = [int]($entries | Group-Object Indent |
Sort-Object @{ Expression = 'Count'; Descending = $true }, @{ Expression = { [int]$_.Name }; Descending = $true } |
Select-Object -First 1).Name
$testIndent = $assertIndent - 2
$current = $null
foreach ($entry in $entries) {
if ($entry.Indent -eq $testIndent) {
# "<label> - <library>:<VI>": the VI is after the last " - ".
$label = $entry.Text
$vi = ''
$cut = $label.LastIndexOf(' - ')
if ($cut -ge 0) { $vi = $label.Substring($cut + 3).Trim(); $label = $label.Substring(0, $cut).Trim() }
$current = [pscustomobject]@{ Label = $label.TrimEnd(' ', '-'); Vi = ($vi -replace '^.*:', ''); Result = $entry.Result; Asserts = @() }
$tests += $current
} elseif ($entry.Indent -gt $testIndent) {
if ($current) { $current.Asserts += [pscustomobject]@{ Label = $entry.Text.TrimEnd(' ', '-'); Result = $entry.Result } }
} else {
# Back above the tests (a caller or suite line): no current test.
$current = $null
}
}
}

Expand All @@ -132,8 +162,10 @@ $open = if ($failed.Count -gt 0) { ' open' } else { '' }
[void]$md.AppendLine('| Test | VI | Result | Checks |')
[void]$md.AppendLine('|---|---|---|---|')
foreach ($test in $tests) {
$checks = ($test.Asserts | ForEach-Object { "$($_.Label): $($_.Result.ToLower())" }) -join '<br>'
[void]$md.AppendLine("| $($test.Label) | ``$($test.Vi)`` | $(Format-Result $test.Result) | $checks |")
$checks = ($test.Asserts | ForEach-Object { "$(ConvertTo-MarkdownText $_.Label): $($_.Result.ToLower())" }) -join '<br>'
# In a code span only | needs escaping in a table; a backtick would end it.
$vi = $test.Vi.Replace('`', "'").Replace('|', '\|')
[void]$md.AppendLine("| $(ConvertTo-MarkdownText $test.Label) | ``$vi`` | $(Format-Result $test.Result) | $checks |")
}
[void]$md.AppendLine()
[void]$md.AppendLine('</details>')
Expand Down
19 changes: 6 additions & 13 deletions tools/ci/Invoke-CliTest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function Write-Log([string]$Text) {
if ($LogFile) { [System.IO.File]::WriteAllText($LogFile, $Text, $utf8) }
}

if (-not (Test-Path $VIPath)) {
if (-not (Test-Path -LiteralPath $VIPath)) {
$message = "$VIPath does not exist on this branch."
Write-Host $message
Write-Log $message
Expand All @@ -41,18 +41,11 @@ if (-not (Get-LabVIEWCliPath)) {
}

function Invoke-RunVI([int]$Port) {
# LabVIEW CLI writes its errors to stderr; Windows PowerShell turns
# redirected stderr lines into errors, so they must not stop the script.
$previous = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
$out = & LabVIEWCLI -OperationName RunVI -VIPath $VIPath -PortNumber $Port `
-LabVIEWPath $LabVIEWPath 2>&1 | ForEach-Object {
# An empty stderr line arrives as an error record whose text is its type name.
if ($_ -is [System.Management.Automation.ErrorRecord]) { $_.Exception.Message } else { "$_" }
} | Out-String
$script:code = $LASTEXITCODE
$ErrorActionPreference = $previous
return $out
# LabVIEW CLI writes its errors to stderr; Invoke-NativeCommand keeps them
# as text in the output.
$run = Invoke-NativeCommand 'LabVIEWCLI' @('-OperationName', 'RunVI', '-VIPath', $VIPath, '-PortNumber', $Port, '-LabVIEWPath', $LabVIEWPath)
$script:code = $run.ExitCode
return ($run.Output | Out-String)
}

$output = Invoke-RunVI $PortNumber
Expand Down
Loading
Loading