Nacre/tools/smoke.sh

111 lines
5.2 KiB
Shell
Executable file

#!/usr/bin/env bash
# Smoke run: start Nacre on a hidden X display (Xvfb) with a throwaway profile, play a NACRE_SCRIPT, collect results.
#
# Usage: tools/smoke.sh <script.txt> [outdir]
# outdir defaults to target/smoke/<script name>-<time>. It gets:
# nacre.log stdout + stderr of the browser (RUST_BACKTRACE=1)
# selftest.log every script line run, plus js results and warnings
# <name>.png `shot <name>` screenshots; <name>.json `status <name>` files
# home/ the throwaway HOME / XDG_* folders (profile, history, session)
# Exit 0 only if the browser exited by itself with status 0, no "panicked" is in nacre.log and the script
# wrote `status done` (so every script must end with `status done` then `quit`) and no `assert` failed.
#
# Environment:
# SMOKE_HOME=dir reuse this throwaway home (e.g. a previous run's outdir/home) to test session restore
# SMOKE_TIMEOUT=s kill the browser after s seconds (default 180)
# SMOKE_SIZE=WxH screen size (default 1280x800)
# SMOKE_BIN=path browser binary (default target/release/nacre)
# NACRE_NO_BRIDGE=1 passed through: no network bridge
# SMOKE_VISIBLE=1 show the window on the user's screen (their Wayland display, niri monitor
# SMOKE_MONITOR, default DP-1) with the real GPU instead of hidden Xvfb + software GL;
# only the Wayland socket is shared, profile/runtime dir/bus stay throwaway
#
# Script commands (src/selftest.rs has the full list): wait <s>, waitload [s], open <url>, newtab <url>,
# page <nacre://...>, shot <name>, status <name>, set <json pointer> <json>, flyout <name>, do <action>,
# cmd <name> [arg], js <script>, assert <js>, click|rclick <x> <y>, pagetype <text>, log <text>, quit. Lines starting with # are notes.
# In the script, @PAGES@ is replaced by the file:// URL of tests/smoke/pages, @PAGESDIR@ by its path, @HOME@ by the
# throwaway home.
#
# Never uses the user's profile, display, Discord socket or session bus; never sets a proxy.
set -u
root=$(cd "$(dirname "$0")/.." && pwd)
script=${1:?usage: tools/smoke.sh <script.txt> [outdir]}
[ -f "$script" ] || { echo "smoke: no such script $script" >&2; exit 2; }
out=${2:-$root/target/smoke/$(basename "$script" .txt)-$(date +%Y%m%d-%H%M%S)}
bin=${SMOKE_BIN:-$root/target/release/nacre}
[ -x "$bin" ] || { echo "smoke: no binary at $bin (build first)" >&2; exit 2; }
visible=${SMOKE_VISIBLE:-}
if [ -n "$visible" ]; then
[ -n "${WAYLAND_DISPLAY:-}" ] && [ -S "${XDG_RUNTIME_DIR:-}/$WAYLAND_DISPLAY" ] \
|| { echo "smoke: SMOKE_VISIBLE needs the user's Wayland session" >&2; exit 2; }
else
command -v Xvfb >/dev/null || { echo "smoke: Xvfb is missing (pacman -S xorg-server-xvfb)" >&2; exit 2; }
fi
mkdir -p "$out"
out=$(cd "$out" && pwd)
home=${SMOKE_HOME:-$out/home}
mkdir -p "$home"
home=$(cd "$home" && pwd)
rm -f "$out"/*.png "$out"/*.json "$out/selftest.log" "$out/nacre.log"
sed -e "s|@PAGES@|file://$root/tests/smoke/pages|g" -e "s|@PAGESDIR@|$root/tests/smoke/pages|g" -e "s|@HOME@|$home|g" "$script" >"$out/script.txt"
runtime=$(mktemp -d /tmp/nacre-smoke-XXXXXX)
chmod 700 "$runtime"
xvfb_pid=
cleanup() {
[ -n "$xvfb_pid" ] && kill "$xvfb_pid" 2>/dev/null
rm -rf "$runtime"
}
trap cleanup EXIT
if [ -n "$visible" ]; then
# Share only the compositor socket; open the window on the chosen monitor.
ln -s "$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY" "$runtime/wayland-0"
command -v niri >/dev/null && niri msg action focus-monitor "${SMOKE_MONITOR:-DP-1}" >/dev/null 2>&1
dispvars=(WAYLAND_DISPLAY=wayland-0 XDG_SESSION_TYPE=wayland)
else
# Xvfb picks a free display number and writes it to fd 3.
exec 3<>"$runtime/display"
Xvfb -displayfd 3 -screen 0 "${SMOKE_SIZE:-1280x800}x24" -nolisten tcp -noreset >"$out/xvfb.log" 2>&1 &
xvfb_pid=$!
for _ in $(seq 50); do
display=$(head -n1 "$runtime/display" 2>/dev/null)
[ -n "$display" ] && break
sleep 0.1
done
[ -n "${display:-}" ] || { echo "smoke: Xvfb did not start (see $out/xvfb.log)" >&2; exit 1; }
dispvars=(DISPLAY=":$display" XDG_SESSION_TYPE=x11 LIBGL_ALWAYS_SOFTWARE=1)
fi
env -u WAYLAND_DISPLAY -u DISPLAY -u DBUS_SESSION_BUS_ADDRESS -u NACRE_HOME \
-u http_proxy -u https_proxy -u all_proxy -u HTTP_PROXY -u HTTPS_PROXY -u ALL_PROXY \
"${dispvars[@]}" \
HOME="$home" XDG_CONFIG_HOME="$home/.config" XDG_DATA_HOME="$home/.local/share" \
XDG_CACHE_HOME="$home/.cache" XDG_STATE_HOME="$home/.local/state" XDG_RUNTIME_DIR="$runtime" \
RUST_BACKTRACE=1 NACRE_SCRIPT="$out/script.txt" \
timeout -k 10 "${SMOKE_TIMEOUT:-180}" "$bin" >"$out/nacre.log" 2>&1
code=$?
fail=0
if [ "$code" -ne 0 ]; then
echo "smoke: browser exited with $code$([ "$code" -eq 124 ] && echo ' (timeout)')"
fail=1
fi
if grep -q panicked "$out/nacre.log"; then
echo "smoke: panic in nacre.log:"
grep -m3 -A2 panicked "$out/nacre.log"
fail=1
fi
if grep -q "ASSERT FAILED" "$out/selftest.log" 2>/dev/null; then
echo "smoke: failed asserts:"
grep "ASSERT FAILED" "$out/selftest.log"
fail=1
fi
if [ ! -f "$out/done.json" ]; then
echo "smoke: script did not reach 'status done'"
fail=1
fi
echo "smoke: $([ $fail -eq 0 ] && echo ok || echo FAILED), results in $out"
exit $fail