use rstest::rstest;
use worktrunk::git::Repository;
mod common {
pub use crate::common::*;
}
use common::{TestRepo, repo};
#[rstest]
fn test_repository_clone_shares_cache(repo: TestRepo) {
let repo1 = Repository::at(repo.root_path()).unwrap();
let repo2 = repo1.clone();
assert!(
repo1.shares_cache_with(&repo2),
"Cloned repositories should share the same cache"
);
}
#[rstest]
fn test_cache_shared_between_clones(repo: TestRepo) {
let repo1 = Repository::at(repo.root_path()).unwrap();
let repo2 = repo1.clone();
let default1 = repo1.default_branch().unwrap();
let default2 = repo2.default_branch().unwrap();
assert_eq!(default1, default2);
assert_eq!(default1, "main"); }
#[rstest]
fn test_merge_base_cache_shared(mut repo: TestRepo) {
repo.add_worktree("feature");
let feature_path = repo.worktree_path("feature");
repo.commit_in_worktree(feature_path, "feature.txt", "content", "feature commit");
let repo1 = Repository::at(repo.root_path()).unwrap();
let repo2 = repo1.clone();
let main_head = repo.head_sha();
let feature_head = repo.head_sha_in(feature_path);
let base1 = repo1.merge_base(&main_head, &feature_head).unwrap();
let base2 = repo2.merge_base(&main_head, &feature_head).unwrap();
assert_eq!(base1, base2);
assert_eq!(base1, Some(main_head));
}
#[rstest]
fn test_parallel_tasks_share_cache(mut repo: TestRepo) {
use std::thread;
repo.add_worktree("feature-a");
repo.add_worktree("feature-b");
let repo1 = Repository::at(repo.root_path()).unwrap();
let handles: Vec<_> = (0..4)
.map(|i| {
let repo_clone = repo1.clone();
thread::spawn(move || {
let default = repo_clone.default_branch().unwrap();
(i, default)
})
})
.collect();
let results: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect();
for (_, branch) in &results {
assert_eq!(branch, "main");
}
}
#[rstest]
fn test_separate_repositories_have_separate_caches(repo: TestRepo) {
let repo1 = Repository::at(repo.root_path()).unwrap();
let repo2 = Repository::at(repo.root_path()).unwrap();
assert!(
!repo1.shares_cache_with(&repo2),
"Separately created repositories should have independent caches"
);
let default1 = repo1.default_branch().unwrap();
let default2 = repo2.default_branch().unwrap();
assert_eq!(default1, default2);
}