diff --git a/README.md b/README.md index fab1228..b4cd7fa 100644 --- a/README.md +++ b/README.md @@ -22,21 +22,22 @@ nothing is configured twice: | Layer | Owns | Mechanism | |----------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------| -| **[omnishell](https://github.com/JtheGunner/omnishell)** | rc *lines*: plugin inits, history, completion, `fzf`, `zoxide`, `modern-aliases`, `colorized-man`, `direnv`, `broot`, `mise`, `starship` (prompt + its seeded config), `root-loops` (OSC palette push), `tmux` (installs it; auto-attach is off) | one marker block in `~/.zshrc` / `~/.bashrc`, sourcing a generated `init.` | +| **[omnishell](https://github.com/JtheGunner/omnishell)** | rc *lines*: plugin inits, history, completion, `zsh-completions`, `fzf`, `zoxide`, `modern-aliases`, `colorized-man`, `direnv`, `broot`, `mise`, `starship` (prompt + its seeded config), `root-loops` (OSC palette push), `tmux` (installs it; auto-attach is off) | one marker block in `~/.zshrc` / `~/.bashrc`, sourcing a generated `init.` | | **this repo: stow packages** | standalone config *files*: `git`, `tmux` (`.tmux.conf` only), `bat`, `ghostty` (+ optional `nvim`) | [GNU Stow](https://www.gnu.org/software/stow/) symlinks into `$HOME` | | **this repo: rc libraries** | the base `~/.zshrc` / `~/.bashrc` content: env, keybindings, zsh completion styling | `zsh/zshrc.zsh` + `bash/bashrc.bash`, **`source`d** from a generated real rc file (not stowed, see below) | | **this repo: `shell.d/`** | personal rc lines not worth a public module: k8s / docker aliases, `linuxbrew`, helper functions | a second marker block, sourced *after* omnishell | | **`~/.zshrc.local` / `~/.bashrc.local`** | machine-specific and secret: per-host `PATH`, tool completions, tokens. **Not version-controlled.** | sourced last by the same marker block | omnishell is consumed as a released binary (Homebrew tap / curl installer); this -repo never modifies it. **omnishell 0.8.0 or newer is required**: older releases +repo never modifies it. **omnishell 0.9.0 or newer is required**: older releases don't know all of the `starship`, `root-loops`, `tmux`, `broot`, `direnv`, `mise` and `colorized-man` modules, and install `mise`, `starship` and `broot` with a long source build where no distro package exists, 0.7.0 is the first with `omnishell tui`, which `--interactive` uses, before 0.7.1 a tool installed from a release binary or a source build is not on `PATH` at shell start (no -`starship` prompt), and before 0.8.0 `modern-aliases` is degraded where the -distro has no `eza` package. `bootstrap.sh` upgrades an older +`starship` prompt), before 0.8.0 `modern-aliases` is degraded where the +distro has no `eza` package, and before 0.9.0 there is no `zsh-completions` +module. `bootstrap.sh` upgrades an older install, or stops with a message if it can't. ```text @@ -78,11 +79,11 @@ exec $SHELL `bootstrap.sh`: 1. checks that nothing points at another checkout (see [Multiple checkouts](#multiple-checkouts)) -2. installs dependencies, omnishell (upgrading it below 0.8.0) and Ghostty (unless switched off), and zsh if you opted in, then offers to make zsh your login shell (see [Settings file](#-settings-file)); on apt systems with a CPU other than x86_64 / arm64 also a Rust toolchain +2. installs dependencies, omnishell (upgrading it below 0.9.0) and Ghostty (unless switched off), and zsh if you opted in, then offers to make zsh your login shell (see [Settings file](#-settings-file)); on apt systems with a CPU other than x86_64 / arm64 also a Rust toolchain 3. writes real `~/.zshrc` / `~/.bashrc` 4. stows the packages (unless skipped) 5. writes the git `delta` config and asks for your git identity (unless skipped) (see [Git identity](#-git-identity)) -6. applies `omnishell/config.toml`, which also installs `starship`, `mise`, `tmux`, `direnv` and `broot` +6. on Homebrew, removes the group/other write bit from `share` and `share/zsh` so zsh's `compinit` accepts the `zsh-completions` directory (unless switched off), then applies `omnishell/config.toml`, which also installs `zsh-completions`, `starship`, `mise`, `tmux`, `direnv` and `broot` 7. appends the `shell.d` block to both rc files 8. renders the terminal colour themes @@ -180,6 +181,7 @@ overwritten. Point `DOTFILES_CONFIG` at another path to use a different file. | `[bootstrap]` `skip_packages` | `fzf` `zoxide` `ripgrep` `fd` `bat` `git-delta` (list) | `[]` | - | `DOTFILES_SKIP_PACKAGES` | | `[bootstrap]` `skip_stow` | `git` `tmux` `bat` `ghostty` (list) | `[]` | - | `DOTFILES_SKIP_STOW` | | `[bootstrap]` `terminal_app_profile` | `true` · `false` | `true` | - | `DOTFILES_TERMINAL_APP_PROFILE` | +| `[bootstrap]` `fix_completion_permissions` | `true` · `false` | `true` | - | `DOTFILES_FIX_COMPLETION_PERMISSIONS` | | `[bootstrap]` `assume_yes` | `true` · `false` | `false` | `--yes` | `DOTFILES_ASSUME_YES` | | `[bootstrap]` `terminals` | list of package names | `[]` | - | `DOTFILES_TERMINALS` | | `[ghostty]` `keybinds` | `"auto"` · `"mac"` · `"linux"` | `"auto"`| - | `DOTFILES_GHOSTTY_KEYBINDS` | @@ -221,6 +223,12 @@ size = 10000 > Ghostty installation. The `ghostty` config is still stowed, so installing Ghostty later needs no > further setup. The environment variable accepts `1`/`0`, `true`/`false` or `yes`/`no`. > +> `fix_completion_permissions = false` (or `DOTFILES_FIX_COMPLETION_PERMISSIONS=0`) stops +> `bootstrap.sh` from changing permissions under the Homebrew prefix. By default it runs +> `chmod go-w` on `share` and `share/zsh` (recursively, directories only) when they are group- or +> world-writable, they belong to you and the `zsh-completions` module is on; a directory that belongs +> to someone else is reported with the `sudo chmod go-w` command to run. Without Homebrew nothing happens. +> > `skip_packages` leaves base packages out of the installation and `skip_stow` leaves stow > packages unlinked and their generated config unrendered. Skipping only stops bootstrap from > acting: a package that is already installed or linked stays that way (nothing is uninstalled or @@ -372,6 +380,7 @@ omnishell doctor # check for drift / degraded modules | Module | What it does | Notable options | |-------------------------------------------------------------------|-----------------------------------------------------------------------------|----------------------------------------------| | `completion`, `history` | zsh/bash plugin baseline | `history.size = 50000` | +| `zsh-completions` | zsh only: extra completion definitions, on `fpath` before `compinit` | - | | `autosuggestions`, `syntax-highlighting` | zsh only: grey inline suggestions, command colouring as you type | - | | `fzf` | key bindings + defaults | `ctrl_r`, `ctrl_t`, `default_opts` | | `zoxide` | smarter `cd` | `cmd = "z"` | diff --git a/bootstrap.sh b/bootstrap.sh index 2cc1937..27862b0 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -107,6 +107,7 @@ read_conf_values() { CONF_SKIP_PACKAGES="$(settings_get bootstrap.skip_packages)" CONF_SKIP_STOW="$(settings_get bootstrap.skip_stow)" CONF_TERMINAL_APP_PROFILE="$(settings_get bootstrap.terminal_app_profile)" + CONF_FIX_COMPLETION_PERMISSIONS="$(settings_get bootstrap.fix_completion_permissions)" CONF_ASSUME_YES="$(settings_get bootstrap.assume_yes)" CONF_TERMINALS="$(settings_get bootstrap.terminals)" CONF_GHOSTTY_KEYBINDS="$(settings_get ghostty.keybinds)" @@ -215,11 +216,13 @@ reload_settings() { # its vendor/bin on PATH, without which a release binary or source build is not # found at shell start (no starship prompt, no mise / broot); 0.8.0 installs # eza as a release binary where the distro has no package (Debian 12), instead -# of degrading modern-aliases. What has no +# of degrading modern-aliases; 0.9.0 adds the zsh-completions module (fpath before +# compinit) and the `omnishell doctor` check for writable completion directories. +# What has no # binary (broot on 32-bit ARM, mise on armv6, everything on other CPUs) still # builds from source, which needs Rust 1.95 for mise / starship (broot: 1.85) - # newer than what Debian / Ubuntu LTS ship as `cargo`. -OMNISHELL_MIN_VERSION="0.8.0" +OMNISHELL_MIN_VERSION="0.9.0" RUST_MIN_VERSION="1.95" # What a cargo build of mise needs besides Rust: a C toolchain, cmake (libz-ng-sys), @@ -1158,6 +1161,55 @@ warn_skipped_packages_needed_by_modules() { done } +# Whether fix_completion_permissions() changes anything, highest precedence first: +# DOTFILES_FIX_COMPLETION_PERMISSIONS (1, true, yes / 0, false, no), +# fix_completion_permissions in the settings file, default on. +_fix_completion_permissions_enabled() { + local mode="" + if [ -n "${DOTFILES_FIX_COMPLETION_PERMISSIONS:-}" ]; then + case "$DOTFILES_FIX_COMPLETION_PERMISSIONS" in + 1 | true | yes) mode=yes ;; + 0 | false | no) mode=no ;; + *) warn "invalid DOTFILES_FIX_COMPLETION_PERMISSIONS value '$DOTFILES_FIX_COMPLETION_PERMISSIONS' (use 1, true, yes, 0, false or no) - ignored" ;; + esac + fi + if [ -z "$mode" ]; then + case "$CONF_FIX_COMPLETION_PERMISSIONS" in false) mode=no ;; *) mode=yes ;; esac + fi + [ "$mode" = yes ] +} + +_dir_is_mine() { [ -O "$1" ]; } + +# zsh's compinit refuses to load completions from a directory that group or others +# can write to ("zsh compinit: insecure directories"). Homebrew's share/ and +# share/zsh/ are group-writable on some installs, so drop the group/other write bit +# there - but only when the omnishell zsh-completions module is on and the +# directory is yours; someone else's is reported with the command to run. +fix_completion_permissions() { + command -v brew >/dev/null 2>&1 || return 0 + _is_in_list zsh-completions "$(_enabled_omnishell_modules)" || return 0 + _fix_completion_permissions_enabled || { log "skipping completion directory permissions (fix_completion_permissions)"; return 0; } + local prefix dir fixed=0 + prefix="$(brew --prefix 2>/dev/null)" || return 0 + [ -n "$prefix" ] || return 0 + while IFS= read -r dir; do + [ -n "$dir" ] || continue + if _dir_is_mine "$dir"; then + if chmod go-w "$dir"; then fixed=$((fixed + 1)); else warn "could not run chmod go-w on $dir"; fi + else + # %q: a directory name is data, so the suggested command must stay one safe line + warn "$dir can be written by group or others but belongs to someone else - run: sudo chmod go-w $(printf '%q' "$dir")" + fi + done < <( + find "$prefix/share" -maxdepth 0 -type d \( -perm -020 -o -perm -002 \) 2>/dev/null || true + find "$prefix/share/zsh" -type d \( -perm -020 -o -perm -002 \) 2>/dev/null || true + ) + if [ "$fixed" -gt 0 ]; then + log "removed group/other write permission from $fixed directories under $prefix/share (zsh compinit rejects them)" + fi +} + apply_omnishell() { prepare_omnishell_config warn_skipped_packages_needed_by_modules @@ -1500,6 +1552,7 @@ main() { setup_git_identity setup_git_signing_key fi + fix_completion_permissions apply_omnishell wire_shell_d log "rendering Root Loops colors" diff --git a/config.toml.example b/config.toml.example index 047d6f3..4b07378 100644 --- a/config.toml.example +++ b/config.toml.example @@ -56,6 +56,13 @@ # Environment: DOTFILES_TERMINAL_APP_PROFILE (1/0, true/false, yes/no) #terminal_app_profile = true # +# Drop the group/other write bit from Homebrew's share and share/zsh directories when the +# omnishell zsh-completions module is enabled: true or false. zsh's compinit rejects +# completions from writable directories ("insecure directories"). Only directories you +# own are changed; others are reported with the command to run. Only matters with Homebrew. +# Environment: DOTFILES_FIX_COMPLETION_PERMISSIONS (1/0, true/false, yes/no) +#fix_completion_permissions = true +# # Answer "yes" to every prompt, e.g. switch rc files and stow links from another # checkout to this one. # Flag: --yes Environment: DOTFILES_ASSUME_YES (1 or 0) diff --git a/lib/settings.sh b/lib/settings.sh index db6e955..59b2702 100644 --- a/lib/settings.sh +++ b/lib/settings.sh @@ -119,6 +119,7 @@ bootstrap.install_ghostty bool bootstrap.skip_packages list bootstrap.skip_stow list bootstrap.terminal_app_profile bool +bootstrap.fix_completion_permissions bool ghostty.keybinds enum:auto,mac,linux ghostty.font_family string ghostty.font_size positive diff --git a/omnishell/config.toml b/omnishell/config.toml index ff9bc25..6ea4cc5 100644 --- a/omnishell/config.toml +++ b/omnishell/config.toml @@ -20,6 +20,12 @@ shells = ["zsh", "bash"] [modules.completion] enabled = true +# Extra zsh completion definitions, put on fpath before the compinit of the module +# above (zsh only; omnishell >= 0.9.0). bootstrap.sh also drops the group/other write +# bit from Homebrew's share directories, which compinit would otherwise reject. +[modules.zsh-completions] +enabled = true + [modules.history] enabled = true [modules.history.options] diff --git a/tests/test-interactive.sh b/tests/test-interactive.sh index 8e8f496..b18537b 100644 --- a/tests/test-interactive.sh +++ b/tests/test-interactive.sh @@ -43,7 +43,7 @@ check "the error names the settings file" 'grep -qF "$CONF" <<< "$OU check "and nothing was written" '[ ! -e "$WORK/home/.zshrc" ] && cmp -s "$CONF" "$DOTFILES/config.toml.example"' check "--help lists --interactive" '"$DOTFILES/bootstrap.sh" --help | grep -q -- --interactive' ix_run '' 'printf %s "$OMNISHELL_MIN_VERSION"' -check "omnishell 0.8.0 is the floor" '[ "$OUT" = 0.8.0 ]' +check "omnishell 0.9.0 is the floor" '[ "$OUT" = 0.9.0 ]' echo ">> reloading settings" fresh @@ -55,14 +55,14 @@ empties() { local i; for ((i = 0; i < $1; i++)); do printf '\\n'; done; } echo ">> prompts: Enter keeps everything" fresh -ix_run "$(empties 25)" 'interactive_settings; echo AFTER' +ix_run "$(empties 26)" 'interactive_settings; echo AFTER' check "Enter at every prompt leaves the file byte-identical" 'cmp -s "$CONF" "$DOTFILES/config.toml.example"' check "no backup is made and the run continues" '[ ! -e "$CONF.bak" ] && grep -q AFTER <<< "$OUT"' check "the prompts show table.key and unset" 'grep -q "bootstrap.install_zsh" <<< "$OUT" && grep -q "\[unset\]" <<< "$OUT"' echo ">> prompts: answers are validated and written" fresh -ix_run "yes\n\n\nfoot, kitty\n\n\n\n\nbogus\nlinux\n$(empties 16)y\n" 'interactive_settings; printf "%s|%s" "$CONF_INSTALL_ZSH" "$CONF_TERMINALS"' +ix_run "yes\n\n\nfoot, kitty\n\n\n\n\n\nbogus\nlinux\n$(empties 16)y\n" 'interactive_settings; printf "%s|%s" "$CONF_INSTALL_ZSH" "$CONF_TERMINALS"' check "the run succeeds and reloads the settings" '[ "$RC" = 0 ] && grep -q "yes|foot kitty\$" <<< "$OUT"' check "install_zsh is written" 'grep -qx "install_zsh = \"yes\"" "$CONF"' check "the list is written" 'grep -qx "terminals = \[\"foot\", \"kitty\"\]" "$CONF"' @@ -73,7 +73,7 @@ check "the template comments are still there" 'grep -q "^# Install echo ">> prompts: skip_stow is written and applied" fresh -ix_run "$(empties 6)tmux\n$(empties 18)y\n" 'interactive_settings; printf "%s|%s" "$CONF_SKIP_STOW" "${PACKAGES[*]}"' +ix_run "$(empties 6)tmux\n$(empties 19)y\n" 'interactive_settings; printf "%s|%s" "$CONF_SKIP_STOW" "${PACKAGES[*]}"' check "the run succeeds and reloads the list" '[ "$RC" = 0 ] && grep -q "tmux|zsh git" <<< "$OUT"' check "skip_stow is written" 'grep -qx "skip_stow = \[\"tmux\"\]" "$CONF"' check "PACKAGES no longer contains tmux after the reload" 'grep -q "tmux|zsh git bat ghostty\$" <<< "$OUT"' @@ -81,12 +81,12 @@ check "PACKAGES no longer contains tmux after the reload" 'grep -q "tmux|zsh g echo ">> prompts: a dash clears a key" fresh printf '[bootstrap]\ninstall_zsh = "yes"\n# note\n' > "$CONF" -ix_run "-\n$(empties 24)y\n" 'interactive_settings' +ix_run "-\n$(empties 25)y\n" 'interactive_settings' check "the key is gone and the rest stays" '! grep -q install_zsh "$CONF" && grep -qx "# note" "$CONF" && grep -qx "\[bootstrap\]" "$CONF"' echo ">> review: declining and end of input" fresh -ix_run "yes\n$(empties 24)n\n" 'interactive_settings; echo AFTER' +ix_run "yes\n$(empties 25)n\n" 'interactive_settings; echo AFTER' check "n ends the run with exit 0" '[ "$RC" = 0 ] && ! grep -q AFTER <<< "$OUT" && grep -q "nothing written" <<< "$OUT"' check "n leaves the file untouched" 'cmp -s "$CONF" "$DOTFILES/config.toml.example" && [ ! -e "$CONF.bak" ]' check "the diff was shown" 'grep -q "^+install_zsh = \"yes\"" <<< "$OUT"' @@ -137,7 +137,7 @@ check "both steps do nothing without --interactive" '[ "$RC" = 0 ] && gr echo ">> docs" check "the README names --interactive" 'grep -q -- "--interactive" "$DOTFILES/README.md"' -check "the README names the 0.8.0 floor" 'grep -q "omnishell 0.8.0 or newer" "$DOTFILES/README.md" && ! grep -q "0\.6\.0" "$DOTFILES/README.md"' +check "the README names the 0.9.0 floor" 'grep -q "omnishell 0.9.0 or newer" "$DOTFILES/README.md" && ! grep -q "0\.6\.0" "$DOTFILES/README.md"' check "the template mentions --interactive" 'grep -q -- "--interactive" "$DOTFILES/config.toml.example"' echo ">> modules: a declined selection does not linger" @@ -155,28 +155,28 @@ check "the README tells both n cases apart" 'grep -q "setting echo ">> hardening: temp files, atomic write, one backup" fresh -ix_run "yes\n$(empties 24)" 'interactive_settings' +ix_run "yes\n$(empties 25)" 'interactive_settings' check "end of input at the settings review leaves no temp file" '[ "$RC" = 1 ] && [ -z "$(ls -A "$WORK/tmp")" ]' fresh printf 'printf "\\n[modules.testmod]\\nenabled = true\\n" >> "%s"\n' "$LIVE" > "$WORK/tui.sh" ix_run "" 'interactive_modules' check "end of input at the module review leaves no temp file" '[ "$RC" = 1 ] && [ -z "$(ls -A "$WORK/tmp")" ]' fresh -ix_run "yes\n$(empties 24)n\n" 'interactive_settings' +ix_run "yes\n$(empties 25)n\n" 'interactive_settings' check "declining leaves no temp file either" '[ "$RC" = 0 ] && [ -z "$(ls -A "$WORK/tmp")" ]' fresh; chmod 644 "$CONF" -ix_run "yes\n$(empties 24)y\n" 'interactive_settings' +ix_run "yes\n$(empties 25)y\n" 'interactive_settings' check "the write leaves no staging file next to the settings file" '[ "$(ls "$WORK/cfg" | tr "\n" " ")" = "config.toml config.toml.bak " ]' check "and keeps the file mode" '[ "$(ls -l "$CONF" | cut -c1-10)" = "-rw-r--r--" ]' fresh -ix_run "yes\n$(empties 24)y\ny\n" 'interactive_settings; interactive_modules' +ix_run "yes\n$(empties 25)y\ny\n" 'interactive_settings; interactive_modules' check "both steps write: both changes are in the file" 'grep -qx "install_zsh = \"yes\"" "$CONF" && grep -qx "\[modules.testmod\]" "$CONF"' check "and the .bak still holds the original" 'cmp -s "$CONF.bak" "$DOTFILES/config.toml.example"' echo ">> review fixes: a symlinked settings file" fresh mkdir -p "$WORK/priv"; mv "$CONF" "$WORK/priv/config.toml"; ln -s "$WORK/priv/config.toml" "$CONF" -ix_run "yes\n$(empties 24)y\n" 'interactive_settings' +ix_run "yes\n$(empties 25)y\n" 'interactive_settings' check "the symlink is still a symlink" '[ -L "$CONF" ]' check "the content went to the file it points at" 'grep -qx "install_zsh = \"yes\"" "$WORK/priv/config.toml"' check "no staging file is left next to the target" '[ "$(ls "$WORK/priv" | tr "\n" " ")" = "config.toml " ]' @@ -210,7 +210,7 @@ check "the same probe sees the four traps once they are set" '[ "$OUT" = 4 ]' echo ">> a settings directory that is not writable" if [ "$(id -u)" -ne 0 ]; then fresh; chmod a-w "$WORK/cfg" - ix_run "yes\n$(empties 24)y\n" 'interactive_settings' + ix_run "yes\n$(empties 25)y\n" 'interactive_settings' chmod u+w "$WORK/cfg" check "the write fails and the message names the directory" '[ "$RC" = 1 ] && grep -q "must be writable" "$WORK/err" && grep -qF "$WORK/cfg" "$WORK/err"' check "the settings file is untouched" 'cmp -s "$CONF" "$DOTFILES/config.toml.example"' @@ -221,7 +221,7 @@ echo ">> review fixes: a read-only target behind a symlink, a failed write at th if [ "$(id -u)" -ne 0 ]; then fresh; mkdir -p "$WORK/priv"; mv "$CONF" "$WORK/priv/config.toml"; ln -s "$WORK/priv/config.toml" "$CONF" printf 'OLD\n' > "$CONF.bak"; chmod a-w "$WORK/priv" - ix_run "yes\n$(empties 24)y\n" 'interactive_settings' + ix_run "yes\n$(empties 25)y\n" 'interactive_settings' chmod u+w "$WORK/priv" check "the message names the directory of the real file" '[ "$RC" = 1 ] && grep -qE "directory [^ ]*/priv must be writable" "$WORK/err"' check "an existing backup survives a failed write" '[ "$(cat "$CONF.bak")" = OLD ]' diff --git a/tests/test-omnishell-toolchain.sh b/tests/test-omnishell-toolchain.sh index 4c35836..94a9eca 100644 --- a/tests/test-omnishell-toolchain.sh +++ b/tests/test-omnishell-toolchain.sh @@ -18,8 +18,8 @@ fail() { printf ' FAIL %s\n' "$1"; failures=$((failures + 1)); } check() { if eval "$2"; then pass "$1"; else fail "$1"; fi; } # the omnishell an upgrade would install -NEW_OMNISHELL="$WORK/omnishell-0.8.0" -printf '#!/bin/sh\n[ "$1" = version ] && echo "0.8.0 (commit abc, built now)"\n' > "$NEW_OMNISHELL" +NEW_OMNISHELL="$WORK/omnishell-0.9.0" +printf '#!/bin/sh\n[ "$1" = version ] && echo "0.9.0 (commit abc, built now)"\n' > "$NEW_OMNISHELL" chmod +x "$NEW_OMNISHELL" # a fresh stub dir (BIN) and HOME for each case @@ -64,20 +64,20 @@ check "1.75.0 < 1.95 (different field counts)" '[ "$OUT" = no ]' echo ">> install_omnishell: current version" new_case current -omnishell_stub "$BIN" 0.8.0 +omnishell_stub "$BIN" 0.9.0 stub curl 'exit 1' run_fn install_omnishell -check "0.8.0 is kept" '[ "$RC" = 0 ] && grep -q "already installed (0.8.0" <<< "$OUT"' +check "0.9.0 is kept" '[ "$RC" = 0 ] && grep -q "already installed (0.9.0" <<< "$OUT"' check "no installer was fetched" '! grep -q "^curl" "$CALLS"' echo ">> install_omnishell: too old, curl installer upgrades it" new_case upgrade -omnishell_stub "$BIN" 0.7.1 # the release just below the floor +omnishell_stub "$BIN" 0.8.0 # the release just below the floor # the stub installer drops a newer omnishell into ~/.local/bin, which comes first on PATH stub curl "echo 'mkdir -p \"\$HOME/.local/bin\"; cp \"$NEW_OMNISHELL\" \"\$HOME/.local/bin/omnishell\"'" run_fn install_omnishell check "old version triggers the upgrade" 'grep -q "^curl .*omnishell/main/install.sh" "$CALLS"' -check "reports the upgrade" 'grep -q "below the minimum 0.8.0" <<< "$OUT"' +check "reports the upgrade" 'grep -q "below the minimum 0.9.0" <<< "$OUT"' check "run succeeds" '[ "$RC" = 0 ]' echo ">> install_omnishell: too old and the upgrade does not help" @@ -86,7 +86,7 @@ omnishell_stub "$BIN" 0.5.0 stub curl 'echo true' run_fn install_omnishell check "stops instead of carrying on" '[ "$RC" -ne 0 ]' -check "names the version and the fix" 'grep -q "0.5.0" <<< "$OUT" && grep -q "0.8.0" <<< "$OUT"' +check "names the version and the fix" 'grep -q "0.5.0" <<< "$OUT" && grep -q "0.9.0" <<< "$OUT"' echo ">> install_omnishell: Homebrew upgrades it" new_case brew diff --git a/tests/test-optional-parts.sh b/tests/test-optional-parts.sh index d47c749..014d6be 100755 --- a/tests/test-optional-parts.sh +++ b/tests/test-optional-parts.sh @@ -14,7 +14,7 @@ check() { if eval "$2"; then pass "$1"; else fail "$1"; fi; } # A PATH with only what bootstrap.sh and the library need when sourced. mkdir -p "$WORK/bin" -for tool in head rm cat cp mkdir basename dirname uname id tr awk grep sed ln readlink cmp mv mktemp tee git; do +for tool in head rm cat cp mkdir basename dirname uname id tr awk grep sed ln readlink cmp mv mktemp tee git find chmod stat; do ln -s "$(command -v "$tool")" "$WORK/bin/$tool" done @@ -182,23 +182,79 @@ for setting in true false; do check "on Linux nothing runs or is logged (file $setting)" '[ ! -s "$CALLS" ] && ! grep -q "Terminal.app" <<<"$OUT"' done +echo ">> fix_completion_permissions closes writable Homebrew completion directories" +PREFIX="$WORK/prefix" +# GNU stat first: its -f means "filesystem status" and exits 0, so BSD's -f must come second +mode_of() { stat -c %a "$1" 2>/dev/null || stat -f %Lp "$1"; } +# a Homebrew-like tree whose share/ and share/zsh/ are writable by the group +open_tree() { + rm -rf "$PREFIX" + mkdir -p "$PREFIX/share/zsh/site-functions" "$PREFIX/share/man" + chmod 775 "$PREFIX/share" "$PREFIX/share/zsh" + chmod 777 "$PREFIX/share/zsh/site-functions" + chmod 775 "$PREFIX/share/man" +} +# : run the step with a stubbed brew +perm_run() { + sh_run "$1" 'brew() { [ "$1" = --prefix ] && echo "'"$PREFIX"'"; }; '"${2:-:}"'; fix_completion_permissions' +} +fresh; : > "$CONF"; open_tree +perm_run '' '' +check "share, share/zsh and its subdirectories lose group and other write" '[ "$(mode_of "$PREFIX/share")" = 755 ] && [ "$(mode_of "$PREFIX/share/zsh")" = 755 ] && [ "$(mode_of "$PREFIX/share/zsh/site-functions")" = 755 ]' +check "directories outside share/zsh are left alone" '[ "$(mode_of "$PREFIX/share/man")" = 775 ]' +check "the change is logged once" 'grep -q "removed group/other write permission from 3 directories" <<<"$OUT"' +perm_run '' '' +check "a second run changes nothing and prints nothing" '[ "$RC" = 0 ] && [ -z "$OUT" ]' + +fresh; conf '[bootstrap]' 'fix_completion_permissions = false'; open_tree +perm_run '' '' +check "file false leaves the permissions and logs the skip" '[ "$(mode_of "$PREFIX/share")" = 775 ] && grep -q "skipping completion directory permissions (fix_completion_permissions)" <<<"$OUT"' +perm_run 'DOTFILES_FIX_COMPLETION_PERMISSIONS=1' '' +check "the environment beats a file false" '[ "$(mode_of "$PREFIX/share")" = 755 ]' + +fresh; conf '[bootstrap]' 'fix_completion_permissions = true'; open_tree +perm_run 'DOTFILES_FIX_COMPLETION_PERMISSIONS=no' '' +check "the environment can switch it off" '[ "$(mode_of "$PREFIX/share")" = 775 ]' +fresh; conf '[bootstrap]' 'fix_completion_permissions = false'; open_tree +perm_run 'DOTFILES_FIX_COMPLETION_PERMISSIONS=bogus' '' +check "an invalid value warns and falls back to the file" '[ "$(mode_of "$PREFIX/share")" = 775 ] && grep -q "invalid DOTFILES_FIX_COMPLETION_PERMISSIONS value .bogus." "$WORK/err"' + +fresh; conf '[modules.zsh-completions]' 'enabled = false'; open_tree +perm_run '' '' +check "nothing happens while the zsh-completions module is off" '[ "$(mode_of "$PREFIX/share")" = 775 ] && [ -z "$OUT" ]' + +fresh; : > "$CONF"; open_tree +sh_run '' 'fix_completion_permissions' +check "nothing happens without Homebrew" '[ "$RC" = 0 ] && [ -z "$OUT" ] && [ "$(mode_of "$PREFIX/share")" = 775 ]' + +fresh; : > "$CONF"; open_tree +perm_run '' '_dir_is_mine() { return 1; }' +check "a directory owned by someone else is only reported, with the command" '[ "$(mode_of "$PREFIX/share")" = 775 ] && grep -q "sudo chmod go-w" "$WORK/err" && ! grep -q "removed group/other" <<<"$OUT"' + +fresh; : > "$CONF"; open_tree +mkdir "$PREFIX/share/zsh/odd dir'x"; chmod 777 "$PREFIX/share/zsh/odd dir'x" +perm_run '' '_dir_is_mine() { return 1; }' +check "a suggested command quotes an unusual directory name" 'grep -qF "sudo chmod go-w $(printf "%q" "$PREFIX/share/zsh/odd dir'"'"'x")" "$WORK/err"' + +check "omnishell/config.toml enables the zsh-completions module" 'awk "/^\[modules\.zsh-completions\]/ {f=1; next} /^\[/ {f=0} f && /^enabled = true/ {ok=1} END {exit !ok}" "$DOTFILES/omnishell/config.toml"' + echo ">> main guards the generated config per stow package" -STEPS="check_interactive_preconditions check_checkout_consistency seed_bootstrap_config interactive_settings install_deps ensure_zsh ensure_default_shell install_ghostty install_omnishell interactive_modules write_rc_base stow_packages setup_ghostty_keybinds render_ghostty_settings render_tmux_settings render_git_settings setup_git_delta setup_git_identity setup_git_signing_key apply_omnishell wire_shell_d setup_terminal_app finish" +STEPS="check_interactive_preconditions check_checkout_consistency seed_bootstrap_config interactive_settings install_deps ensure_zsh ensure_default_shell install_ghostty install_omnishell interactive_modules write_rc_base stow_packages setup_ghostty_keybinds render_ghostty_settings render_tmux_settings render_git_settings setup_git_delta setup_git_identity setup_git_signing_key fix_completion_permissions apply_omnishell wire_shell_d setup_terminal_app finish" # : run main with every step stubbed; OUT lists the steps that ran main_run() { fresh; conf '[bootstrap]' "skip_stow = [$1]" sh_run '' 'for f in '"$STEPS"'; do eval "$f() { echo call:$f; }"; done; bash() { :; }; main' } main_run '' -check "main runs every step by default" '[ "$(grep -c "^call:" <<<"$OUT")" = 23 ]' +check "main runs every step by default" '[ "$(grep -c "^call:" <<<"$OUT")" = 24 ]' main_run '"git"' -check "skip git drops the four git steps only" '[ "$(grep -c "^call:" <<<"$OUT")" = 19 ] && ! grep -qE "^call:(render_git_settings|setup_git_delta|setup_git_identity|setup_git_signing_key)$" <<<"$OUT"' +check "skip git drops the four git steps only" '[ "$(grep -c "^call:" <<<"$OUT")" = 20 ] && ! grep -qE "^call:(render_git_settings|setup_git_delta|setup_git_identity|setup_git_signing_key)$" <<<"$OUT"' main_run '"tmux", "bat"' check "main logs each skipped stow package once, in the final settings" '[ "$(grep -c "^.*skipping stow package tmux (skip_stow)" <<<"$OUT")" = 1 ] && [ "$(grep -c "skipping stow package bat (skip_stow)" <<<"$OUT")" = 1 ]' main_run '"tmux"' -check "skip tmux drops render_tmux_settings only" '[ "$(grep -c "^call:" <<<"$OUT")" = 22 ] && ! grep -q "^call:render_tmux_settings$" <<<"$OUT"' +check "skip tmux drops render_tmux_settings only" '[ "$(grep -c "^call:" <<<"$OUT")" = 23 ] && ! grep -q "^call:render_tmux_settings$" <<<"$OUT"' main_run '"ghostty"' -check "skip ghostty drops keybinds and settings but keeps install_ghostty" '[ "$(grep -c "^call:" <<<"$OUT")" = 21 ] && ! grep -qE "^call:(setup_ghostty_keybinds|render_ghostty_settings)$" <<<"$OUT" && grep -q "^call:install_ghostty$" <<<"$OUT"' +check "skip ghostty drops keybinds and settings but keeps install_ghostty" '[ "$(grep -c "^call:" <<<"$OUT")" = 22 ] && ! grep -qE "^call:(setup_ghostty_keybinds|render_ghostty_settings)$" <<<"$OUT" && grep -q "^call:install_ghostty$" <<<"$OUT"' echo ">> warn when a skipped package is still used by an enabled module" # the warning goes to stderr; sh_run keeps it in $WORK/err diff --git a/tests/test-settings.sh b/tests/test-settings.sh index 4ead13b..797c4c9 100644 --- a/tests/test-settings.sh +++ b/tests/test-settings.sh @@ -121,7 +121,7 @@ check "the last assignment wins" '[ "$(settings_get bootstrap.install_zsh)" = ye settings_load "$WORK/missing.toml" 2>"$ERR" check "a missing file is not an error" '[ $? -eq 0 ] && [ -z "$(settings_get bootstrap.install_zsh)" ] && [ ! -s "$ERR" ]' -check "the schema lists every key" '[ "$(settings_schema_keys | tr "\n" " ")" = "bootstrap.install_zsh bootstrap.set_default_shell bootstrap.assume_yes bootstrap.terminals bootstrap.install_ghostty bootstrap.skip_packages bootstrap.skip_stow bootstrap.terminal_app_profile ghostty.keybinds ghostty.font_family ghostty.font_size ghostty.background_opacity tmux.prefix tmux.mouse tmux.mode_keys tmux.base_index tmux.escape_time tmux.history_limit tmux.status_position git.user_name git.user_email git.signing_key git.default_branch git.editor git.pull_rebase " ]' +check "the schema lists every key" '[ "$(settings_schema_keys | tr "\n" " ")" = "bootstrap.install_zsh bootstrap.set_default_shell bootstrap.assume_yes bootstrap.terminals bootstrap.install_ghostty bootstrap.skip_packages bootstrap.skip_stow bootstrap.terminal_app_profile bootstrap.fix_completion_permissions ghostty.keybinds ghostty.font_family ghostty.font_size ghostty.background_opacity tmux.prefix tmux.mouse tmux.mode_keys tmux.base_index tmux.escape_time tmux.history_limit tmux.status_position git.user_name git.user_email git.signing_key git.default_branch git.editor git.pull_rebase " ]' echo ">> omnishell merge" DEFAULT="$WORK/default.toml"