gunk-dev/the-valley

knot-agree.sh

source9b67d021e86fmain's published tip history
2.5 KiB
1# shellcheck shell=bash
2# The plan graph's two validators, held to agreeing: the Go one (knot/),
3# which is the authority for refs/the-valley/knots, and the CUE schema
4# with the knowledge lint, which read a graph kept in a project's tree.
5# Both are run over the same graphs, the shared fixtures and graphs
6# generated from a fixed seed, and the check fails on any graph where
7# either verdict is not the one the case states. Every rejected case names
8# the rule it breaks, and that rule must be one the two share.
9# Paths and fixtures come from the derivation environment.
10# shellcheck disable=SC2154
11set -euo pipefail
12
13w="$TMPDIR/knot-agree"
14mkdir -p "$w"
15
16fail() {
17 printf 'knot-agree: %s\n' "$*" >&2
18 exit 1
19}
20
21# The constants the lint restates must be the contract's.
22knotcheck contract > "$w/contract.json"
23cutoff="$(sed -n 's/.*"cutoff":"\([^"]*\)".*/\1/p' "$w/contract.json")"
24grep -qx "ID_RULE_CUTOFF = \"$cutoff\"" "$lint" \
25 || fail "the lint's ID_RULE_CUTOFF is not the contract's $cutoff"
26cue export -e '#Directories' "$nodeSchema" --out json | tr -d ' \n' > "$w/cue-dirs"
27sed -n 's/.*"directories":\({[^}]*}\).*/\1/p' "$w/contract.json" > "$w/go-dirs"
28python3 -c 'import json,sys; a,b=(json.load(open(p)) for p in sys.argv[1:]); sys.exit(a!=b)' \
29 "$w/cue-dirs" "$w/go-dirs" || fail "the type directories differ: $(cat "$w/cue-dirs") against $(cat "$w/go-dirs")"
30
31knotcheck rules > "$w/shared"
32knotcheck fixtures "$w/fixtures"
33knotcheck generate "$w/generated" 40 20261015
34
35checked=0
36for set in fixtures generated; do
37 while IFS=$'\t' read -r case verdict rule; do
38 dir="$w/$set/$case"
39 if [ "$verdict" = reject ] && ! grep -qx -- "$rule" "$w/shared"; then
40 fail "$case breaks $rule, which is no rule the validators share"
41 fi
42 by_go="accept"
43 knotcheck validate "$dir/graph" > "$dir/go.out" || by_go="reject"
44 by_lint="accept"
45 mkdir -p "$dir/work"
46 python3 "$lint" --tree "$dir/tree" --root knots --schema "$nodeSchema" \
47 --work "$dir/work" > "$dir/lint.out" 2>&1 || by_lint="reject"
48 if [ "$by_go" != "$verdict" ] || [ "$by_lint" != "$verdict" ]; then
49 echo "knot-agree: $set/$case should be $verdict${rule:+ ($rule)}; Go says $by_go, the lint says $by_lint" >&2
50 cat "$dir/go.out" "$dir/lint.out" >&2
51 find "$dir/graph" -type f -print -exec cat {} \; >&2
52 exit 1
53 fi
54 checked=$((checked + 1))
55 done < "$w/$set/cases.tsv"
56done
57[ "$checked" -gt 60 ] || fail "only $checked graphs were checked"
58echo "knot-agree: $checked graphs, the validators agree on each"
59touch "$out"