#!/bin/bash # self-test of src/tools/xref.sh --check: the checked-in baseline passes (exit 0); a copy of it # with one writer removed from one line fails (exit 1); and a scratch copy of src/compiler with one # new write to a global (namecount) fails and names that global. works in a scratch directory; the # tree, src/compiler, tests/xref.baseline and build/xref are not touched. # usage: src/tools/xref_selftest.sh [SERVER] (default build/bin/zclsp) set -u cd "$(dirname "$0")/../.." usage="usage: src/tools/xref_selftest.sh [SERVER] (default build/bin/zclsp)" case "${1:-}" in -h|--help) echo "$usage"; exit 0;; -*) echo "xref_selftest: unknown option $1; $usage" >&2; exit 2;; esac [ $# -le 1 ] || { echo "xref_selftest: too many arguments; $usage" >&2; exit 2; } server=${1:-build/bin/zclsp} tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT fail=0 bash src/tools/xref.sh --server "$server" --out "$tmp/out" --check > "$tmp/ok.log" 2>&1; r=$? if [ $r = 0 ]; then echo "ok xref --check passes against tests/xref.baseline" else echo "FAIL xref --check against tests/xref.baseline: exit $r"; cat "$tmp/ok.log"; fail=1; fi # drop the last writer of the first global that has one: --check must report it as a new writer line=$(awk 'NF>=2{print NR; exit}' tests/xref.baseline) sed "${line}s/ [^ ]*\$//" tests/xref.baseline > "$tmp/baseline" bash src/tools/xref.sh --server "$server" --out "$tmp/out" --baseline "$tmp/baseline" --check > "$tmp/bad.log" 2>&1; r=$? if [ $r = 1 ] && grep -q 'is now written by' "$tmp/bad.log"; then echo "ok xref --check fails when the baseline misses a writer" else echo "FAIL xref --check with a writer removed from the baseline: exit $r (want 1)"; cat "$tmp/bad.log"; fail=1; fi # a real new write in a copy of the compiler: a new function that assigns namecount g=namecount mkdir "$tmp/compiler"; cp src/compiler/* "$tmp/compiler/" last=$(tr -d '\r' < "$tmp/compiler/parts" | tail -1) printf '\nint xref_selftest_probe() { %s = %s + 1; return 0; }\n' "$g" "$g" >> "$tmp/compiler/$last" bash src/tools/xref.sh --server "$server" --out "$tmp/out" --compiler-dir "$tmp/compiler" --check > "$tmp/src.log" 2>&1; r=$? if [ $r = 1 ] && grep -q "^ $g is now written by xref_selftest_probe" "$tmp/src.log"; then echo "ok xref --check fails and names $g when a copy of the compiler writes it in a new function" else echo "FAIL xref --check with a new write to $g in a copy of the compiler: exit $r (want 1 naming $g)"; cat "$tmp/src.log"; fail=1; fi exit $fail