Skip to content

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

Add black-box completion tests for zsh, bash, and fish (#117)

Maximilian Roos798e7cfd49c5
diff --git a/tests/integration_tests/completion_validation.rs b/tests/integration_tests/completion_validation.rs
index 0df186aad..6e1c22712 100644
--- a/tests/integration_tests/completion_validation.rs
+++ b/tests/integration_tests/completion_validation.rs
@@ -21,7 +21,6 @@ enum Severity {
enum Category {
HiddenFlag,
Consistency,
- ZshDescribeSyntax,
}
impl Issue {
@@ -97,81 +96,6 @@ fn validate_zsh(content: &str) -> Vec<Issue> {
}
}
- // Validate _describe syntax in the sed command
- // The zsh _describe function accepts these options (from its getopts string):
- // -o → sets type to "options" (NOT -o nosort!)
- // -O → sets type to "options" with noprefix
- // -t: → sets type to OPTARG
- // -1|-2|-J|-V|-x → flags added to _jvx12 array
- // Importantly: -V does NOT take an argument, and -o nosort is INVALID.
- issues.extend(validate_zsh_describe_syntax(content));
-
- issues
-}
-
-/// Validate _describe syntax in the sed command within zsh init script
-fn validate_zsh_describe_syntax(content: &str) -> Vec<Issue> {
- let mut issues = Vec::new();
-
- // Find sed commands that modify _describe
- for line in content.lines() {
- if !line.contains("sed") || !line.contains("_describe") {
- continue;
- }
-
- // Extract the sed replacement pattern (the part after the second /)
- // sed "s/SEARCH/REPLACEMENT/" - we want REPLACEMENT
- // Example: sed "s/_describe 'values'/_describe -V 'values'/"
- // SEARCH = _describe 'values'
- // REPLACEMENT = _describe -V 'values'
- if let Some(sed_start) = line.find("sed \"s/") {
- let after_sed = &line[sed_start + 7..]; // Skip 'sed "s/'
- // Find the second / (end of search pattern, start of replacement)
- if let Some(second_slash) = after_sed.find('/') {
- let replacement_start = second_slash + 1;
- let replacement_part = &after_sed[replacement_start..];
- // Find the closing / of replacement
- if let Some(third_slash) = replacement_part.find('/') {
- let replacement = &replacement_part[..third_slash];
-
- // Check for "-V word" where word is not an option and not 'values'
- // The -V flag in _describe does NOT take an argument
- if let Some(v_pos) = replacement.find("-V ") {
- let after_v = &replacement[v_pos + 3..];
- let next_word: &str = after_v.split_whitespace().next().unwrap_or("");
- // If the next word is not an option (starting with -) and not 'values',
- // then -V is being incorrectly given an argument
- if !next_word.starts_with('-')
- && !next_word.starts_with('\'')
- && !next_word.is_empty()
- {
- issues.push(Issue::error(
- "zsh",
- Category::ZshDescribeSyntax,
- format!(
- "_describe's -V flag does not take an argument, \
- but found: -V {}",
- next_word
- ),
- ));
- }
- }
-
- // Check for "-o nosort" which is invalid for _describe
- // In _describe, -o means "options mode", not ordering
- if replacement.contains("-o nosort") {
- issues.push(Issue::error(
- "zsh",
- Category::ZshDescribeSyntax,
- "_describe's -o flag means 'options mode', not ordering. \
- '-o nosort' is invalid syntax. Use just -V for unsorted groups.",
- ));
- }
- }
- }
- }
- }
-
issues
}
diff --git a/tests/integration_tests/shell_wrapper.rs b/tests/integration_tests/shell_wrapper.rs
index 525213097..181f74d6e 100644
--- a/tests/integration_tests/shell_wrapper.rs
+++ b/tests/integration_tests/shell_wrapper.rs
@@ -2961,6 +2961,111 @@ fi
);
}
+ /// Black-box test: zsh completion produces correct subcommands.
+ ///
+ /// Sources actual `wt config shell init zsh`, triggers completion, snapshots result.
+ #[test]
+ fn test_zsh_completion_subcommands() {
+ let wt_bin = get_cargo_bin("wt");
+ let init = std::process::Command::new(&wt_bin)
+ .args(["config", "shell", "init", "zsh"])
+ .output()
+ .unwrap();
+ let shell_integration = String::from_utf8_lossy(&init.stdout);
+
+ // Override _describe to print completions (it normally writes to zsh's internal state)
+ let script = format!(
+ r#"
+autoload -Uz compinit && compinit -i 2>/dev/null
+_describe() {{
+ while [[ "$1" == -* ]]; do shift; done; shift
+ for arr in "$@"; do for item in "${{(@P)arr}}"; do echo "${{item%%:*}}"; done; done
+}}
+{shell_integration}
+words=(wt "") CURRENT=2
+_wt_lazy_complete
+"#
+ );
+
+ let output = std::process::Command::new("zsh")
+ .arg("-c")
+ .arg(&script)
+ .env(
+ "PATH",
+ format!(
+ "{}:{}",
+ wt_bin.parent().unwrap().display(),
+ std::env::var("PATH").unwrap_or_default()
+ ),
+ )
+ .output()
+ .unwrap();
+
+ assert_snapshot!(String::from_utf8_lossy(&output.stdout));
+ }
+
+ /// Black-box test: bash completion produces correct subcommands.
+ ///
+ /// Sources actual `wt config shell init bash`, triggers completion, snapshots result.
+ #[test]
+ fn test_bash_completion_subcommands() {
+ let wt_bin = get_cargo_bin("wt");
+ let init = std::process::Command::new(&wt_bin)
+ .args(["config", "shell", "init", "bash"])
+ .output()
+ .unwrap();
+ let shell_integration = String::from_utf8_lossy(&init.stdout);
+
+ let script = format!(
+ r#"
+{shell_integration}
+COMP_WORDS=(wt "") COMP_CWORD=1
+_wt_lazy_complete
+for c in "${{COMPREPLY[@]}}"; do echo "${{c%% *}}"; done
+"#
+ );
+
+ let output = std::process::Command::new("bash")
+ .arg("-c")
+ .arg(&script)
+ .env(
+ "PATH",
+ format!(
+ "{}:{}",
+ wt_bin.parent().unwrap().display(),
+ std::env::var("PATH").unwrap_or_default()
+ ),
+ )
+ .output()
+ .unwrap();
+
+ assert_snapshot!(String::from_utf8_lossy(&output.stdout));
+ }
+
+ /// Black-box test: fish completion produces correct subcommands.
+ ///
+ /// Fish completions call binary with COMPLETE=fish (separate from init script).
+ #[test]
+ fn test_fish_completion_subcommands() {
+ let wt_bin = get_cargo_bin("wt");
+
+ let output = std::process::Command::new(&wt_bin)
+ .args(["--", "wt", ""])
+ .env("COMPLETE", "fish")
+ .env("_CLAP_COMPLETE_INDEX", "1")
+ .output()
+ .unwrap();
+
+ // Fish format is "value\tdescription" - extract just values
+ let completions: String = String::from_utf8_lossy(&output.stdout)
+ .lines()
+ .map(|line| line.split('\t').next().unwrap_or(line))
+ .collect::<Vec<_>>()
+ .join("\n");
+
+ assert_snapshot!(completions);
+ }
+
// ========================================================================
// Stderr/Stdout Redirection Tests
// ========================================================================
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__bash_completion_subcommands.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__bash_completion_subcommands.snap
new file mode 100644
index 000000000..7ca2d2d17
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__bash_completion_subcommands.snap
@@ -0,0 +1,12 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: "String::from_utf8_lossy(&output.stdout)"
+---
+config
+step
+hook
+select
+list
+switch
+remove
+merge
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_completion_subcommands.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_completion_subcommands.snap
new file mode 100644
index 000000000..db1a7d92d
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__fish_completion_subcommands.snap
@@ -0,0 +1,12 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: completions
+---
+config
+step
+hook
+select
+list
+switch
+remove
+merge
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__zsh_completion_subcommands.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__zsh_completion_subcommands.snap
new file mode 100644
index 000000000..7ca2d2d17
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__zsh_completion_subcommands.snap
@@ -0,0 +1,12 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: "String::from_utf8_lossy(&output.stdout)"
+---
+config
+step
+hook
+select
+list
+switch
+remove
+merge