gunk-dev/the-valley

policy-deriver.sh

sourcebc00697b19b5main's published tip history
13.3 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# Assert the carry mode the report states. The floor gives flat
57# `same-class` and nested `exact`, and no other class a mode.
58expect_carry() {
59 local got
60 got="$(sed -n 's/^carry mode: //p' <<<"$report")"
61 if [ "$got" != "$1" ]; then
62 echo "policy-deriver: $2" >&2
63 echo " expected carry mode: $1" >&2
64 echo " got: $got" >&2
65 echo "$report" >&2
66 exit 1
67 fi
68}
69
70# `**` crosses segments, `*` does not. One file under src/
71# matches both patterns; one a segment deeper matches only
72# the `**` one.
73case_start flat
74echo edit > src/one.rs
75git commit --quiet -am flat
76report="$(derive)"
77expect "c-deep c-flat" "src/*.rs must match a file directly under src/"
78# Overlapping classes compose to the strictest mode: flat's same-class
79# over deep's same-paths.
80expect_carry same-class "a path in a same-class class and a same-paths one must be same-class"
81
82case_start deep
83echo edit > src/deep/two.rs
84git commit --quiet -am deep
85report="$(derive)"
86expect "c-deep" "src/*.rs must not match across a path separator"
87expect_carry same-paths "a class that asks for no mode must be same-paths"
88
89# A `**` between two segments matches no segment at all …
90case_start nested-none
91echo edit > a/b
92git commit --quiet -am nested-none
93report="$(derive)"
94expect "c-nested" "a/**/b must match a/b"
95expect_carry exact "a path in an exact class must be exact"
96
97# Paths in different classes compose the same way: exact over same-class.
98case_start strictest
99echo edit > a/b
100echo edit > src/one.rs
101git commit --quiet -am strictest
102report="$(derive)"
103expect "c-deep c-flat c-nested" "a change to two classes must require both"
104expect_carry exact "an exact path beside a same-class one must be exact"
105
106# … and any number of them.
107case_start nested-many
108echo edit > a/x/y/b
109git commit --quiet -am nested-many
110report="$(derive)"
111expect "c-nested" "a/**/b must match a/x/y/b"
112
113# A rename counts on both sides: the class covering the old
114# path is required as much as the one covering the new.
115case_start renamed
116git mv docs/moved.md notes/moved.md
117git commit --quiet -m renamed
118git diff --name-status -M "$base" HEAD | grep -q '^R' \
119 || { echo "policy-deriver: the rename case is not a rename" >&2; exit 1; }
120report="$(derive)"
121expect "c-docs c-notes c-tmpl" "a rename must count on both sides"
122
123# A deleted path is still a changed path.
124case_start deleted
125git rm --quiet docs/gone.md
126git commit --quiet -m deleted
127report="$(derive)"
128expect "c-docs c-tmpl" "a deletion must count"
129
130# The form of the value is the mandatory bit: the floor's
131# concrete `true` against the template's `bool | *true`.
132grep -qE '^ c-docs +mandatory +docs$' <<<"$report"
133grep -qE '^ c-tmpl +default +docs$' <<<"$report"
134
135# A check's cost is a declared property and changes nothing required. The
136# same diff under a project layer that marks c-docs slow requires the same
137# checks, for the same classes, with the same mandatory bits: every line above the
138# cost summary is the same. Only the summary moves c-docs.
139cp -R "$policy/project" "$policy/project-slow"
140cat > "$policy/project-slow/slow.cue" <<'EOF'
141package verification
142project: checks: "c-docs": {runner: "nix", attribute: "c-docs", cost: "slow"}
143EOF
144derive_slow() {
145 valley checks --schema "$schema" \
146 --instance "$policy/instance" --project "$policy/project-slow" \
147 "$@" "$base" HEAD
148}
149quick_report="$report"
150report="$(derive_slow)"
151expect "c-docs c-tmpl" "a slow check must be required as a quick one is"
152above_summary() { sed -e '/^quick:/,$d' -e '/^policy:/d' <<<"$1"; }
153[ "$(above_summary "$quick_report")" = "$(above_summary "$report")" ] || {
154 echo "policy-deriver: marking a check slow changed what is required" >&2
155 diff <(above_summary "$quick_report") <(above_summary "$report") >&2
156 exit 1
157}
158grep -qx 'quick: c-docs, c-tmpl' <<<"$quick_report"
159grep -qx 'slow: none' <<<"$quick_report"
160grep -qx 'quick: c-tmpl' <<<"$report"
161grep -qx 'slow: c-docs' <<<"$report"
162
163# The names alone are what an agent reads, so they are exactly the required
164# names, one per line, and --cost filters them.
165[ "$(derive_slow --names)" = "$(printf 'c-docs\nc-tmpl')" ]
166[ "$(derive_slow --names --cost quick)" = c-tmpl ]
167[ "$(derive_slow --names --cost slow)" = c-docs ]
168[ "$(derive --names --cost slow)" = "" ]
169if derive_slow --names --cost fast 2> "$TMPDIR/cost.err"; then
170 echo "policy-deriver: --cost fast was accepted" >&2
171 exit 1
172fi
173grep -q "cost takes quick or slow, not 'fast'" "$TMPDIR/cost.err"
174if derive_slow --cost quick 2> /dev/null; then
175 echo "policy-deriver: --cost without --names was accepted" >&2
176 exit 1
177fi
178
179# A change that requires nothing prints no names and no cost summary.
180report="$(valley checks --schema "$schema" \
181 --instance "$policy/instance" --project "$policy/project-slow" "$base" "$base")"
182grep -qx 'required checks: none' <<<"$report"
183expect_carry same-paths "a change that touches nothing must be same-paths"
184if grep -q '^quick:' <<<"$report"; then
185 echo "policy-deriver: a change that requires nothing printed a cost summary" >&2
186 exit 1
187fi
188[ "$(valley checks --schema "$schema" --names \
189 --instance "$policy/instance" --project "$policy/project-slow" "$base" "$base")" = "" ]
190
191# A schema that predates `cost` gives no check a cost to read. The deriver
192# refuses rather than reading a policy whose checks cost nothing.
193grep -v '^ cost: \*"quick" | "slow"$' "$schema" > "$TMPDIR/costless.cue"
194if cmp -s "$schema" "$TMPDIR/costless.cue"; then
195 echo "policy-deriver: the costless schema still declares cost, so this case proves nothing" >&2
196 exit 1
197fi
198if valley checks --schema "$TMPDIR/costless.cue" \
199 --instance "$policy/instance" --project "$policy/project" \
200 "$base" HEAD > "$TMPDIR/costless.out" 2>&1; then
201 echo "policy-deriver: a schema with no cost derived an answer" >&2
202 cat "$TMPDIR/costless.out" >&2
203 exit 1
204fi
205grep -q '^valley: ' "$TMPDIR/costless.out"
206
207# The same for a schema that predates the carry modes: with no resolved
208# mode to read, the deriver refuses rather than reading every class as
209# same-paths.
210grep -v '^_mode: ' "$schema" > "$TMPDIR/modeless.cue"
211if cmp -s "$schema" "$TMPDIR/modeless.cue"; then
212 echo "policy-deriver: the modeless schema still resolves modes, so this case proves nothing" >&2
213 exit 1
214fi
215if valley checks --schema "$TMPDIR/modeless.cue" \
216 --instance "$policy/instance" --project "$policy/project" \
217 "$base" HEAD > "$TMPDIR/modeless.out" 2>&1; then
218 echo "policy-deriver: a schema with no carry modes derived an answer" >&2
219 cat "$TMPDIR/modeless.out" >&2
220 exit 1
221fi
222grep -q '^valley: ' "$TMPDIR/modeless.out"
223
224# A document may refine `policy` directly, and a mode written there counts.
225# The floor gives the deep class no mode, and a project document that
226# refines `policy` makes it exact.
227cp -R "$policy/project" "$policy/project-refined"
228cat > "$policy/project-refined/refined.cue" <<'EOF'
229package verification
230policy: classes: deep: carry: exact: true
231EOF
232git checkout --quiet deep
233report="$(valley checks --schema "$schema" \
234 --instance "$policy/instance" --project "$policy/project-refined" "$base" HEAD)"
235expect "c-deep" "refining policy's carry must not change what deep requires"
236expect_carry exact "a mode written by refining policy must count"
237
238# A path no class covers takes the unclassified set, and says
239# so by name. It is same-paths.
240case_start uncovered
241echo edit > Makefile
242git commit --quiet -am uncovered
243report="$(derive)"
244expect "c-uncovered" "an uncovered path must take the unclassified set"
245grep -q '^classes matched: unclassified$' <<<"$report"
246grep -q '^unclassified: 1 path(s) matched no class$' <<<"$report"
247grep -q '^ Makefile$' <<<"$report"
248expect_carry same-paths "an unclassified path must be same-paths"
249
250# A replacement ref changes nothing the derivation reads. Here
251# refs/replace/<head> makes git read the uncovered case's head as
252# the flat case's, so a deriver that left replacement refs on
253# would derive the src/ checks for a change that touches only
254# the Makefile — and the caller asking for them is not listened
255# to either.
256git update-ref "refs/replace/$(git rev-parse uncovered)" "$(git rev-parse flat)"
257git diff --name-only "$base" uncovered | grep -x src/one.rs > /dev/null \
258 || { echo "policy-deriver: the replacement did not take, so this case proves nothing" >&2; exit 1; }
259git checkout --quiet uncovered
260report="$(GIT_CONFIG_PARAMETERS="'core.usereplacerefs'='true'" derive)"
261expect "c-uncovered" "a replacement ref must not change the derived checks"
262git update-ref -d "refs/replace/$(git rev-parse uncovered)"
263
264# ----------------------------------------------------------------------
265# Every producer fails closed. The derivation's calls of git and cue are
266# failed one at a time, one per run, by fail-nth standing in for the tool
267# first on PATH. A failure read as an empty list — no paths, no policy —
268# derives a change that requires nothing. So every failure must end the
269# run non-zero, say so, and print nothing on stdout: no report, and no
270# names.
271#
272# This runs the raw binary, because the package's wrapper puts its own git
273# ahead of anything on PATH. The case
274# touches a class, a second class through `**`, the template's default,
275# and an uncovered path, so every producer has something to lose.
276case_start mixed
277echo edit > docs/kept.md
278echo edit > src/one.rs
279echo edit > Makefile
280git commit --quiet -am mixed
281# An unclassified path beside a same-class one makes the change no looser.
282report="$(derive)"
283expect_carry same-class "an unclassified path must not loosen a same-class change"
284
285shims="$TMPDIR/shims"
286mkdir -p "$shims"
287for tool in git cue; do ln -s "$failNth" "$shims/$tool"; done
288
289# shimmed TOOL N [option…]: derive the mixed case with TOOL's Nth call
290# failing (0 fails none). The count of TOOL's calls is left in
291# $TMPDIR/shim-count.
292shimmed() {
293 local tool="$1" at="$2" status=2
294 shift 2
295 [ "$tool" != git ] || status=128 # git's own status for a fatal error
296 local head
297 head="$(git rev-parse HEAD)"
298 echo 0 > "$TMPDIR/shim-count"
299 SHIM_REAL_PATH="$PATH" SHIM_TOOL="$tool" SHIM_FAIL_AT="$at" \
300 SHIM_STATUS="$status" SHIM_COUNT="$TMPDIR/shim-count" PATH="$shims:$PATH" \
301 "$valleyUnwrapped" checks --schema "$schema" --instance "$policy/instance" \
302 --project "$policy/project" "$@" "$base" "$head"
303}
304
305# The check names a run's stdout requires, sorted, on one line.
306required_in() {
307 if [ "$1" = names ]; then
308 sort "$2" | paste -sd' ' -
309 else
310 report="$(cat "$2")"
311 named
312 fi
313}
314
315shim_fail() {
316 echo "policy-deriver: $1" >&2
317 for f in shim.out shim.err; do
318 echo "--- $f" >&2
319 cat "$TMPDIR/$f" >&2
320 done
321 exit 1
322}
323
324for tool in git cue; do
325 for mode in report names; do
326 opts=()
327 [ "$mode" = report ] || opts=(--names)
328 shimmed "$tool" 0 "${opts[@]}" > "$TMPDIR/shim.want"
329 want="$(required_in "$mode" "$TMPDIR/shim.want")"
330 [ "$want" = "c-deep c-docs c-flat c-tmpl c-uncovered" ] || {
331 echo "policy-deriver: the mixed case requires $want" >&2
332 exit 1
333 }
334 calls="$(cat "$TMPDIR/shim-count")"
335 [ "$calls" -gt 0 ] || {
336 echo "policy-deriver: the $mode run never called $tool, so failing it proves nothing" >&2
337 exit 1
338 }
339 for ((at = 1; at <= calls; at++)); do
340 what="$mode run, $tool call $at of $calls"
341 if shimmed "$tool" "$at" "${opts[@]}" > "$TMPDIR/shim.out" 2> "$TMPDIR/shim.err"; then
342 # The one failure a run may survive is a cue probe (composed/model.go):
343 # it asks whether a requirement is mandatory, and a probe that fails
344 # reads as mandatory, the strict answer. What is required must not
345 # move.
346 [ "$tool" = cue ] \
347 || shim_fail "the $what failed and the deriver still answered"
348 [ "$(required_in "$mode" "$TMPDIR/shim.out")" = "$want" ] \
349 || shim_fail "the $what failed and changed what is required"
350 continue
351 fi
352 [ ! -s "$TMPDIR/shim.out" ] \
353 || shim_fail "the $what failed and the deriver printed to stdout"
354 grep -q '^valley: ' "$TMPDIR/shim.err" \
355 || shim_fail "the $what failed and the deriver did not say so"
356 done
357 done
358done
359
360touch "$out"