Skip to content
pi-c Artifact [7c4923809c]
Login

Artifact [7c4923809c]

Artifact 7c4923809c27004a7f88a1ee40f5664f75e3424fca404852a4d947707c00d6dd:


#!/bin/sh
# pic help: the manual and reference compiled in. pic help lists every topic,
# pic help TOPIC prints its file exactly, an unknown topic exits 1 with the
# list on stderr, and pic --help is a synopsis of at most 40 lines and 100
# columns that points to pic help.
set -eu
PIC=${PIC:?set PIC to the pic binary}
root=$(cd "$(dirname "$0")/../.." && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-help.XXXXXX")
export HOME="$T/home"
mkdir -p "$HOME"
trap 'rm -rf -- "$T"' EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }

"$PIC" help > "$T/index" || fail "pic help exits $?"
n=0
for f in "$root"/docs/manual/*.md "$root"/docs/reference/*.md; do
    name=$(basename "$f" .md)
    title=$(head -1 "$f" | sed 's/^# //')
    grep -qE "^  $name +$title\$" "$T/index" || fail "pic help does not list $name ($title)"
    "$PIC" help "$name" > "$T/topic" || fail "pic help $name exits $?"
    cmp -s "$f" "$T/topic" || fail "pic help $name differs from $f"
    n=$((n + 1))
done
[ "$n" -gt 20 ] || fail "only $n topics found"
printf 'PASS: pic help lists %d topics and prints each file exactly\n' "$n"

if "$PIC" help no-such-topic > "$T/out" 2> "$T/err"; then fail "an unknown topic exits 0"; fi
[ ! -s "$T/out" ] || fail "an unknown topic printed to stdout"
grep -q 'no-such-topic' "$T/err" && grep -qE '^  start +Getting started$' "$T/err" ||
    fail "an unknown topic does not name it and list the topics: $(cat "$T/err")"
if "$PIC" help start keys > /dev/null 2>&1; then fail "two topics exit 0"; fi
printf 'PASS: an unknown topic exits 1 and lists the topics\n'

for h in --help -h; do
    "$PIC" $h > "$T/usage" || fail "pic $h exits $?"
    lines=$(wc -l < "$T/usage" | tr -d ' ')
    [ "$lines" -le 40 ] || fail "pic $h is $lines lines; the budget is 40"
    awk 'length($0) > 100 { printf "line %d is %d columns\n", NR, length($0); bad = 1 } END { exit bad }' "$T/usage" ||
        fail "pic $h is wider than 100 columns"
    grep -q 'pic help' "$T/usage" || fail "pic $h does not point to pic help"
done
# A usage error prints the same synopsis to stderr and exits 2.
if "$PIC" tui --no-such-flag > /dev/null 2> "$T/err"; then fail "a bad flag exits 0"; else rc=$?; fi
[ "$rc" -eq 2 ] || fail "a bad flag exits $rc, not 2"
tail -n "$lines" "$T/err" | cmp -s - "$T/usage" || fail "a usage error does not end with the synopsis"
# Every command the synopsis names is in docs/reference/cli.md, so the two cannot drift apart.
grep -oE 'pic [a-z][a-z-]+' "$T/usage" | sort -u | while read -r _ cmd; do
    grep -qF "\`pic $cmd" "$root/docs/reference/cli.md" || printf '%s\n' "$cmd"
done > "$T/undocumented"
[ ! -s "$T/undocumented" ] || fail "pic --help names commands docs/reference/cli.md does not: $(tr '\n' ' ' < "$T/undocumented")"
printf 'PASS: pic --help is a %s-line synopsis that points to pic help, every command documented\n' "$lines"

# The terminal's /help TOPIC: the topic rendered as markdown (headings drawn,
# not shown as "##"), keys followed by the bindings in effect, an unknown
# topic named with the list, and bare /help pointing to the topics.
[ -n "${DRIVE:-}" ] || { printf 'SKIP: /help TOPIC needs DRIVE (the pty driver)\n'; exit 0; }
mkdir -p "$T/ws"
"$DRIVE" "$T/help.tx" 100 30 "$PIC" --tui-mode regular --db "$T/j" --workspace "$T/ws" --model fake/m <<'EOF2' ||
expect pic fake/m
send /help sessions\r
expect Compaction and branch summaries
send /help keys\r
expect In effect now:
expect Rebind any action
send /help nope\r
expect no help topic nope; the topics: start,
send /help\r
expect /help TOPIC shows a topic of the manual: start
send \x04\x04
EOF2
    fail "/help: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/help.tx" | grep -v '^\s*$' | tail -8)"
perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/help.tx" > "$T/help.txt"
! grep -q '^## Forking' "$T/help.txt" || fail "/help sessions shows the markdown, not the rendering"
grep -q 'Forking' "$T/help.txt" || fail "/help sessions lost its heading"
printf 'PASS: /help TOPIC renders the topic; keys adds the bindings; unknown topics are named\n'