Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Fix zsh _describe syntax and add validation tests
Maximilian Roos6ae9d0f93989
diff --git a/templates/zsh.zsh b/templates/zsh.zshindex 73864a465..c093d5480 100644--- a/templates/zsh.zsh+++ b/templates/zsh.zsh@@ -55,11 +55,13 @@ if command -v {{ cmd_prefix }} >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]];# Without this, `{{ cmd_prefix }}` would call the shell function which evals# the completion script internally but doesn't re-emit it.#- # The sed adds -V (unsorted group) and -o nosort to _describe, preserving- # our recency-based ordering instead of zsh's default alphabetical sort.+ # 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:-{{ cmd_prefix }}}" 2>/dev/null | sed "s/_describe 'values'/_describe -V wt -o nosort 'values'/")" || return+ eval "$(COMPLETE=zsh command "${WORKTRUNK_BIN:-{{ cmd_prefix }}}" 2>/dev/null | sed "s/_describe 'values'/_describe -V 'values'/")" || returnfi_clap_dynamic_completer_{{ cmd_prefix }} "$@"}diff --git a/tests/integration_tests/completion_validation.rs b/tests/integration_tests/completion_validation.rsindex 6e1c22712..0df186aad 100644--- a/tests/integration_tests/completion_validation.rs+++ b/tests/integration_tests/completion_validation.rs@@ -21,6 +21,7 @@ enum Severity {enum Category {HiddenFlag,Consistency,+ ZshDescribeSyntax,}impl Issue {@@ -96,6 +97,81 @@ 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/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snapindex 0a4fe5dcd..9aac4fca5 100644--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap@@ -94,11 +94,13 @@ wt_exec() {# Without this, `wt` would call the shell function which evals# the completion script internally but doesn't re-emit it.#- # The sed adds -V (unsorted group) and -o nosort to _describe, preserving- # our recency-based ordering instead of zsh's default alphabetical sort.+ # 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 wt -o nosort 'values'/")" || return+ eval "$(COMPLETE=zsh command "${WORKTRUNK_BIN:-wt}" 2>/dev/null | sed "s/_describe 'values'/_describe -V 'values'/")" || returnfi_clap_dynamic_completer_wt "$@"}