Skip to content

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

fix(test): replace sleep with polling for racy git detection (#492)

Maximilian Roos505acc446f4d
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index 4be3a6e9b..8e7857e4d 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -2493,6 +2493,39 @@ pub fn wait_for_valid_json(path: &Path) -> serde_json::Value {
);
}
+/// Poll until a condition is met, with exponential backoff.
+///
+/// Use this instead of fixed sleeps for any condition that may take time to become true.
+/// Fast initial checks (10ms) catch quick completions; backs off to reduce CPU on slow CI.
+///
+/// # Arguments
+/// * `description` - Human-readable description for the panic message if timeout is reached
+/// * `check` - Closure that returns `true` when the condition is met
+///
+/// # Example
+/// ```ignore
+/// // Wait for git to detect file changes (handles "racy git" timing issues)
+/// wait_for("git to detect dirty working tree", || {
+/// repo.git_command()
+/// .args(["status", "--porcelain"])
+/// .output()
+/// .map(|o| !o.stdout.is_empty())
+/// .unwrap_or(false)
+/// });
+/// ```
+pub fn wait_for(description: &str, mut check: impl FnMut() -> bool) {
+ let start = std::time::Instant::now();
+ let mut attempt = 0;
+ while start.elapsed() < BG_TIMEOUT {
+ if check() {
+ return;
+ }
+ exponential_sleep(attempt);
+ attempt += 1;
+ }
+ panic!("Condition not met within {:?}: {}", BG_TIMEOUT, description);
+}
+
/// Convert Unix timestamp to ISO 8601 format for consistent git date handling
///
/// Git interprets `@timestamp` format inconsistently across versions and platforms.
diff --git a/tests/integration_tests/list.rs b/tests/integration_tests/list.rs
index e765cf2bc..258374f69 100644
--- a/tests/integration_tests/list.rs
+++ b/tests/integration_tests/list.rs
@@ -2197,31 +2197,23 @@ fn test_list_full_working_tree_conflicts(mut repo: TestRepo) {
// Now add uncommitted changes to feature that would conflict with main
std::fs::write(feature.join("shared.txt"), "feature's uncommitted version").unwrap();
- // Git's mtime-based change detection may need a moment to see the file change
- // due to lower timestamp resolution in CI environments (Windows, Linux tmpfs)
- std::thread::sleep(std::time::Duration::from_millis(100));
-
- // Force git to refresh its index to detect the file modification immediately
- // Without this, git's mtime-based caching can miss the change on fast filesystems (especially macOS/Windows)
- repo.git_command()
- .args(["update-index", "--refresh"])
- .current_dir(&feature)
- .output()
- .unwrap();
-
- // Verify git sees the change (especially important on Windows)
- let status_output = repo
- .git_command()
- .args(["status", "--porcelain"])
- .current_dir(&feature)
- .output()
- .unwrap();
- let status_str = String::from_utf8(status_output.stdout).unwrap();
- assert!(
- !status_str.trim().is_empty(),
- "Git should detect the modified file. Status output: {:?}",
- status_str
- );
+ // Wait for git to detect the file change (handles "racy git" timing issues where
+ // file modification within the same timestamp granularity may not be detected)
+ crate::common::wait_for("git to detect dirty working tree", || {
+ // Refresh index to pick up mtime changes
+ let _ = repo
+ .git_command()
+ .args(["update-index", "--refresh"])
+ .current_dir(&feature)
+ .output();
+ // Check if git sees the change
+ repo.git_command()
+ .args(["status", "--porcelain"])
+ .current_dir(&feature)
+ .output()
+ .map(|o| !o.stdout.is_empty())
+ .unwrap_or(false)
+ });
// Without --full: no conflict symbol (only checks commit-level)
assert_cmd_snapshot!(