Files
lightgui_rust/tools/visreg/run.sh
cnmcdee d507697ae8 tools/visreg: refresh 5 stale goldens, raise window-wait to 20s
First full run: 537 PASS, 9 FAIL, 1 SKIP (547 total).
Five RMSE failures were stale docs-sweep goldens (mid-draw
captures: chart data present, border/title/overlay lines missing)
— IndAdaptiveCyberCycle, IndCyberCycle, IndRegressionChannel,
IndRelativeVolatilityIndex, InstTankLevelAlarms. Each refreshed
golden is a settled render verified bit-stable across two
independent runs (RMSE 0). Four no-window failures were load
flakes under parallel execution; window-wait budget 10s -> 20s.
Confirmation re-run of all 9: 9 PASS, 0 FAIL, all 0.0000% RMSE.
2026-12-02 10:10:41 +00:00

290 lines
11 KiB
Bash
Executable File

#!/usr/bin/env bash
# tools/visreg/run.sh — visual regression harness for the Rust LightGUI demos.
#
# Re-renders every xframe-demo widget exactly the way the docs sweep did
# (prebuilt target/debug/examples/<Name> binary, 800x600 window under
# Xvfb 1280x1024x24, captured with `import -window <wid>`), diffs each
# new screenshot against its golden in tools/visreg/golden/ with
# ImageMagick `compare -metric RMSE`, and writes a per-widget report.
#
# Usage:
# tools/visreg/run.sh [--golden-dir DIR] [--out DIR] [--threshold PCT]
# [--widgets "A B C" | @file] [--rebuild]
# [--update-goldens] [--keep-shots]
#
# --golden-dir goldens to compare against (default: tools/visreg/golden)
# --out report directory (default: tools/visreg/report)
# --threshold max RMSE percent for PASS (default 0.5; see README.md)
# --widgets run only these widgets (space-separated, or @file with
# one name per line); default is all 547 from
# `xframe-demo --list`
# --rebuild force `cargo build -p xframe-demo --examples` first;
# otherwise the build runs only if a binary is missing
# --update-goldens replace goldens with the new shots (ONLY after
# reviewing the diffs and deciding the change is intended)
# --keep-shots keep every new screenshot (default: only failures' diffs
# are kept; passing shots are deleted)
#
# Exit code: 0 when every compared widget passes (or is skipped), 1 when
# any widget fails. The report is written either way:
# <out>/report.csv one row per widget: name,status,rmse_pct,rmse_raw,notes
# <out>/report.md human-readable summary + failure table
# <out>/summary.txt one-line counts
# <out>/diffs/ ImageMagick diff images, failures only
# <out>/shots/ new screenshots (only with --keep-shots)
#
# GtkInvisible is skipped by design: it draws nothing, so there is no
# screenshot to compare (its golden is a blank 800x600 window).
set -u
VISREG_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO="$(cd "$VISREG_DIR/../.." && pwd)"
GOLDEN_DIR="$VISREG_DIR/golden"
OUT_DIR="$VISREG_DIR/report"
THRESHOLD="0.5"
WIDGETS_ARG=""
REBUILD=0
UPDATE_GOLDENS=0
KEEP_SHOTS=0
while [ $# -gt 0 ]; do
case "$1" in
--golden-dir) GOLDEN_DIR="$2"; shift 2;;
--out) OUT_DIR="$2"; shift 2;;
--threshold) THRESHOLD="$2"; shift 2;;
--widgets) WIDGETS_ARG="$2"; shift 2;;
--rebuild) REBUILD=1; shift;;
--update-goldens) UPDATE_GOLDENS=1; shift;;
--keep-shots) KEEP_SHOTS=1; shift;;
--in-xvfb) shift;; # internal: already under Xvfb, ignore
*) echo "visreg: unknown option: $1" >&2; exit 2;;
esac
done
# --update-goldens needs the new shots kept around for the copy step.
[ "$UPDATE_GOLDENS" = 1 ] && KEEP_SHOTS=1
# Re-exec under Xvfb when there is no display (same screen the docs
# sweep used: 1280x1024x24; the demo window is 800x600 at 100,100).
# NOTE: build the re-exec argv explicitly — `${VAR:+...}` alternates
# would fire on the "0" defaults too.
if [ -z "${DISPLAY:-}" ] && [ -z "${VISREG_IN_XVFB:-}" ]; then
re_args=(--in-xvfb --golden-dir "$GOLDEN_DIR" --out "$OUT_DIR"
--threshold "$THRESHOLD")
[ -n "$WIDGETS_ARG" ] && re_args+=(--widgets "$WIDGETS_ARG")
[ "$REBUILD" = 1 ] && re_args+=(--rebuild)
[ "$UPDATE_GOLDENS" = 1 ] && re_args+=(--update-goldens)
[ "$KEEP_SHOTS" = 1 ] && re_args+=(--keep-shots)
VISREG_IN_XVFB=1 exec xvfb-run -a -s "-screen 0 1280x1024x24" \
env VISREG_IN_XVFB=1 "$0" "${re_args[@]}"
fi
export PATH="$HOME/.cargo/bin:/root/.cargo/bin:$PATH"
BIN_DIR="$REPO/target/debug/examples"
DEMO_BIN="$REPO/target/debug/xframe-demo"
SHOTS_DIR="$OUT_DIR/shots"
DIFFS_DIR="$OUT_DIR/diffs"
mkdir -p "$OUT_DIR" "$SHOTS_DIR" "$DIFFS_DIR"
# --- widget list -----------------------------------------------------------
if [ -n "$WIDGETS_ARG" ]; then
if [ "${WIDGETS_ARG#@}" != "$WIDGETS_ARG" ]; then
mapfile -t NAMES < "${WIDGETS_ARG#@}"
else
# shellcheck disable=SC2206
NAMES=($WIDGETS_ARG)
fi
elif [ -x "$DEMO_BIN" ]; then
mapfile -t NAMES < <("$DEMO_BIN" --list)
else
# Fall back to the golden directory listing.
mapfile -t NAMES < <(cd "$GOLDEN_DIR" && printf '%s\n' *.png | sed 's/\.png$//' | sort)
fi
# Drop empty lines (e.g. trailing newline in a @file).
TMP=()
for n in "${NAMES[@]}"; do [ -n "$n" ] && TMP+=("$n"); done
NAMES=("${TMP[@]}")
echo "visreg: ${#NAMES[@]} widget(s), threshold ${THRESHOLD}% RMSE, goldens $GOLDEN_DIR"
# --- build (only if needed) ------------------------------------------------
need_build=0
[ "$REBUILD" = 1 ] && need_build=1
if [ "$need_build" = 0 ]; then
for n in "${NAMES[@]}"; do
[ -x "$BIN_DIR/$n" ] || { need_build=1; break; }
done
fi
if [ "$need_build" = 1 ]; then
echo "visreg: building examples (cargo build -p xframe-demo --examples)..."
(cd "$REPO" && cargo build -p xframe-demo --examples) || {
echo "visreg: cargo build failed" >&2; exit 1; }
fi
# --- sweep -----------------------------------------------------------------
REPORT_CSV="$OUT_DIR/report.csv"
echo "name,status,rmse_pct,rmse_raw,notes" > "$REPORT_CSV"
pass=0; fail=0; skip=0
FAILED_NAMES=()
for name in "${NAMES[@]}"; do
golden="$GOLDEN_DIR/$name.png"
shot="$SHOTS_DIR/$name.png"
diff="$DIFFS_DIR/$name.png"
# GtkInvisible draws nothing by design: no screenshot to compare.
if [ "$name" = "GtkInvisible" ]; then
echo "$name,SKIP,,,\"invisible by design (no screenshot)\"" >> "$REPORT_CSV"
skip=$((skip + 1)); echo "SKIP $name (invisible by design)"
continue
fi
if [ ! -f "$golden" ]; then
echo "$name,FAIL,,,\"no golden screenshot\"" >> "$REPORT_CSV"
fail=$((fail + 1)); FAILED_NAMES+=("$name (no golden)")
echo "FAIL $name (no golden)"
continue
fi
if [ ! -x "$BIN_DIR/$name" ]; then
echo "$name,FAIL,,,\"example binary missing\"" >> "$REPORT_CSV"
fail=$((fail + 1)); FAILED_NAMES+=("$name (binary missing)")
echo "FAIL $name (binary missing)"
continue
fi
# Launch, wait for the window, screenshot it, kill it. (Same method
# as the docs pilot sweep: timeout backstop, xdotool window lookup
# by title, ImageMagick import of that window — no window manager,
# so the capture is exactly the 800x600 client area.)
timeout 20 "$BIN_DIR/$name" >/dev/null 2>&1 &
pid=$!
wid=""
# 100 x 0.2s = 20s budget: under parallel load a big example binary
# can take seconds to map its window; a short budget misreports a
# slow start as a missing window.
for _ in $(seq 1 100); do
wid=$(xdotool search --name "$name - XFrame demo" 2>/dev/null | head -1)
[ -n "$wid" ] && break
sleep 0.2
done
if [ -z "$wid" ]; then
kill "$pid" 2>/dev/null; wait "$pid" 2>/dev/null
echo "$name,FAIL,,,\"no window appeared\"" >> "$REPORT_CSV"
fail=$((fail + 1)); FAILED_NAMES+=("$name (no window)")
echo "FAIL $name (no window)"
continue
fi
sleep 0.5
# Settle: capture until two consecutive screenshots are identical.
# A capture can race the X server mid-draw — the docs sweep's
# GtkCalendar golden caught exactly that (fill drawn, border/text
# not yet). Two identical frames in a row means the draw is done.
settle_note=""
shot_ok=0
prev=""
for _ in 1 2 3 4; do
tmp_shot="$SHOTS_DIR/.settle.png"
import -window "$wid" "$tmp_shot" 2>/dev/null || break
if [ -n "$prev" ]; then
if compare -metric RMSE "$prev" "$tmp_shot" null: 2>&1 | grep -q "(0)"; then
mv "$tmp_shot" "$shot"
rm -f "$prev"
shot_ok=1
break
fi
rm -f "$prev"
fi
prev="$tmp_shot"
sleep 0.3
done
if [ "$shot_ok" = 0 ]; then
if [ -n "$prev" ] && [ -f "$prev" ]; then
mv "$prev" "$shot" # never settled; use the last frame
settle_note="capture never settled; used last frame"
else
kill "$pid" 2>/dev/null; wait "$pid" 2>/dev/null
echo "$name,FAIL,,,\"screenshot failed\"" >> "$REPORT_CSV"
fail=$((fail + 1)); FAILED_NAMES+=("$name (screenshot failed)")
echo "FAIL $name (screenshot failed)"
continue
fi
fi
kill "$pid" 2>/dev/null; wait "$pid" 2>/dev/null
size=$(identify -format "%wx%h" "$shot" 2>/dev/null || echo "?")
if [ "$size" != "800x600" ]; then
echo "$name,FAIL,,,\"screenshot size $size (expected 800x600)\"" >> "$REPORT_CSV"
fail=$((fail + 1)); FAILED_NAMES+=("$name (size $size)")
echo "FAIL $name (size $size)"
continue
fi
# ImageMagick RMSE: `compare` prints e.g. "12.3456 (0.0001884)" to
# stderr and exits 1 when images differ; the parenthesised value is
# the normalised RMSE in [0,1]. A few pixels of font/AA wobble are
# expected, so PASS is pct <= threshold, not pct == 0.
cmp_out=$(compare -metric RMSE "$golden" "$shot" "$diff" 2>&1)
norm=$(echo "$cmp_out" | sed -n 's/.*(\([0-9.]*\)).*/\1/p' | head -1)
if [ -z "$norm" ]; then
echo "$name,FAIL,,,\"compare failed: $cmp_out\"" >> "$REPORT_CSV"
fail=$((fail + 1)); FAILED_NAMES+=("$name (compare failed)")
echo "FAIL $name (compare failed)"
continue
fi
pct=$(awk -v n="$norm" 'BEGIN { printf "%.4f", n * 100 }')
raw=$(echo "$cmp_out" | awk '{print $1}')
if awk -v p="$pct" -v t="$THRESHOLD" 'BEGIN { exit !(p <= t) }'; then
echo "$name,PASS,$pct,$raw,\"$settle_note\"" >> "$REPORT_CSV"
pass=$((pass + 1))
rm -f "$diff"
[ "$KEEP_SHOTS" = 0 ] && rm -f "$shot"
echo "PASS $name (${pct}%)"
else
echo "$name,FAIL,$pct,$raw,\"RMSE ${pct}% > ${THRESHOLD}% threshold${settle_note:+; $settle_note}\"" >> "$REPORT_CSV"
fail=$((fail + 1)); FAILED_NAMES+=("$name (${pct}%)")
echo "FAIL $name (${pct}%)"
fi
done
[ "$KEEP_SHOTS" = 0 ] && rmdir "$SHOTS_DIR" 2>/dev/null
rmdir "$DIFFS_DIR" 2>/dev/null # only remains if it holds failure diffs
total=${#NAMES[@]}
{
echo "visual regression: $pass PASS, $fail FAIL, $skip SKIP, $total total"
echo "threshold: ${THRESHOLD}% RMSE | goldens: $GOLDEN_DIR | report: $OUT_DIR"
if [ "$fail" -gt 0 ]; then
echo "failures:"
for f in "${FAILED_NAMES[@]}"; do echo " - $f"; done
fi
} | tee "$OUT_DIR/summary.txt"
{
echo "# Visual regression report"
echo
echo "- **Run:** $(date -u '+%Y-%m-%d %H:%M:%S UTC')"
echo "- **Threshold:** ${THRESHOLD}% RMSE (ImageMagick \`compare -metric RMSE\`)"
echo "- **Goldens:** \`$GOLDEN_DIR\`"
echo "- **Result:** $pass PASS, $fail FAIL, $skip SKIP, $total total"
echo
if [ "$fail" -gt 0 ]; then
echo "## Failures"
echo
echo "| widget | RMSE % | notes |"
echo "|--------|--------|-------|"
awk -F, '$2 == "FAIL" { gsub(/"/, "", $5); print "| " $1 " | " $3 " | " $5 " |" }' "$REPORT_CSV"
echo
fi
echo "Full per-widget rows: [report.csv](report.csv)"
} > "$OUT_DIR/report.md"
if [ "$UPDATE_GOLDENS" = 1 ]; then
echo "visreg: --update-goldens: replacing goldens with new shots..."
for name in "${NAMES[@]}"; do
[ "$name" = "GtkInvisible" ] && continue
[ -f "$SHOTS_DIR/$name.png" ] && cp "$SHOTS_DIR/$name.png" "$GOLDEN_DIR/$name.png"
done
echo "visreg: goldens updated (review with git diff before committing)"
fi
[ "$fail" = 0 ]