| 1 | # shellcheck shell=bash |
| 2 | # valley review with no change, end to end and unmocked, over the |
| 3 | # projects one test host serves: the walk through what needs the operator, |
| 4 | # across projects, in landing order, through the packaged CLI. The host is |
| 5 | # made by the integrator host's rendered valley-init, as valley-changes |
| 6 | # makes it, so discovery reads the project list a host publishes and every |
| 7 | # record and request is pushed through the hook that host installs. |
| 8 | # Paths and fixtures come from the derivation environment. |
| 9 | # shellcheck disable=SC2154 |
| 10 | set -euo pipefail |
| 11 | |
| 12 | w="$TMPDIR/valley-walk" |
| 13 | mkdir -p "$w" |
| 14 | export HOME="$w" XDG_CACHE_HOME="$w/cache" XDG_STATE_HOME="$w/state" TERM=xterm |
| 15 | export GIT_CONFIG_NOSYSTEM=1 |
| 16 | # Nobody is anybody by default: a commit made where no identity is set |
| 17 | # fails, so a review in a cache clone commits only with the identity the |
| 18 | # walk lends it from the checkout. |
| 19 | git config --global user.useConfigOnly true |
| 20 | git config --global init.defaultBranch main |
| 21 | git config --global advice.detachedHead false |
| 22 | unset VALLEY_KNOWN_SIGNERS VALLEY_INTEGRATOR_SIGNER VISUAL || true |
| 23 | |
| 24 | fail() { |
| 25 | printf 'valley-walk: %s\n' "$*" >&2 |
| 26 | for f in "$w"/out "$w"/err; do |
| 27 | if [ -s "$f" ]; then cat "$f" >&2; fi |
| 28 | done |
| 29 | exit 1 |
| 30 | } |
| 31 | |
| 32 | ok() { |
| 33 | "$@" > "$w/out" 2> "$w/err" || fail "$* failed" |
| 34 | } |
| 35 | |
| 36 | line() { |
| 37 | grep -qE -- "$1" "$w/out" || fail "no line matches $1" |
| 38 | } |
| 39 | |
| 40 | no_line() { |
| 41 | if grep -qE -- "$1" "$w/out"; then fail "a line matches $1"; fi |
| 42 | } |
| 43 | |
| 44 | # before <ERE> <ERE>: the first line matching the first comes before the |
| 45 | # first line matching the second. |
| 46 | before() { |
| 47 | local a b |
| 48 | a="$(grep -nE -m 1 -- "$1" "$w/out" | cut -d: -f1 || true)" |
| 49 | b="$(grep -nE -m 1 -- "$2" "$w/out" | cut -d: -f1 || true)" |
| 50 | [ -n "$a" ] && [ -n "$b" ] && [ "$a" -lt "$b" ] || fail "$1 does not come before $2" |
| 51 | } |
| 52 | |
| 53 | # walk <status> <answers> [<flag>...]: valley review with no change, from |
| 54 | # the open checkout, answering from a pipe; it exits with <status>. |
| 55 | walk() { |
| 56 | local want="$1" answers="$2" status=0 |
| 57 | shift 2 |
| 58 | printf '%b' "$answers" | valley review "$@" > "$w/out" 2> "$w/err" || status=$? |
| 59 | [ "$status" = "$want" ] || fail "the walk exited $status, not $want" |
| 60 | } |
| 61 | |
| 62 | # wait_for <file> <ERE>: wait until the file shows a line matching it. The |
| 63 | # walk's output is a file a child writes, which a shell has no event for, |
| 64 | # so it is read again every tenth of a second, for a minute at most. |
| 65 | wait_for() { |
| 66 | local i |
| 67 | for i in $(seq 600); do |
| 68 | if grep -qE -- "$2" "$1" 2> /dev/null; then return 0; fi |
| 69 | sleep 0.1 |
| 70 | done |
| 71 | fail "$1 never showed $2" |
| 72 | } |
| 73 | |
| 74 | # The host: guarded, open, released and sealed, with sealed the instance |
| 75 | # repository, as valley-changes makes it. sealed's main carries the floor |
| 76 | # approving derives the policy from. |
| 77 | data="$w/srv/git" |
| 78 | mkdir -p "$data" |
| 79 | sed "s|/srv/git|$data|g" "$initScriptPath" > "$w/init.sh" |
| 80 | bash "$w/init.sh" 2> "$w/init.err" || fail "valley-init failed: $(cat "$w/init.err")" |
| 81 | instance="$data/sealed.git" |
| 82 | for name in guarded open released sealed; do |
| 83 | git -C "$data/$name.git" config --unset core.sharedRepository || true |
| 84 | done |
| 85 | git init --quiet "$w/floor" |
| 86 | mkdir -p "$w/floor/policy/instance" |
| 87 | cp "$floor"/*.cue "$w/floor/policy/instance/" |
| 88 | git -C "$w/floor" add policy |
| 89 | git -C "$w/floor" -c user.name=integrator -c user.email=integrator@localhost commit --quiet -m floor |
| 90 | git -C "$w/floor" push --quiet "$instance" HEAD:refs/heads/floor |
| 91 | git -C "$instance" update-ref refs/heads/main "$(git -C "$w/floor" rev-parse HEAD)" |
| 92 | |
| 93 | id() { printf '%s%s' "$1" "$(printf 'k%.0s' $(seq $((32 - ${#1}))))"; } |
| 94 | G1="$(id kmkm)" G2="$(id knkn)" O1="$(id popo)" S1="$(id sqsq)" S2="$(id srsr)" |
| 95 | M1="$(id mlml)" M2="$(id mnmn)" M3="$(id mpmp)" |
| 96 | |
| 97 | # A checkout of a project whose main names sealed as its owning valley, |
| 98 | # with alice's identity in its own configuration alone. |
| 99 | export VALLEY_PRINCIPAL=alice |
| 100 | checkout() { |
| 101 | local dir="$w/$1" |
| 102 | git clone --quiet "$data/$1.git" "$dir" 2> /dev/null |
| 103 | git -C "$dir" config valley.principal alice |
| 104 | git -C "$dir" config user.name alice |
| 105 | git -C "$dir" config user.email alice@example.org |
| 106 | mkdir -p "$dir/policy" |
| 107 | printf '%s\n' "$instance" > "$dir/policy/valley" |
| 108 | git -C "$dir" add policy/valley |
| 109 | git -C "$dir" commit --quiet -m base |
| 110 | VALLEY_PRINCIPAL=integrator git -C "$dir" push --quiet origin main |
| 111 | } |
| 112 | |
| 113 | # branch <name> <from> <subject…>: a branch with a commit per subject, |
| 114 | # pushed to origin. |
| 115 | branch() { |
| 116 | local name="$1" from="$2" s |
| 117 | shift 2 |
| 118 | git checkout --quiet -b "$name" "$from" |
| 119 | for s in "$@"; do |
| 120 | echo "$s" > "$s.txt" |
| 121 | git add "$s.txt" |
| 122 | git commit --quiet -m "$s" |
| 123 | done |
| 124 | git push --quiet origin "$name" |
| 125 | } |
| 126 | |
| 127 | # ready <id> <branch>: the branch readied as the change, each a second |
| 128 | # after the last, so the landing order is the order they are readied in. |
| 129 | at=1700000000 |
| 130 | ready() { |
| 131 | at=$((at + 1)) |
| 132 | GIT_COMMITTER_DATE="@$at +0000" ok valley ready --change "$1" "$2" |
| 133 | } |
| 134 | |
| 135 | checkout open |
| 136 | checkout guarded |
| 137 | cd "$w/guarded" |
| 138 | branch g-one main g-one |
| 139 | ready "$G1" g-one |
| 140 | branch g-two main g-two |
| 141 | ready "$G2" g-two |
| 142 | cd "$w/open" |
| 143 | branch o-one main o-one |
| 144 | ready "$O1" o-one |
| 145 | |
| 146 | # ---------------------------------------------------------------------- |
| 147 | # Three ready changes in two projects, guarded's read through the walk's |
| 148 | # cache clone: the first approved and the second rejected there, the |
| 149 | # third skipped. Each step is headed by its place, project, change and |
| 150 | # checks, and the summary names every outcome, then what still needs the |
| 151 | # operator: the rejected change's record is still ready. |
| 152 | walk 0 'a\nr\nyes\ns\n' |
| 153 | line '^needs you \(3, in landing order\)$' |
| 154 | before '^\[1/3\] guarded · kmkm v1 · 1 check pending$' '^\[2/3\] guarded · knkn v1 · 1 check pending$' |
| 155 | before '^\[2/3\] guarded · knkn v1' '^\[3/3\] open · popo v1 · 1 check pending$' |
| 156 | line 'rejected g-two: deleted from origin$' |
| 157 | line '^ ✓ guarded kmkm v1 approved$' |
| 158 | line '^ ✓ guarded knkn v1 rejected · branch deleted$' |
| 159 | line '^ ◇ open popo v1 skipped$' |
| 160 | line '^still needs you \(2\): guarded knkn, open popo$' |
| 161 | no_line 'nothing needs you' |
| 162 | [ "$(git -C "$data/guarded.git" rev-parse "refs/the-valley/integration-requests/main/c-$G1")" = \ |
| 163 | "$(git -C "$w/guarded" rev-parse g-one)" ] || fail "the approval filed no request at kmkm's head" |
| 164 | if git -C "$data/guarded.git" rev-parse --verify --quiet refs/heads/g-two > /dev/null; then |
| 165 | fail "the rejection left g-two on origin" |
| 166 | fi |
| 167 | clone="$(echo "$XDG_CACHE_HOME"/valley/repos/*-guarded)" |
| 168 | [ -d "$clone" ] || fail "guarded was read through no cache clone" |
| 169 | for key in valley.principal user.name user.email; do |
| 170 | if git -C "$clone" config --local --get "$key" > /dev/null; then fail "the walk wrote $key into the cache clone"; fi |
| 171 | done |
| 172 | |
| 173 | # ---------------------------------------------------------------------- |
| 174 | # A stale stack in guarded, based from the walk with [b]ase onto main: the |
| 175 | # rebase commits and writes records in the cache clone, as alice, with the |
| 176 | # identity the walk lent it. The rejected change before it is not |
| 177 | # reviewed, since its branch is gone, and the walk goes on. |
| 178 | cd "$w/guarded" |
| 179 | git checkout --quiet main |
| 180 | branch s-one main s-one |
| 181 | ready "$S1" s-one |
| 182 | branch s-two s-one s-two |
| 183 | ready "$S2" s-two |
| 184 | git checkout --quiet main |
| 185 | echo moved > moved.txt |
| 186 | git add moved.txt |
| 187 | git commit --quiet -m "main moves" |
| 188 | VALLEY_PRINCIPAL=integrator git push --quiet origin main |
| 189 | cd "$w/open" |
| 190 | walk 0 'b\nn\ns\n' --repo guarded |
| 191 | line '^needs you \(2, in landing order\)$' |
| 192 | line '^\[2/2\] guarded · srsr v1 · stack of 2 · 1 check pending$' |
| 193 | line '^knkn is not reviewed: its branch is gone from origin$' |
| 194 | line '^ ⊘ guarded knkn v1 not reviewed · its branch is gone from origin$' |
| 195 | line '^ ◇ guarded srsr v1 skipped$' |
| 196 | tip="$(git -C "$data/guarded.git" rev-parse main)" |
| 197 | for b in s-one s-two; do |
| 198 | git -C "$data/guarded.git" merge-base --is-ancestor "$tip" "$b" || fail "the base left $b off main" |
| 199 | done |
| 200 | cd "$w/guarded" |
| 201 | ok valley-go resolve "$S2" |
| 202 | [ "$(cut -f4 "$w/out")" = 2 ] || fail "the base wrote no second version of srsr: $(cat "$w/out")" |
| 203 | |
| 204 | # ---------------------------------------------------------------------- |
| 205 | # Changes that move after the walk read its queue: while popo's prompt |
| 206 | # waits, mlml is unreadied and mnmn readied at a second version. mlml is |
| 207 | # not reviewed, and mnmn is said to have moved, then reviewed and approved |
| 208 | # at its new version. |
| 209 | cd "$w/open" |
| 210 | branch m-one main m-one |
| 211 | ready "$M1" m-one |
| 212 | branch m-two main m-two |
| 213 | ready "$M2" m-two |
| 214 | branch m-three main m-three |
| 215 | ready "$M3" m-three |
| 216 | rm -f "$w/fifo" |
| 217 | mkfifo "$w/fifo" |
| 218 | valley review --repo open < "$w/fifo" > "$w/walk.out" 2> "$w/walk.err" & |
| 219 | walker=$! |
| 220 | exec 3> "$w/fifo" |
| 221 | wait_for "$w/walk.out" '\[s\]kip' |
| 222 | ok valley unready "$M1" |
| 223 | git checkout --quiet m-two |
| 224 | echo again > m-two.txt |
| 225 | git commit --quiet -am "m-two again" |
| 226 | git push --quiet origin m-two |
| 227 | ready "$M2" m-two |
| 228 | printf 's\n' >&3 |
| 229 | wait_for "$w/walk.out" 'moved to v2' |
| 230 | printf 'a\ns\n' >&3 |
| 231 | exec 3>&- |
| 232 | status=0 |
| 233 | wait "$walker" || status=$? |
| 234 | mv "$w/walk.out" "$w/out" |
| 235 | mv "$w/walk.err" "$w/err" |
| 236 | [ "$status" = 0 ] || fail "the walk with moving changes exited $status" |
| 237 | line '^\[2/4\] open · mlml v1' |
| 238 | line '^mlml is not reviewed: it is a draft$' |
| 239 | line '^mnmn moved to v2 since the queue was read$' |
| 240 | line '^ ⊘ open mlml v1 not reviewed · it is a draft$' |
| 241 | line '^ ✓ open mnmn v1 approved v2$' |
| 242 | line '^ ◇ open mpmp v1 skipped$' |
| 243 | [ "$(git -C "$data/open.git" rev-parse "refs/the-valley/integration-requests/main/c-$M2")" = "$(git rev-parse m-two)" ] || |
| 244 | fail "mnmn was not approved at its second version" |
| 245 | |
| 246 | # ---------------------------------------------------------------------- |
| 247 | # Ctrl-C at a step's prompt, on a terminal: the step is interrupted, the |
| 248 | # summary so far is printed, and the walk exits 130. The diff is paged |
| 249 | # first, by a less that quits since it fits on one screen. |
| 250 | git config pager.diff 'less -FX' |
| 251 | status=0 |
| 252 | # On a terminal the prompt's keys are styled, so its plain tail is waited |
| 253 | # on. A walk that never gets there is ended rather than left waiting. |
| 254 | { |
| 255 | wait_for "$w/session.log" '\]kip: ' |
| 256 | printf '\003' |
| 257 | wait_for "$w/session.log" 'walked' |
| 258 | } | timeout 300 script -qec "valley review --repo open" /dev/null > "$w/session.log" 2>&1 || status=$? |
| 259 | sed -e 's/\x1b\[[0-9;?]*[a-zA-Z]//g' -e 's/\r//g' "$w/session.log" > "$w/out" |
| 260 | [ "$status" = 130 ] || fail "the interrupted walk exited $status, not 130: $(cat "$w/out")" |
| 261 | line '^walked \(1 of 2\)$' |
| 262 | line '^ ◇ open popo v1 interrupted$' |
| 263 | no_line 'still needs you|nothing needs you' |
| 264 | git config --unset pager.diff |
| 265 | |
| 266 | # ---------------------------------------------------------------------- |
| 267 | # A project that does not read is said, and nothing says that nothing |
| 268 | # needs the operator. An empty queue says that nothing does, with the |
| 269 | # counts pending gives, and exits 0. |
| 270 | mv "$data/released.git" "$data/released.moved" |
| 271 | walk 1 '' --repo released |
| 272 | line '^✗ released did not read: ' |
| 273 | no_line 'nothing needs you' |
| 274 | mv "$data/released.moved" "$data/released.git" |
| 275 | ok valley withdraw "$O1" |
| 276 | ok valley withdraw "$M3" |
| 277 | walk 0 '' --repo open |
| 278 | line '^nothing needs you · 1 moving · 1 draft' |
| 279 | |
| 280 | touch "$out" |