This commit is contained in:
lda
2026-04-06 04:57:20 +07:00 Unverified
parent 32eb01efe6
commit 67aa50ec5c
2 changed files with 125 additions and 43 deletions
+19 -3
View File
@@ -15,6 +15,22 @@ function Get-DefaultPassword {
return (Get-Content -Raw $pwPath).Trim()
}
function Normalize-LineEndings {
param([string]$Text)
if ($null -eq $Text) {
return $null
}
return ($Text -replace "`r`n", "`n" -replace "`r", "")
}
function Quote-ShArg {
param([AllowNull()][string]$Value)
if ($null -eq $Value) {
return "''"
}
return "'" + ($Value -replace "'", ("'" + '"' + "'" + '"' + "'")) + "'"
}
function Invoke-Ext {
param($Exe, $Arguments, $Label)
$displayArgs = $Arguments.Clone()
@@ -27,8 +43,8 @@ function Invoke-Ext {
$out = & $Exe @Arguments 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Error ("{0} failed ({1}):`n{2}" -f $Label, $LASTEXITCODE, ($out -join "`n"))
throw ("{0} failed ({1})" -f $Label, $LASTEXITCODE)
Write-Error ("{0} exited with code {1}:`n{2}" -f $Label, $LASTEXITCODE, ($out -join "`n"))
throw ("{0} exited with code {1}" -f $Label, $LASTEXITCODE)
}
return $out
}
@@ -55,7 +71,7 @@ function Invoke-Scp {
function Invoke-Ssh {
param($Cmd, $User, $Remote, $Pass, [switch]$Quiet)
$Cmd = $Cmd -replace "`r`n", "`n" -replace "`r", ""
$Cmd = Normalize-LineEndings $Cmd
if ($plink = Get-Command plink.exe -ErrorAction SilentlyContinue) {
$arguments = @('-batch', '-ssh')
if ($Pass) { $arguments += @('-pw', $Pass) }
+100 -34
View File
@@ -3,14 +3,21 @@
param(
[string]$Package = "lda-ipjs",
[string]$TestName = "",
[switch]$AllPackages,
[string]$Filter = "",
[switch]$Exact,
[switch]$List,
[ValidateSet("debug", "release")]
[string]$BuildProfile = "debug",
[string]$password,
[switch]$Verbose,
[string]$RemoteTestPath = "/root/.bin/test",
[string]$RemoteHost = "[email protected]",
[switch]$Ignored
[switch]$Ignored,
[switch]$IncludeIgnored,
[switch]$NoCapture,
[switch]$ShowOutput,
[int]$Threads = 0
)
$ErrorActionPreference = "Stop"
@@ -19,49 +26,108 @@ $ErrorActionPreference = "Stop"
$password = Get-DefaultPassword $password
# Build tests and capture output
Write-Host "Building tests for $Package..." -ForegroundColor Cyan
function Get-WorkspacePackages {
$metadata = cargo metadata --no-deps --format-version 1 | ConvertFrom-Json
$workspaceMembers = [System.Collections.Generic.HashSet[string]]::new()
foreach ($member in $metadata.workspace_members) {
[void]$workspaceMembers.Add($member)
}
# Stream cargo output anc convert to text
$cargoOutput = cargo test --no-run -p $Package --target armv7-unknown-linux-musleabihf $(if ($BuildProfile -eq "release") { "-r" }) 2>&1 |
@(
$metadata.packages |
Where-Object { $workspaceMembers.Contains($_.id) } |
Select-Object -ExpandProperty name
)
}
function Get-TestBinaryPaths {
param([string[]]$CargoOutput)
@(
$CargoOutput |
Select-String -Pattern "Executable.*\((.+)\)" |
ForEach-Object { $_.Matches.Groups[1].Value }
)
}
function Build-RemoteExecCommand {
param(
[string]$RemotePath,
[string[]]$Arguments
)
$quotedPath = Quote-ShArg $RemotePath
$quotedArgs = @($Arguments | ForEach-Object { Quote-ShArg "$_" })
$exec = (@($quotedPath) + $quotedArgs) -join " "
"chmod +x $quotedPath && $exec"
}
$packages = if ($AllPackages) { Get-WorkspacePackages } else { @($Package) }
$failures = New-Object System.Collections.Generic.List[string]
foreach ($packageName in $packages) {
Write-Host "Building tests for $packageName..." -ForegroundColor Cyan
# Stream cargo output and convert to text
$cargoOutput = cargo test --no-run -p $packageName --target armv7-unknown-linux-musleabihf $(if ($BuildProfile -eq "release") { "-r" }) 2>&1 |
ForEach-Object {
$line = if ($_ -is [System.Management.Automation.ErrorRecord]) { $_.ToString() } else { $_ }
if ($Verbose) {
Write-Host $line
}
$line # Pass through to capture
$line
}
# Parse test binary paths from cargo output
$testBinaries = $cargoOutput |
Select-String -Pattern "Executable.*\((.+)\)" |
ForEach-Object { $_.Matches.Groups[1].Value } |
Get-Item
if ($LASTEXITCODE -ne 0) {
$failures.Add("$packageName (build failed)")
Write-Warning "Build failed for $packageName"
continue
}
$testBinaryPaths = Get-TestBinaryPaths $cargoOutput
$testBinaries = @($testBinaryPaths | Get-Item)
if ($testBinaries.Count -eq 0) {
Write-Error "No test binaries found! Exiting..."
# Write-Host "Cargo output:" -ForegroundColor Yellow
# $cargoOutput | ForEach-Object { Write-Host $_ }
Write-Host "Skipping ${packageName}: no test executables" -ForegroundColor DarkYellow
continue
}
Write-Host "Found $($testBinaries.Count) test $($testBinaries.Count -eq 1 ? "binary" : "binaries") for $packageName" -ForegroundColor Green
foreach ($testBinary in $testBinaries) {
Write-Host "`nTesting: $packageName / $($testBinary.Name)" -ForegroundColor Cyan
try {
# Copy and make executable
Invoke-Scp -Local $testBinary.FullName -Dest "${RemoteHost}:$RemoteTestPath" -Pass $password -Quiet
# Build test args
$parts = @()
if ($Filter) { $parts += $Filter }
if ($Exact) { $parts += "--exact" }
if ($List) { $parts += "--list" }
if ($Ignored) { $parts += "--ignored" }
if ($IncludeIgnored) { $parts += "--include-ignored" }
if ($ShowOutput -or $Verbose) { $parts += "--show-output" }
if ($NoCapture -or $Verbose) { $parts += "--nocapture" }
if ($Threads -gt 0) { $parts += "--test-threads"; $parts += $Threads }
$remoteCmd = Build-RemoteExecCommand -RemotePath $RemoteTestPath -Arguments $parts
# Run test binary (with chmod to ensure executable)
Invoke-Ssh -Cmd $remoteCmd -Remote $RemoteHost -Pass $password
}
catch {
$failures.Add("$packageName / $($testBinary.Name)")
Write-Warning "Test binary failed: $packageName / $($testBinary.Name)"
Write-Warning $_.Exception.Message
continue
}
}
}
if ($failures.Count -gt 0) {
Write-Error ("One or more test binaries failed: {0}" -f ($failures -join ", "))
exit 1
}
Write-Host "Found $($testBinaries.Count) test $($testBinaries.Count -eq 1 ? "binary" : "binaries")" -ForegroundColor Green
# Run each test binary
foreach ($testBinary in $testBinaries) {
Write-Host "`nTesting: $($testBinary.Name)" -ForegroundColor Cyan
# Copy to target
Invoke-Scp -Local $testBinary.FullName -Dest "${RemoteHost}:$RemoteTestPath" -Pass $password -Quiet
# Run on target
$parts = @()
if ($TestName) { $parts += $TestName }
if ($Ignored) { $parts += "--ignored" }
if ($Verbose) { $parts += "--nocapture"; $parts += "--show-output" }
$testArgs = $parts -join " "
Invoke-Ssh -Cmd "chmod +x $RemoteTestPath && $RemoteTestPath $testArgs" -Remote $RemoteHost -Pass $password
}
Write-Host "`nDone!" -ForegroundColor Green