Skip to content

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

Refactor zsh compinit check to use shell_exec helper

Maximilian Roosa87b95074785
diff --git a/src/commands/config.rs b/src/commands/config.rs
index 4404af9f0..459b2a6b0 100644
--- a/src/commands/config.rs
+++ b/src/commands/config.rs
@@ -501,7 +501,8 @@ fn render_ci_tool_status(
/// Returns true if compinit is NOT enabled (i.e., user needs to add it).
/// Returns false if compinit is enabled or we can't determine (fail-safe: don't warn).
fn check_zsh_compinit_missing() -> bool {
- use std::process::{Command, Stdio};
+ use std::process::Command;
+ use worktrunk::shell_exec::run;
// Allow tests to bypass this check since zsh subprocess behavior varies across CI envs
if std::env::var("WORKTRUNK_TEST_COMPINIT_CONFIGURED").is_ok() {
@@ -518,17 +519,14 @@ fn check_zsh_compinit_missing() -> bool {
// This ensures we're checking the USER's configuration, not system defaults
// Suppress stderr to avoid noise like "can't change option: zle"
// The (( ... )) arithmetic returns exit 0 if true (compdef exists), 1 if false
- let status = Command::new("zsh")
- .args(["--no-globalrcs", "-ic", "(( $+functions[compdef] ))"])
- .stdout(Stdio::null())
- .stderr(Stdio::null())
- .status()
- .ok();
+ let mut cmd = Command::new("zsh");
+ cmd.args(["--no-globalrcs", "-ic", "(( $+functions[compdef] ))"]);
+ let Ok(output) = run(&mut cmd, None) else {
+ return false; // Can't determine, don't warn
+ };
- match status {
- Some(s) => !s.success(), // compdef NOT found = need to warn
- None => false, // Can't determine, don't warn
- }
+ // compdef NOT found = need to warn
+ !output.status.success()
}
/// Core logic for determining user config path from env var values
@@ -984,11 +982,10 @@ fn handle_state_show_json(repo: &Repository) -> anyhow::Result<()> {
let ci_status: Vec<serde_json::Value> = ci_entries
.into_iter()
.map(|(branch, cached)| {
- let status = cached.status.as_ref().map(|s| {
- serde_json::to_string(&s.ci_status)
- .map(|s| s.trim_matches('"').to_string())
- .unwrap_or_else(|_| "unknown".to_string())
- });
+ let status = cached
+ .status
+ .as_ref()
+ .map(|s| -> &'static str { s.ci_status.into() });
serde_json::json!({
"branch": branch,
"status": status,
@@ -1110,9 +1107,10 @@ fn handle_state_show_table(repo: &Repository) -> anyhow::Result<()> {
table.push_str("|--------|--------|-----|------|\n");
for (branch, cached) in entries {
let status = match &cached.status {
- Some(pr_status) => serde_json::to_string(&pr_status.ci_status)
- .map(|s| s.trim_matches('"').to_string())
- .unwrap_or_else(|_| "unknown".to_string()),
+ Some(pr_status) => {
+ let status: &'static str = pr_status.ci_status.into();
+ status.to_string()
+ }
None => "none".to_string(),
};
let age = format_relative_time_short(cached.checked_at as i64);
diff --git a/tests/integration_tests/config_show.rs b/tests/integration_tests/config_show.rs
index 26aacbc32..fcbd9098f 100644
--- a/tests/integration_tests/config_show.rs
+++ b/tests/integration_tests/config_show.rs
@@ -219,6 +219,100 @@ if command -v wt >/dev/null 2>&1; then eval "$(command wt config shell init zsh)
});
}
+/// Smoke-test the actual zsh probe path (no WORKTRUNK_TEST_COMPINIT_* overrides).
+///
+/// This is behind shell-integration-tests because it requires `zsh` to be installed.
+#[rstest]
+#[cfg(all(unix, feature = "shell-integration-tests"))]
+fn test_config_show_zsh_compinit_real_probe_warns_when_missing(
+ mut repo: TestRepo,
+ temp_home: TempDir,
+) {
+ // Setup mock gh/glab for deterministic BINARIES output
+ repo.setup_mock_ci_tools_unauthenticated();
+
+ // Create global config
+ let global_config_dir = temp_home.path().join(".config").join("worktrunk");
+ fs::create_dir_all(&global_config_dir).unwrap();
+ fs::write(global_config_dir.join("config.toml"), "").unwrap();
+
+ // Create .zshrc with the canonical integration line (exact match required for config show),
+ // plus an explicit removal of compdef so the probe is deterministic.
+ fs::write(
+ temp_home.path().join(".zshrc"),
+ r#"unset -f compdef 2>/dev/null
+if command -v wt >/dev/null 2>&1; then eval "$(command wt config shell init zsh)"; fi
+"#,
+ )
+ .unwrap();
+
+ let settings = setup_snapshot_settings_with_home(&repo, &temp_home);
+ settings.bind(|| {
+ let mut cmd = wt_command();
+ repo.clean_cli_env(&mut cmd);
+ // Keep PATH minimal so the probe zsh doesn't find a globally-installed `wt`.
+ cmd.env("PATH", "/usr/bin:/bin");
+ cmd.env("ZDOTDIR", temp_home.path());
+ cmd.arg("config").arg("show").current_dir(repo.root_path());
+ set_temp_home_env(&mut cmd, temp_home.path());
+
+ let output = cmd.output().unwrap();
+ assert!(output.status.success());
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ assert!(
+ stderr.contains("Completions won't work; add to"),
+ "Expected compinit warning, got:\n{stderr}"
+ );
+ });
+}
+
+/// Smoke-test the actual zsh probe path when compdef exists.
+///
+/// This is behind shell-integration-tests because it requires `zsh` to be installed.
+#[rstest]
+#[cfg(all(unix, feature = "shell-integration-tests"))]
+fn test_config_show_zsh_compinit_real_probe_no_warning_when_present(
+ mut repo: TestRepo,
+ temp_home: TempDir,
+) {
+ // Setup mock gh/glab for deterministic BINARIES output
+ repo.setup_mock_ci_tools_unauthenticated();
+
+ // Create global config
+ let global_config_dir = temp_home.path().join(".config").join("worktrunk");
+ fs::create_dir_all(&global_config_dir).unwrap();
+ fs::write(global_config_dir.join("config.toml"), "").unwrap();
+
+ // Define compdef directly to avoid relying on compinit behavior (which can warn
+ // about insecure directories in CI). The probe checks for compdef presence.
+ fs::write(
+ temp_home.path().join(".zshrc"),
+ r#"compdef() { :; }
+if command -v wt >/dev/null 2>&1; then eval "$(command wt config shell init zsh)"; fi
+"#,
+ )
+ .unwrap();
+
+ let settings = setup_snapshot_settings_with_home(&repo, &temp_home);
+ settings.bind(|| {
+ let mut cmd = wt_command();
+ repo.clean_cli_env(&mut cmd);
+ // Keep PATH minimal so the probe zsh doesn't find a globally-installed `wt`.
+ cmd.env("PATH", "/usr/bin:/bin");
+ cmd.env("ZDOTDIR", temp_home.path());
+ cmd.arg("config").arg("show").current_dir(repo.root_path());
+ set_temp_home_env(&mut cmd, temp_home.path());
+
+ let output = cmd.output().unwrap();
+ assert!(output.status.success());
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ assert!(
+ !stderr.contains("Completions won't work; add to"),
+ "Expected no compinit warning, got:\n{stderr}"
+ );
+ });
+}
+
/// Test `wt config show` warns about unknown/misspelled keys in project config
#[rstest]
fn test_config_show_warns_unknown_project_keys(mut repo: TestRepo, temp_home: TempDir) {