Skip to content

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

refactor: share TestRepo across lib and bin unit tests (#1963)

Maximilian Roosf7672eae4891
diff --git a/src/commands/list/collect/execution.rs b/src/commands/list/collect/execution.rs
index 7f0bf50cf..808d13687 100644
--- a/src/commands/list/collect/execution.rs
+++ b/src/commands/list/collect/execution.rs
@@ -351,20 +351,13 @@ pub fn work_items_for_branch(
mod tests {
use super::*;
use std::collections::HashSet;
- use worktrunk::shell_exec::Cmd;
#[test]
fn test_skip_url_status_suppresses_placeholder_and_task() {
- let dir = tempfile::tempdir().expect("tempdir");
- Cmd::new("git")
- .args(["init"])
- .current_dir(dir.path())
- .run()
- .expect("git init");
-
- let repo = Repository::at(dir.path()).expect("repo");
+ let test = worktrunk::testing::TestRepo::new();
+ let repo = Repository::at(test.path()).expect("repo");
let wt = WorktreeInfo {
- path: dir.path().to_path_buf(),
+ path: test.path().to_path_buf(),
head: "deadbeef".to_string(),
branch: Some("main".to_string()),
bare: false,
@@ -402,16 +395,10 @@ mod tests {
#[test]
fn test_no_llm_command_skips_summary_generate() {
- let dir = tempfile::tempdir().expect("tempdir");
- Cmd::new("git")
- .args(["init"])
- .current_dir(dir.path())
- .run()
- .expect("git init");
-
- let repo = Repository::at(dir.path()).expect("repo");
+ let test = worktrunk::testing::TestRepo::new();
+ let repo = Repository::at(test.path()).expect("repo");
let wt = WorktreeInfo {
- path: dir.path().to_path_buf(),
+ path: test.path().to_path_buf(),
head: "deadbeef".to_string(),
branch: Some("main".to_string()),
bare: false,
diff --git a/src/commands/list/json_output.rs b/src/commands/list/json_output.rs
index 361bb5f2a..1b7712d1a 100644
--- a/src/commands/list/json_output.rs
+++ b/src/commands/list/json_output.rs
@@ -873,15 +873,10 @@ mod tests {
"#);
}
- fn test_repo() -> (tempfile::TempDir, Repository) {
- let tmp = tempfile::tempdir().unwrap();
- worktrunk::shell_exec::Cmd::new("git")
- .args(["init", "-b", "main"])
- .current_dir(tmp.path())
- .run()
- .unwrap();
- let repo = Repository::at(tmp.path()).unwrap();
- (tmp, repo)
+ fn test_repo() -> (worktrunk::testing::TestRepo, Repository) {
+ let t = worktrunk::testing::TestRepo::new();
+ let repo = Repository::at(t.path()).unwrap();
+ (t, repo)
}
#[test]
diff --git a/src/commands/picker/summary.rs b/src/commands/picker/summary.rs
index fd718df79..ec866477e 100644
--- a/src/commands/picker/summary.rs
+++ b/src/commands/picker/summary.rs
@@ -58,59 +58,47 @@ mod tests {
use super::*;
use crate::commands::list::model::{ItemKind, WorktreeData};
use std::fs;
- use worktrunk::shell_exec::Cmd;
-
- fn git_init(dir: &std::path::Path, args: &[&str]) {
- Cmd::new("git")
- .args(args.iter().copied())
- .current_dir(dir)
- .run()
- .unwrap();
- }
-
- fn configure_test_identity(repo: &Repository) {
- repo.run_command(&["config", "user.name", "Test"]).unwrap();
- repo.run_command(&["config", "user.email", "[email protected]"])
- .unwrap();
- }
+ use worktrunk::testing::{TestRepo, set_test_identity};
/// Create a minimal temp git repo (for cache-only tests that don't need branches).
- fn temp_repo() -> (tempfile::TempDir, Repository) {
- let dir = tempfile::tempdir().unwrap();
- git_init(dir.path(), &["init", "--initial-branch=main"]);
- let repo = Repository::at(dir.path()).unwrap();
- configure_test_identity(&repo);
- repo.run_command(&["commit", "--allow-empty", "-m", "init"])
+ fn temp_repo() -> (TestRepo, Repository) {
+ let t = TestRepo::new();
+ set_test_identity(&t.repo);
+ t.repo
+ .run_command(&["commit", "--allow-empty", "-m", "init"])
.unwrap();
- (dir, repo)
+ let repo = Repository::at(t.path()).unwrap();
+ (t, repo)
}
/// Create a temp repo with main branch, default-branch config, and a real commit.
- fn temp_repo_configured() -> (tempfile::TempDir, Repository, String) {
- let dir = tempfile::tempdir().unwrap();
- git_init(dir.path(), &["init", "--initial-branch=main"]);
- let repo = Repository::at(dir.path()).unwrap();
- configure_test_identity(&repo);
- repo.run_command(&["config", "worktrunk.default-branch", "main"])
+ fn temp_repo_configured() -> (TestRepo, Repository, String) {
+ let t = TestRepo::new();
+ set_test_identity(&t.repo);
+ t.repo
+ .run_command(&["config", "worktrunk.default-branch", "main"])
.unwrap();
- fs::write(dir.path().join("README.md"), "# Project\n").unwrap();
- repo.run_command(&["add", "README.md"]).unwrap();
- repo.run_command(&["commit", "-m", "initial commit"])
+ fs::write(t.path().join("README.md"), "# Project\n").unwrap();
+ t.repo.run_command(&["add", "README.md"]).unwrap();
+ t.repo
+ .run_command(&["commit", "-m", "initial commit"])
.unwrap();
- let head = repo
+ let head = t
+ .repo
.run_command(&["rev-parse", "HEAD"])
.unwrap()
.trim()
.to_string();
- (dir, repo, head)
+ let repo = Repository::at(t.path()).unwrap();
+ (t, repo, head)
}
/// Create a temp repo with main + feature branch that has real changes.
- fn temp_repo_with_feature() -> (tempfile::TempDir, Repository, String) {
- let (dir, repo, _) = temp_repo_configured();
+ fn temp_repo_with_feature() -> (TestRepo, Repository, String) {
+ let (t, repo, _) = temp_repo_configured();
repo.run_command(&["checkout", "-b", "feature"]).unwrap();
- fs::write(dir.path().join("new.txt"), "new content\n").unwrap();
+ fs::write(t.path().join("new.txt"), "new content\n").unwrap();
repo.run_command(&["add", "new.txt"]).unwrap();
repo.run_command(&["commit", "-m", "add new file"]).unwrap();
@@ -119,8 +107,8 @@ mod tests {
.unwrap()
.trim()
.to_string();
- let repo = Repository::at(dir.path()).unwrap();
- (dir, repo, head)
+ let repo = Repository::at(t.path()).unwrap();
+ (t, repo, head)
}
fn feature_item(head: &str, path: &std::path::Path) -> ListItem {
@@ -135,7 +123,7 @@ mod tests {
#[test]
fn test_cache_roundtrip() {
use crate::summary::{CachedSummary, read_cache, write_cache};
- let (_dir, repo) = temp_repo();
+ let (_t, repo) = temp_repo();
let branch = "feature/test-branch";
let cached = CachedSummary {
summary: "Add tests\n\nThis adds unit tests for cache.".to_string(),
@@ -155,7 +143,7 @@ mod tests {
#[test]
fn test_write_cache_handles_unwritable_path() {
use crate::summary::{CachedSummary, read_cache, write_cache};
- let (_dir, repo) = temp_repo();
+ let (_t, repo) = temp_repo();
// Block cache directory creation by placing a file where the directory should be
let wt_dir = repo.wt_dir();
fs::create_dir_all(&wt_dir).unwrap();
@@ -181,7 +169,7 @@ mod tests {
use crate::summary::{CachedSummary, cache_dir, read_cache, write_cache};
use std::os::unix::fs::PermissionsExt;
- let (_dir, repo) = temp_repo();
+ let (_t, repo) = temp_repo();
let cache_path = cache_dir(&repo);
fs::create_dir_all(&cache_path).unwrap();
// Make directory read-only so file writes fail
@@ -203,7 +191,7 @@ mod tests {
#[test]
fn test_cache_invalidation_by_hash() {
use crate::summary::{CachedSummary, read_cache, write_cache};
- let (_dir, repo) = temp_repo();
+ let (_t, repo) = temp_repo();
let branch = "main";
let cached = CachedSummary {
summary: "Old summary".to_string(),
@@ -219,7 +207,7 @@ mod tests {
#[test]
fn test_cache_file_uses_sanitized_branch() {
use crate::summary::cache_file;
- let (_dir, repo) = temp_repo();
+ let (_t, repo) = temp_repo();
let path = cache_file(&repo, "feature/my-branch");
let filename = path.file_name().unwrap().to_str().unwrap();
assert!(filename.starts_with("feature-my-branch-"));
@@ -229,7 +217,7 @@ mod tests {
#[test]
fn test_cache_dir_under_git() {
use crate::summary::cache_dir;
- let (_dir, repo) = temp_repo();
+ let (_t, repo) = temp_repo();
let dir = cache_dir(&repo);
assert!(dir.to_str().unwrap().contains("wt"));
assert!(dir.to_str().unwrap().contains("summaries"));
@@ -339,9 +327,9 @@ mod tests {
#[test]
fn test_compute_combined_diff_with_branch_changes() {
use crate::summary::compute_combined_diff;
- let (dir, repo, head) = temp_repo_with_feature();
+ let (t, repo, head) = temp_repo_with_feature();
- let result = compute_combined_diff("feature", &head, Some(dir.path()), &repo);
+ let result = compute_combined_diff("feature", &head, Some(t.path()), &repo);
assert!(result.is_some());
let combined = result.unwrap();
assert!(combined.diff.contains("new.txt"));
@@ -351,21 +339,21 @@ mod tests {
#[test]
fn test_compute_combined_diff_default_branch_no_changes() {
use crate::summary::compute_combined_diff;
- let (dir, repo, head) = temp_repo_configured();
+ let (t, repo, head) = temp_repo_configured();
- let result = compute_combined_diff("main", &head, Some(dir.path()), &repo);
+ let result = compute_combined_diff("main", &head, Some(t.path()), &repo);
assert!(result.is_none());
}
#[test]
fn test_compute_combined_diff_with_uncommitted_changes() {
use crate::summary::compute_combined_diff;
- let (dir, repo, head) = temp_repo_with_feature();
+ let (t, repo, head) = temp_repo_with_feature();
// Add uncommitted changes
- fs::write(dir.path().join("uncommitted.txt"), "wip\n").unwrap();
+ fs::write(t.path().join("uncommitted.txt"), "wip\n").unwrap();
repo.run_command(&["add", "uncommitted.txt"]).unwrap();
- let result = compute_combined_diff("feature", &head, Some(dir.path()), &repo);
+ let result = compute_combined_diff("feature", &head, Some(t.path()), &repo);
assert!(result.is_some());
let combined = result.unwrap();
// Should contain both the branch diff and the working tree diff
@@ -376,7 +364,7 @@ mod tests {
#[test]
fn test_compute_combined_diff_branch_only_no_worktree() {
use crate::summary::compute_combined_diff;
- let (_dir, repo, head) = temp_repo_with_feature();
+ let (_t, repo, head) = temp_repo_with_feature();
// Branch-only item (no worktree data) — only branch diff included
let result = compute_combined_diff("feature", &head, None, &repo);
assert!(result.is_some());
@@ -392,9 +380,13 @@ mod tests {
// "master", "develop", "trunk"). This ensures default_branch() returns
// None, exercising the code path where branch diff is skipped.
let dir = tempfile::tempdir().unwrap();
- git_init(dir.path(), &["init", "--initial-branch=init-branch"]);
+ worktrunk::shell_exec::Cmd::new("git")
+ .args(["init", "--initial-branch=init-branch"])
+ .current_dir(dir.path())
+ .run()
+ .unwrap();
let setup_repo = Repository::at(dir.path()).unwrap();
- configure_test_identity(&setup_repo);
+ set_test_identity(&setup_repo);
fs::write(dir.path().join("README.md"), "# Project\n").unwrap();
setup_repo.run_command(&["add", "README.md"]).unwrap();
setup_repo
@@ -435,12 +427,12 @@ mod tests {
#[test]
fn test_generate_summary_calls_llm() {
- let (dir, repo, head) = temp_repo_with_feature();
+ let (t, repo, head) = temp_repo_with_feature();
let summary = crate::summary::generate_summary(
"feature",
&head,
- Some(dir.path()),
+ Some(t.path()),
"cat >/dev/null && echo 'Add new file'",
&repo,
);
@@ -449,12 +441,12 @@ mod tests {
#[test]
fn test_generate_summary_caches_result() {
- let (dir, repo, head) = temp_repo_with_feature();
+ let (t, repo, head) = temp_repo_with_feature();
let summary1 = crate::summary::generate_summary(
"feature",
&head,
- Some(dir.path()),
+ Some(t.path()),
"cat >/dev/null && echo 'Add new file'",
&repo,
);
@@ -464,7 +456,7 @@ mod tests {
let summary2 = crate::summary::generate_summary(
"feature",
&head,
- Some(dir.path()),
+ Some(t.path()),
"cat >/dev/null && echo 'Different output'",
&repo,
);
@@ -473,12 +465,12 @@ mod tests {
#[test]
fn test_generate_summary_no_changes() {
- let (dir, repo, head) = temp_repo_configured();
+ let (t, repo, head) = temp_repo_configured();
let summary = crate::summary::generate_summary(
"main",
&head,
- Some(dir.path()),
+ Some(t.path()),
"echo 'should not run'",
&repo,
);
@@ -487,12 +479,12 @@ mod tests {
#[test]
fn test_generate_summary_llm_error() {
- let (dir, repo, head) = temp_repo_with_feature();
+ let (t, repo, head) = temp_repo_with_feature();
let summary = crate::summary::generate_summary(
"feature",
&head,
- Some(dir.path()),
+ Some(t.path()),
"cat >/dev/null && echo 'fail' >&2 && exit 1",
&repo,
);
@@ -501,8 +493,8 @@ mod tests {
#[test]
fn test_generate_and_cache_summary_populates_cache() {
- let (dir, repo, head) = temp_repo_with_feature();
- let item = feature_item(&head, dir.path());
+ let (t, repo, head) = temp_repo_with_feature();
+ let item = feature_item(&head, t.path());
let cache: DashMap<PreviewCacheKey, String> = DashMap::new();
generate_and_cache_summary(
diff --git a/src/config/expansion.rs b/src/config/expansion.rs
index 1aab2bc75..0c390c985 100644
--- a/src/config/expansion.rs
+++ b/src/config/expansion.rs
@@ -538,7 +538,7 @@ mod tests {
use super::*;
use crate::shell_exec::Cmd;
- use crate::testutil::TestRepo;
+ use crate::testing::TestRepo;
fn test_repo() -> TestRepo {
TestRepo::new()
diff --git a/src/config/mod.rs b/src/config/mod.rs
index 686e5dddd..694446b08 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -112,7 +112,7 @@ mod tests {
use insta::assert_snapshot;
use super::*;
- use crate::testutil::TestRepo;
+ use crate::testing::TestRepo;
fn test_repo() -> TestRepo {
TestRepo::new()
diff --git a/src/config/test.rs b/src/config/test.rs
index 15ebaa6b8..307ec511a 100644
--- a/src/config/test.rs
+++ b/src/config/test.rs
@@ -4,7 +4,7 @@
//! edge cases in template variable substitution.
use super::expand_template;
-use crate::testutil::TestRepo;
+use crate::testing::TestRepo;
use std::collections::HashMap;
fn test_repo() -> TestRepo {
diff --git a/src/config/user/tests.rs b/src/config/user/tests.rs
index 22944f78a..34c78cf2a 100644
--- a/src/config/user/tests.rs
+++ b/src/config/user/tests.rs
@@ -1,7 +1,7 @@
use super::*;
use crate::config::HooksConfig;
use crate::git::HookType;
-use crate::testutil::TestRepo;
+use crate::testing::TestRepo;
fn test_repo() -> TestRepo {
TestRepo::new()
diff --git a/src/git/recover.rs b/src/git/recover.rs
index dd87be7df..186fb6305 100644
--- a/src/git/recover.rs
+++ b/src/git/recover.rs
@@ -208,7 +208,7 @@ fn paths_match(worktree_path: &Path, deleted_path: &Path) -> bool {
mod tests {
use super::*;
use crate::shell_exec::Cmd;
- use crate::testutil::set_test_identity;
+ use crate::testing::set_test_identity;
use ansi_str::AnsiStr;
fn git_init(path: &Path) {
diff --git a/src/lib.rs b/src/lib.rs
index 8c006d4b3..cdd259204 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -20,8 +20,8 @@ pub mod sync;
pub mod trace;
pub mod utils;
-#[cfg(test)]
-pub(crate) mod testutil;
+#[doc(hidden)]
+pub mod testing;
// Re-export HookType for convenience
pub use git::HookType;
diff --git a/src/testutil.rs b/src/testing.rs
similarity index 85%
rename from src/testutil.rs
rename to src/testing.rs
index 7c46888bf..d1046b3be 100644
--- a/src/testutil.rs
+++ b/src/testing.rs
@@ -1,8 +1,12 @@
-//! Shared test utilities for unit tests within the worktrunk crate.
+//! Shared test fixtures for worktrunk unit tests.
//!
//! Provides lightweight git repository fixtures for tests that need a real
//! `.git` directory (template expansion, config resolution, work item generation).
//!
+//! This module is `#[doc(hidden)] pub` so both library (`src/`) and binary
+//! (`src/commands/`) unit tests can use it — `#[cfg(test)]` modules are only
+//! visible within their own crate.
+//!
//! For integration tests, use `tests/common/mod.rs` (`TestRepo`) instead — it
//! provides full CLI isolation, snapshot filters, and mock commands.
@@ -23,6 +27,7 @@ impl TestRepo {
/// Create a new repo with `git init -b main`.
///
/// Uses explicit `-b main` for determinism regardless of system git config.
+ #[allow(clippy::new_without_default)]
pub fn new() -> Self {
let dir = tempfile::tempdir().unwrap();
Cmd::new("git")