Skip to content

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

test: add integration tests for post-commit hook execution (#1711)

worktrunk-botaedfc2375ef8
diff --git a/tests/integration_tests/user_hooks.rs b/tests/integration_tests/user_hooks.rs
index b6620570b..b377f7e31 100644
--- a/tests/integration_tests/user_hooks.rs
+++ b/tests/integration_tests/user_hooks.rs
@@ -918,6 +918,116 @@ lint = "exit 1"
);
}
+// ============================================================================
+// User Post-Commit Hook Tests (Background, via `wt step commit`)
+// ============================================================================
+
+/// Helper for step commit snapshots
+fn snapshot_step_commit(
+ test_name: &str,
+ repo: &TestRepo,
+ args: &[&str],
+ cwd: Option<&std::path::Path>,
+) {
+ let settings = setup_snapshot_settings(repo);
+ settings.bind(|| {
+ let mut cmd = make_snapshot_cmd(repo, "step", &[], cwd);
+ cmd.arg("commit");
+ cmd.args(args);
+ cmd.env(
+ "WORKTRUNK_COMMIT__GENERATION__COMMAND",
+ "cat >/dev/null && echo 'feat: test commit'",
+ );
+ assert_cmd_snapshot!(test_name, cmd);
+ });
+}
+
+#[rstest]
+fn test_user_post_commit_hook_executes(mut repo: TestRepo) {
+ // Create feature worktree with staged changes
+ let feature_wt = repo.add_worktree("feature");
+ fs::write(feature_wt.join("new_file.txt"), "content").unwrap();
+
+ // Write user config with post-commit hook
+ repo.write_test_config(
+ r#"[post-commit]
+notify = "echo 'USER_POST_COMMIT_RAN' > user_postcommit.txt"
+"#,
+ );
+
+ snapshot_step_commit("user_post_commit_executes", &repo, &[], Some(&feature_wt));
+
+ // Post-commit runs in background in the worktree where the commit happened
+ let marker_file = feature_wt.join("user_postcommit.txt");
+ wait_for_file_content(&marker_file);
+
+ let contents = fs::read_to_string(&marker_file).unwrap();
+ assert!(
+ contents.contains("USER_POST_COMMIT_RAN"),
+ "User post-commit hook should have run, got: {contents}"
+ );
+}
+
+#[rstest]
+fn test_user_post_commit_skipped_with_no_verify(mut repo: TestRepo) {
+ // Create feature worktree with staged changes
+ let feature_wt = repo.add_worktree("feature");
+ fs::write(feature_wt.join("new_file.txt"), "content").unwrap();
+
+ // Write user config with post-commit hook
+ repo.write_test_config(
+ r#"[post-commit]
+notify = "echo 'USER_POST_COMMIT_RAN' > user_postcommit.txt"
+"#,
+ );
+
+ snapshot_step_commit(
+ "user_post_commit_skipped_no_verify",
+ &repo,
+ &["--no-verify"],
+ Some(&feature_wt),
+ );
+
+ // Wait to ensure background hook would have had time to run
+ thread::sleep(SLEEP_FOR_ABSENCE_CHECK);
+
+ let marker_file = feature_wt.join("user_postcommit.txt");
+ assert!(
+ !marker_file.exists(),
+ "User post-commit hook should be skipped with --no-verify"
+ );
+}
+
+#[rstest]
+fn test_user_post_commit_failure_does_not_block_commit(mut repo: TestRepo) {
+ // Create feature worktree with staged changes
+ let feature_wt = repo.add_worktree("feature");
+ fs::write(feature_wt.join("new_file.txt"), "content").unwrap();
+
+ // Write user config with failing post-commit hook
+ repo.write_test_config(
+ r#"[post-commit]
+failing = "exit 1"
+"#,
+ );
+
+ snapshot_step_commit("user_post_commit_failure", &repo, &[], Some(&feature_wt));
+
+ // The commit should have succeeded despite post-commit hook failure
+ // (post-commit runs in background and doesn't affect exit code)
+ let output = repo
+ .git_command()
+ .current_dir(&feature_wt)
+ .args(["log", "--oneline", "-1"])
+ .output()
+ .unwrap();
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ assert!(
+ stdout.contains("feat: test commit"),
+ "Commit should have succeeded despite post-commit hook failure, got: {stdout}"
+ );
+}
+
// ============================================================================
// Template Variable Tests
// ============================================================================
diff --git a/tests/snapshots/integration__integration_tests__user_hooks__user_post_commit_executes.snap b/tests/snapshots/integration__integration_tests__user_hooks__user_post_commit_executes.snap
new file mode 100644
index 000000000..7ab9fdde4
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__user_hooks__user_post_commit_executes.snap
@@ -0,0 +1,51 @@
+---
+source: tests/integration_tests/user_hooks.rs
+info:
+ program: wt
+ args:
+ - step
+ - commit
+ 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]"
+ NO_COLOR: ""
+ PATH: "[PATH]"
+ PSModulePath: ""
+ RUST_LOG: warn
+ SHELL: ""
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
+ WORKTRUNK_COMMIT__GENERATION__COMMAND: "cat >/dev/null && echo 'feat: test commit'"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
+ WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
+ WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
+ WORKTRUNK_TEST_EPOCH: "1735776000"
+ WORKTRUNK_TEST_NUSHELL_ENV: "0"
+ WORKTRUNK_TEST_POWERSHELL_ENV: "0"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+
+----- stderr -----
+▲ Auto-staging 1 untracked path:
+  new_file.txt
+◎ Generating commit message and committing changes... (1 file, +1)
+  feat: test commit
+✓ Committed changes @ [HASH]
+◎ Running post-commit: user:notify
diff --git a/tests/snapshots/integration__integration_tests__user_hooks__user_post_commit_failure.snap b/tests/snapshots/integration__integration_tests__user_hooks__user_post_commit_failure.snap
new file mode 100644
index 000000000..ce070b5fe
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__user_hooks__user_post_commit_failure.snap
@@ -0,0 +1,51 @@
+---
+source: tests/integration_tests/user_hooks.rs
+info:
+ program: wt
+ args:
+ - step
+ - commit
+ 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]"
+ NO_COLOR: ""
+ PATH: "[PATH]"
+ PSModulePath: ""
+ RUST_LOG: warn
+ SHELL: ""
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
+ WORKTRUNK_COMMIT__GENERATION__COMMAND: "cat >/dev/null && echo 'feat: test commit'"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
+ WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
+ WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
+ WORKTRUNK_TEST_EPOCH: "1735776000"
+ WORKTRUNK_TEST_NUSHELL_ENV: "0"
+ WORKTRUNK_TEST_POWERSHELL_ENV: "0"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+
+----- stderr -----
+▲ Auto-staging 1 untracked path:
+  new_file.txt
+◎ Generating commit message and committing changes... (1 file, +1)
+  feat: test commit
+✓ Committed changes @ [HASH]
+◎ Running post-commit: user:failing
diff --git a/tests/snapshots/integration__integration_tests__user_hooks__user_post_commit_skipped_no_verify.snap b/tests/snapshots/integration__integration_tests__user_hooks__user_post_commit_skipped_no_verify.snap
new file mode 100644
index 000000000..d7ddf8488
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__user_hooks__user_post_commit_skipped_no_verify.snap
@@ -0,0 +1,51 @@
+---
+source: tests/integration_tests/user_hooks.rs
+info:
+ program: wt
+ args:
+ - step
+ - commit
+ - "--no-verify"
+ 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]"
+ NO_COLOR: ""
+ PATH: "[PATH]"
+ PSModulePath: ""
+ RUST_LOG: warn
+ SHELL: ""
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
+ WORKTRUNK_COMMIT__GENERATION__COMMAND: "cat >/dev/null && echo 'feat: test commit'"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
+ WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
+ WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
+ WORKTRUNK_TEST_EPOCH: "1735776000"
+ WORKTRUNK_TEST_NUSHELL_ENV: "0"
+ WORKTRUNK_TEST_POWERSHELL_ENV: "0"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+
+----- stderr -----
+▲ Auto-staging 1 untracked path:
+  new_file.txt
+◎ Generating commit message and committing changes... (1 file, +1)
+  feat: test commit
+✓ Committed changes @ [HASH]