gunk-dev/the-valley

policy-deriver.sh

source9ee0a5aa589dmain's published tip history
3.5 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 path no class covers takes the unclassified set, and says
108# so by name.
109case_start uncovered
110echo edit > Makefile
111git commit --quiet -am uncovered
112report="$(derive)"
113expect "c-uncovered" "an uncovered path must take the unclassified set"
114grep -q '^classes matched: unclassified$' <<<"$report"
115grep -q '^unclassified: 1 path(s) matched no class$' <<<"$report"
116grep -q '^ Makefile$' <<<"$report"
117
118touch "$out"