diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index 5ac0938..cd7ba73 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -7,6 +7,45 @@ on: branches: [main, develop] jobs: + validate-completion: + name: Isolated completion (${{ matrix.os }}) + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest] + runs-on: ${{ matrix.os }} + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install test dependencies (Linux) + if: runner.os == 'Linux' + run: sudo apt-get update && sudo apt-get install -y zsh shellcheck + + - name: Install lint dependency (macOS) + if: runner.os == 'macOS' + run: command -v shellcheck || brew install shellcheck + + - name: Verify native shells and PTY test capability + run: | + /bin/bash --version + /bin/zsh --version + python3 -c 'import pty' + + - name: Syntax and lint (including system Bash 3.2 on macOS) + run: | + for script in config/dotfiles/*completion.sh tests/test_completion*.sh; do + /bin/bash --noprofile --norc -n "$script" + shellcheck "$script" + done + /bin/zsh -df -n config/dotfiles/completion.sh + + - name: Isolated completion install, upgrade, and removal + run: /bin/bash tests/test_completion_install.sh + + - name: Native shell state and PTY completion tests (no adapters) + run: /bin/bash tests/test_completion_runtime.sh --bash /bin/bash --zsh /bin/zsh + validate-linux: name: Validate Linux Setup runs-on: ubuntu-latest diff --git a/CHANGELOG.md b/CHANGELOG.md index a3c184a..8c8e726 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Explicit completion-only Bash/Zsh opt-in with native Tab/Shift+Tab cycling, + separate validated rc hooks, backups, mutation-free preview, disable switch, + and feature-only removal. Older Readline (including macOS Bash 3.2) gets + forward Tab only and preserves existing Shift+Tab. Ordinary setup is unchanged. + Includes isolated lifecycle and actual shell/PTY tests on Linux/macOS. (#516) - Isolated shutdown-helper regression coverage under Bash/Zsh on Linux/macOS, including macOS system Bash 3.2, with non-forwarding command stubs. (#514) diff --git a/README.md b/README.md index 349636a..d0d63dd 100644 --- a/README.md +++ b/README.md @@ -280,6 +280,26 @@ Full definitions: - **Linux/macOS:** `config/dotfiles/.aliases` (bash/zsh only -- see header for the non-POSIX features in use) - **Windows:** `scripts/windows/setup.ps1` (the `Write-PowerShellProfile` function) +## Opt-in Native Completion Cycling + +Bash/Zsh users can opt in without rerunning setup or reinstalling dotfiles: + +```bash +bash config/dotfiles/install-completion.sh --dry-run +bash config/dotfiles/install-completion.sh +``` + +Open a fresh shell: **Tab** selects the next match, **Shift+Tab** the previous +match. **macOS system Bash 3.2 supports forward Tab only; its existing Shift+Tab +binding is left untouched** because that Readline lacks a native backward widget. +Ordinary installation never opts you in. No plugins or system inputrc changes. + +Use `DEV_SETUP_COMPLETION=0` before the hook to disable in fresh shells, or +`bash config/dotfiles/install-completion.sh --uninstall` to remove only this +feature. See the [completion guide](config/dotfiles/README.md#opt-in-tab-completion-cycling-bash--zsh) +for backups, `ZDOTDIR`, framework/login-shell ordering, terminal support, and +optional completion providers. + ## Shell Functions The following helper functions are available after setup: diff --git a/config/dotfiles/README.md b/config/dotfiles/README.md index 75e446c..3d226c9 100644 --- a/config/dotfiles/README.md +++ b/config/dotfiles/README.md @@ -13,6 +13,8 @@ These templates give every Dev Container and Codespace a sensible, consistent en | `.npmrc.template` | `$HOME/.npmrc` | Copy | Editable per-machine | | `.vimrc` | `$HOME/.vimrc` | Symlink | Vim configuration | | `install.sh` | -- | Script | Idempotent installer | +| `install-completion.sh` | -- | Script | Separate, explicit completion-only opt-in | +| `completion.sh` | `$HOME/.dev-setup-completion.sh` | Symlink | Interactive Bash/Zsh native completion bindings | --- @@ -28,6 +30,145 @@ bash config/dotfiles/install.sh --dry-run --- +## Opt-in Tab completion cycling (Bash / Zsh) + +**This is not enabled by `setup.sh` or `install.sh`.** Both existing and fresh +installations use the same separate command, without reinstalling other dotfiles: + +```bash +# From the repository root, after your normal shell/framework setup: +bash config/dotfiles/install-completion.sh --dry-run +bash config/dotfiles/install-completion.sh +# Open a fresh interactive shell afterward. +``` + +| Shell | Tab | Shift+Tab | +|-------|-----|-----------| +| Bash with native `menu-complete-backward` | Next match | Previous match | +| **macOS system Bash 3.2 / older Readline** | **Next match** | **Existing binding unchanged; no backward cycling added** | +| Zsh | Next match | Previous match | + +The helper detects native Readline widget availability, not a Bash version +number. It deliberately does **not** add a macro fallback or reserve extra keys. +Opting in explicitly overrides existing Tab and (where supported) Shift+Tab +bindings in Bash's `emacs-standard` / `vi-insert` and Zsh's `emacs` / `viins` +keymaps. Editing mode, vi command mode, and unrelated bindings are preserved. +Custom keymaps outside these maps remain user-managed. The helper does nothing +in noninteractive shells. It is not a PowerShell feature. + +### Ordering, terminals, and providers + +- The installer appends a **separate** `dev-setup completion v1` hook at the end + of each rc. Keep the entire hook, including its preceding separator newline, + intact. Existing general DevSetup managed blocks are never edited. +- Put framework initialization, `compinit`, completion providers, and general + key bindings **before** this hook. Later user configuration wins; frameworks + loaded later may override it. If moving the hook, move its separator too. +- Zsh's native completion works without `compinit`. If your framework already + initialized completion, these widget names reuse its providers/styles. This + helper never runs `compinit` (which would also bind unrelated keys), installs + a framework, or bypasses `compinit`'s security audit. Follow your framework's + normal initialization/security instructions for richer completion. +- Commands, files, and directories work natively. Git branches, tool flags, + package names, etc. depend on separately configured completion providers + (such as `bash-completion` or Zsh's completion system). Their filtering and + ordering still apply. This is not fuzzy search or an autosuggestion plugin. +- Shift+Tab must reach the shell as **ESC `[Z`**. Some terminals, IDEs, and + multiplexers intercept it or send another sequence; configure them yourself. + Tab/Ctrl-I are the same terminal key. Existing Readline variables and Zsh + completion styles may affect listing, ordering, or wraparound behavior. +- Bash login shells, especially macOS Terminal, may not read `.bashrc` unless + your existing login profile sources it. This command **never edits + `.bash_profile`**; choose that startup arrangement yourself. + +### `ZDOTDIR` and ownership safety + +The command targets **both** `$HOME/.bashrc` and `${ZDOTDIR:-$HOME}/.zshrc`; +Zsh need not be installed at opt-in time. If you use `ZDOTDIR`, export its +**effective absolute path** when installing, previewing, or uninstalling: + +```bash +ZDOTDIR="$HOME/.config/zsh" bash config/dotfiles/install-completion.sh +``` + +The directory must already exist. The command never executes `.zshenv` to +infer configuration. If `$HOME/.zshenv` exists, an explicit `ZDOTDIR` is required +(use `ZDOTDIR="$HOME"` if it does not relocate rc files). Ensure it matches the +value used by your shell; changing `ZDOTDIR` later requires removing the old +hook using the old value first. Empty/relative/missing `ZDOTDIR` is rejected. + +Trailing/repeated slashes and `.` or `..` components are supported for real +directories. **Every component of the absolute `ZDOTDIR` path is checked for +symlinks**, including ancestors: `link/`, `link/.`, `link/child`, and +`link/../other` are all rejected if `link` is a symlink. The supplied path is +not resolved or rewritten to silently choose a target. Use a symlink-free +absolute path (for example, `/private/tmp/...` rather than `/tmp/...` on macOS +when `/tmp` is a symlink). This is preflight validation, not a filesystem-race +or sandbox guarantee. + +Both rc files and the helper destination are validated before any mutation. +Symlinked rc files, symlinks in `ZDOTDIR` paths, conflicting helper links, +user-owned files at the helper destination, and malformed/modified/duplicate +hooks are rejected rather than overwritten. Binary/NUL rc content and paths +containing newlines are unsupported. Spaces and shell metacharacters in paths +are supported. If validation fails, review the indicated configuration manually; +do not delete user configuration just to make the check pass. + +Existing rc files are backed up to unique +`.dev-setup-completion.bak.XXXXXX` files before edits. Backups retain file +permissions and are not automatically pruned or used to overwrite later edits. +Bytes outside the owned hook are retained. When the hook is at EOF, removal +restores the original bytes exactly, including a missing final newline. When +text follows the hook, removal retains one separator newline if otherwise a +nonempty, unterminated preceding line would join that following text. No extra +separator is retained if the preceding text is empty, already ends in a newline, +or the following text starts with a newline. This keeps later assignments +separate without restoring a whole backup or changing any user-added bytes. +Re-running the command does not duplicate hooks or create new backups. +`--dry-run` performs validation but creates no files, directories, links, or backups. + +The helper link points into **this checkout**. Keep it in place; updating +`completion.sh` updates already opted-in shells on their next startup, without +rewriting hooks. To move/remove a checkout, uninstall this feature using the +original checkout first. A link into a different checkout is a conflict, not an +implicit migration. + +### Disable, remove, or recover + +For a temporary disable, export this **before the hook** (or in the parent +process environment), then open a **fresh** shell: + +```bash +export DEV_SETUP_COMPLETION=0 +``` + +This leaves that fresh shell's prior bindings intact. Setting it after the +helper already ran does not undo current-shell bindings. Unset it to re-enable +in future shells. Do not place the switch inside the owned hook. + +To remove only this feature, without reinstalling or restoring other dotfiles: + +```bash +bash config/dotfiles/install-completion.sh --uninstall --dry-run +bash config/dotfiles/install-completion.sh --uninstall +``` + +Removal validates both targets, backs up changed rc files, removes only the +owned hook (with the separator rule above) and expected helper link, and preserves +later user additions. Newly created rc files are left empty rather than deleted. +Repeated removal is safe. +Open a fresh shell to get normal bindings back. Remove this feature separately +**before** the general DevSetup uninstaller or deleting the checkout; the general +uninstaller does not manage this opt-in hook. + +Avoid concurrent edits/installers. Replacements are prepared and backups made +before activating hooks, with a snapshot recheck, but this is **not a multi-file +transaction or a filesystem lock**. On an I/O failure, inspect the diagnostic, +current hooks, helper link, and named backups; fix the underlying problem and +rerun. Never blindly restore a backup over newer user additions. + +--- + ## Customisation ### `.gitconfig` diff --git a/config/dotfiles/completion.sh b/config/dotfiles/completion.sh new file mode 100644 index 0000000..569dd5a --- /dev/null +++ b/config/dotfiles/completion.sh @@ -0,0 +1,33 @@ +# DevSetup opt-in completion. Sourced by the separate completion rc hook. +# Bash 3.2+ / Zsh. No effect in noninteractive shells or when disabled. +# shellcheck shell=bash +case $- in *i*) ;; *) return 0 ;; esac +[ "${DEV_SETUP_COMPLETION:-1}" != 0 ] || return 0 + +_dev_setup_completion() { + local map widgets + if [ -n "${BASH_VERSION:-}" ]; then + widgets=$'\n'"$(bind -l)"$'\n' + case "$widgets" in *$'\nmenu-complete\n'*) ;; *) return 1 ;; esac + for map in emacs-standard vi-insert; do + bind -m "$map" '"\C-i": menu-complete' + # Readline 5.2 (macOS Bash 3.2) has no backward widget. Leave the + # existing Shift+Tab binding alone; do not reserve extra keys. + case "$widgets" in + *$'\nmenu-complete-backward\n'*) + bind -m "$map" '"\e[Z": menu-complete-backward' ;; + esac + done + elif [ -n "${ZSH_VERSION:-}" ]; then + # These are native widgets even without compinit. If a framework has + # initialized completion, the same widget names use its providers. + # Do not run compinit here: it also changes unrelated key bindings. + # Users/frameworks retain responsibility for its normal security audit. + for map in emacs viins; do + bindkey -M "$map" '^I' menu-complete + bindkey -M "$map" '^[[Z' reverse-menu-complete + done + fi +} +_dev_setup_completion +unset -f _dev_setup_completion diff --git a/config/dotfiles/install-completion.sh b/config/dotfiles/install-completion.sh new file mode 100644 index 0000000..a53fd51 --- /dev/null +++ b/config/dotfiles/install-completion.sh @@ -0,0 +1,176 @@ +#!/usr/bin/env bash +# Completion-only opt-in. Does not run install.sh or source any user startup file. +# Bash 3.2 compatible; only standard Linux/macOS utilities are required. +set -euo pipefail +export LC_ALL=C + +usage() { + cat <<'HELP' +Usage: bash config/dotfiles/install-completion.sh [--dry-run] [--uninstall] + +Opt in BOTH ~/.bashrc and ${ZDOTDIR:-$HOME}/.zshrc, or remove only this feature. +Ordinary DevSetup installation does not enable completion cycling. +Tab cycles forward. Shift+Tab cycles backward in Zsh and in Bash with native +menu-complete-backward; macOS Bash 3.2 leaves existing Shift+Tab UNCHANGED. +Export DEV_SETUP_COMPLETION=0 before the hook to disable in a fresh shell. +Export ZDOTDIR explicitly if ~/.zshenv exists; no startup files are executed to +infer its value. Symlinked rc files and conflicting helper links are rejected. +Existing rc files are backed up before edits. Keep the repository in place. +See config/dotfiles/README.md for ordering, recovery, and compatibility limits. +HELP +} +die() { printf 'completion: %s\n' "$*" >&2; exit 1; } +DRY_RUN=false +UNINSTALL=false +for arg in "$@"; do + case "$arg" in + --dry-run) DRY_RUN=true ;; + --uninstall) UNINSTALL=true ;; + --help|-h) usage; exit 0 ;; + *) die "unknown argument: $arg (see --help)" ;; + esac +done + +DOTFILES_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P) +SOURCE="$DOTFILES_DIR/completion.sh" +[[ -f "$SOURCE" && ! -L "$SOURCE" ]] || die "missing regular helper: $SOURCE" +[[ "${HOME:-}" == /* && -d "$HOME" && -w "$HOME" && -x "$HOME" ]] || die 'HOME must be an existing writable, searchable absolute directory' +if [[ -z "${ZDOTDIR+x}" && ( -e "$HOME/.zshenv" || -L "$HOME/.zshenv" ) ]]; then + die 'export the effective ZDOTDIR (use HOME if unchanged); ~/.zshenv exists and will not be executed' +fi +ZSH_DIR=${ZDOTDIR-$HOME} +[[ "$ZSH_DIR" == /* && -d "$ZSH_DIR" && -w "$ZSH_DIR" && -x "$ZSH_DIR" ]] || + die 'ZDOTDIR must be an existing writable, searchable absolute directory' +# Check every component before a trailing slash, dot or later .. can hide a +# symlink from -L. Empty slash-separated components are harmless; do not resolve +# the path or collapse .. before checking preceding components. Keep ZSH_DIR +# unchanged for file operations. This preflight does not prevent filesystem races. +directory_remaining=${ZSH_DIR#/} +directory_prefix='' +while [[ -n "$directory_remaining" ]]; do + component=${directory_remaining%%/*} + if [[ "$directory_remaining" == */* ]]; then directory_remaining=${directory_remaining#*/}; + else directory_remaining=''; fi + [[ -n "$component" ]] || continue + directory_prefix="$directory_prefix/$component" + [[ ! -L "$directory_prefix" ]] || die "symlink in ZDOTDIR path: $directory_prefix" +done +# Newlines in paths are deliberately unsupported; spaces, quotes and shell +# metacharacters are safe because the hook uses HOME, not interpolated code. +case "$HOME$ZSH_DIR$DOTFILES_DIR" in *$'\n'*) die 'newline in configuration path is unsupported' ;; esac +LINK="$HOME/.dev-setup-completion.sh" +if [[ -L "$LINK" ]]; then + [[ "$(readlink "$LINK")" == "$SOURCE" ]] || die "conflicting helper symlink: $LINK" +elif [[ -e "$LINK" ]]; then + die "helper path is user-managed, not the expected symlink: $LINK" +fi + +# The leading separator newline belongs to this block. At EOF, removal restores +# an original missing final newline; between user text, retain it if needed to +# keep lines separate. Hook text stays stable across helper upgrades. +# shellcheck disable=SC2016 +BLOCK=$'\n''# >>> dev-setup completion v1 (do not edit) >>> +[ ! -r "$HOME/.dev-setup-completion.sh" ] || . "$HOME/.dev-setup-completion.sh" +# <<< dev-setup completion v1 <<<'$'\n' +FILES=("$HOME/.bashrc" "$ZSH_DIR/.zshrc") +BEFORE=() +AFTER=() +CHANGED=(false false) +EXISTED=(false false) +TEMPS=('' '') + +# Validate EVERY target before creating links, backups, files or directories. +for i in 0 1; do + file=${FILES[$i]} + [[ ! -L "$file" ]] || die "symlinked rc is ambiguous: $file" + text='' + if [[ -e "$file" ]]; then + [[ -f "$file" && -r "$file" && -w "$file" ]] || die "rc must be a readable writable regular file: $file" + EXISTED[i]=true + # The sentinel retains all trailing newlines. Reject NUL/binary content + # that Bash cannot represent rather than silently dropping bytes. + text=$(cat "$file"; printf '.') + text=${text%.} + cmp -s "$file" <(printf '%s' "$text") || die "rc is not representable as shell text: $file" + fi + BEFORE[i]=$text + remainder=${text/"$BLOCK"/} + if [[ "$remainder" == *'dev-setup completion'* || "$remainder" == *'.dev-setup-completion.sh'* ]]; then + die "malformed, duplicate or modified completion hook: $file (review manually)" + fi + if [[ "$UNINSTALL" == true ]]; then + if [[ "$remainder" != "$text" ]]; then + prefix=${text%%"$BLOCK"*} + suffix=${text#*"$BLOCK"} + # Do not fuse an unterminated original line with a later addition. + if [[ -n "$prefix" && "$prefix" != *$'\n' && -n "$suffix" && "$suffix" != $'\n'* ]]; then + remainder="$prefix"$'\n'"$suffix" + fi + fi + AFTER[i]=$remainder + elif [[ "$remainder" == "$text" ]]; then + AFTER[i]="$text$BLOCK" + else + AFTER[i]=$text + fi + if [[ "${AFTER[$i]}" != "$text" ]]; then CHANGED[i]=true; fi +done + +printf 'Completion: Tab forward; Shift+Tab backward only with a native backward widget.\n' +printf 'macOS Bash 3.2: forward only; existing Shift+Tab is preserved. Zsh: both directions.\n' +for i in 0 1; do + if [[ "${CHANGED[$i]}" == true ]]; then + printf '%s: %s\n' "${FILES[$i]}" 'would back up (if present) and update completion hook' + else + printf '%s: unchanged\n' "${FILES[$i]}" + fi +done +if [[ "$DRY_RUN" == true ]]; then + printf '[dry-run] No changes. Helper link would be reconciled for this operation: %s\n' "$LINK" + exit 0 +fi + +cleanup() { + for i in 0 1; do + if [[ -n "${TEMPS[$i]}" ]]; then rm -f -- "${TEMPS[$i]}"; fi + done +} +trap cleanup EXIT +# Prepare replacements and ALL backups before activating any hook. Unique +# backup names never overwrite or prune earlier recovery copies. +for i in 0 1; do + [[ "${CHANGED[$i]}" == true ]] || continue + file=${FILES[$i]} + TEMPS[i]=$(mktemp "$file.dev-setup-completion.tmp.XXXXXX") + if [[ "${EXISTED[$i]}" == true ]]; then + cp -p "$file" "${TEMPS[$i]}" + backup=$(mktemp "$file.dev-setup-completion.bak.XXXXXX") + cp -p "$file" "$backup" + printf 'Backup: %s\n' "$backup" + fi + printf '%s' "${AFTER[$i]}" > "${TEMPS[$i]}" +done +# Recheck snapshots before committing edits; never knowingly overwrite a file +# edited during preflight. This is not a multi-file transaction or a lock. +for i in 0 1; do + file=${FILES[$i]} + [[ ! -L "$file" ]] || die "rc became a symlink: $file" + if [[ "${EXISTED[$i]}" == true ]]; then + cmp -s "$file" <(printf '%s' "${BEFORE[$i]}") || die "rc changed during installation: $file" + else + [[ ! -e "$file" ]] || die "rc appeared during installation: $file" + fi +done +if [[ "$UNINSTALL" == false && ! -L "$LINK" ]]; then + ln -s "$SOURCE" "$LINK" +fi +for i in 0 1; do + [[ "${CHANGED[$i]}" == true ]] || continue + mv -f -- "${TEMPS[$i]}" "${FILES[$i]}" + TEMPS[i]='' +done +if [[ "$UNINSTALL" == true && -L "$LINK" ]]; then + [[ "$(readlink "$LINK")" == "$SOURCE" ]] || die 'helper link changed during uninstall; left in place' + rm -- "$LINK" +fi +printf 'Done. Open a fresh shell; current-shell bindings are not changed or restored.\n' diff --git a/tests/README.md b/tests/README.md index a072f5b..c6c3771 100644 --- a/tests/README.md +++ b/tests/README.md @@ -2,6 +2,60 @@ Idempotency test suite for the `dev-setup` scripts. +## Isolated completion tests (no machine setup) + +```bash +/bin/bash tests/test_completion_install.sh +/bin/bash tests/test_completion_runtime.sh --bash /bin/bash --zsh /bin/zsh +``` + +These tally-based suites do **not** run `setup.sh`, source personal rc files, or +modify the real HOME. All generated homes, copied checkouts, and PTY fixtures +live under ignored `.pi-herdsman/completion-*` directories. They are removed on +success and retained with a printed path on assertion failure; local evidence +is not backed up by Git. Bash 3.2 can drive both suites. Runtime tests require +both shell executables and **Python 3** for real pseudo-terminal interaction. + +- `test_completion_install.sh`: old managed-block and exact rc byte preservation + (CRLF, backslashes, missing final newline), quoted paths, backups, missing rc + files, repeated install/removal, linked-helper upgrades, dry-run, later user + additions, explicit `ZDOTDIR`, and fail-before-mutation handling of malformed, + duplicate, modified, binary, or ambiguous symlink configuration. +- `test_completion_runtime.sh`: installs the actual hook into isolated homes; + verifies forward/backward file completion, command and directory completion, + and later overrides using real keystrokes. Compares editing mode and unrelated + key bindings, tests repeated sourcing, disabled/noninteractive/uninstalled + fresh shells, reuses Zsh initialization and a custom provider, and verifies an + insecure `compinit` is not bypassed. Tests both emacs and vi insertion modes. +- Native backward-widget availability is detected at runtime. On macOS system + Bash 3.2, tests require forward cycling and preservation of a preexisting + Shift+Tab macro. A separately labeled unavailable-widget simulation exercises + that branch on modern Bash; it is **not** native Bash 3.2 compatibility evidence. + +The dedicated `validate-completion` CI matrix runs these without full setup on +Linux and macOS, using `/bin/bash` (macOS 3.2) and `/bin/zsh`, plus syntax checks +and ShellCheck. CI uses no shell adapters. A developer with an explicitly +unpacked local Zsh may pass `--zsh-init /absolute/path/to/test-init.zsh` to set its +`module_path` and `fpath`; this is test-only, not a production fallback. The +harness isolates user startup files, but system `/etc/zshenv` is still trusted. +The fixtures execute reviewed repository code; they are not a hostile-code sandbox. + +Zsh provider-reuse/security fixtures copy the selected shell's **standard native +function sources** into private fixture-owned files, preserving lookup precedence +but excluding site/vendor directories and compiled `.zwc` caches. Discovery +requires one standard `functions` (macOS) or `functions/Completion` (Linux) tree +containing `compinit`; missing/ambiguous resources fail rather than falling back. +Only those copies are on `fpath` when initializing completion. The real +`compinit -D` and `compaudit` run with their normal security checks: no bypass +flags, prompt acceptance, or changes to runner/global permissions. + +Both reuse cases inject an insecure ambient directory, verify the native audit +rejects it, then verify the isolated `fpath` excludes it while provider integration +and PTY cycling work. The separate security case adds one intentionally insecure +directory to the isolated inputs and still requires detection and initialization +abort. This avoids depending on hosted-runner user/vendor completion permissions; +the installed shell's standard function sources remain trusted test dependencies. + ## What is idempotency? A script is **idempotent** when running it multiple times produces the same diff --git a/tests/test_completion_install.sh b/tests/test_completion_install.sh new file mode 100644 index 0000000..5ac0b97 --- /dev/null +++ b/tests/test_completion_install.sh @@ -0,0 +1,257 @@ +#!/usr/bin/env bash +# Isolated completion-only lifecycle tests, also run with macOS /bin/bash 3.2. +set -uo pipefail +PASS=0 FAIL=0 +pass() { printf 'PASS: %s\n' "$1"; PASS=$((PASS + 1)); } +fail() { printf 'FAIL: %s\n' "$1"; FAIL=$((FAIL + 1)); } +die() { printf 'ERROR: %s\n' "$1" >&2; exit 1; } +check() { local label=$1; shift; if "$@"; then pass "$label"; else fail "$label"; fi; } +ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P) +mkdir -p "$ROOT/.pi-herdsman" || exit 1 +# Disposable local fixtures must remain ignored, including in fresh CI clones. +if [[ ! -e "$ROOT/.pi-herdsman/.gitignore" ]]; then printf '*\n' > "$ROOT/.pi-herdsman/.gitignore"; fi +FIXTURE=$(mktemp -d "$ROOT/.pi-herdsman/completion-install.XXXXXX") || exit 1 +trap 'if [[ "$FAIL" -eq 0 ]]; then rm -rf -- "$FIXTURE"; else printf "Failure evidence: %s\n" "$FIXTURE"; fi' EXIT +BASH_PATH=${BASH:-/bin/bash} +# A repository path AND a HOME containing shell metacharacters must remain literal. +REPO="$FIXTURE/repo ' \$literal" +mkdir -p "$REPO" +cp "$ROOT/config/dotfiles/"*completion.sh "$REPO/" || exit 1 +INSTALLER="$REPO/install-completion.sh" +new_home() { + HOME_DIR="$FIXTURE/$1 home ' \$literal" + mkdir "$HOME_DIR" || die 'cannot create HOME fixture' + ZSH_DIR=$HOME_DIR +} +run() { + env -i PATH="$PATH" HOME="$HOME_DIR" ZDOTDIR="$ZSH_DIR" \ + "$BASH_PATH" "$INSTALLER" "$@" > "$FIXTURE/stdout" 2> "$FIXTURE/stderr" +} +snapshot() { + (cd "$HOME_DIR" && { find . -print; find . -type f -exec cksum {} \; -o -type l -exec readlink {} \;; } | LC_ALL=C sort) +} +assert_rejected() { + local label=$1 before after + shift + before=$(snapshot) + if run "$@"; then fail "$label rejected"; else pass "$label rejected"; fi + after=$(snapshot) + check "$label mutation-free" test "$before" = "$after" +} + +new_home existing +# Preserve a real old DevSetup managed block without running the general installer. +printf '# custom\r\nexport KEEP_ME="yes"\n' > "$HOME_DIR/.bashrc" +sed -n '/^# --- dev-setup managed block /,/^# --- end dev-setup managed block ---/p' \ + "$ROOT/config/dotfiles/.zshrc.template" >> "$HOME_DIR/.bashrc" +printf '\n# literal backslash \\ and no final newline' >> "$HOME_DIR/.bashrc" +cp "$HOME_DIR/.bashrc" "$HOME_DIR/.zshrc" +cp "$HOME_DIR/.bashrc" "$FIXTURE/original" +printf 'do not touch\n' > "$HOME_DIR/.gitconfig" +printf 'do not touch\n' > "$HOME_DIR/.bash_profile" +before=$(snapshot) +check 'existing dry-run succeeds' run --dry-run +check 'existing dry-run has zero mutations' test "$before" = "$(snapshot)" +check 'install succeeds with quoted paths and no final newline' run +check 'helper is a literal correct link' test "$(readlink "$HOME_DIR/.dev-setup-completion.sh")" = "$REPO/completion.sh" +for rc in .bashrc .zshrc; do + backups=("$HOME_DIR/$rc.dev-setup-completion.bak."*) + check "$rc exact pre-edit backup" cmp -s "$FIXTURE/original" "${backups[0]}" + check "$rc preserved original byte prefix" cmp -s "$FIXTURE/original" <(head -c "$(wc -c < "$FIXTURE/original")" "$HOME_DIR/$rc") +done +check 'unrelated dotfile untouched' test "$(cat "$HOME_DIR/.gitconfig")" = 'do not touch' +check 'bash_profile untouched' test "$(cat "$HOME_DIR/.bash_profile")" = 'do not touch' +before=$(snapshot) +check 'repeat install succeeds' run +check 'repeat install changes no bytes or backups' test "$before" = "$(snapshot)" +printf '\n# upgraded helper fixture\n' >> "$REPO/completion.sh" +check 'helper upgrade succeeds' run +check 'helper upgrade leaves hooks/backups unchanged' test "$before" = "$(snapshot)" +check 'helper link exposes upgraded source' cmp -s "$REPO/completion.sh" "$HOME_DIR/.dev-setup-completion.sh" +check 'uninstall dry-run succeeds' run --uninstall --dry-run +check 'uninstall dry-run is mutation-free' test "$before" = "$(snapshot)" +# Later user additions must survive uninstall, not be rolled back from a backup. +printf '# later user addition\n' >> "$HOME_DIR/.bashrc" +cat "$FIXTURE/original" > "$FIXTURE/expected" +printf '\n# later user addition\n' >> "$FIXTURE/expected" +check 'uninstall succeeds' run --uninstall +check 'uninstall preserves later additions with a line separator' cmp -s "$FIXTURE/expected" "$HOME_DIR/.bashrc" +check 'uninstall restores missing final newline' cmp -s "$FIXTURE/original" "$HOME_DIR/.zshrc" +check 'uninstall removes only helper link' test ! -L "$HOME_DIR/.dev-setup-completion.sh" +before=$(snapshot) +check 'repeat uninstall succeeds' run --uninstall +check 'repeat uninstall changes nothing' test "$before" = "$(snapshot)" + +# Removing a hook must not fuse executable user configuration across its +# separator. Only these innocuous assignment fixtures are sourced, in children +# with an empty environment and no personal startup files. +for prefix_kind in no_newline newline empty; do + for suffix_kind in eof text separated; do + new_home "boundary-$prefix_kind-$suffix_kind" + expected_a=1 + case "$prefix_kind" in + no_newline) printf 'export A=1' > "$FIXTURE/prefix" ;; + newline) printf 'export A=1\n' > "$FIXTURE/prefix" ;; + empty) : > "$FIXTURE/prefix"; expected_a='' ;; + esac + expected_b=2 + case "$suffix_kind" in + eof) : > "$FIXTURE/suffix"; expected_b='' ;; + text) printf 'export B=2\n' > "$FIXTURE/suffix" ;; + separated) printf '\nexport B=2\n' > "$FIXTURE/suffix" ;; + esac + cp "$FIXTURE/prefix" "$HOME_DIR/.bashrc" + cp "$FIXTURE/prefix" "$HOME_DIR/.zshrc" + label="boundary $prefix_kind/$suffix_kind" + check "$label install" run + cat "$FIXTURE/suffix" >> "$HOME_DIR/.bashrc" + cat "$FIXTURE/suffix" >> "$HOME_DIR/.zshrc" + cp "$FIXTURE/prefix" "$FIXTURE/expected" + if [[ "$prefix_kind/$suffix_kind" == no_newline/text ]]; then + printf '\n' >> "$FIXTURE/expected" + fi + cat "$FIXTURE/suffix" >> "$FIXTURE/expected" + check "$label uninstall" run --uninstall + for rc in .bashrc .zshrc; do + check "$label $rc exact bytes" cmp -s "$FIXTURE/expected" "$HOME_DIR/$rc" + # shellcheck disable=SC2016 + check "$label $rc separate assignments" env -i PATH="$PATH" HOME="$HOME_DIR" ZDOTDIR="$ZSH_DIR" \ + "$BASH_PATH" --noprofile --norc -c \ + '. "$1" && [ "${A-}" = "$2" ] && [ "${B-}" = "$3" ]' \ + completion-rc-check "$HOME_DIR/$rc" "$expected_a" "$expected_b" + done + done +done + +new_home missing +before=$(snapshot) +check 'missing rc dry-run succeeds' run --dry-run +check 'missing rc dry-run creates nothing' test "$before" = "$(snapshot)" +check 'missing rc uninstall succeeds' run --uninstall +check 'missing rc uninstall creates nothing' test "$before" = "$(snapshot)" +check 'missing rc install succeeds' run +check 'both missing rc files created' test -f "$HOME_DIR/.zshrc" +check 'fresh install makes no fake backup' test "$(find "$HOME_DIR" -name '*.bak.*' | wc -l | tr -d ' ')" = 0 +check 'fresh uninstall succeeds' run --uninstall +check 'new bashrc is left empty, not deleted' test ! -s "$HOME_DIR/.bashrc" +check 'new zshrc is left empty, not deleted' test ! -s "$HOME_DIR/.zshrc" + +new_home zdotdir +ZSH_DIR="$HOME_DIR/zsh dir" +mkdir "$ZSH_DIR" +printf 'untouched\n' > "$HOME_DIR/.zshrc" +printf 'export ZDOTDIR="do not execute me"\n' > "$HOME_DIR/.zshenv" +check 'explicit ZDOTDIR install succeeds' run +check 'HOME zshrc remains untouched with ZDOTDIR' test "$(cat "$HOME_DIR/.zshrc")" = untouched +check 'effective ZDOTDIR rc created' test -f "$ZSH_DIR/.zshrc" +check 'explicit ZDOTDIR uninstall succeeds' run --uninstall +before=$(snapshot) +if env -i PATH="$PATH" HOME="$HOME_DIR" "$BASH_PATH" "$INSTALLER" > "$FIXTURE/stdout" 2>&1; then + fail 'unresolved zshenv rejected' +else pass 'unresolved zshenv rejected'; fi +check 'unresolved zshenv changes nothing' test "$before" = "$(snapshot)" +ZSH_DIR=relative +assert_rejected 'relative ZDOTDIR' +ZSH_DIR="$HOME_DIR/absent" +assert_rejected 'missing ZDOTDIR' + +# Lexical directory spellings must not hide a symlink from preflight. Keep +# both the link and its real target inside HOME so snapshots cover every edit. +case_number=0 +printf '# preserve directory-boundary fixture\n' > "$FIXTURE/path-original" +for path_suffix in '/' '///' '/.' '/./' '/child/..' '/child/../'; do + case_number=$((case_number + 1)) + new_home "zdotdir-real-$case_number" + mkdir -p "$HOME_DIR/real-zsh/child" + cp "$FIXTURE/path-original" "$HOME_DIR/.bashrc" + cp "$FIXTURE/path-original" "$HOME_DIR/real-zsh/.zshrc" + ZSH_DIR="$HOME_DIR/real-zsh$path_suffix" + before=$(snapshot) + check "real ZDOTDIR $path_suffix preview" run --dry-run + check "real ZDOTDIR $path_suffix preview mutation-free" test "$before" = "$(snapshot)" + check "real ZDOTDIR $path_suffix install" run + check "real ZDOTDIR $path_suffix uninstall" run --uninstall + check "real ZDOTDIR $path_suffix preserves bashrc" cmp -s "$FIXTURE/path-original" "$HOME_DIR/.bashrc" + check "real ZDOTDIR $path_suffix preserves zshrc" cmp -s "$FIXTURE/path-original" "$HOME_DIR/real-zsh/.zshrc" +done +for path_suffix in '' '/' '///' '/.' '/./' '/child' '/child/.' '/./child//' '/child/..' '/../real-zsh'; do + case_number=$((case_number + 1)) + for mode in install dry-run uninstall uninstall-dry-run; do + new_home "zdotdir-link-$case_number-$mode" + mkdir -p "$HOME_DIR/real-zsh/child" + ln -s "$HOME_DIR/real-zsh" "$HOME_DIR/zsh-link" + ZSH_DIR="$HOME_DIR/real-zsh" + case "$path_suffix" in '/child'|'/child/.'|'/./child//') ZSH_DIR="$ZSH_DIR/child" ;; esac + cp "$FIXTURE/path-original" "$HOME_DIR/.bashrc" + cp "$FIXTURE/path-original" "$ZSH_DIR/.zshrc" + case "$mode" in + uninstall*) check "seed real ZDOTDIR for $path_suffix $mode" run ;; + esac + ZSH_DIR="$HOME_DIR/zsh-link$path_suffix" + [[ -d "$ZSH_DIR" ]] || die 'symlink variant must resolve to an existing directory' + case "$mode" in + install) set -- ;; + dry-run) set -- --dry-run ;; + uninstall) set -- --uninstall ;; + uninstall-dry-run) set -- --uninstall --dry-run ;; + esac + assert_rejected "symlink ZDOTDIR $path_suffix $mode" "$@" + done +done + +# Use an actual generated valid block as a fixture, then corrupt it in ways +# whose safe response is rejection of the entire install AND uninstall. +new_home hook_source +check 'generate valid hook fixture' run +cp "$HOME_DIR/.bashrc" "$FIXTURE/valid-hook" +for kind in truncated duplicate modified stray_end no_separator no_final_newline missing_markers; do + new_home "$kind" + printf '# first target must stay unchanged\n' > "$HOME_DIR/.bashrc" + case "$kind" in + truncated) head -n 3 "$FIXTURE/valid-hook" > "$HOME_DIR/.zshrc" ;; + duplicate) cat "$FIXTURE/valid-hook" "$FIXTURE/valid-hook" > "$HOME_DIR/.zshrc" ;; + modified) sed 's/! -r/! -f/' "$FIXTURE/valid-hook" > "$HOME_DIR/.zshrc" ;; + stray_end) printf '# <<< dev-setup completion v1 <<<\n' > "$HOME_DIR/.zshrc" ;; + no_separator) tail -n +2 "$FIXTURE/valid-hook" > "$HOME_DIR/.zshrc" ;; + no_final_newline) printf '%s' "$(cat "$FIXTURE/valid-hook")" > "$HOME_DIR/.zshrc" ;; + missing_markers) sed '/dev-setup completion/d' "$FIXTURE/valid-hook" > "$HOME_DIR/.zshrc" ;; + esac + assert_rejected "$kind hook" + before=$(snapshot) + if run --uninstall; then fail "$kind uninstall rejected"; else pass "$kind uninstall rejected"; fi + check "$kind uninstall mutation-free" test "$before" = "$(snapshot)" +done +for kind in symlink_rc dangling_rc directory_rc user_helper foreign_helper nul_rc; do + new_home "$kind" + printf '# first target unchanged\n' > "$HOME_DIR/.bashrc" + case "$kind" in + symlink_rc) printf '# linked\n' > "$HOME_DIR/realrc"; ln -s "$HOME_DIR/realrc" "$HOME_DIR/.zshrc" ;; + dangling_rc) ln -s "$HOME_DIR/absent" "$HOME_DIR/.zshrc" ;; + directory_rc) mkdir "$HOME_DIR/.zshrc" ;; + user_helper) printf '# mine\n' > "$HOME_DIR/.dev-setup-completion.sh" ;; + foreign_helper) ln -s "$HOME_DIR/foreign" "$HOME_DIR/.dev-setup-completion.sh" ;; + nul_rc) printf '# binary\000content\n' > "$HOME_DIR/.zshrc" ;; + esac + assert_rejected "$kind" +done +if [[ "$EUID" -ne 0 ]]; then + new_home permissions + printf '# preserve first target\n' > "$HOME_DIR/.bashrc" + printf '# read-only target\n' > "$HOME_DIR/.zshrc" + chmod 400 "$HOME_DIR/.zshrc" + assert_rejected 'read-only rc' + chmod 600 "$HOME_DIR/.zshrc" + ZSH_DIR="$FIXTURE/unsearchable" + mkdir "$ZSH_DIR" + chmod 200 "$ZSH_DIR" + assert_rejected 'unsearchable ZDOTDIR' + chmod 700 "$ZSH_DIR" +else + printf 'SKIP: permission-bit rejection cases require a non-root user\n' +fi +new_home no_zdotdir_env +check 'unset ZDOTDIR without zshenv uses HOME' env -i PATH="$PATH" HOME="$HOME_DIR" \ + "$BASH_PATH" "$INSTALLER" --dry-run + +printf '\nResults: %s passed, %s failed\n' "$PASS" "$FAIL" +[[ "$FAIL" -eq 0 ]] diff --git a/tests/test_completion_runtime.sh b/tests/test_completion_runtime.sh new file mode 100644 index 0000000..f584fad --- /dev/null +++ b/tests/test_completion_runtime.sh @@ -0,0 +1,316 @@ +#!/usr/bin/env bash +# Actual isolated shell + PTY tests. Python 3 is a TEST-only dependency. +# --zsh-init is only for explicitly supplied local unpacked-Zsh module/fpath setup. +# CI uses the native system shells without an adapter. +set -uo pipefail +PASS=0 FAIL=0 +pass() { printf 'PASS: %s\n' "$1"; PASS=$((PASS + 1)); } +fail() { printf 'FAIL: %s\n' "$1"; FAIL=$((FAIL + 1)); } +die() { printf 'ERROR: %s\n' "$1" >&2; exit 1; } +ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P) +BASH_PATH=/bin/bash +ZSH_PATH=$(command -v zsh || :) +ZSH_INIT='' +while [[ $# -gt 0 ]]; do + [[ $# -ge 2 ]] || die 'each option requires a path' + case "$1" in + --bash) BASH_PATH=$2 ;; + --zsh) ZSH_PATH=$2 ;; + --zsh-init) ZSH_INIT=$2 ;; + *) die "unknown option: $1" ;; + esac + shift 2 +done +for shell in "$BASH_PATH" "$ZSH_PATH"; do + [[ "$shell" == /* && -x "$shell" ]] || die 'both Bash and Zsh executable absolute paths are required' +done +[[ -z "$ZSH_INIT" || ( "$ZSH_INIT" == /* && -f "$ZSH_INIT" ) ]] || die 'invalid local Zsh adapter' +command -v python3 >/dev/null || die 'Python 3 is required for PTY tests' +mkdir -p "$ROOT/.pi-herdsman" || exit 1 +if [[ ! -e "$ROOT/.pi-herdsman/.gitignore" ]]; then printf '*\n' > "$ROOT/.pi-herdsman/.gitignore"; fi +FIXTURE=$(mktemp -d "$ROOT/.pi-herdsman/completion-runtime.XXXXXX") || exit 1 +trap 'if [[ "$FAIL" -eq 0 ]]; then rm -rf -- "$FIXTURE"; else printf "Failure evidence: %s\n" "$FIXTURE"; fi' EXIT +cat > "$FIXTURE/runtime.py" <<'PY' +import errno +import os +from pathlib import Path +import pty +import select +import shlex +import signal +import subprocess +import sys +import time + +shell, kind, scenario, root, base, adapter, installer_bash = sys.argv[1:] +work = Path(base) / (kind + '-' + scenario) +work.mkdir(mode=0o700) +home = work / 'home' +home.mkdir() +env = {'HOME': str(home), 'ZDOTDIR': str(home), 'INPUTRC': '/dev/null', + 'HISTFILE': '/dev/null', 'TERM': 'xterm', 'LC_ALL': 'C', 'PATH': os.environ['PATH']} +q = shlex.quote +if kind == 'zsh' and adapter: + # Load modules before Zsh first attempts line editing, without reading any + # other rc files. Native CI uses -df and needs no such environment adapter. + (home / '.zshenv').write_text('. ' + q(adapter) + '\nunsetopt RCS GLOBAL_RCS\n') +installer = str(Path(root) / 'config/dotfiles/install-completion.sh') +for rc in ('.bashrc', '.zshrc'): + (home / rc).write_text('# existing fixture configuration\n') +result = subprocess.run([installer_bash, installer], env=env, cwd=work, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=20) +assert result.returncode == 0, result.stderr.decode(errors='replace') +if scenario == 'uninstalled': + result = subprocess.run([installer_bash, installer, '--uninstall'], env=env, cwd=work, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=20) + assert result.returncode == 0, result.stderr.decode(errors='replace') +helper = str(home / ('.bashrc' if kind == 'bash' else '.zshrc')) +setup = '' +mode = 'vi' if 'vi' in scenario else 'emacs' +is_noop = scenario in ('disabled', 'noninteractive', 'uninstalled') +if kind == 'bash': + setup += 'set -o ' + mode + '\n' + if scenario.startswith('reuse'): + setup += '''_test_provider() { COMPREPLY=(ds516-aa ds516-ab ds516-ac); } +complete -F _test_provider ds516provider +''' + setup += '''for map in emacs-standard vi-insert; do + bind -m "$map" '"\\C-g": backward-char' + bind -m "$map" '"\\e[Z": "KEEP"' +done +snapshot() { + set +o + for map in emacs-standard vi-insert vi-command; do + bind -m "$map" -p + bind -m "$map" -s + done + complete -p +} +''' + if scenario.startswith('unavailable'): + # Simulation tests the availability branch, NOT native Bash 3.2 behavior. + setup += '''bind() { + if [ "${1:-}" = -l ]; then builtin bind -l | grep -v '^menu-complete-backward$'; + else builtin bind "$@"; fi +} +''' + setup += 'bind -l > widgets\n' +else: + if scenario.startswith('reuse') or scenario == 'security': + # Discover the selected shell's standard functions tree, not its + # site/vendor completion directories. Native Linux and macOS use + # functions/Completion and functions respectively. Fail on ambiguity. + ambient_fpath = '' + if scenario.startswith('reuse'): + ambient = work / 'ambient-insecure' + ambient.mkdir(mode=0o777) + ambient.chmod(0o777) + (ambient / '_ambient_fixture').write_text('#compdef ambient-fixture\n') + ambient_fpath = 'fpath=(' + q(str(ambient)) + ' $fpath)\n' + probe = ('. ' + q(adapter) + '\n' if adapter else '') + ambient_fpath + probe += 'builtin printf \'%s\\0\' "${fpath[@]}"' + result = subprocess.run([shell, '-d', '-f', '-c', probe], env=env, cwd=work, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=20) + assert result.returncode == 0, result.stderr.decode(errors='replace') + (work / 'native-fpath').write_bytes(result.stdout) + paths = list(dict.fromkeys(Path(os.fsdecode(p)) for p in result.stdout.split(b'\0') if p)) + cores = [p for p in paths if (p / 'compinit').is_file() and + (p.name == 'functions' or (p.name == 'Completion' and p.parent.name == 'functions'))] + assert len(cores) == 1, 'expected one native functions tree containing compinit: ' + repr(cores) + native_root = cores[0] if cores[0].name == 'functions' else cores[0].parent + assert native_root.is_absolute(), 'native function source must be absolute' + fixture_functions = work / 'functions' + fixture_functions.mkdir(mode=0o700) + # Flatten native fpath entries in lookup order, preserving first wins. + # Copy source bytes, not source permissions or compiled .zwc caches. + for directory in paths: + if not directory.is_dir() or (directory != native_root and native_root not in directory.parents): + continue + for source in sorted(directory.iterdir()): + target = fixture_functions / source.name + if source.is_file() and not source.name.endswith('.zwc') and not target.exists(): + target.write_bytes(source.read_bytes()) + target.chmod(0o600) + for required in ('compinit', 'compaudit', 'compdump', '_main_complete'): + assert (fixture_functions / required).is_file(), 'missing native function: ' + required + assert not (fixture_functions / '_ambient_fixture').exists(), 'ambient provider was copied' + isolated_fpath = 'fpath=(' + q(str(fixture_functions)) + ')\n' + setup += 'bindkey -' + ('v' if mode == 'vi' else 'e') + '\n' + setup += '''_test_shift() { LBUFFER+='KEEP'; } +zle -N _test_shift +for map in emacs viins; do + bindkey -M "$map" '^G' backward-char + bindkey -M "$map" '^[[Z' _test_shift +done +snapshot() { + setopt + bindkey -lL main + for map in emacs viins vicmd; do bindkey -M "$map"; done +} +''' + if scenario.startswith('reuse'): + setup += ambient_fpath + # The real native audit must reject the injected ambient directory. + # Then initialize over ONLY the secure copies, without bypass flags. + setup += 'if . ' + q(str(fixture_functions / 'compaudit')) + ' > ambient-audit; then exit 85; fi\n' + setup += isolated_fpath + setup += '''autoload -Uz compinit +compinit -D || exit 81 +print -rl -- $fpath > active-fpath +_test_provider() { compadd ds516-aa ds516-ab ds516-ac; } +compdef _test_provider ds516provider +zstyle ':completion:*' verbose yes +compinit() { print 'unexpected compinit rerun' >&2; exit 82; } +''' + if scenario == 'security': + insecure = work / 'insecure' + insecure.mkdir(mode=0o777) + insecure.chmod(0o777) + (insecure / '_unsafe').write_text('#compdef unsafe\n') + setup += isolated_fpath + setup += 'fpath=(' + q(str(insecure)) + ' $fpath)\n' + setup += 'autoload -Uz compaudit; compaudit > security-audit\n' + setup += 'autoload -Uz compinit; compinit -D || :\n' +setup += 'snapshot > before\n' +if scenario == 'disabled': + setup += 'export DEV_SETUP_COMPLETION=0\n' +setup += '. ' + q(helper) + '\nsnapshot > after\n' +# Sourcing twice must not disturb existing editing state or completion providers. +if not is_noop: + setup += '. ' + q(helper) + '\nsnapshot > repeated\n' +if kind == 'zsh': + setup += 'typeset -f compdef > compdef-state\n' + if scenario.startswith('reuse'): + setup += '[[ ${_comps[ds516provider]} == _test_provider ]] || exit 83\n' + setup += "zstyle -t ':completion:*' verbose || exit 84\n" +setup += '''ds516command-aa() { printf '__COMMAND__aa\\n'; } +ds516command-ab() { printf '__COMMAND__ab\\n'; } +ds516provider() { printf '__RESULT__%s\\n' "$1"; } +PS1='__PROMPT__ ' +PS2='__CONTINUE__ ' +printf '__READY__\\n' +''' +startup = work / 'startup' +startup.write_text(setup) +options = ['--noprofile', '--norc'] if kind == 'bash' else (['-d'] if adapter else ['-d', '-f']) + +def unrelated(lines): + if kind == 'bash': + # bind -p prints synthetic comments for unbound functions; these are + # not bindings. ESC is rendered as \\M- in -p, but as \\e in -s. + return [s for s in lines if not s.startswith(('# ', '"\\C-i":', '"\\e[Z":', '"\\M-[Z":'))] + return [s for s in lines if not s.startswith(('"^I" ', '"^[[Z" '))] + +if is_noop or scenario == 'security': + flags = [] if scenario == 'noninteractive' else ['-i'] + result = subprocess.run([shell] + options + flags + [str(startup)], env=env, + cwd=work, input=b'', stdout=subprocess.PIPE, + stderr=subprocess.PIPE, timeout=20) + (work / 'stdout').write_bytes(result.stdout) + (work / 'stderr').write_bytes(result.stderr) + assert result.returncode == 0, result.stderr.decode(errors='replace') + if is_noop: + assert (work / 'before').read_bytes() == (work / 'after').read_bytes(), 'noop changed state' + else: + assert unrelated((work / 'before').read_text().splitlines()) == unrelated((work / 'after').read_text().splitlines()), 'security case changed unrelated state' + if kind == 'zsh': + assert (work / 'compdef-state').read_bytes() == b'', 'noop initialized completion' + if scenario == 'security': + assert (work / 'security-audit').read_text().splitlines() == [str(insecure)], 'audit must detect only the intentional insecure directory' + assert b'initialization aborted' in result.stderr, 'compinit security was not exercised' + sys.exit(0) + +for name in ('ds516-aa', 'ds516-ab', 'ds516-ac'): + (work / name).touch() +for name in ('ds516dir-aa', 'ds516dir-ab'): + (work / name).mkdir() +pid, fd = pty.fork() +if pid == 0: + os.chdir(work) + os.execve(shell, [shell] + options + ['-i'], env) +transcript = bytearray() + +def send(data): + os.write(fd, data) + +def until(token, timeout=15): + received = bytearray() + end = time.monotonic() + timeout + while time.monotonic() < end: + if select.select([fd], [], [], 0.1)[0]: + try: + chunk = os.read(fd, 65536) + except OSError as e: + if e.errno == errno.EIO: + break + raise + if not chunk: + break + received.extend(chunk) + transcript.extend(chunk) + if token in received: + return bytes(received) + raise AssertionError('PTY did not produce ' + repr(token) + ': ' + repr(received)) + +try: + send(('. ' + q(str(startup)) + '\n').encode()) + until(b'__READY__') + if kind == 'zsh' and scenario.startswith('reuse'): + assert str(ambient) in (work / 'ambient-audit').read_text().splitlines(), 'ambient insecurity was not exercised' + assert (work / 'active-fpath').read_text().splitlines() == [str(fixture_functions)], 'ambient fpath leaked into compinit' + # Only Tab and Shift+Tab entries may differ. Mode, vi command map, and all + # other bindings must be byte-identical, even after repeated sourcing. + before = (work / 'before').read_text(encoding='latin1').splitlines() + after = (work / 'after').read_text(encoding='latin1').splitlines() + assert unrelated(before) == unrelated(after), 'unrelated binding or editing mode changed' + assert (work / 'after').read_bytes() == (work / 'repeated').read_bytes(), 'repeat changed state' + backward = kind == 'zsh' or 'menu-complete-backward' in (work / 'widgets').read_text().splitlines() + if not backward: + assert [s for s in before if s.startswith('"\\e[Z":')] == [s for s in after if s.startswith('"\\e[Z":')], 'old Readline Shift+Tab changed' + # Complete actual filenames, going forward twice then backward once. + prefix = b'ds516provider ' if scenario.startswith('reuse') else b"printf '__RESULT__%s\\n' " + send(prefix + b'ds516-\t\t' + (b'\x1b[Z' if backward else b'') + b'\n') + until(b'__RESULT__ds516-aa' if backward else b'__RESULT__ds516-ab') + if not backward: + send(b"printf '__PRESERVED__%s\\n' value\x1b[Z\n") + until(b'__PRESERVED__valueKEEP') + # Native command and directory providers, not only programmable completion. + send(b'ds516command-\t\n') + until(b'__COMMAND__aa') + send(b"printf '__DIRECTORY__%s\\n' ds516dir-\t\n") + # Zsh's initialized completion may remove the auto-added slash on accept. + until(b'__DIRECTORY__ds516dir-aa') + # Later user bindings win; no recurring prompt-time rebinding. + if kind == 'bash': + send(b'''bind -m emacs-standard '"\\C-i": "LATER"'; bind -m vi-insert '"\\C-i": "LATER"'; printf '__OVERRIDE__\\n'\n''') + else: + send(b'''_later() { LBUFFER+='LATER'; }; zle -N _later; bindkey -M emacs '^I' _later; bindkey -M viins '^I' _later; printf '__OVERRIDE__\\n'\n''') + until(b'__OVERRIDE__\r\n') + send(b"printf '__LATER__%s\\n' value\t\n") + until(b'__LATER__valueLATER') + print('native backward widget: ' + str(backward)) +finally: + (work / 'terminal.log').write_bytes(transcript) + # Only this exact isolated child is terminated; no process-name cleanup. + os.close(fd) + try: + os.kill(pid, signal.SIGHUP) + except ProcessLookupError: + pass + os.waitpid(pid, 0) +PY +for kind in bash zsh; do + case "$kind" in + bash) shell=$BASH_PATH; scenarios='emacs vi disabled noninteractive uninstalled unavailable unavailable-vi reuse-emacs reuse-vi' ;; + zsh) shell=$ZSH_PATH; scenarios='emacs vi disabled noninteractive uninstalled reuse-emacs reuse-vi security' ;; + esac + for scenario in $scenarios; do + if python3 "$FIXTURE/runtime.py" "$shell" "$kind" "$scenario" "$ROOT" "$FIXTURE" "$ZSH_INIT" "$BASH_PATH"; then + pass "$kind $scenario runtime and state" + else + fail "$kind $scenario runtime and state" + fi + done +done +printf '\nResults: %s passed, %s failed\n' "$PASS" "$FAIL" +[[ "$FAIL" -eq 0 ]]