Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Replace shell tests with snapshot-based assertions

Maximilian Roos81973cffc263
diff --git a/src/commands/configure_shell.rs b/src/commands/configure_shell.rs
index 08ba81f4b..b041d3da7 100644
--- a/src/commands/configure_shell.rs
+++ b/src/commands/configure_shell.rs
@@ -5,8 +5,8 @@ use worktrunk::path::format_path_for_display;
use worktrunk::shell::{self, Shell};
use worktrunk::shell_exec::ShellConfig;
use worktrunk::styling::{
- INFO_SYMBOL, PROGRESS_SYMBOL, PROMPT_SYMBOL, SUCCESS_SYMBOL, format_bash_with_gutter,
- format_with_gutter, hint_message, warning_message,
+ INFO_SYMBOL, PROMPT_SYMBOL, SUCCESS_SYMBOL, format_bash_with_gutter, format_with_gutter,
+ hint_message, warning_message,
};
pub struct ConfigureResult {
@@ -66,7 +66,7 @@ impl UninstallAction {
pub fn symbol(&self) -> &'static str {
match self {
UninstallAction::Removed => SUCCESS_SYMBOL,
- UninstallAction::WouldRemove => PROGRESS_SYMBOL,
+ UninstallAction::WouldRemove => INFO_SYMBOL,
}
}
}
@@ -96,7 +96,7 @@ impl ConfigAction {
match self {
ConfigAction::Added | ConfigAction::Created => SUCCESS_SYMBOL,
ConfigAction::AlreadyExists => INFO_SYMBOL,
- ConfigAction::WouldAdd | ConfigAction::WouldCreate => PROGRESS_SYMBOL,
+ ConfigAction::WouldAdd | ConfigAction::WouldCreate => INFO_SYMBOL,
}
}
}
@@ -987,7 +987,7 @@ mod tests {
#[test]
fn test_uninstall_action_emoji() {
assert_eq!(UninstallAction::Removed.symbol(), SUCCESS_SYMBOL);
- assert_eq!(UninstallAction::WouldRemove.symbol(), PROGRESS_SYMBOL);
+ assert_eq!(UninstallAction::WouldRemove.symbol(), INFO_SYMBOL);
}
#[test]
@@ -1007,8 +1007,8 @@ mod tests {
assert_eq!(ConfigAction::Added.symbol(), SUCCESS_SYMBOL);
assert_eq!(ConfigAction::Created.symbol(), SUCCESS_SYMBOL);
assert_eq!(ConfigAction::AlreadyExists.symbol(), INFO_SYMBOL);
- assert_eq!(ConfigAction::WouldAdd.symbol(), PROGRESS_SYMBOL);
- assert_eq!(ConfigAction::WouldCreate.symbol(), PROGRESS_SYMBOL);
+ assert_eq!(ConfigAction::WouldAdd.symbol(), INFO_SYMBOL);
+ assert_eq!(ConfigAction::WouldCreate.symbol(), INFO_SYMBOL);
}
#[test]
@@ -1060,17 +1060,11 @@ mod tests {
#[test]
fn test_fish_completion_content() {
- let content = fish_completion_content("wt");
- assert!(content.contains("# worktrunk completions for fish"));
- assert!(content.contains("--command wt"));
- assert!(content.contains("COMPLETE=fish"));
- assert!(content.contains("WORKTRUNK_BIN"));
+ insta::assert_snapshot!(fish_completion_content("wt"));
}
#[test]
fn test_fish_completion_content_custom_cmd() {
- let content = fish_completion_content("myapp");
- assert!(content.contains("--command myapp"));
- assert!(content.contains("type -P myapp"));
+ insta::assert_snapshot!(fish_completion_content("myapp"));
}
}
diff --git a/src/commands/snapshots/wt__commands__configure_shell__tests__fish_completion_content.snap b/src/commands/snapshots/wt__commands__configure_shell__tests__fish_completion_content.snap
new file mode 100644
index 000000000..1dfc220f5
--- /dev/null
+++ b/src/commands/snapshots/wt__commands__configure_shell__tests__fish_completion_content.snap
@@ -0,0 +1,6 @@
+---
+source: src/commands/configure_shell.rs
+expression: "fish_completion_content(\"wt\")"
+---
+# worktrunk completions for fish
+complete --keep-order --exclusive --command wt --arguments "(test -n \"\$WORKTRUNK_BIN\"; or set -l WORKTRUNK_BIN (type -P wt); COMPLETE=fish \$WORKTRUNK_BIN -- (commandline --current-process --tokenize --cut-at-cursor) (commandline --current-token))"
diff --git a/src/commands/snapshots/wt__commands__configure_shell__tests__fish_completion_content_custom_cmd.snap b/src/commands/snapshots/wt__commands__configure_shell__tests__fish_completion_content_custom_cmd.snap
new file mode 100644
index 000000000..dc9549aab
--- /dev/null
+++ b/src/commands/snapshots/wt__commands__configure_shell__tests__fish_completion_content_custom_cmd.snap
@@ -0,0 +1,6 @@
+---
+source: src/commands/configure_shell.rs
+expression: "fish_completion_content(\"myapp\")"
+---
+# worktrunk completions for fish
+complete --keep-order --exclusive --command myapp --arguments "(test -n \"\$WORKTRUNK_BIN\"; or set -l WORKTRUNK_BIN (type -P myapp); COMPLETE=fish \$WORKTRUNK_BIN -- (commandline --current-process --tokenize --cut-at-cursor) (commandline --current-token))"
diff --git a/src/shell.rs b/src/shell.rs
index 776e8131b..1f6c73fd3 100644
--- a/src/shell.rs
+++ b/src/shell.rs
@@ -453,33 +453,14 @@ mod tests {
}
#[test]
- fn test_shell_config_line_bash() {
- let line = Shell::Bash.config_line("wt");
- assert!(line.contains("eval"));
- assert!(line.contains("wt config shell init bash"));
- assert!(line.contains("command -v wt"));
- }
-
- #[test]
- fn test_shell_config_line_zsh() {
- let line = Shell::Zsh.config_line("wt");
- assert!(line.contains("eval"));
- assert!(line.contains("wt config shell init zsh"));
- }
-
- #[test]
- fn test_shell_config_line_fish() {
- let line = Shell::Fish.config_line("wt");
- assert!(line.contains("type -q wt"));
- assert!(line.contains("wt config shell init fish"));
- assert!(line.contains("source"));
- }
-
- #[test]
- fn test_shell_config_line_powershell() {
- let line = Shell::PowerShell.config_line("wt");
- assert!(line.contains("Invoke-Expression"));
- assert!(line.contains("wt config shell init powershell"));
+ fn test_shell_config_line() {
+ insta::assert_snapshot!("config_line_bash", Shell::Bash.config_line("wt"));
+ insta::assert_snapshot!("config_line_zsh", Shell::Zsh.config_line("wt"));
+ insta::assert_snapshot!("config_line_fish", Shell::Fish.config_line("wt"));
+ insta::assert_snapshot!(
+ "config_line_powershell",
+ Shell::PowerShell.config_line("wt")
+ );
}
#[test]
@@ -487,52 +468,21 @@ mod tests {
// When using a custom prefix, the generated shell config line must use that prefix
// throughout - both in the command check AND the command invocation.
// This prevents the bug where we check for `git-wt` but then call `wt`.
- let prefix = "git-wt";
-
- // Bash/Zsh
- let bash_line = Shell::Bash.config_line(prefix);
- assert!(
- bash_line.contains("command -v git-wt"),
- "bash should check for git-wt"
- );
- assert!(
- bash_line.contains("command git-wt config shell init"),
- "bash should call git-wt, not wt"
- );
-
- // Fish
- let fish_line = Shell::Fish.config_line(prefix);
- assert!(
- fish_line.contains("type -q git-wt"),
- "fish should check for git-wt"
- );
- assert!(
- fish_line.contains("command git-wt config shell init"),
- "fish should call git-wt, not wt"
- );
-
- // PowerShell
- let ps_line = Shell::PowerShell.config_line(prefix);
- assert!(
- ps_line.contains("Get-Command git-wt"),
- "powershell should check for git-wt"
- );
- assert!(
- ps_line.contains("& git-wt config shell init"),
- "powershell should call git-wt, not wt"
+ insta::assert_snapshot!("config_line_bash_custom", Shell::Bash.config_line("git-wt"));
+ insta::assert_snapshot!("config_line_zsh_custom", Shell::Zsh.config_line("git-wt"));
+ insta::assert_snapshot!("config_line_fish_custom", Shell::Fish.config_line("git-wt"));
+ insta::assert_snapshot!(
+ "config_line_powershell_custom",
+ Shell::PowerShell.config_line("git-wt")
);
}
#[test]
fn test_shell_init_generate() {
- // Test that shell init generates valid output for each shell
- let shells = [Shell::Bash, Shell::Zsh, Shell::Fish, Shell::PowerShell];
- for shell in shells {
+ for shell in [Shell::Bash, Shell::Zsh, Shell::Fish, Shell::PowerShell] {
let init = ShellInit::with_prefix(shell, "wt".to_string());
- let result = init.generate();
- assert!(result.is_ok(), "Failed to generate for {:?}", shell);
- let output = result.unwrap();
- assert!(!output.is_empty(), "Empty output for {:?}", shell);
+ let output = init.generate().expect("Failed to generate");
+ insta::assert_snapshot!(format!("init_{shell}"), output);
}
}
@@ -627,13 +577,7 @@ mod tests {
#[test]
fn test_shell_init_with_custom_prefix() {
let init = ShellInit::with_prefix(Shell::Bash, "custom".to_string());
- let result = init.generate();
- assert!(result.is_ok(), "Should generate with custom prefix");
- let output = result.unwrap();
- assert!(
- output.contains("custom"),
- "Output should contain custom prefix"
- );
+ insta::assert_snapshot!(init.generate().expect("Should generate with custom prefix"));
}
/// Verify that `config_line()` generates lines that
diff --git a/src/snapshots/worktrunk__shell__tests__config_line_bash.snap b/src/snapshots/worktrunk__shell__tests__config_line_bash.snap
new file mode 100644
index 000000000..45152a021
--- /dev/null
+++ b/src/snapshots/worktrunk__shell__tests__config_line_bash.snap
@@ -0,0 +1,5 @@
+---
+source: src/shell.rs
+expression: "Shell::Bash.config_line(\"wt\")"
+---
+if command -v wt >/dev/null 2>&1; then eval "$(command wt config shell init bash)"; fi
diff --git a/src/snapshots/worktrunk__shell__tests__config_line_bash_custom.snap b/src/snapshots/worktrunk__shell__tests__config_line_bash_custom.snap
new file mode 100644
index 000000000..1a4c0fd2d
--- /dev/null
+++ b/src/snapshots/worktrunk__shell__tests__config_line_bash_custom.snap
@@ -0,0 +1,5 @@
+---
+source: src/shell.rs
+expression: "Shell::Bash.config_line(\"git-wt\")"
+---
+if command -v git-wt >/dev/null 2>&1; then eval "$(command git-wt config shell init bash)"; fi
diff --git a/src/snapshots/worktrunk__shell__tests__config_line_fish.snap b/src/snapshots/worktrunk__shell__tests__config_line_fish.snap
new file mode 100644
index 000000000..b5d64285d
--- /dev/null
+++ b/src/snapshots/worktrunk__shell__tests__config_line_fish.snap
@@ -0,0 +1,5 @@
+---
+source: src/shell.rs
+expression: "Shell::Fish.config_line(\"wt\")"
+---
+if type -q wt; command wt config shell init fish | source; end
diff --git a/src/snapshots/worktrunk__shell__tests__config_line_fish_custom.snap b/src/snapshots/worktrunk__shell__tests__config_line_fish_custom.snap
new file mode 100644
index 000000000..5b21cf7b8
--- /dev/null
+++ b/src/snapshots/worktrunk__shell__tests__config_line_fish_custom.snap
@@ -0,0 +1,5 @@
+---
+source: src/shell.rs
+expression: "Shell::Fish.config_line(\"git-wt\")"
+---
+if type -q git-wt; command git-wt config shell init fish | source; end
diff --git a/src/snapshots/worktrunk__shell__tests__config_line_powershell.snap b/src/snapshots/worktrunk__shell__tests__config_line_powershell.snap
new file mode 100644
index 000000000..8d832994e
--- /dev/null
+++ b/src/snapshots/worktrunk__shell__tests__config_line_powershell.snap
@@ -0,0 +1,5 @@
+---
+source: src/shell.rs
+expression: "Shell::PowerShell.config_line(\"wt\")"
+---
+if (Get-Command wt -ErrorAction SilentlyContinue) { Invoke-Expression (& wt config shell init powershell) }
diff --git a/src/snapshots/worktrunk__shell__tests__config_line_powershell_custom.snap b/src/snapshots/worktrunk__shell__tests__config_line_powershell_custom.snap
new file mode 100644
index 000000000..883eca1de
--- /dev/null
+++ b/src/snapshots/worktrunk__shell__tests__config_line_powershell_custom.snap
@@ -0,0 +1,5 @@
+---
+source: src/shell.rs
+expression: "Shell::PowerShell.config_line(\"git-wt\")"
+---
+if (Get-Command git-wt -ErrorAction SilentlyContinue) { Invoke-Expression (& git-wt config shell init powershell) }
diff --git a/src/snapshots/worktrunk__shell__tests__config_line_zsh.snap b/src/snapshots/worktrunk__shell__tests__config_line_zsh.snap
new file mode 100644
index 000000000..8abbbed18
--- /dev/null
+++ b/src/snapshots/worktrunk__shell__tests__config_line_zsh.snap
@@ -0,0 +1,5 @@
+---
+source: src/shell.rs
+expression: "Shell::Zsh.config_line(\"wt\")"
+---
+if command -v wt >/dev/null 2>&1; then eval "$(command wt config shell init zsh)"; fi
diff --git a/src/snapshots/worktrunk__shell__tests__config_line_zsh_custom.snap b/src/snapshots/worktrunk__shell__tests__config_line_zsh_custom.snap
new file mode 100644
index 000000000..0d3333de5
--- /dev/null
+++ b/src/snapshots/worktrunk__shell__tests__config_line_zsh_custom.snap
@@ -0,0 +1,5 @@
+---
+source: src/shell.rs
+expression: "Shell::Zsh.config_line(\"git-wt\")"
+---
+if command -v git-wt >/dev/null 2>&1; then eval "$(command git-wt config shell init zsh)"; fi
diff --git a/src/snapshots/worktrunk__shell__tests__init_bash.snap b/src/snapshots/worktrunk__shell__tests__init_bash.snap
new file mode 100644
index 000000000..5ec2b443d
--- /dev/null
+++ b/src/snapshots/worktrunk__shell__tests__init_bash.snap
@@ -0,0 +1,63 @@
+---
+source: src/shell.rs
+expression: output
+---
+# worktrunk shell integration for bash
+
+# Only initialize if wt is available (in PATH or via WORKTRUNK_BIN)
+if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
+
+ # Override wt command with file-based directive passing.
+ # Creates a temp file, passes path via WORKTRUNK_DIRECTIVE_FILE, sources it after.
+ # WORKTRUNK_BIN can override the binary path (for testing dev builds).
+ wt() {
+ local use_source=false
+ local args=()
+
+ for arg in "$@"; do
+ if [[ "$arg" == "--source" ]]; then use_source=true; else args+=("$arg"); fi
+ done
+
+ # Completion mode: call binary directly, no directive file needed.
+ # This check MUST be here (not in the binary) because clap's completion
+ # handler runs before argument parsing.
+ if [[ -n "${COMPLETE:-}" ]]; then
+ command "${WORKTRUNK_BIN:-wt}" "${args[@]}"
+ return
+ fi
+
+ local directive_file exit_code=0
+ directive_file="$(mktemp)"
+
+ # --source: use cargo run (builds from source)
+ if [[ "$use_source" == true ]]; then
+ WORKTRUNK_DIRECTIVE_FILE="$directive_file" cargo run --bin wt --quiet -- "${args[@]}" || exit_code=$?
+ else
+ WORKTRUNK_DIRECTIVE_FILE="$directive_file" command "${WORKTRUNK_BIN:-wt}" "${args[@]}" || exit_code=$?
+ fi
+
+ if [[ -s "$directive_file" ]]; then
+ source "$directive_file"
+ if [[ $exit_code -eq 0 ]]; then
+ exit_code=$?
+ fi
+ fi
+
+ rm -f "$directive_file"
+ return "$exit_code"
+ }
+
+ # Lazy completions - generate on first TAB, then delegate to clap's completer
+ _wt_lazy_complete() {
+ # Generate completions function once (check if clap's function exists)
+ if ! declare -F _clap_complete_wt >/dev/null; then
+ # Use `command` to bypass the shell function and call the binary directly.
+ # Without this, `wt` would call the shell function which evals
+ # the completion script internally but doesn't re-emit it.
+ eval "$(COMPLETE=bash command "${WORKTRUNK_BIN:-wt}" 2>/dev/null)" || return
+ fi
+ _clap_complete_wt "$@"
+ }
+
+ complete -o nospace -o bashdefault -F _wt_lazy_complete wt
+fi
diff --git a/src/snapshots/worktrunk__shell__tests__init_fish.snap b/src/snapshots/worktrunk__shell__tests__init_fish.snap
new file mode 100644
index 000000000..d178aa6d3
--- /dev/null
+++ b/src/snapshots/worktrunk__shell__tests__init_fish.snap
@@ -0,0 +1,49 @@
+---
+source: src/shell.rs
+expression: output
+---
+# worktrunk shell integration for fish
+
+# Only initialize if wt is available (in PATH or via WORKTRUNK_BIN)
+if type -q wt; or test -n "$WORKTRUNK_BIN"
+
+ # Override wt command with file-based directive passing.
+ # Creates a temp file, passes path via WORKTRUNK_DIRECTIVE_FILE, evals it after.
+ # WORKTRUNK_BIN can override the binary path (for testing dev builds).
+ #
+ # Note: We use `eval (cat ... | string collect)` instead of `source` because:
+ # 1. fish's `source` doesn't propagate `exit` to the parent function
+ # 2. `eval (cat ...)` without `string collect` splits on newlines, breaking multiline directives
+ # With `string collect`, `exit 42` properly exits the function with code 42.
+ function wt
+ set -l use_source false
+ set -l args
+
+ for arg in $argv
+ if test "$arg" = "--source"; set use_source true; else; set -a args $arg; end
+ end
+
+ test -n "$WORKTRUNK_BIN"; or set -l WORKTRUNK_BIN (type -P wt)
+ set -l directive_file (mktemp)
+
+ # --source: use cargo run (builds from source)
+ if test $use_source = true
+ WORKTRUNK_DIRECTIVE_FILE=$directive_file cargo run --bin wt --quiet -- $args
+ else
+ WORKTRUNK_DIRECTIVE_FILE=$directive_file command $WORKTRUNK_BIN $args
+ end
+ set -l exit_code $status
+
+ if test -s "$directive_file"
+ eval (cat "$directive_file" | string collect)
+ if test $exit_code -eq 0
+ set exit_code $status
+ end
+ end
+
+ rm -f "$directive_file"
+ return $exit_code
+ end
+
+ # Completions are in ~/.config/fish/completions/wt.fish (installed by `wt config shell install`)
+end
diff --git a/src/snapshots/worktrunk__shell__tests__init_powershell.snap b/src/snapshots/worktrunk__shell__tests__init_powershell.snap
new file mode 100644
index 000000000..5e7c7b7b3
--- /dev/null
+++ b/src/snapshots/worktrunk__shell__tests__init_powershell.snap
@@ -0,0 +1,75 @@
+---
+source: src/shell.rs
+expression: output
+---
+# worktrunk shell integration for PowerShell
+#
+# Limitations compared to bash/zsh/fish:
+# - Hooks using bash syntax won't work without Git Bash
+#
+# For full hook compatibility on Windows, install Git for Windows and use bash integration.
+
+# Only initialize if wt is available
+if (Get-Command wt -ErrorAction SilentlyContinue) {
+
+ # wt wrapper function - uses temp file for directives
+ function wt {
+ param(
+ [Parameter(ValueFromRemainingArguments = $true)]
+ [string[]]$Arguments
+ )
+
+ $wtBin = (Get-Command wt -CommandType Application).Source
+ $directiveFile = [System.IO.Path]::GetTempFileName()
+
+ try {
+ # Run wt with WORKTRUNK_DIRECTIVE_FILE env var
+ # WORKTRUNK_SHELL tells the binary to use PowerShell-compatible escaping
+ # stdout and stderr both go to console normally
+ $env:WORKTRUNK_DIRECTIVE_FILE = $directiveFile
+ $env:WORKTRUNK_SHELL = "powershell"
+ & $wtBin @Arguments
+ $exitCode = $LASTEXITCODE
+ }
+ finally {
+ Remove-Item Env:\WORKTRUNK_DIRECTIVE_FILE -ErrorAction SilentlyContinue
+ Remove-Item Env:\WORKTRUNK_SHELL -ErrorAction SilentlyContinue
+ }
+
+ # Execute the directive script if it has content
+ try {
+ if ((Test-Path $directiveFile) -and (Get-Item $directiveFile).Length -gt 0) {
+ $script = Get-Content -Path $directiveFile -Raw
+ if ($script.Trim()) {
+ Invoke-Expression $script
+ # If wt succeeded, use the directive script's exit code
+ if ($exitCode -eq 0) {
+ $exitCode = $LASTEXITCODE
+ }
+ }
+ }
+ }
+ finally {
+ # Cleanup even if Invoke-Expression throws
+ Remove-Item $directiveFile -ErrorAction SilentlyContinue
+ }
+
+ # Propagate exit code so $? and $LASTEXITCODE are consistent for scripts/CI
+ $global:LASTEXITCODE = $exitCode
+ if ($exitCode -ne 0) {
+ # Write error to set $? = $false without throwing
+ Write-Error "wt exited with code $exitCode" -ErrorAction SilentlyContinue
+ }
+ return $exitCode
+ }
+
+ # Tab completion - generate clap's completer script and eval it
+ # This registers Register-ArgumentCompleter with proper handling
+ $env:COMPLETE = "powershell"
+ try {
+ & (Get-Command wt -CommandType Application) | Out-String | Invoke-Expression
+ }
+ finally {
+ Remove-Item Env:\COMPLETE -ErrorAction SilentlyContinue
+ }
+}
diff --git a/src/snapshots/worktrunk__shell__tests__init_zsh.snap b/src/snapshots/worktrunk__shell__tests__init_zsh.snap
new file mode 100644
index 000000000..207136d3c
--- /dev/null
+++ b/src/snapshots/worktrunk__shell__tests__init_zsh.snap
@@ -0,0 +1,85 @@
+---
+source: src/shell.rs
+expression: output
+---
+# worktrunk shell integration for zsh
+#
+# Completions require zsh's completion system (compinit). If completions don't work:
+# autoload -Uz compinit && compinit # add before this line in your .zshrc
+
+# Only initialize if wt is available (in PATH or via WORKTRUNK_BIN)
+if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
+
+ # Override wt command with file-based directive passing.
+ # Creates a temp file, passes path via WORKTRUNK_DIRECTIVE_FILE, sources it after.
+ # WORKTRUNK_BIN can override the binary path (for testing dev builds).
+ wt() {
+ local use_source=false
+ local -a args
+
+ for arg in "$@"; do
+ if [[ "$arg" == "--source" ]]; then use_source=true; else args+=("$arg"); fi
+ done
+
+ # Completion mode: call binary directly, no directive file needed.
+ # This check MUST be here (not in the binary) because clap's completion
+ # handler runs before argument parsing.
+ if [[ -n "${COMPLETE:-}" ]]; then
+ command "${WORKTRUNK_BIN:-wt}" "${args[@]}"
+ return
+ fi
+
+ local directive_file exit_code=0
+ directive_file="$(mktemp)"
+
+ # --source: use cargo run (builds from source)
+ if [[ "$use_source" == true ]]; then
+ WORKTRUNK_DIRECTIVE_FILE="$directive_file" cargo run --bin wt --quiet -- "${args[@]}" || exit_code=$?
+ else
+ WORKTRUNK_DIRECTIVE_FILE="$directive_file" command "${WORKTRUNK_BIN:-wt}" "${args[@]}" || exit_code=$?
+ fi
+
+ if [[ -s "$directive_file" ]]; then
+ source "$directive_file"
+ if [[ $exit_code -eq 0 ]]; then
+ exit_code=$?
+ fi
+ fi
+
+ rm -f "$directive_file"
+ return "$exit_code"
+ }
+
+ # Lazy completions - generate on first TAB, then delegate to clap's completer
+ _wt_lazy_complete() {
+ # Generate completions function once (check if clap's function exists)
+ if ! (( $+functions[_clap_dynamic_completer_wt] )); then
+ # Use `command` to bypass the shell function and call the binary directly.
+ # Without this, `wt` would call the shell function which evals
+ # the completion script internally but doesn't re-emit it.
+ #
+ # The -V flag creates an unsorted group, preserving our recency-based
+ # ordering instead of zsh's default alphabetical sort.
+ # Note: _describe's -V does NOT take an argument - it just sets a flag.
+ # The _describe function internally passes -o nosort to compadd.
+ # TODO(clap): Ideally clap_complete would preserve ordering natively.
+ # See: https://github.com/clap-rs/clap/issues/5752
+ eval "$(COMPLETE=zsh command "${WORKTRUNK_BIN:-wt}" 2>/dev/null | sed "s/_describe 'values'/_describe -V 'values'/")" || return
+ fi
+ _clap_dynamic_completer_wt "$@"
+ }
+
+ # Register completion (silently skip if compinit hasn't run yet).
+ # We don't warn here because this script runs on every shell startup - users
+ # shouldn't see warnings every time they open a terminal. Instead, `wt config
+ # shell install` detects missing compinit and shows a one-time advisory.
+ if (( $+functions[compdef] )); then
+ compdef _wt_lazy_complete wt
+ # Single-column display keeps descriptions visually associated with each branch.
+ # Users can override: zstyle ':completion:*:wt:*' list-max ''
+ zstyle ':completion:*:wt:*' list-max 1
+ # Prevent grouping branches with identical descriptions (same timestamp) on one line.
+ # Without this, "release main -- + 12m" instead of separate lines per branch.
+ zstyle ':completion:*:*:wt:*' list-grouped false
+ fi
+fi
diff --git a/src/snapshots/worktrunk__shell__tests__shell_init_with_custom_prefix.snap b/src/snapshots/worktrunk__shell__tests__shell_init_with_custom_prefix.snap
new file mode 100644
index 000000000..86cb4edb2
--- /dev/null
+++ b/src/snapshots/worktrunk__shell__tests__shell_init_with_custom_prefix.snap
@@ -0,0 +1,63 @@
+---
+source: src/shell.rs
+expression: "init.generate().expect(\"Should generate with custom prefix\")"
+---
+# worktrunk shell integration for bash
+
+# Only initialize if custom is available (in PATH or via WORKTRUNK_BIN)
+if command -v custom >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
+
+ # Override custom command with file-based directive passing.
+ # Creates a temp file, passes path via WORKTRUNK_DIRECTIVE_FILE, sources it after.
+ # WORKTRUNK_BIN can override the binary path (for testing dev builds).
+ custom() {
+ local use_source=false
+ local args=()
+
+ for arg in "$@"; do
+ if [[ "$arg" == "--source" ]]; then use_source=true; else args+=("$arg"); fi
+ done
+
+ # Completion mode: call binary directly, no directive file needed.
+ # This check MUST be here (not in the binary) because clap's completion
+ # handler runs before argument parsing.
+ if [[ -n "${COMPLETE:-}" ]]; then
+ command "${WORKTRUNK_BIN:-custom}" "${args[@]}"
+ return
+ fi
+
+ local directive_file exit_code=0
+ directive_file="$(mktemp)"
+
+ # --source: use cargo run (builds from source)
+ if [[ "$use_source" == true ]]; then
+ WORKTRUNK_DIRECTIVE_FILE="$directive_file" cargo run --bin custom --quiet -- "${args[@]}" || exit_code=$?
+ else
+ WORKTRUNK_DIRECTIVE_FILE="$directive_file" command "${WORKTRUNK_BIN:-custom}" "${args[@]}" || exit_code=$?
+ fi
+
+ if [[ -s "$directive_file" ]]; then
+ source "$directive_file"
+ if [[ $exit_code -eq 0 ]]; then
+ exit_code=$?
+ fi
+ fi
+
+ rm -f "$directive_file"
+ return "$exit_code"
+ }
+
+ # Lazy completions - generate on first TAB, then delegate to clap's completer
+ _custom_lazy_complete() {
+ # Generate completions function once (check if clap's function exists)
+ if ! declare -F _clap_complete_custom >/dev/null; then
+ # Use `command` to bypass the shell function and call the binary directly.
+ # Without this, `custom` would call the shell function which evals
+ # the completion script internally but doesn't re-emit it.
+ eval "$(COMPLETE=bash command "${WORKTRUNK_BIN:-custom}" 2>/dev/null)" || return
+ fi
+ _clap_complete_custom "$@"
+ }
+
+ complete -o nospace -o bashdefault -F _custom_lazy_complete custom
+fi
diff --git a/tests/integration_tests/configure_shell.rs b/tests/integration_tests/configure_shell.rs
index 0f193bb94..64b11c3d6 100644
--- a/tests/integration_tests/configure_shell.rs
+++ b/tests/integration_tests/configure_shell.rs
@@ -918,3 +918,102 @@ fn test_configure_shell_no_warning_when_shell_unset(repo: TestRepo, temp_home: T
");
});
}
+
+// PTY-based tests for interactive install preview
+#[cfg(all(unix, feature = "shell-integration-tests"))]
+mod pty_tests {
+ use crate::common::{TestRepo, configure_pty_command, open_pty, repo, temp_home};
+ use insta::assert_snapshot;
+ use insta_cmd::get_cargo_bin;
+ use portable_pty::CommandBuilder;
+ use rstest::rstest;
+ use std::fs;
+ use std::io::{Read, Write};
+ use tempfile::TempDir;
+
+ /// Execute shell install command in a PTY with interactive input
+ fn exec_install_in_pty(temp_home: &TempDir, repo: &TestRepo, input: &str) -> (String, i32) {
+ let pair = open_pty();
+
+ let mut cmd = CommandBuilder::new(get_cargo_bin("wt"));
+ cmd.arg("-C");
+ cmd.arg(repo.root_path());
+ cmd.arg("config");
+ cmd.arg("shell");
+ cmd.arg("install");
+ cmd.cwd(repo.root_path());
+
+ configure_pty_command(&mut cmd);
+ cmd.env("HOME", temp_home.path());
+ cmd.env("SHELL", "/bin/zsh");
+
+ let mut child = pair.slave.spawn_command(cmd).unwrap();
+ drop(pair.slave);
+
+ let mut reader = pair.master.try_clone_reader().unwrap();
+ let mut writer = pair.master.take_writer().unwrap();
+
+ // Write input to the PTY (simulating user typing)
+ writer.write_all(input.as_bytes()).unwrap();
+ writer.flush().unwrap();
+ drop(writer);
+
+ let mut buf = String::new();
+ reader.read_to_string(&mut buf).unwrap();
+
+ let exit_status = child.wait().unwrap();
+ let exit_code = exit_status.exit_code() as i32;
+
+ (buf, exit_code)
+ }
+
+ /// Normalize output for snapshot testing
+ fn normalize_output(output: &str, temp_home: &TempDir) -> String {
+ // PTYs use \r\n line endings, normalize to \n
+ let output = output.replace("\r\n", "\n");
+
+ // Remove ^D control sequence that appears on macOS
+ let output = regex::Regex::new(r"\^D\x08+")
+ .unwrap()
+ .replace_all(&output, "");
+
+ // Replace temp home path with ~/
+ let home_path = temp_home.path().to_string_lossy();
+ output.replace(&*home_path, "~").to_string()
+ }
+
+ /// Test that `wt config shell install` shows preview with gutter-formatted config lines
+ #[rstest]
+ fn test_install_preview_with_gutter(repo: TestRepo, temp_home: TempDir) {
+ // Create zsh config file
+ let zshrc_path = temp_home.path().join(".zshrc");
+ fs::write(&zshrc_path, "# Existing config\n").unwrap();
+
+ let (output, exit_code) = exec_install_in_pty(&temp_home, &repo, "y\n");
+ let normalized = normalize_output(&output, &temp_home);
+
+ assert_eq!(exit_code, 0);
+ assert_snapshot!(normalized);
+ }
+
+ /// Test that declining install shows preview but doesn't modify files
+ #[rstest]
+ fn test_install_preview_declined(repo: TestRepo, temp_home: TempDir) {
+ let zshrc_path = temp_home.path().join(".zshrc");
+ fs::write(&zshrc_path, "# Existing config\n").unwrap();
+
+ let (output, exit_code) = exec_install_in_pty(&temp_home, &repo, "n\n");
+ let normalized = normalize_output(&output, &temp_home);
+
+ // User declined, so exit code is 1
+ assert_eq!(exit_code, 1);
+ assert_snapshot!(normalized);
+
+ // Verify file was not modified
+ let content = fs::read_to_string(&zshrc_path).unwrap();
+ assert!(
+ !content.contains("wt config shell init"),
+ "File should not be modified when user declines"
+ );
+ }
+}
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__configure_shell__pty_tests__install_preview_declined.snap b/tests/integration_tests/snapshots/integration__integration_tests__configure_shell__pty_tests__install_preview_declined.snap
new file mode 100644
index 000000000..dbe477b83
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__configure_shell__pty_tests__install_preview_declined.snap
@@ -0,0 +1,11 @@
+---
+source: tests/integration_tests/configure_shell.rs
+expression: normalized
+---
+n
+
+○ Will add shell extension & completions for zsh @ ~/.zshrc
+ if command -v wt >/dev/null 2>&1; then eval "$(command wt config shell init zsh)"; fi
+
+❯ Proceed? [y/N]
+✗ Cancelled by user
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__configure_shell__pty_tests__install_preview_with_gutter.snap b/tests/integration_tests/snapshots/integration__integration_tests__configure_shell__pty_tests__install_preview_with_gutter.snap
new file mode 100644
index 000000000..3e9d4a23b
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__configure_shell__pty_tests__install_preview_with_gutter.snap
@@ -0,0 +1,18 @@
+---
+source: tests/integration_tests/configure_shell.rs
+expression: normalized
+---
+y
+
+○ Will add shell extension & completions for zsh @ ~/.zshrc
+ if command -v wt >/dev/null 2>&1; then eval "$(command wt config shell init zsh)"; fi
+
+❯ Proceed? [y/N]
+▲ Completions won't work; add to ~/.zshrc before the wt line:
+ autoload -Uz compinit && compinit
+✓ Added shell extension & completions for zsh @ ~/.zshrc
+↳ Skipped bash; ~/.bashrc not found
+↳ Skipped fish; ~/.config/fish/conf.d not found
+
+✓ Configured 1 shell
+↳ Restart shell or run: source ~/.zshrc