Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: show both template and error on hook preview expansion failure (#722)
Maximilian Roosffb19bf295e5
diff --git a/src/commands/hook_commands.rs b/src/commands/hook_commands.rsindex b09fb58ca..35c76d18f 100644--- a/src/commands/hook_commands.rs+++ b/src/commands/hook_commands.rs@@ -626,7 +626,7 @@ fn expand_command_template(template: &str, ctx: &CommandContext, hook_type: Hook.collect();// Use the standard template expansion (shell-escaped)- // Falls back to raw template if expansion fails (e.g., missing optional variable)+ // On any error, show both the template and error messageworktrunk::config::expand_template(template, &vars, true, ctx.repo, "hook preview")- .unwrap_or_else(|_| template.to_string())+ .unwrap_or_else(|err| format!("# Template error: {}\n{}", err, template))}diff --git a/tests/integration_tests/hook_show.rs b/tests/integration_tests/hook_show.rsindex b55ba191b..b871c5c31 100644--- a/tests/integration_tests/hook_show.rs+++ b/tests/integration_tests/hook_show.rs@@ -289,3 +289,114 @@ approved-commands = ["cargo build", "cargo test", "npm install"]assert_cmd_snapshot!(cmd);});}++/// Test that syntax errors in templates are shown (not swallowed) with --expanded.+#[rstest]+fn test_hook_show_expanded_syntax_error(repo: TestRepo, temp_home: TempDir) {+ // Create user config without hooks+ 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"),+ r#"worktree-path = "../{{ repo }}.{{ branch }}"+"#,+ )+ .unwrap();++ // Create project config with broken template syntax (unclosed brace)+ repo.write_project_config(+ r#"[pre-commit]+broken = "echo {{ branch"+"#,+ );+ repo.commit("Add project config with broken template");++ let settings = setup_snapshot_settings_with_home(&repo, &temp_home);+ settings.bind(|| {+ let mut cmd = wt_command();+ repo.configure_wt_cmd(&mut cmd);+ cmd.arg("hook")+ .arg("show")+ .arg("pre-commit")+ .arg("--expanded")+ .current_dir(repo.root_path());+ set_temp_home_env(&mut cmd, temp_home.path());++ assert_cmd_snapshot!(cmd);+ });+}++/// Test that undefined variable errors show both template and error with --expanded.+/// The `base` variable is only defined for post-create hooks, so using it in pre-commit+/// will trigger an undefined variable error that shows both the error and raw template.+#[rstest]+fn test_hook_show_expanded_undefined_var(repo: TestRepo, temp_home: TempDir) {+ // Create user config without hooks+ 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"),+ r#"worktree-path = "../{{ repo }}.{{ branch }}"+"#,+ )+ .unwrap();++ // Create project config with `base` variable (only defined for post-create hooks)+ // In pre-commit context, this will be undefined and should show error + template+ repo.write_project_config(+ r#"[pre-commit]+optional-var = "echo {{ base }}"+"#,+ );+ repo.commit("Add project config with optional variable");++ let settings = setup_snapshot_settings_with_home(&repo, &temp_home);+ settings.bind(|| {+ let mut cmd = wt_command();+ repo.configure_wt_cmd(&mut cmd);+ cmd.arg("hook")+ .arg("show")+ .arg("pre-commit")+ .arg("--expanded")+ .current_dir(repo.root_path());+ set_temp_home_env(&mut cmd, temp_home.path());++ assert_cmd_snapshot!(cmd);+ });+}++/// Test that valid templates expand correctly with --expanded.+#[rstest]+fn test_hook_show_expanded_valid_template(repo: TestRepo, temp_home: TempDir) {+ // Create user config without hooks+ 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"),+ r#"worktree-path = "../{{ repo }}.{{ branch }}"+"#,+ )+ .unwrap();++ // Create project config with valid template using defined variables+ repo.write_project_config(+ r#"[pre-commit]+valid = "echo branch={{ branch }} repo={{ repo }}"+"#,+ );+ repo.commit("Add project config with valid template");++ let settings = setup_snapshot_settings_with_home(&repo, &temp_home);+ settings.bind(|| {+ let mut cmd = wt_command();+ repo.configure_wt_cmd(&mut cmd);+ cmd.arg("hook")+ .arg("show")+ .arg("pre-commit")+ .arg("--expanded")+ .current_dir(repo.root_path());+ set_temp_home_env(&mut cmd, temp_home.path());++ assert_cmd_snapshot!(cmd);+ });+}diff --git a/tests/snapshots/integration__integration_tests__hook_show__hook_show_expanded_syntax_error.snap b/tests/snapshots/integration__integration_tests__hook_show__hook_show_expanded_syntax_error.snapnew file mode 100644index 000000000..b4105763f--- /dev/null+++ b/tests/snapshots/integration__integration_tests__hook_show__hook_show_expanded_syntax_error.snap@@ -0,0 +1,45 @@+---+source: tests/integration_tests/hook_show.rs+info:+ program: wt+ args:+ - hook+ - show+ - pre-commit+ - "--expanded"+ env:+ APPDATA: "[TEST_CONFIG_HOME]"+ CLICOLOR_FORCE: "1"+ COLUMNS: "500"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ GIT_TERMINAL_PROMPT: "0"+ HOME: "[TEST_HOME]"+ LANG: C+ LC_ALL: C+ MOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"+ PATH: "[PATH]"+ RUST_LOG: warn+ SHELL: ""+ SOURCE_DATE_EPOCH: "1735776000"+ TERM: alacritty+ USERPROFILE: "[TEST_HOME]"+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"+---+success: true+exit_code: 0+----- stdout -----++----- stderr -----+[36mUSER HOOKS[39m [TEST_CONFIG]+[2m↳[22m [2m(none configured)[22m++[36mPROJECT HOOKS[39m _REPO_/.config/wt.toml+[36m❯[39m pre-commit [1mbroken[22m: [2m(requires approval)[22m+[107m [0m [2m# Template error: Template syntax error: syntax error: unexpected end of input, expected end of variable block (in hook preview:1)[0m[2m+[107m [0m [2m[0m[2m[34mecho[0m[2m {{ branchdiff --git a/tests/snapshots/integration__integration_tests__hook_show__hook_show_expanded_undefined_var.snap b/tests/snapshots/integration__integration_tests__hook_show__hook_show_expanded_undefined_var.snapnew file mode 100644index 000000000..4b6929244--- /dev/null+++ b/tests/snapshots/integration__integration_tests__hook_show__hook_show_expanded_undefined_var.snap@@ -0,0 +1,45 @@+---+source: tests/integration_tests/hook_show.rs+info:+ program: wt+ args:+ - hook+ - show+ - pre-commit+ - "--expanded"+ env:+ APPDATA: "[TEST_CONFIG_HOME]"+ CLICOLOR_FORCE: "1"+ COLUMNS: "500"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ GIT_TERMINAL_PROMPT: "0"+ HOME: "[TEST_HOME]"+ LANG: C+ LC_ALL: C+ MOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"+ PATH: "[PATH]"+ RUST_LOG: warn+ SHELL: ""+ SOURCE_DATE_EPOCH: "1735776000"+ TERM: alacritty+ USERPROFILE: "[TEST_HOME]"+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"+---+success: true+exit_code: 0+----- stdout -----++----- stderr -----+[36mUSER HOOKS[39m [TEST_CONFIG]+[2m↳[22m [2m(none configured)[22m++[36mPROJECT HOOKS[39m _REPO_/.config/wt.toml+[36m❯[39m pre-commit [1moptional-var[22m: [2m(requires approval)[22m+[107m [0m [2m# Template error: Template render error: undefined value (in hook preview:1)[0m[2m+[107m [0m [2m[0m[2m[34mecho[0m[2m {{ base }}diff --git a/tests/snapshots/integration__integration_tests__hook_show__hook_show_expanded_valid_template.snap b/tests/snapshots/integration__integration_tests__hook_show__hook_show_expanded_valid_template.snapnew file mode 100644index 000000000..78107a1c0--- /dev/null+++ b/tests/snapshots/integration__integration_tests__hook_show__hook_show_expanded_valid_template.snap@@ -0,0 +1,44 @@+---+source: tests/integration_tests/hook_show.rs+info:+ program: wt+ args:+ - hook+ - show+ - pre-commit+ - "--expanded"+ env:+ APPDATA: "[TEST_CONFIG_HOME]"+ CLICOLOR_FORCE: "1"+ COLUMNS: "500"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ GIT_TERMINAL_PROMPT: "0"+ HOME: "[TEST_HOME]"+ LANG: C+ LC_ALL: C+ MOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"+ PATH: "[PATH]"+ RUST_LOG: warn+ SHELL: ""+ SOURCE_DATE_EPOCH: "1735776000"+ TERM: alacritty+ USERPROFILE: "[TEST_HOME]"+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"+---+success: true+exit_code: 0+----- stdout -----++----- stderr -----+[36mUSER HOOKS[39m [TEST_CONFIG]+[2m↳[22m [2m(none configured)[22m++[36mPROJECT HOOKS[39m _REPO_/.config/wt.toml+[36m❯[39m pre-commit [1mvalid[22m: [2m(requires approval)[22m+[107m [0m [2m[0m[2m[34mecho[0m[2m branch=main repo=repo