| 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 | touch "$out" |