| 1 | # shellcheck shell=bash |
| 2 | # Paths and fixtures come from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | export HOME="$TMPDIR" XDG_STATE_HOME="$TMPDIR/state" TERM=xterm |
| 5 | export GIT_CONFIG_NOSYSTEM=1 |
| 6 | unset VISUAL || true |
| 7 | git config --global user.name valley-check |
| 8 | git config --global user.email valley-check@localhost |
| 9 | git config --global init.defaultBranch main |
| 10 | |
| 11 | # A project with an origin and one branch to review: two |
| 12 | # files, changed twice and once, far enough apart that the |
| 13 | # diff has two hunks in the first file and one in the second. |
| 14 | git init --quiet --bare "$TMPDIR/origin.git" |
| 15 | git init --quiet "$TMPDIR/work" |
| 16 | cd "$TMPDIR/work" || exit 1 |
| 17 | git remote add origin "$TMPDIR/origin.git" |
| 18 | seq 1 20 | sed 's/^/alpha /' > alpha.md |
| 19 | seq 1 10 | sed 's/^/beta /' > beta.md |
| 20 | git add -A |
| 21 | git commit --quiet -m seed |
| 22 | git push --quiet -u origin main |
| 23 | git checkout --quiet -b scratch |
| 24 | sed -i -e '3s/.*/alpha 3 changed/' -e '15s/.*/alpha 15 changed/' alpha.md |
| 25 | sed -i -e '5s/.*/beta 5 changed/' beta.md |
| 26 | git commit --quiet -am "touch both files" |
| 27 | git push --quiet origin scratch |
| 28 | git checkout --quiet main |
| 29 | git fetch --quiet origin |
| 30 | |
| 31 | # A pager filter that restructures the diff, as delta's |
| 32 | # rendering does: if v were handed what less displays, every |
| 33 | # anchor below would be wrong. |
| 34 | git config --global pager.diff "sed -e 's/^/| /'" |
| 35 | export EDITOR="$reviewer" |
| 36 | mkdir -p "$XDG_STATE_HOME" |
| 37 | |
| 38 | # v opens the editor, RETURN dismisses less's LESSOPEN |
| 39 | # warning, q leaves the pager, s is the verdict. |
| 40 | { printf 'v\nq'; sleep 2; printf 's\n'; sleep 1; } \ |
| 41 | | script -qec "valley review scratch" /dev/null > session.log 2>&1 |
| 42 | sed -e 's/\x1b\[[0-9;?]*[a-zA-Z]//g' -e 's/\r//g' session.log > plain.log |
| 43 | |
| 44 | # The anchors, exactly: above every file, on a file header |
| 45 | # above the first hunk, mid-hunk, in a second hunk whose |
| 46 | # numbering starts elsewhere, and in a second file. |
| 47 | diff -u - "$XDG_STATE_HOME/valley/notes/scratch.md" <<EOF |
| 48 | ## scratch at $(git rev-parse --short origin/scratch) |
| 49 | |
| 50 | (unanchored): a note above every file |
| 51 | alpha.md: alpha reads like two files |
| 52 | alpha.md:3: why is 3 changed at all? |
| 53 | alpha.md:13: and this hunk is fine |
| 54 | beta.md:5: beta wants the same treatment |
| 55 | |
| 56 | EOF |
| 57 | |
| 58 | # The editor was given the buffer valley wrote, not the |
| 59 | # rendering less displayed, and given a line to open at. |
| 60 | grep -qE 'editor got: \+[0-9]+ .*/review\.diff$' \ |
| 61 | "$XDG_STATE_HOME/editor.log" |
| 62 | # The filter did run: what the pager showed was its output. |
| 63 | grep -q '^| @@ ' plain.log |
| 64 | # The line the reviewer edited instead of inserting is gone, |
| 65 | # and said so. |
| 66 | grep -q 'changed or deleted diff line(s) ignored' plain.log |
| 67 | ! grep -q 'alpha SEVENTEEN' "$XDG_STATE_HOME/valley/notes/scratch.md" |
| 68 | |
| 69 | # The verdict is unchanged, and the notes come after it. |
| 70 | grep -q 'skipped scratch; nothing done' plain.log |
| 71 | verdict="$(grep -n 'skipped scratch' plain.log | cut -d: -f1)" |
| 72 | notes="$(grep -n '5 note(s) on scratch' plain.log | cut -d: -f1)" |
| 73 | [ "$verdict" -lt "$notes" ] |
| 74 | |
| 75 | # Piped, review is what it always was: the diff on stdout, |
| 76 | # the verdict from stdin, and no notes to take. |
| 77 | printf 's\n' | valley review scratch > piped.log |
| 78 | grep -q '^diff --git a/alpha.md b/alpha.md$' piped.log |
| 79 | grep -q 'skipped scratch; nothing done' piped.log |
| 80 | ! grep -q 'note(s) on scratch' piped.log |
| 81 | |
| 82 | # The pending list titles each branch by the subject of its first own |
| 83 | # commit, on the line under its name, and cuts a long one short with … to |
| 84 | # fit 80 columns when stdout is no terminal. Nothing it prints there is an |
| 85 | # escape code. |
| 86 | valley pending > pending.log |
| 87 | grep -A1 '^scratch ' pending.log | grep -q '^ touch both files$' |
| 88 | git checkout --quiet -b longsubject origin/main |
| 89 | git commit --quiet --allow-empty -m "$(printf 'x%.0s' $(seq 1 80))" |
| 90 | git push --quiet origin longsubject |
| 91 | git checkout --quiet main |
| 92 | valley pending > pending.log |
| 93 | grep -A1 '^longsubject ' pending.log | grep -qE '^ x{77}…$' |
| 94 | if grep -q $'\x1b' pending.log; then exit 1; fi |
| 95 | |
| 96 | # On a terminal, pending fits the terminal's width and sets each name in |
| 97 | # bold. With NO_COLOR set, it prints no escape code. |
| 98 | script -qec "stty cols 100; valley pending" /dev/null < /dev/null > tty.log 2>&1 |
| 99 | grep -q $'^\x1b\\[1mlongsubject\x1b\\[0m ' tty.log |
| 100 | grep -qE $'^ x{80}\r?$' tty.log |
| 101 | NO_COLOR=1 script -qec "stty cols 100; valley pending" /dev/null < /dev/null > tty.log 2>&1 |
| 102 | grep -q '^longsubject ' tty.log |
| 103 | if grep -q $'\x1b' tty.log; then exit 1; fi |
| 104 | |
| 105 | # ---------------------------------------------------------------------- |
| 106 | # A stack: a branch, then two changes with records, each on the one |
| 107 | # below, in a project whose floor requires nothing, so approving files a |
| 108 | # request with no evidence. Its origin logs every push it receives, which |
| 109 | # is how "publishes nothing" is read. |
| 110 | stack="$TMPDIR/stack.git" |
| 111 | pushlog="$TMPDIR/pushlog" |
| 112 | git init --quiet --bare "$stack" |
| 113 | cat > "$stack/hooks/pre-receive" << EOF |
| 114 | #!/bin/sh |
| 115 | echo '--- push' >> $pushlog |
| 116 | cat >> $pushlog |
| 117 | EOF |
| 118 | chmod +x "$stack/hooks/pre-receive" |
| 119 | git init --quiet "$TMPDIR/stackwork" |
| 120 | cd "$TMPDIR/stackwork" || exit 1 |
| 121 | git remote add origin "$stack" |
| 122 | git config valley.principal operator |
| 123 | mkdir -p policy/instance |
| 124 | cat > policy/instance/floor.cue << 'EOF' |
| 125 | package verification |
| 126 | |
| 127 | floor: { |
| 128 | classes: everything: { |
| 129 | paths: "**": true |
| 130 | requires: {} |
| 131 | } |
| 132 | unclassified: {} |
| 133 | } |
| 134 | EOF |
| 135 | git add -A |
| 136 | git commit --quiet -m seed |
| 137 | git push --quiet -u origin main |
| 138 | from=main |
| 139 | for layer in one two three; do |
| 140 | git checkout --quiet -b "$layer" "$from" |
| 141 | echo "layer $layer" > "$layer.md" |
| 142 | git add -A |
| 143 | git commit --quiet -m "layer $layer" |
| 144 | git push --quiet origin "$layer" |
| 145 | from="$layer" |
| 146 | done |
| 147 | two="$(git rev-parse two)" three="$(git rev-parse three)" |
| 148 | q=qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq p=pppppppppppppppppppppppppppppppp |
| 149 | valley ready --change "$q" two > /dev/null |
| 150 | valley ready --change "$p" three > /dev/null |
| 151 | git checkout --quiet main |
| 152 | requests="refs/the-valley/integration-requests/main" |
| 153 | |
| 154 | # s at the first layer, on a terminal, with a note written in its diff: |
| 155 | # the note is kept under the first layer's branch and printed at the end, |
| 156 | # and nothing is published. |
| 157 | cat > "$TMPDIR/stack-editor" << 'EOF' |
| 158 | #!/bin/sh |
| 159 | f="$1" |
| 160 | case "$1" in +*) f="$2" ;; esac |
| 161 | sed -i '/^+layer one$/a one wants a second line' "$f" |
| 162 | EOF |
| 163 | chmod +x "$TMPDIR/stack-editor" |
| 164 | : > "$pushlog" |
| 165 | { printf 'v\nq'; sleep 2; printf 's\n'; sleep 1; } \ |
| 166 | | EDITOR="$TMPDIR/stack-editor" script -qec "valley review pppp" /dev/null > stack-tty.log 2>&1 |
| 167 | sed -e 's/\x1b\[[0-9;?]*[a-zA-Z]//g' -e 's/\r//g' stack-tty.log > stack-plain.log |
| 168 | grep -q 'pppp is a stack of 3 changes; reviewing from the bottom, each against the one below it:$' stack-plain.log |
| 169 | grep -q '| change 1 of 3: one on main$' stack-plain.log |
| 170 | grep -q 'one: \[n\]ext, \[a\]pprove up to here, \[s\]kip?' stack-plain.log |
| 171 | grep -q 'skipped pppp at change 1 of 3, one; nothing done' stack-plain.log |
| 172 | if grep -q 'change 2 of 3' stack-plain.log; then exit 1; fi |
| 173 | grep -qx 'one.md:1: one wants a second line' "$XDG_STATE_HOME/valley/notes/one.md" |
| 174 | grep -q '1 note(s) on one, appended to .*/valley/notes/one.md:' stack-plain.log |
| 175 | grep -q '^ one.md:1: one wants a second line$' stack-plain.log |
| 176 | [ ! -e "$XDG_STATE_HOME/valley/notes/two.md" ] |
| 177 | [ ! -s "$pushlog" ] |
| 178 | |
| 179 | # n, n, a: the layers are paged bottom up, each diff holds only its own |
| 180 | # layer's file, and the top's request is filed at the top's head. |
| 181 | : > "$pushlog" |
| 182 | printf 'n\nn\na\n' | valley review pppp > stack.out 2> stack.err |
| 183 | grep -q '^ 1\. one (branch): layer one$' stack.out |
| 184 | grep -q '^ 2\. qqqq (ready): layer two$' stack.out |
| 185 | grep -q '^ 3\. pppp (ready): layer three$' stack.out |
| 186 | # Piped, each page's first line follows the prompt before it. |
| 187 | [ "$(grep -o 'change [0-9] of 3: [a-z]* on [a-z]*$' stack.out)" = "$(printf '%s\n' \ |
| 188 | 'change 1 of 3: one on main' 'change 2 of 3: qqqq on one' 'change 3 of 3: pppp on qqqq')" ] |
| 189 | for n in 1 2 3; do |
| 190 | awk -v n="$n" '/change [0-9] of 3: /{ on = index($0, "change " n " of 3: ") > 0 } on' stack.out > "page$n" |
| 191 | done |
| 192 | [ "$(grep '^diff --git' page1)" = 'diff --git a/one.md b/one.md' ] |
| 193 | [ "$(grep '^diff --git' page2)" = 'diff --git a/two.md b/two.md' ] |
| 194 | [ "$(grep '^diff --git' page3)" = 'diff --git a/three.md b/three.md' ] |
| 195 | grep -q '^three: \[a\]pprove, \[r\]eject, \[s\]kip?' stack.out |
| 196 | [ "$(git -C "$stack" rev-parse "$requests/c-$p")" = "$three" ] |
| 197 | if git -C "$stack" rev-parse --verify --quiet "$requests/c-$q" > /dev/null; then exit 1; fi |
| 198 | git -C "$stack" update-ref -d "$requests/c-$p" |
| 199 | |
| 200 | # a at the second layer files the second layer's request at its head, and |
| 201 | # nothing for the top. |
| 202 | : > "$pushlog" |
| 203 | printf 'n\na\n' | valley review pppp > stack-two.out 2> stack-two.err |
| 204 | [ "$(git -C "$stack" rev-parse "$requests/c-$q")" = "$two" ] |
| 205 | if git -C "$stack" rev-parse --verify --quiet "$requests/c-$p" > /dev/null; then exit 1; fi |
| 206 | grep -q "c-$q" "$pushlog" |
| 207 | if grep -q "c-$p" "$pushlog"; then exit 1; fi |
| 208 | if grep -q 'change 3 of 3' stack-two.out; then exit 1; fi |
| 209 | |
| 210 | # A draft in the middle is named in the warning before anything is paged. |
| 211 | valley unready qqqq > /dev/null 2>&1 |
| 212 | printf 's\n' | valley review pppp > stack-draft.out 2>&1 |
| 213 | grep -q '^warning: qqqq, change 2 of 3, is a draft\. The integrator refuses to land it or anything above it (refused-not-ready)\.$' stack-draft.out |
| 214 | |
| 215 | # A change with no stack under it pages and prompts as it always has. |
| 216 | printf 's\n' | valley review one > stack-one.out 2>&1 |
| 217 | grep -q '^branch one, log against origin/main:$' stack-one.out |
| 218 | grep -q '^one: \[a\]pprove, \[r\]eject, \[s\]kip? skipped one; nothing done$' stack-one.out |
| 219 | if grep -q 'stack of\|change 1 of' stack-one.out; then exit 1; fi |
| 220 | |
| 221 | touch "$out" |