Skip to content

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

Refactor snapshot test helpers for reusability

Maximilian Roosf1523cdb47a1
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index e24b67b28..139f51086 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -340,3 +340,45 @@ impl TestRepo {
output.status.success()
}
}
+
+/// Create configured insta Settings for snapshot tests
+///
+/// This extracts the common settings configuration while allowing the
+/// `assert_cmd_snapshot!` macro to remain in test files for correct module path capture.
+pub fn setup_snapshot_settings(repo: &TestRepo) -> insta::Settings {
+ let mut settings = insta::Settings::clone_current();
+ settings.set_snapshot_path("../snapshots");
+
+ // Normalize paths
+ settings.add_filter(repo.root_path().to_str().unwrap(), "[REPO]");
+ for (name, path) in &repo.worktrees {
+ settings.add_filter(
+ path.to_str().unwrap(),
+ &format!("[WORKTREE_{}]", name.to_uppercase().replace('-', "_")),
+ );
+ }
+
+ // Normalize git SHAs and backslashes
+ settings.add_filter(r"\b[0-9a-f]{7,40}\b", "[SHA]");
+ settings.add_filter(r"\\", "/");
+
+ settings
+}
+
+/// Create a configured Command for snapshot testing
+///
+/// This extracts the common command setup while allowing the test file
+/// to call the macro with the correct module path for snapshot naming.
+pub fn make_snapshot_cmd(
+ repo: &TestRepo,
+ subcommand: &str,
+ args: &[&str],
+ cwd: Option<&Path>,
+) -> Command {
+ let mut cmd = Command::new(insta_cmd::get_cargo_bin("wt"));
+ repo.clean_cli_env(&mut cmd);
+ cmd.arg(subcommand)
+ .args(args)
+ .current_dir(cwd.unwrap_or(repo.root_path()));
+ cmd
+}
diff --git a/tests/integration_tests/finish.rs b/tests/integration_tests/finish.rs
index badcbb124..d00bc0eb8 100644
--- a/tests/integration_tests/finish.rs
+++ b/tests/integration_tests/finish.rs
@@ -1,33 +1,12 @@
-use crate::common::TestRepo;
-use insta::Settings;
-use insta_cmd::{assert_cmd_snapshot, get_cargo_bin};
+use crate::common::{TestRepo, make_snapshot_cmd, setup_snapshot_settings};
+use insta_cmd::assert_cmd_snapshot;
use std::process::Command;
/// Helper to create snapshot with normalized paths
fn snapshot_finish(test_name: &str, repo: &TestRepo, args: &[&str], cwd: Option<&std::path::Path>) {
- let mut settings = Settings::clone_current();
- settings.set_snapshot_path("../snapshots");
-
- // Normalize paths
- settings.add_filter(repo.root_path().to_str().unwrap(), "[REPO]");
- for (name, path) in &repo.worktrees {
- settings.add_filter(
- path.to_str().unwrap(),
- format!("[WORKTREE_{}]", name.to_uppercase().replace('-', "_")),
- );
- }
-
- // Normalize git SHAs
- settings.add_filter(r"\b[0-9a-f]{7,40}\b", "[SHA]");
- settings.add_filter(r"\\", "/");
-
+ let settings = setup_snapshot_settings(repo);
settings.bind(|| {
- let mut cmd = Command::new(get_cargo_bin("wt"));
- repo.clean_cli_env(&mut cmd);
- cmd.arg("finish")
- .args(args)
- .current_dir(cwd.unwrap_or(repo.root_path()));
-
+ let mut cmd = make_snapshot_cmd(repo, "finish", args, cwd);
assert_cmd_snapshot!(test_name, cmd);
});
}
diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rs
index 934398379..c0a69a522 100644
--- a/tests/integration_tests/merge.rs
+++ b/tests/integration_tests/merge.rs
@@ -1,33 +1,12 @@
-use crate::common::TestRepo;
-use insta::Settings;
-use insta_cmd::{assert_cmd_snapshot, get_cargo_bin};
+use crate::common::{TestRepo, make_snapshot_cmd, setup_snapshot_settings};
+use insta_cmd::assert_cmd_snapshot;
use std::process::Command;
/// Helper to create snapshot with normalized paths
fn snapshot_merge(test_name: &str, repo: &TestRepo, args: &[&str], cwd: Option<&std::path::Path>) {
- let mut settings = Settings::clone_current();
- settings.set_snapshot_path("../snapshots");
-
- // Normalize paths
- settings.add_filter(repo.root_path().to_str().unwrap(), "[REPO]");
- for (name, path) in &repo.worktrees {
- settings.add_filter(
- path.to_str().unwrap(),
- &format!("[WORKTREE_{}]", name.to_uppercase().replace('-', "_")),
- );
- }
-
- // Normalize git SHAs
- settings.add_filter(r"\b[0-9a-f]{7,40}\b", "[SHA]");
- settings.add_filter(r"\\", "/");
-
+ let settings = setup_snapshot_settings(repo);
settings.bind(|| {
- let mut cmd = Command::new(get_cargo_bin("wt"));
- repo.clean_cli_env(&mut cmd);
- cmd.arg("merge")
- .args(args)
- .current_dir(cwd.unwrap_or(repo.root_path()));
-
+ let mut cmd = make_snapshot_cmd(repo, "merge", args, cwd);
assert_cmd_snapshot!(test_name, cmd);
});
}
diff --git a/tests/integration_tests/push.rs b/tests/integration_tests/push.rs
index 758a25bd6..bcf22dc6e 100644
--- a/tests/integration_tests/push.rs
+++ b/tests/integration_tests/push.rs
@@ -1,33 +1,12 @@
-use crate::common::TestRepo;
-use insta::Settings;
-use insta_cmd::{assert_cmd_snapshot, get_cargo_bin};
+use crate::common::{TestRepo, make_snapshot_cmd, setup_snapshot_settings};
+use insta_cmd::assert_cmd_snapshot;
use std::process::Command;
/// Helper to create snapshot with normalized paths
fn snapshot_push(test_name: &str, repo: &TestRepo, args: &[&str], cwd: Option<&std::path::Path>) {
- let mut settings = Settings::clone_current();
- settings.set_snapshot_path("../snapshots");
-
- // Normalize paths
- settings.add_filter(repo.root_path().to_str().unwrap(), "[REPO]");
- for (name, path) in &repo.worktrees {
- settings.add_filter(
- path.to_str().unwrap(),
- format!("[WORKTREE_{}]", name.to_uppercase().replace('-', "_")),
- );
- }
-
- // Normalize git SHAs
- settings.add_filter(r"\b[0-9a-f]{7,40}\b", "[SHA]");
- settings.add_filter(r"\\", "/");
-
+ let settings = setup_snapshot_settings(repo);
settings.bind(|| {
- let mut cmd = Command::new(get_cargo_bin("wt"));
- repo.clean_cli_env(&mut cmd);
- cmd.arg("push")
- .args(args)
- .current_dir(cwd.unwrap_or(repo.root_path()));
-
+ let mut cmd = make_snapshot_cmd(repo, "push", args, cwd);
assert_cmd_snapshot!(test_name, cmd);
});
}
diff --git a/tests/integration_tests/switch.rs b/tests/integration_tests/switch.rs
index 874f479d2..3389473a4 100644
--- a/tests/integration_tests/switch.rs
+++ b/tests/integration_tests/switch.rs
@@ -1,33 +1,12 @@
-use crate::common::TestRepo;
-use insta::Settings;
-use insta_cmd::{assert_cmd_snapshot, get_cargo_bin};
+use crate::common::{TestRepo, make_snapshot_cmd, setup_snapshot_settings};
+use insta_cmd::assert_cmd_snapshot;
use std::process::Command;
/// Helper to create snapshot with normalized paths and SHAs
fn snapshot_switch(test_name: &str, repo: &TestRepo, args: &[&str]) {
- let mut settings = Settings::clone_current();
- settings.set_snapshot_path("../snapshots");
-
- // Normalize paths - replace absolute paths with semantic names
- settings.add_filter(repo.root_path().to_str().unwrap(), "[REPO]");
- for (name, path) in &repo.worktrees {
- settings.add_filter(
- path.to_str().unwrap(),
- format!("[WORKTREE_{}]", name.to_uppercase().replace('-', "_")),
- );
- }
-
- // Normalize git SHAs (7-40 hex chars) to [SHA]
- settings.add_filter(r"\b[0-9a-f]{7,40}\b", "[SHA]");
-
- // Normalize Windows paths to Unix style
- settings.add_filter(r"\\", "/");
-
+ let settings = setup_snapshot_settings(repo);
settings.bind(|| {
- let mut cmd = Command::new(get_cargo_bin("wt"));
- repo.clean_cli_env(&mut cmd);
- cmd.arg("switch").args(args).current_dir(repo.root_path());
-
+ let mut cmd = make_snapshot_cmd(repo, "switch", args, None);
assert_cmd_snapshot!(test_name, cmd);
});
}