gunk-dev/the-valley

policy-deriver.sh

source37f58f5c0dcfmain's published tip history
11.6 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4export HOME="$TMPDIR"
5export GIT_CONFIG_NOSYSTEM=1
6git config --global user.name valley-check
7git config --global user.email valley-check@localhost
8git config --global init.defaultBranch main
9
10policy="$TMPDIR/policy"
11mkdir -p "$policy/instance" "$policy/project"
12cp "$fixtures/floor.cue" "$policy/instance/floor.cue"
13cp "$fixtures/template.cue" "$policy/instance/template.cue"
14cp "$fixtures/project.cue" "$policy/project/policy.cue"
15
16repo="$TMPDIR/tree"
17git init --quiet "$repo"
18cd "$repo" || exit 1
19mkdir -p docs notes src/deep a/x/y
20echo moved > docs/moved.md
21for f in docs/kept.md docs/gone.md src/one.rs src/deep/two.rs \
22 a/b a/x/y/b Makefile; do
23 echo seed > "$f"
24done
25git add -A
26git commit --quiet -m base
27base="$(git rev-parse HEAD)"
28
29derive() {
30 valley checks --schema "$schema" \
31 --instance "$policy/instance" --project "$policy/project" \
32 "$@" "$base" HEAD
33}
34
35# The check names in the deriver's table, sorted, on one line.
36named() {
37 awk '/^required checks:$/ {f = 1; next}
38 /^$/ {f = 0}
39 f && /^ / {print $1}' <<<"$report" | sort | paste -sd' ' -
40}
41
42# Start a case from the base tree; assert its check table.
43case_start() { git checkout --quiet -B "$1" "$base"; }
44expect() {
45 local got
46 got="$(named)"
47 if [ "$got" != "$1" ]; then
48 echo "policy-deriver: $2" >&2
49 echo " expected: $1" >&2
50 echo " got: $got" >&2
51 echo "$report" >&2
52 exit 1
53 fi
54}
55
56# `**` crosses segments, `*` does not. One file under src/
57# matches both patterns; one a segment deeper matches only
58# the `**` one.
59case_start flat
60echo edit > src/one.rs
61git commit --quiet -am flat
62report="$(derive)"
63expect "c-deep c-flat" "src/*.rs must match a file directly under src/"
64
65case_start deep
66echo edit > src/deep/two.rs
67git commit --quiet -am deep
68report="$(derive)"
69expect "c-deep" "src/*.rs must not match across a path separator"
70
71# A `**` between two segments matches no segment at all …
72case_start nested-none
73echo edit > a/b
74git commit --quiet -am nested-none
75report="$(derive)"
76expect "c-nested" "a/**/b must match a/b"
77
78# … and any number of them.
79case_start nested-many
80echo edit > a/x/y/b
81git commit --quiet -am nested-many
82report="$(derive)"
83expect "c-nested" "a/**/b must match a/x/y/b"
84
85# A rename counts on both sides: the class covering the old
86# path is required as much as the one covering the new.
87case_start renamed
88git mv docs/moved.md notes/moved.md
89git commit --quiet -m renamed
90git diff --name-status -M "$base" HEAD | grep -q '^R' \
91 || { echo "policy-deriver: the rename case is not a rename" >&2; exit 1; }
92report="$(derive)"
93expect "c-docs c-notes c-tmpl" "a rename must count on both sides"
94
95# A deleted path is still a changed path.
96case_start deleted
97git rm --quiet docs/gone.md
98git commit --quiet -m deleted
99report="$(derive)"
100expect "c-docs c-tmpl" "a deletion must count"
101
102# The form of the value is the mandatory bit: the floor's
103# concrete `true` against the template's `bool | *true`.
104grep -qE '^ c-docs +mandatory +docs$' <<<"$report"
105grep -qE '^ c-tmpl +default +docs$' <<<"$report"
106
107# A check's cost is a declared property and owes nothing. The same diff under a
108# project layer that marks c-docs slow owes the same checks, for the
109# same classes, with the same mandatory bits: every line above the
110# cost summary is the same. Only the summary moves c-docs.
111cp -R "$policy/project" "$policy/project-slow"
112cat > "$policy/project-slow/slow.cue" <<'EOF'
113package verification
114project: checks: "c-docs": {runner: "nix", attribute: "c-docs", cost: "slow"}
115EOF
116derive_slow() {
117 valley checks --schema "$schema" \
118 --instance "$policy/instance" --project "$policy/project-slow" \
119 "$@" "$base" HEAD
120}
121quick_report="$report"
122report="$(derive_slow)"
123expect "c-docs c-tmpl" "a slow check must be owed as a quick one is"
124above_summary() { sed -e '/^quick:/,$d' -e '/^policy:/d' <<<"$1"; }
125[ "$(above_summary "$quick_report")" = "$(above_summary "$report")" ] || {
126 echo "policy-deriver: marking a check slow changed what is owed" >&2
127 diff <(above_summary "$quick_report") <(above_summary "$report") >&2
128 exit 1
129}
130grep -qx 'quick: c-docs, c-tmpl' <<<"$quick_report"
131grep -qx 'slow: none' <<<"$quick_report"
132grep -qx 'quick: c-tmpl' <<<"$report"
133grep -qx 'slow: c-docs' <<<"$report"
134
135# The names alone are what an agent reads, so they are exactly the owed
136# names, one per line, and --cost filters them.
137[ "$(derive_slow --names)" = "$(printf 'c-docs\nc-tmpl')" ]
138[ "$(derive_slow --names --cost quick)" = c-tmpl ]
139[ "$(derive_slow --names --cost slow)" = c-docs ]
140[ "$(derive --names --cost slow)" = "" ]
141if derive_slow --names --cost fast 2> "$TMPDIR/cost.err"; then
142 echo "policy-deriver: --cost fast was accepted" >&2
143 exit 1
144fi
145grep -q "cost takes quick or slow, not 'fast'" "$TMPDIR/cost.err"
146if derive_slow --cost quick 2> /dev/null; then
147 echo "policy-deriver: --cost without --names was accepted" >&2
148 exit 1
149fi
150
151# A change that owes nothing prints no names and no cost summary.
152report="$(valley checks --schema "$schema" \
153 --instance "$policy/instance" --project "$policy/project-slow" "$base" "$base")"
154grep -qx 'required checks: none' <<<"$report"
155if grep -q '^quick:' <<<"$report"; then
156 echo "policy-deriver: a change owing nothing printed a cost summary" >&2
157 exit 1
158fi
159[ "$(valley checks --schema "$schema" --names \
160 --instance "$policy/instance" --project "$policy/project-slow" "$base" "$base")" = "" ]
161
162# A schema that predates `cost` cannot render the composed policy. The
163# deriver refuses rather than reading the empty render as a policy that
164# owes nothing.
165grep -v '^ cost: \*"quick" | "slow"$' "$schema" > "$TMPDIR/costless.cue"
166if cmp -s "$schema" "$TMPDIR/costless.cue"; then
167 echo "policy-deriver: the costless schema still declares cost, so this case proves nothing" >&2
168 exit 1
169fi
170if valley checks --schema "$TMPDIR/costless.cue" \
171 --instance "$policy/instance" --project "$policy/project" \
172 "$base" HEAD > "$TMPDIR/costless.out" 2>&1; then
173 echo "policy-deriver: a schema the render cannot read derived an answer" >&2
174 cat "$TMPDIR/costless.out" >&2
175 exit 1
176fi
177grep -q 'does not render' "$TMPDIR/costless.out"
178
179# A path no class covers takes the unclassified set, and says
180# so by name.
181case_start uncovered
182echo edit > Makefile
183git commit --quiet -am uncovered
184report="$(derive)"
185expect "c-uncovered" "an uncovered path must take the unclassified set"
186grep -q '^classes matched: unclassified$' <<<"$report"
187grep -q '^unclassified: 1 path(s) matched no class$' <<<"$report"
188grep -q '^ Makefile$' <<<"$report"
189
190# A replacement ref changes nothing the derivation reads. Here
191# refs/replace/<head> makes git read the uncovered case's head as
192# the flat case's, so a deriver that left replacement refs on
193# would derive the src/ checks for a change that touches only
194# the Makefile — and the caller asking for them is not listened
195# to either.
196git update-ref "refs/replace/$(git rev-parse uncovered)" "$(git rev-parse flat)"
197git diff --name-only "$base" uncovered | grep -x src/one.rs > /dev/null \
198 || { echo "policy-deriver: the replacement did not take, so this case proves nothing" >&2; exit 1; }
199git checkout --quiet uncovered
200report="$(GIT_CONFIG_PARAMETERS="'core.usereplacerefs'='true'" derive)"
201expect "c-uncovered" "a replacement ref must not change the derived checks"
202git update-ref -d "refs/replace/$(git rev-parse uncovered)"
203
204# ----------------------------------------------------------------------
205# Every producer fails closed. The deriver's calls of git, sort and cue
206# are failed one at a time, one per run, by fail-nth standing in for the
207# tool first on PATH. A producer read through a process substitution
208# loses its status, and its failure then reads as an empty list — no
209# paths, no catalogue, no classes — which derives a change that owes
210# nothing. So every failure must end the run non-zero, say so, and print
211# nothing on stdout: no report, and no names.
212#
213# This runs bin/valley itself, because the package's wrapper puts its own
214# git ahead of anything on PATH. The case touches a class, a second class
215# through `**`, the template's default, and an uncovered path, so every
216# producer has something to lose.
217case_start mixed
218echo edit > docs/kept.md
219echo edit > src/one.rs
220echo edit > Makefile
221git commit --quiet -am mixed
222
223shims="$TMPDIR/shims"
224mkdir -p "$shims"
225for tool in git sort cue; do ln -s "$failNth" "$shims/$tool"; done
226
227# shimmed TOOL N [option…]: derive the mixed case with TOOL's Nth call
228# failing (0 fails none). The count of TOOL's calls is left in
229# $TMPDIR/shim-count.
230shimmed() {
231 local tool="$1" at="$2" status=2
232 shift 2
233 [ "$tool" != git ] || status=128 # git's own status for a fatal error
234 echo 0 > "$TMPDIR/shim-count"
235 SHIM_REAL_PATH="$PATH" SHIM_TOOL="$tool" SHIM_FAIL_AT="$at" \
236 SHIM_STATUS="$status" SHIM_COUNT="$TMPDIR/shim-count" PATH="$shims:$PATH" \
237 bash "$cliScript" checks --schema "$schema" \
238 --instance "$policy/instance" --project "$policy/project" \
239 "$@" "$base" HEAD
240}
241
242# The check names a run's stdout owes, sorted, on one line.
243owed_in() {
244 if [ "$1" = names ]; then
245 sort "$2" | paste -sd' ' -
246 else
247 report="$(cat "$2")"
248 named
249 fi
250}
251
252shim_fail() {
253 echo "policy-deriver: $1" >&2
254 for f in shim.out shim.err; do
255 echo "--- $f" >&2
256 cat "$TMPDIR/$f" >&2
257 done
258 exit 1
259}
260
261for tool in git sort cue; do
262 for mode in report names; do
263 opts=()
264 [ "$mode" = report ] || opts=(--names)
265 shimmed "$tool" 0 "${opts[@]}" > "$TMPDIR/shim.want"
266 want="$(owed_in "$mode" "$TMPDIR/shim.want")"
267 [ "$want" = "c-deep c-docs c-flat c-tmpl c-uncovered" ] || {
268 echo "policy-deriver: the mixed case owes $want" >&2
269 exit 1
270 }
271 calls="$(cat "$TMPDIR/shim-count")"
272 [ "$calls" -gt 0 ] || {
273 echo "policy-deriver: the $mode run never called $tool, so failing it proves nothing" >&2
274 exit 1
275 }
276 for ((at = 1; at <= calls; at++)); do
277 what="$mode run, $tool call $at of $calls"
278 if shimmed "$tool" "$at" "${opts[@]}" > "$TMPDIR/shim.out" 2> "$TMPDIR/shim.err"; then
279 # The one failure a run may survive is a cue probe after the
280 # composition and the render: it asks whether a requirement is
281 # mandatory, and a probe that fails reads as mandatory, the strict
282 # answer. What is owed must not move.
283 [ "$tool" = cue ] && [ "$at" -gt 2 ] \
284 || shim_fail "the $what failed and the deriver still answered"
285 [ "$(owed_in "$mode" "$TMPDIR/shim.out")" = "$want" ] \
286 || shim_fail "the $what failed and changed what is owed"
287 continue
288 fi
289 [ ! -s "$TMPDIR/shim.out" ] \
290 || shim_fail "the $what failed and the deriver printed to stdout"
291 grep -q '^valley: ' "$TMPDIR/shim.err" \
292 || shim_fail "the $what failed and the deriver did not say so"
293 done
294 done
295done
296
297# A diff cut short mid-record is a failure too, even though git exited 0:
298# cut inside the first path, after one status, and inside the second
299# status, after one whole record. The last is the one a plain `while
300# read` ends on quietly, dropping the record it cut.
301for cut in 1 2; do
302 for opts in "" --names; do
303 # shellcheck disable=SC2086 # an empty $opts is no option at all
304 if SHIM_TRUNCATE="$cut" SHIM_REAL_PATH="$PATH" PATH="$shims:$PATH" \
305 bash "$cliScript" checks --schema "$schema" \
306 --instance "$policy/instance" --project "$policy/project" $opts \
307 "$base" HEAD > "$TMPDIR/shim.out" 2> "$TMPDIR/shim.err"; then
308 shim_fail "a diff cut after $cut field(s) derived an answer"
309 fi
310 [ ! -s "$TMPDIR/shim.out" ] \
311 || shim_fail "a diff cut after $cut field(s) printed to stdout"
312 grep -q 'truncated diff output' "$TMPDIR/shim.err" \
313 || shim_fail "a diff cut after $cut field(s) was not named as one"
314 done
315done
316
317touch "$out"