#!/usr/bin/env bash # The shell integration's command record (llembas/shell/llembas-term.sh), in a real interactive # bash and zsh on a terminal of their own (script(1): bash tells a command from an empty line # only on a terminal): the file and the user variable, what counts as a command, and that # nothing at all happens outside LLeMbas TERM. # # llembas/tests/shell-test.sh set -uo pipefail ROOT="$(cd "$(dirname "$0")/../.." && pwd)" work="$(mktemp -d)" trap 'rm -rf "${work:?}"' EXIT failed=0 ok() { echo "ok $1"; } bad() { echo "FAIL $1" [ -z "${GITHUB_ACTIONS:-}" ] || printf '::error title=shell::%s\n' "$(printf '%s' "$1" | head -c 900 | tr '\n\r' ' ' | sed 's/%/%25/g')" failed=1 } expect() { if printf '%s' "$2" | grep -Eq -- "$3"; then ok "$1"; else bad "$1: wanted /$3/ in: $2"; fi; } # run : the lines on stdin are typed into that shell, in a home and a # runtime directory of its own. Extra variables come through EXTRA (one VAR=value a word). run() { local name="$1" shift mkdir -p "$work/$name/run" "$work/$name/home" # shellcheck disable=SC2086 # EXTRA and SHELL_DIR_OFF are words on purpose # The lines are typed slowly enough for each prompt to be there first. while IFS= read -r line || [ -n "$line" ]; do printf '%s\n' "$line" sleep 0.3 done | env -i PATH=/usr/bin:/bin TERM=xterm-256color HOME="$work/$name/home" XDG_RUNTIME_DIR="$work/$name/run" \ LLEMBAS_TERM_SHELL_DIR="$ROOT/assets/shell-integration" ${EXTRA-} ${SHELL_DIR_OFF-} \ script -qec "$*" /dev/null 2>&1 | cat -v } # field : the value of one key of a record. field() { printf '%s\n' "$2" | sed -n "s/^$1=//p" | head -n 1; } # The records in what a shell printed (the user variable, decoded), one a line, each as # " ". records() { printf '%s\n' "$1" | grep -o 'SetUserVar=llembas_term_status=[A-Za-z0-9+/=]*' | while IFS= read -r v; do text="$(printf '%s\n' "${v#SetUserVar=llembas_term_status=}" | base64 -d)" e="$(field exit "$text")" r="$(field ran "$text")" printf '%s %s %s %s\n' "$(field seq "$text")" "$(field state "$text")" "${e:--}" "${r:--}" done } for shell in bash zsh; do command -v "$shell" >/dev/null 2>&1 || { echo "skip $shell: not here"; continue; } case "$shell" in bash) words=(bash --norc --noprofile -i) ;; *) words=(zsh -f -i +Z) ;; esac # A first prompt, a command that works, an empty line, one that fails with 3 and is all a # subshell (which bash-preexec alone would not see). script=". '$ROOT/llembas/shell/llembas-term.sh'"$'\ntrue\n\n(exit 3)\nexit\n' want='^1 prompt - -,1 running - -,2 prompt 0 1,3 prompt 0 -,3 running 0 -,4 prompt 3 1,4 running 3 -,$' out="$(printf '%s' "$script" | EXTRA="LLEMBAS_TERM=1.0.0 LLEMBAS_TERM_PANE=7" run "$shell-in" "${words[@]}")" file="$work/$shell-in/run/llembas-term/pane-7.status" expect "$shell: a record at every prompt and when a command starts; an empty line ran nothing" "$(records "$out" | tr '\n' ',')" "$want" text="$(cat "$file" 2>/dev/null)" expect "$shell: the file starts with the format's line" "$(printf '%s\n' "$text" | head -n 1)" '^llembas-term-status=1$' expect "$shell: the file holds the last record (exit, as it was typed, started)" "$(field seq "$text") $(field state "$text") $(field exit "$text")" '^4 running 3$' expect "$shell: the record names the shell, its process and its directory" "$(field shell "$text") $(field pid "$text") $(field id "$text") $(field cwd "$text") $(field time "$text")" "^$shell [0-9]+ [0-9]+\.[0-9]+ /.* [0-9]{9,}\$" expect "$shell: the file is the person's alone" "$(stat -c %a "$file" 2>/dev/null)" '^600$' expect "$shell: nothing is left beside the file" "$(cd "$work/$shell-in/run/llembas-term" 2>/dev/null && find . -type f | sort | tr '\n' ' ')" '^\./pane-7\.status $' expect "$shell: the exit status still reaches the prompt" "$(printf '%s' ". '$ROOT/llembas/shell/llembas-term.sh'"$'\n(exit 5)\necho "status=$?"\nexit\n' | EXTRA="LLEMBAS_TERM=1.0.0 LLEMBAS_TERM_PANE=7" run "$shell-st" "${words[@]}")" 'status=5' # The directory the terminal names is where the file goes; else the state directory. mkdir -p "$work/$shell-dir/elsewhere" printf '%s' "$script" | EXTRA="LLEMBAS_TERM=1.0.0 LLEMBAS_TERM_PANE=12 LLEMBAS_TERM_STATUS_DIR=$work/$shell-dir/elsewhere" run "$shell-dir" "${words[@]}" >/dev/null expect "$shell: in LLEMBAS_TERM_STATUS_DIR" "$(field seq "$(cat "$work/$shell-dir/elsewhere/pane-12.status" 2>/dev/null)")" '^4$' printf '%s' "$script" | EXTRA="LLEMBAS_TERM=1.0.0 LLEMBAS_TERM_PANE=3 XDG_RUNTIME_DIR= XDG_STATE_HOME=$work/$shell-state/state" run "$shell-state" "${words[@]}" >/dev/null expect "$shell: with no runtime directory, in the state directory" "$(field seq "$(cat "$work/$shell-state/state/llembas-term/pane-3.status" 2>/dev/null)")" '^4$' # A pane id that is not a number names no file; the variable is still set. out="$(printf '%s' "$script" | EXTRA="LLEMBAS_TERM=1.0.0 LLEMBAS_TERM_PANE=../x" run "$shell-bad" "${words[@]}")" expect "$shell: a pane id that is not a number writes no file" "$(find "$work/$shell-bad/run" -type f | wc -l)" '^0$' expect "$shell: and the variable is still set" "$(records "$out" | tr '\n' ',')" "$want" # Over SSH the file would be on the wrong machine: the variable alone, naming the machine. out="$(printf '%s' "$script" | EXTRA="LLEMBAS_TERM=1.0.0 LLEMBAS_TERM_PANE=7 SSH_CONNECTION=x HOSTNAME=workstation HOST=workstation" run "$shell-ssh" "${words[@]}")" one="$(printf '%s\n' "$out" | grep -o 'SetUserVar=llembas_term_status=[A-Za-z0-9+/=]*' | head -n 1 | sed 's/.*=llembas_term_status=//' | base64 -d)" expect "$shell: over SSH no file is written" "$(find "$work/$shell-ssh/run" -type f | wc -l)" '^0$' expect "$shell: over SSH the record names the machine and no process" "host=$(field host "$one") pid=$(field pid "$one")" '^host=workstation pid=$' # Without WezTerm's script (no command marks at all), the record is still kept. out="$(printf '%s' "$script" | EXTRA="LLEMBAS_TERM=1.0.0 LLEMBAS_TERM_PANE=7" SHELL_DIR_OFF="LLEMBAS_TERM_SHELL_DIR=$work/nowhere" run "$shell-alone" "${words[@]}")" expect "$shell: kept without the command marks too" "$(records "$out" | tr '\n' ',')" "$want" # Turned off, and outside LLeMbas TERM: nothing. out="$(printf '%s' "$script" | EXTRA="LLEMBAS_TERM=1.0.0 LLEMBAS_TERM_PANE=7 LLEMBAS_TERM_SKIP_STATUS=1" run "$shell-off" "${words[@]}")" expect "$shell: LLEMBAS_TERM_SKIP_STATUS=1 turns it off" "$(records "$out" | wc -l) $(find "$work/$shell-off/run" -type f | wc -l)" '^0 0$' out="$(printf '%s' "$script" | EXTRA="LLEMBAS_TERM_PANE=7" run "$shell-out" "${words[@]}")" expect "$shell: outside LLeMbas TERM nothing is written and nothing is printed" "$(printf '%s' "$out" | grep -c 'SetUserVar\|133;') $(find "$work/$shell-out/run" -type f | wc -l)" '^0 0$' done [ "$failed" -eq 0 ] && echo "shell: all passed" exit "$failed"