From df3f32dfb4c4ca711b7b4fa41cd67a5fd46af50d Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 3 Dec 2026 01:33:46 +0100 Subject: [PATCH] Verify swatches from the captured PostScript instead of tray preferences --- .github/qa/verify-windows-postscript.py | 38 ++++++++++++++++++----- .github/qa/windows-postscript-capture.ps1 | 20 ++++++++++-- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/.github/qa/verify-windows-postscript.py b/.github/qa/verify-windows-postscript.py index da11a989ae..c333527442 100644 --- a/.github/qa/verify-windows-postscript.py +++ b/.github/qa/verify-windows-postscript.py @@ -1,17 +1,22 @@ """Verify raw Windows PostScript and decode its numbered sheets independently.""" import hashlib +import importlib.util import json from pathlib import Path import re import subprocess +import tempfile ROOT = Path("captured-postscript") VERIFY = Path("shared/crates/print/examples/print_acceptance/verify.py") CASES = {"baseline": [1, 2, 3, 4], "range": [2, 4]} +spec = importlib.util.spec_from_file_location("numbered_sheets", VERIFY) +numbered_sheets = importlib.util.module_from_spec(spec) +spec.loader.exec_module(numbered_sheets) -def verify(folder, case, expected, grayscale): +def verify(folder, case, expected): raw = folder / f"{case}.prn" captured = raw.read_bytes() # A printer may wrap the PostScript in PJL. Keep the spool bytes intact and @@ -40,23 +45,40 @@ def verify(folder, case, expected, grayscale): document.write_bytes(payload) pdf = folder / f"{case}-rendered.pdf" subprocess.run( - ["gs", "-q", "-dSAFER", "-dBATCH", "-dNOPAUSE", "-sDEVICE=pdfwrite", f"-sOutputFile={pdf}", document], + ["gs", "-q", "-dSAFER", "-dBATCH", "-dNOPAUSE", "-dNoOutputFonts", "-sDEVICE=pdfwrite", f"-sOutputFile={pdf}", document], check=True, timeout=60, ) - subprocess.run( - ["python3", VERIFY, pdf, *map(str, expected), *(["--gray"] if grayscale else [])], - check=True, timeout=180, - ) + with tempfile.TemporaryDirectory(prefix="craft-postscript-") as directory: + root = Path(directory) + subprocess.run(["pdftoppm", "-r", "36", pdf, root / "page"], check=True, timeout=60) + sheets = sorted(root.glob("page-*.ppm"), key=lambda path: int(path.stem.split("-")[-1])) + assert len(sheets) == len(expected), (pdf, len(sheets), expected) + # Windows' Color preference isn't reliable for the output PS class + # driver. Verify whichever mode the physical swatches actually use. + try: + numbered_sheets.verify_page(sheets[0], expected[0], plotter=False, gray=False) + grayscale = False + except ValueError: + numbered_sheets.verify_page(sheets[0], expected[0], plotter=False, gray=True) + grayscale = True + for sheet, page in zip(sheets[1:], expected[1:]): + numbered_sheets.verify_page(sheet, page, plotter=False, gray=grayscale) + print(f"{pdf}: verified IDs {expected} with {'gray' if grayscale else 'color'} swatches", flush=True) info = subprocess.check_output(["pdfinfo", pdf], text=True) (folder / f"{case}.pdfinfo").write_text(info) def main(): + failures = [] for model in ("ps-class", "imagesetter"): folder = ROOT / model - configuration = json.loads((folder / "configuration.json").read_text(encoding="utf-8-sig")) for case, expected in CASES.items(): - verify(folder, case, expected, grayscale=configuration["Color"] is False) + try: + verify(folder, case, expected) + except (AssertionError, OSError, ValueError, subprocess.SubprocessError) as error: + failures.append(f"{model}/{case}: {error}") + if failures: + raise SystemExit("\n".join(failures)) if __name__ == "__main__": diff --git a/.github/qa/windows-postscript-capture.ps1 b/.github/qa/windows-postscript-capture.ps1 index 0789ae37ab..80c13039da 100644 --- a/.github/qa/windows-postscript-capture.ps1 +++ b/.github/qa/windows-postscript-capture.ps1 @@ -65,6 +65,9 @@ foreach ($model in $models) { [pscustomobject]@{ Catalog = $_.Name; Status = $auth.Status.ToString(); Signer = $auth.SignerCertificate.Subject } }) $signature | ConvertTo-Json -AsArray | Set-Content -LiteralPath (Join-Path $folder.FullName 'catalog-signatures.json') + if (!($signature | Where-Object { $_.Status -eq 'Valid' -and $_.Signer -like 'CN=Microsoft Windows,*' })) { + throw "No valid Microsoft Windows catalog signature for $($model.Inf)" + } # Only the OS inbox package above is eligible. Install it through the # standard spooler package path and leave signature enforcement enabled. if (!(Get-PrinterDriver -Name $driverName -ErrorAction SilentlyContinue)) { @@ -103,9 +106,20 @@ foreach ($model in $models) { $failed += "$($model.Key): $_" $_ | Out-String | Set-Content -LiteralPath (Join-Path $folder.FullName 'failure.txt') } finally { - if ($queueAdded) { Remove-Printer -Name $queue } - if ($portAdded) { Remove-PrinterPort -Name $port } - if ($driverAdded) { Remove-PrinterDriver -Name $driverName } + try { + if ($queueAdded) { Remove-Printer -Name $queue } + if ($portAdded) { + # The name overload parses backslashes as a server qualifier. + $localPort = Get-PrinterPort | Where-Object Name -eq $port + if ($localPort) { Remove-PrinterPort -InputObject $localPort } + } + if ($driverAdded) { Remove-PrinterDriver -Name $driverName } + } catch { + # The disposable runner is deprovisioned. Record any teardown + # failure and keep the independent captures available. + Write-Warning "Private queue teardown failed: $_" + $_ | Out-String | Set-Content -LiteralPath (Join-Path $folder.FullName 'cleanup.txt') + } } } if ($failed.Count) { throw ($failed -join [Environment]::NewLine) }