gunk-dev/the-valley

valley-walk.sh

source8c40ee5f164cmain's published tip history
10.6 KiB
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
10set -euo pipefail
11
12w="$TMPDIR/valley-walk"
13mkdir -p "$w"
14export HOME="$w" XDG_CACHE_HOME="$w/cache" XDG_STATE_HOME="$w/state" TERM=xterm
15export 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.
19git config --global user.useConfigOnly true
20git config --global init.defaultBranch main
21git config --global advice.detachedHead false
22unset VALLEY_KNOWN_SIGNERS VALLEY_INTEGRATOR_SIGNER VISUAL || true
23
24fail() {
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
32ok() {
33 "$@" > "$w/out" 2> "$w/err" || fail "$* failed"
34}
35
36line() {
37 grep -qE -- "$1" "$w/out" || fail "no line matches $1"
38}
39
40no_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.
46before() {
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>.
55walk() {
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.
65wait_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.
77data="$w/srv/git"
78mkdir -p "$data"
79sed "s|/srv/git|$data|g" "$initScriptPath" > "$w/init.sh"
80bash "$w/init.sh" 2> "$w/init.err" || fail "valley-init failed: $(cat "$w/init.err")"
81instance="$data/sealed.git"
82for name in guarded open released sealed; do
83 git -C "$data/$name.git" config --unset core.sharedRepository || true
84done
85git init --quiet "$w/floor"
86mkdir -p "$w/floor/policy/instance"
87cp "$floor"/*.cue "$w/floor/policy/instance/"
88git -C "$w/floor" add policy
89git -C "$w/floor" -c user.name=integrator -c user.email=integrator@localhost commit --quiet -m floor
90git -C "$w/floor" push --quiet "$instance" HEAD:refs/heads/floor
91git -C "$instance" update-ref refs/heads/main "$(git -C "$w/floor" rev-parse HEAD)"
92
93id() { printf '%s%s' "$1" "$(printf 'k%.0s' $(seq $((32 - ${#1}))))"; }
94G1="$(id kmkm)" G2="$(id knkn)" O1="$(id popo)" S1="$(id sqsq)" S2="$(id srsr)"
95M1="$(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.
99export VALLEY_PRINCIPAL=alice
100checkout() {
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.
115branch() {
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.
129at=1700000000
130ready() {
131 at=$((at + 1))
132 GIT_COMMITTER_DATE="@$at +0000" ok valley ready --change "$1" "$2"
133}
134
135checkout open
136checkout guarded
137cd "$w/guarded"
138branch g-one main g-one
139ready "$G1" g-one
140branch g-two main g-two
141ready "$G2" g-two
142cd "$w/open"
143branch o-one main o-one
144ready "$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 is withdrawn, and pending lists it no more.
152walk 0 'a\nr\nyes\ns\n'
153line '^needs you \(3, in landing order\)$'
154before '^\[1/3\] guarded · kmkm v1 · 1 check pending$' '^\[2/3\] guarded · knkn v1 · 1 check pending$'
155before '^\[2/3\] guarded · knkn v1' '^\[3/3\] open · popo v1 · 1 check pending$'
156line 'rejected g-two: deleted from origin · knkn withdrawn$'
157line '^ ✓ guarded kmkm v1 approved$'
158line '^ ✓ guarded knkn v1 rejected · branch deleted$'
159line '^ ◇ open popo v1 skipped$'
160line '^still needs you \(1\): open popo$'
161no_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"
164if 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"
166fi
167ok valley pending --all
168no_line knkn
169clone="$(echo "$XDG_CACHE_HOME"/valley/repos/*-guarded)"
170[ -d "$clone" ] || fail "guarded was read through no cache clone"
171for key in valley.principal user.name user.email; do
172 if git -C "$clone" config --local --get "$key" > /dev/null; then fail "the walk wrote $key into the cache clone"; fi
173done
174
175# ----------------------------------------------------------------------
176# A stale stack in guarded, based from the walk with [b]ase onto main: the
177# rebase commits and writes records in the cache clone, as alice, with the
178# identity the walk lent it.
179cd "$w/guarded"
180git checkout --quiet main
181branch s-one main s-one
182ready "$S1" s-one
183branch s-two s-one s-two
184ready "$S2" s-two
185git checkout --quiet main
186echo moved > moved.txt
187git add moved.txt
188git commit --quiet -m "main moves"
189VALLEY_PRINCIPAL=integrator git push --quiet origin main
190cd "$w/open"
191walk 0 'b\nn\ns\n' --repo guarded
192line '^needs you \(1, in landing order\)$'
193line '^\[1/1\] guarded · srsr v1 · stack of 2 · 1 check pending$'
194line '^ ◇ guarded srsr v1 skipped$'
195tip="$(git -C "$data/guarded.git" rev-parse main)"
196for b in s-one s-two; do
197 git -C "$data/guarded.git" merge-base --is-ancestor "$tip" "$b" || fail "the base left $b off main"
198done
199cd "$w/guarded"
200ok valley-go resolve "$S2"
201[ "$(cut -f4 "$w/out")" = 2 ] || fail "the base wrote no second version of srsr: $(cat "$w/out")"
202
203# ----------------------------------------------------------------------
204# Changes that move after the walk read its queue: while popo's prompt
205# waits, mlml is unreadied, mnmn readied at a second version, and mpmp's
206# branch deleted. mlml and mpmp are not reviewed, and mnmn is said to have
207# moved, then reviewed and approved at its new version.
208cd "$w/open"
209branch m-one main m-one
210ready "$M1" m-one
211branch m-two main m-two
212ready "$M2" m-two
213branch m-three main m-three
214ready "$M3" m-three
215rm -f "$w/fifo"
216mkfifo "$w/fifo"
217valley review --repo open < "$w/fifo" > "$w/walk.out" 2> "$w/walk.err" &
218walker=$!
219exec 3> "$w/fifo"
220wait_for "$w/walk.out" '\[s\]kip'
221ok valley unready "$M1"
222git checkout --quiet m-two
223echo again > m-two.txt
224git commit --quiet -am "m-two again"
225git push --quiet origin m-two
226ready "$M2" m-two
227git push --quiet origin --delete m-three
228printf 's\n' >&3
229wait_for "$w/walk.out" 'moved to v2'
230printf 'a\n' >&3
231exec 3>&-
232status=0
233wait "$walker" || status=$?
234mv "$w/walk.out" "$w/out"
235mv "$w/walk.err" "$w/err"
236[ "$status" = 0 ] || fail "the walk with moving changes exited $status"
237line '^\[2/4\] open · mlml v1'
238line '^mlml is not reviewed: it is a draft$'
239line '^mnmn moved to v2 since the queue was read$'
240line '^ ⊘ open mlml v1 not reviewed · it is a draft$'
241line '^ ✓ open mnmn v1 approved v2$'
242line '^mpmp is not reviewed: its branch is gone from origin$'
243line '^ ⊘ open mpmp v1 not reviewed · its branch is gone from origin$'
244[ "$(git -C "$data/open.git" rev-parse "refs/the-valley/integration-requests/main/c-$M2")" = "$(git rev-parse m-two)" ] ||
245 fail "mnmn was not approved at its second version"
246
247# ----------------------------------------------------------------------
248# Ctrl-C at a step's prompt, on a terminal: the step is interrupted, the
249# summary so far is printed, and the walk exits 130. The diff is paged
250# first, by a less that quits since it fits on one screen.
251git config pager.diff 'less -FX'
252status=0
253# On a terminal the prompt's keys are styled, so its plain tail is waited
254# on. A walk that never gets there is ended rather than left waiting.
255{
256 wait_for "$w/session.log" '\]kip: '
257 printf '\003'
258 wait_for "$w/session.log" 'walked'
259} | timeout 300 script -qec "valley review --repo open" /dev/null > "$w/session.log" 2>&1 || status=$?
260sed -e 's/\x1b\[[0-9;?]*[a-zA-Z]//g' -e 's/\r//g' "$w/session.log" > "$w/out"
261[ "$status" = 130 ] || fail "the interrupted walk exited $status, not 130: $(cat "$w/out")"
262line '^walked \(1 of 2\)$'
263line '^ ◇ open popo v1 interrupted$'
264no_line 'still needs you|nothing needs you'
265git config --unset pager.diff
266
267# ----------------------------------------------------------------------
268# A project that does not read is said, and nothing says that nothing
269# needs the operator. An empty queue says that nothing does, with the
270# counts pending gives, and exits 0.
271mv "$data/released.git" "$data/released.moved"
272walk 1 '' --repo released
273line '^✗ released did not read: '
274no_line 'nothing needs you'
275mv "$data/released.moved" "$data/released.git"
276ok valley withdraw "$O1"
277ok valley withdraw "$M3"
278walk 0 '' --repo open
279line '^nothing needs you · 1 moving · 1 draft'
280
281touch "$out"