Skip to content

Commit 4442940

Browse files
committed
Tests: Make index_mtime_change_invalidates_cache deterministic
`index_mtime_change_invalidates_cache` used to call `git add` and then `sleep(1100 ms)` so the FS-resolution lottery didn't squash the mtime change on overlayfs (CI's second-resolution timestamps). Worked, but spent ~1.3 s of pure wall-clock per run and the test was the slowest in the git module. - Replaced the sleep with an explicit `filetime::set_file_mtime` bump on `.git/index` by +2 s after the `git add`. `list_status`'s cache check keys off that exact mtime, so the test exercises the same code path with no FS-quirks dependency. - Now runs in 0.117 s (was ~1.3 s).
1 parent 9e23ff2 commit 4442940

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

  • apps/desktop/src-tauri/src/file_system/git

apps/desktop/src-tauri/src/file_system/git/status.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,14 +464,22 @@ mod cache_tests {
464464
"fresh repo had no untracked new.txt"
465465
);
466466

467-
// Stage a new file. `git add` rewrites `.git/index`, bumping the mtime.
467+
// Stage a new file. `git add` rewrites `.git/index`, but on filesystems
468+
// with second-resolution timestamps (CI overlayfs is the canary), the
469+
// new mtime may equal the pre-add mtime and the cache stays warm.
470+
// Instead of sleeping a full second to wait the FS out (the previous
471+
// shape did `sleep(1100ms)`, ~1.3 s of pure wall-clock per run),
472+
// forcibly bump the index mtime via `filetime`. This is the exact
473+
// invariant `list_status`'s cache check keys off, so we're testing the
474+
// same code path — minus the FS-resolution lottery.
468475
std::fs::write(dir.join("new.txt"), "x\n").unwrap();
469476
run(&dir, &["add", "new.txt"]);
470477

471-
// Sleep one filesystem tick so the mtime is guaranteed to change on
472-
// filesystems with second-resolution timestamps. macOS APFS has
473-
// sub-second resolution but CI's overlayfs sometimes doesn't.
474-
std::thread::sleep(std::time::Duration::from_millis(1100));
478+
let index_path = root.join(".git").join("index");
479+
let cur_mtime = std::fs::metadata(&index_path).unwrap().modified().unwrap();
480+
let bumped = cur_mtime + std::time::Duration::from_secs(2);
481+
filetime::set_file_mtime(&index_path, filetime::FileTime::from_system_time(bumped))
482+
.expect("bump .git/index mtime");
475483

476484
let second = list_status(&handle, &dir).unwrap();
477485
let entries_second: Vec<&str> = second.iter().map(|e| e.relative_path.as_str()).collect();

0 commit comments

Comments
 (0)