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