Skip to content

Commit a8be178

Browse files
jamesaphoenixclaude
andcommitted
fix: serialize env-var-based tests with mutex to prevent CI race conditions
All comment cache and refinement cache integration tests now use a shared mutex (ENV_LOCK) to prevent parallel test threads from stomping on each other's DIFFCORE_COMMENT_CACHE_DIR and DIFFCORE_REFINEMENT_CACHE_DIR env vars. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 182ad9e commit a8be178

1 file changed

Lines changed: 27 additions & 19 deletions

File tree

‎crates/diffcore-tauri/tests/comment_integration.rs‎

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,19 @@ fn create_test_repo_on_branch(branch_name: &str) -> (tempfile::TempDir, String)
406406
(tmp, path)
407407
}
408408

409-
/// Helper: set the cache dir env var for isolated testing.
410-
fn with_comment_cache_dir<F: FnOnce()>(dir: &std::path::Path, f: F) {
411-
std::env::set_var("DIFFCORE_COMMENT_CACHE_DIR", dir.as_os_str());
409+
/// Mutex to serialize tests that use env vars (prevents races in parallel execution).
410+
static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
411+
412+
/// Helper: set the cache dir env vars for isolated testing (serialized via mutex).
413+
fn with_cache_dirs<F: FnOnce()>(comment_dir: &std::path::Path, refinement_dir: Option<&std::path::Path>, f: F) {
414+
let _guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
415+
std::env::set_var("DIFFCORE_COMMENT_CACHE_DIR", comment_dir.as_os_str());
416+
if let Some(rd) = refinement_dir {
417+
std::env::set_var("DIFFCORE_REFINEMENT_CACHE_DIR", rd.as_os_str());
418+
}
412419
f();
413420
std::env::remove_var("DIFFCORE_COMMENT_CACHE_DIR");
421+
std::env::remove_var("DIFFCORE_REFINEMENT_CACHE_DIR");
414422
}
415423

416424
// ── Save & Load by branch ───────────────────────────────────────────
@@ -420,7 +428,7 @@ fn cached_save_and_load_single_comment() {
420428
let cache_tmp = tempfile::tempdir().unwrap();
421429
let (_repo_tmp, repo_path) = create_test_repo_on_branch("feature-a");
422430

423-
with_comment_cache_dir(cache_tmp.path(), || {
431+
with_cache_dirs(cache_tmp.path(), None, || {
424432
let comment = make_comment("c1", "code", "Branch comment");
425433
save_comment_cached(repo_path.clone(), comment.clone()).unwrap();
426434

@@ -436,7 +444,7 @@ fn cached_comments_isolated_by_branch() {
436444
let cache_tmp = tempfile::tempdir().unwrap();
437445
let (_repo_tmp, repo_path) = create_test_repo_on_branch("branch-a");
438446

439-
with_comment_cache_dir(cache_tmp.path(), || {
447+
with_cache_dirs(cache_tmp.path(), None, || {
440448
// Save on branch-a
441449
save_comment_cached(
442450
repo_path.clone(),
@@ -480,7 +488,7 @@ fn cached_comments_persist_across_reload() {
480488
let cache_tmp = tempfile::tempdir().unwrap();
481489
let (_repo_tmp, repo_path) = create_test_repo_on_branch("persist-test");
482490

483-
with_comment_cache_dir(cache_tmp.path(), || {
491+
with_cache_dirs(cache_tmp.path(), None, || {
484492
save_comment_cached(
485493
repo_path.clone(),
486494
make_comment("c1", "code", "Persisted"),
@@ -494,7 +502,7 @@ fn cached_comments_persist_across_reload() {
494502
});
495503

496504
// Simulate app restart: re-enter the cache dir context
497-
with_comment_cache_dir(cache_tmp.path(), || {
505+
with_cache_dirs(cache_tmp.path(), None, || {
498506
let loaded = load_comments_cached(repo_path.clone()).unwrap();
499507
assert_eq!(loaded.len(), 2);
500508
assert_eq!(loaded[0].text, "Persisted");
@@ -507,7 +515,7 @@ fn cached_delete_removes_specific_comment() {
507515
let cache_tmp = tempfile::tempdir().unwrap();
508516
let (_repo_tmp, repo_path) = create_test_repo_on_branch("delete-test");
509517

510-
with_comment_cache_dir(cache_tmp.path(), || {
518+
with_cache_dirs(cache_tmp.path(), None, || {
511519
save_comment_cached(repo_path.clone(), make_comment("c1", "code", "Keep")).unwrap();
512520
save_comment_cached(repo_path.clone(), make_comment("c2", "code", "Delete me")).unwrap();
513521
save_comment_cached(repo_path.clone(), make_comment("c3", "code", "Keep too")).unwrap();
@@ -526,7 +534,7 @@ fn cached_delete_nonexistent_is_noop() {
526534
let cache_tmp = tempfile::tempdir().unwrap();
527535
let (_repo_tmp, repo_path) = create_test_repo_on_branch("delete-noop");
528536

529-
with_comment_cache_dir(cache_tmp.path(), || {
537+
with_cache_dirs(cache_tmp.path(), None, || {
530538
save_comment_cached(repo_path.clone(), make_comment("c1", "code", "Only one")).unwrap();
531539
delete_comment_cached(repo_path.clone(), "nonexistent".to_string()).unwrap();
532540

@@ -543,7 +551,7 @@ fn cached_worktree_shares_comments_with_main_repo() {
543551
let wt_parent = tempfile::tempdir().unwrap();
544552
let wt_dir = wt_parent.path().join("wt-checkout");
545553

546-
with_comment_cache_dir(cache_tmp.path(), || {
554+
with_cache_dirs(cache_tmp.path(), None, || {
547555
// Create a worktree on a new branch (don't force — "main" is HEAD)
548556
let repo = Repository::discover(&repo_path).unwrap();
549557
let head_commit = repo.head().unwrap().peel_to_commit().unwrap();
@@ -585,7 +593,7 @@ fn cached_multiple_comments_preserves_order() {
585593
let cache_tmp = tempfile::tempdir().unwrap();
586594
let (_repo_tmp, repo_path) = create_test_repo_on_branch("order-test");
587595

588-
with_comment_cache_dir(cache_tmp.path(), || {
596+
with_cache_dirs(cache_tmp.path(), None, || {
589597
save_comment_cached(repo_path.clone(), make_comment("c1", "code", "First")).unwrap();
590598
save_comment_cached(repo_path.clone(), make_comment("c2", "file", "Second")).unwrap();
591599
save_comment_cached(repo_path.clone(), make_comment("c3", "group", "Third")).unwrap();
@@ -603,7 +611,7 @@ fn cached_load_from_empty_cache_returns_empty() {
603611
let cache_tmp = tempfile::tempdir().unwrap();
604612
let (_repo_tmp, repo_path) = create_test_repo_on_branch("empty-test");
605613

606-
with_comment_cache_dir(cache_tmp.path(), || {
614+
with_cache_dirs(cache_tmp.path(), None, || {
607615
let loaded = load_comments_cached(repo_path.clone()).unwrap();
608616
assert!(loaded.is_empty());
609617
});
@@ -614,7 +622,7 @@ fn cached_all_comment_types_roundtrip() {
614622
let cache_tmp = tempfile::tempdir().unwrap();
615623
let (_repo_tmp, repo_path) = create_test_repo_on_branch("types-test");
616624

617-
with_comment_cache_dir(cache_tmp.path(), || {
625+
with_cache_dirs(cache_tmp.path(), None, || {
618626
let code_comment = make_comment("c1", "code", "Code comment");
619627
let mut file_comment = make_comment("c2", "file", "File comment");
620628
file_comment.start_line = None;
@@ -666,7 +674,7 @@ fn refinement_cache_branch_key_stores_and_loads() {
666674
let cache_tmp = tempfile::tempdir().unwrap();
667675
let (_repo_tmp, repo_path) = create_test_repo_on_branch("feature-x");
668676

669-
with_refinement_cache_dir(cache_tmp.path(), || {
677+
with_cache_dirs(cache_tmp.path(), Some(cache_tmp.path()), || {
670678
let branch_key = comment_cache_key(&repo_path).unwrap();
671679
let refine_key = format!("branch_{}", branch_key);
672680

@@ -685,7 +693,7 @@ fn refinement_cache_different_branches_isolated() {
685693
let cache_tmp = tempfile::tempdir().unwrap();
686694
let (_repo_tmp, repo_path) = create_test_repo_on_branch("branch-a");
687695

688-
with_refinement_cache_dir(cache_tmp.path(), || {
696+
with_cache_dirs(cache_tmp.path(), Some(cache_tmp.path()), || {
689697
// Store on branch-a
690698
let key_a = comment_cache_key(&repo_path).unwrap();
691699
cache::store_cached_refinement(&format!("branch_{}", key_a), sample_refinement_json());
@@ -723,7 +731,7 @@ fn refinement_cache_worktree_sees_same_branch_cache() {
723731
let wt_parent = tempfile::tempdir().unwrap();
724732
let wt_dir = wt_parent.path().join("wt-refine-test");
725733

726-
with_refinement_cache_dir(cache_tmp.path(), || {
734+
with_cache_dirs(cache_tmp.path(), Some(cache_tmp.path()), || {
727735
// Create a worktree on branch "wt-refine"
728736
let repo = Repository::discover(&repo_path).unwrap();
729737
let head = repo.head().unwrap().peel_to_commit().unwrap();
@@ -765,7 +773,7 @@ fn refinement_cache_overwrite_on_same_branch() {
765773
let cache_tmp = tempfile::tempdir().unwrap();
766774
let (_repo_tmp, repo_path) = create_test_repo_on_branch("overwrite-test");
767775

768-
with_refinement_cache_dir(cache_tmp.path(), || {
776+
with_cache_dirs(cache_tmp.path(), Some(cache_tmp.path()), || {
769777
let key = comment_cache_key(&repo_path).unwrap();
770778
let refine_key = format!("branch_{}", key);
771779

@@ -786,7 +794,7 @@ fn refinement_cache_diff_key_and_branch_key_both_stored() {
786794
let cache_tmp = tempfile::tempdir().unwrap();
787795
let (_repo_tmp, repo_path) = create_test_repo_on_branch("dual-key");
788796

789-
with_refinement_cache_dir(cache_tmp.path(), || {
797+
with_cache_dirs(cache_tmp.path(), Some(cache_tmp.path()), || {
790798
let diff_key = "fake_diff_hash_abc123";
791799
let branch_key = comment_cache_key(&repo_path).unwrap();
792800
let branch_refine_key = format!("branch_{}", branch_key);
@@ -806,7 +814,7 @@ fn refinement_cache_fallback_to_branch_when_diff_key_missing() {
806814
let cache_tmp = tempfile::tempdir().unwrap();
807815
let (_repo_tmp, repo_path) = create_test_repo_on_branch("fallback-test");
808816

809-
with_refinement_cache_dir(cache_tmp.path(), || {
817+
with_cache_dirs(cache_tmp.path(), Some(cache_tmp.path()), || {
810818
let branch_key = comment_cache_key(&repo_path).unwrap();
811819
let branch_refine_key = format!("branch_{}", branch_key);
812820

0 commit comments

Comments
 (0)