| 1 | # shellcheck shell=bash |
| 2 | # Paths and fixtures come from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | export HOME="$TMPDIR" |
| 5 | export GIT_CONFIG_NOSYSTEM=1 |
| 6 | git config --global user.name valley-check |
| 7 | git config --global user.email valley-check@localhost |
| 8 | git config --global init.defaultBranch main |
| 9 | |
| 10 | policy="$TMPDIR/policy" |
| 11 | mkdir -p "$policy/instance" "$policy/project" |
| 12 | cp "$fixtures/floor.cue" "$policy/instance/floor.cue" |
| 13 | cp "$fixtures/template.cue" "$policy/instance/template.cue" |
| 14 | cp "$fixtures/project.cue" "$policy/project/policy.cue" |
| 15 | |
| 16 | repo="$TMPDIR/tree" |
| 17 | git init --quiet "$repo" |
| 18 | cd "$repo" || exit 1 |
| 19 | mkdir -p docs notes src/deep a/x/y |
| 20 | echo moved > docs/moved.md |
| 21 | for 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" |
| 24 | done |
| 25 | git add -A |
| 26 | git commit --quiet -m base |
| 27 | base="$(git rev-parse HEAD)" |
| 28 | |
| 29 | derive() { |
| 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. |
| 36 | named() { |
| 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. |
| 43 | case_start() { git checkout --quiet -B "$1" "$base"; } |
| 44 | expect() { |
| 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. |
| 59 | case_start flat |
| 60 | echo edit > src/one.rs |
| 61 | git commit --quiet -am flat |
| 62 | report="$(derive)" |
| 63 | expect "c-deep c-flat" "src/*.rs must match a file directly under src/" |
| 64 | |
| 65 | case_start deep |
| 66 | echo edit > src/deep/two.rs |
| 67 | git commit --quiet -am deep |
| 68 | report="$(derive)" |
| 69 | expect "c-deep" "src/*.rs must not match across a path separator" |
| 70 | |
| 71 | # A `**` between two segments matches no segment at all … |
| 72 | case_start nested-none |
| 73 | echo edit > a/b |
| 74 | git commit --quiet -am nested-none |
| 75 | report="$(derive)" |
| 76 | expect "c-nested" "a/**/b must match a/b" |
| 77 | |
| 78 | # … and any number of them. |
| 79 | case_start nested-many |
| 80 | echo edit > a/x/y/b |
| 81 | git commit --quiet -am nested-many |
| 82 | report="$(derive)" |
| 83 | expect "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. |
| 87 | case_start renamed |
| 88 | git mv docs/moved.md notes/moved.md |
| 89 | git commit --quiet -m renamed |
| 90 | git diff --name-status -M "$base" HEAD | grep -q '^R' \ |
| 91 | || { echo "policy-deriver: the rename case is not a rename" >&2; exit 1; } |
| 92 | report="$(derive)" |
| 93 | expect "c-docs c-notes c-tmpl" "a rename must count on both sides" |
| 94 | |
| 95 | # A deleted path is still a changed path. |
| 96 | case_start deleted |
| 97 | git rm --quiet docs/gone.md |
| 98 | git commit --quiet -m deleted |
| 99 | report="$(derive)" |
| 100 | expect "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`. |
| 104 | grep -qE '^ c-docs +mandatory +docs$' <<<"$report" |
| 105 | grep -qE '^ c-tmpl +default +docs$' <<<"$report" |
| 106 | |
| 107 | # A check's cost is a declared property and changes nothing required. The |
| 108 | # same diff under a project layer that marks c-docs slow requires the same |
| 109 | # checks, for the same classes, with the same mandatory bits: every line above the |
| 110 | # cost summary is the same. Only the summary moves c-docs. |
| 111 | cp -R "$policy/project" "$policy/project-slow" |
| 112 | cat > "$policy/project-slow/slow.cue" <<'EOF' |
| 113 | package verification |
| 114 | project: checks: "c-docs": {runner: "nix", attribute: "c-docs", cost: "slow"} |
| 115 | EOF |
| 116 | derive_slow() { |
| 117 | valley checks --schema "$schema" \ |
| 118 | --instance "$policy/instance" --project "$policy/project-slow" \ |
| 119 | "$@" "$base" HEAD |
| 120 | } |
| 121 | quick_report="$report" |
| 122 | report="$(derive_slow)" |
| 123 | expect "c-docs c-tmpl" "a slow check must be required as a quick one is" |
| 124 | above_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 required" >&2 |
| 127 | diff <(above_summary "$quick_report") <(above_summary "$report") >&2 |
| 128 | exit 1 |
| 129 | } |
| 130 | grep -qx 'quick: c-docs, c-tmpl' <<<"$quick_report" |
| 131 | grep -qx 'slow: none' <<<"$quick_report" |
| 132 | grep -qx 'quick: c-tmpl' <<<"$report" |
| 133 | grep -qx 'slow: c-docs' <<<"$report" |
| 134 | |
| 135 | # The names alone are what an agent reads, so they are exactly the required |
| 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)" = "" ] |
| 141 | if derive_slow --names --cost fast 2> "$TMPDIR/cost.err"; then |
| 142 | echo "policy-deriver: --cost fast was accepted" >&2 |
| 143 | exit 1 |
| 144 | fi |
| 145 | grep -q "cost takes quick or slow, not 'fast'" "$TMPDIR/cost.err" |
| 146 | if derive_slow --cost quick 2> /dev/null; then |
| 147 | echo "policy-deriver: --cost without --names was accepted" >&2 |
| 148 | exit 1 |
| 149 | fi |
| 150 | |
| 151 | # A change that requires nothing prints no names and no cost summary. |
| 152 | report="$(valley checks --schema "$schema" \ |
| 153 | --instance "$policy/instance" --project "$policy/project-slow" "$base" "$base")" |
| 154 | grep -qx 'required checks: none' <<<"$report" |
| 155 | if grep -q '^quick:' <<<"$report"; then |
| 156 | echo "policy-deriver: a change that requires nothing printed a cost summary" >&2 |
| 157 | exit 1 |
| 158 | fi |
| 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 | # requires nothing. |
| 165 | grep -v '^ cost: \*"quick" | "slow"$' "$schema" > "$TMPDIR/costless.cue" |
| 166 | if 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 |
| 169 | fi |
| 170 | if 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 |
| 176 | fi |
| 177 | grep -q 'does not render' "$TMPDIR/costless.out" |
| 178 | |
| 179 | # A path no class covers takes the unclassified set, and says |
| 180 | # so by name. |
| 181 | case_start uncovered |
| 182 | echo edit > Makefile |
| 183 | git commit --quiet -am uncovered |
| 184 | report="$(derive)" |
| 185 | expect "c-uncovered" "an uncovered path must take the unclassified set" |
| 186 | grep -q '^classes matched: unclassified$' <<<"$report" |
| 187 | grep -q '^unclassified: 1 path(s) matched no class$' <<<"$report" |
| 188 | grep -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. |
| 196 | git update-ref "refs/replace/$(git rev-parse uncovered)" "$(git rev-parse flat)" |
| 197 | git 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; } |
| 199 | git checkout --quiet uncovered |
| 200 | report="$(GIT_CONFIG_PARAMETERS="'core.usereplacerefs'='true'" derive)" |
| 201 | expect "c-uncovered" "a replacement ref must not change the derived checks" |
| 202 | git 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 |
| 210 | # requires 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. |
| 217 | case_start mixed |
| 218 | echo edit > docs/kept.md |
| 219 | echo edit > src/one.rs |
| 220 | echo edit > Makefile |
| 221 | git commit --quiet -am mixed |
| 222 | |
| 223 | shims="$TMPDIR/shims" |
| 224 | mkdir -p "$shims" |
| 225 | for 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. |
| 230 | shimmed() { |
| 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 requires, sorted, on one line. |
| 243 | required_in() { |
| 244 | if [ "$1" = names ]; then |
| 245 | sort "$2" | paste -sd' ' - |
| 246 | else |
| 247 | report="$(cat "$2")" |
| 248 | named |
| 249 | fi |
| 250 | } |
| 251 | |
| 252 | shim_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 | |
| 261 | for 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="$(required_in "$mode" "$TMPDIR/shim.want")" |
| 267 | [ "$want" = "c-deep c-docs c-flat c-tmpl c-uncovered" ] || { |
| 268 | echo "policy-deriver: the mixed case requires $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 required must not move. |
| 283 | [ "$tool" = cue ] && [ "$at" -gt 2 ] \ |
| 284 | || shim_fail "the $what failed and the deriver still answered" |
| 285 | [ "$(required_in "$mode" "$TMPDIR/shim.out")" = "$want" ] \ |
| 286 | || shim_fail "the $what failed and changed what is required" |
| 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 |
| 295 | done |
| 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. |
| 301 | for 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 |
| 315 | done |
| 316 | |
| 317 | touch "$out" |