gunk-dev/the-valley

policy-deriver.sh

source0a94c1215767main's published tip history
7.4 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 advice and nothing more. 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 '/^run before pushing:/,$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 'run before pushing: c-docs, c-tmpl' <<<"$quick_report"
131grep -qx "left to the operator's \[a\]sk: none" <<<"$quick_report"
132grep -qx 'run before pushing: c-tmpl' <<<"$report"
133grep -qx "left to the operator's \[a\]sk: 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 '^run before pushing:' <<<"$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
204touch "$out"