Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
test: remove explicit timeout param from wait_for_* functions (#454)
Maximilian Roose7a4da4f021a
diff --git a/tests/CLAUDE.md b/tests/CLAUDE.mdindex ea3d99361..fb0ec790f 100644--- a/tests/CLAUDE.md+++ b/tests/CLAUDE.md@@ -84,16 +84,19 @@ let timeout = Duration::from_millis(100);Use the helpers in `tests/common/mod.rs`:```rust-use crate::common::{wait_for_file, wait_for_file_count};+use crate::common::{wait_for_file, wait_for_file_count, wait_for_file_content};-// ✅ Poll for file existence with 5+ second timeout-wait_for_file(&log_file, Duration::from_secs(5));+// ✅ Poll for file existence (15-second default timeout)+wait_for_file(&log_file);// ✅ Poll for multiple files-wait_for_file_count(&log_dir, "log", 3, Duration::from_secs(5));+wait_for_file_count(&log_dir, "log", 3);++// ✅ Poll for file with non-empty content+wait_for_file_content(&marker_file);```-These use exponential backoff (10ms → 500ms cap) for fast initial checks that back off on slow CI.+These use exponential backoff (10ms → 500ms cap) for fast initial checks that back off on slow CI. The 15-second default timeout is generous enough to avoid flakiness under CI load.**Exception - testing absence:** When verifying something did NOT happen, polling doesn't work. Use a fixed 500ms+ sleep:diff --git a/tests/common/mod.rs b/tests/common/mod.rsindex 9f6a1e59e..def46cc35 100644--- a/tests/common/mod.rs+++ b/tests/common/mod.rs@@ -461,6 +461,10 @@ pub const WEEK: i64 = 7 * DAY;/// Use this when creating test data with timestamps (cache entries, etc.).pub const TEST_EPOCH: u64 = 1735776000;+/// Default timeout for background hook/command completion.+/// Generous to avoid flakiness under CI load; exponential backoff means fast tests when things work.+const BG_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(15);+/// Null device path, platform-appropriate./// Use this for GIT_CONFIG_SYSTEM to disable system config in tests.#[cfg(windows)]@@ -2376,10 +2380,10 @@ fn exponential_sleep(attempt: u32) {/// Wait for a file to exist, polling with exponential backoff./// Use this instead of fixed sleeps for background commands to avoid flaky tests.-pub fn wait_for_file(path: &Path, timeout: std::time::Duration) {+pub fn wait_for_file(path: &Path) {let start = std::time::Instant::now();let mut attempt = 0;- while start.elapsed() < timeout {+ while start.elapsed() < BG_TIMEOUT {if path.exists() {return;}@@ -2388,21 +2392,16 @@ pub fn wait_for_file(path: &Path, timeout: std::time::Duration) {}panic!("File was not created within {:?}: {}",- timeout,+ BG_TIMEOUT,path.display());}/// Wait for a directory to contain at least `expected_count` files with a given extension.-pub fn wait_for_file_count(- dir: &Path,- extension: &str,- expected_count: usize,- timeout: std::time::Duration,-) {+pub fn wait_for_file_count(dir: &Path, extension: &str, expected_count: usize) {let start = std::time::Instant::now();let mut attempt = 0;- while start.elapsed() < timeout {+ while start.elapsed() < BG_TIMEOUT {if let Ok(entries) = std::fs::read_dir(dir) {let count = entries.filter_map(|e| e.ok())@@ -2417,16 +2416,16 @@ pub fn wait_for_file_count(}panic!("Expected {} .{} files in {:?} within {:?}",- expected_count, extension, dir, timeout+ expected_count, extension, dir, BG_TIMEOUT);}/// Wait for a file to have non-empty content, polling with exponential backoff./// Use when a background process creates a file but may not have finished writing.-pub fn wait_for_file_content(path: &Path, timeout: std::time::Duration) {+pub fn wait_for_file_content(path: &Path) {let start = std::time::Instant::now();let mut attempt = 0;- while start.elapsed() < timeout {+ while start.elapsed() < BG_TIMEOUT {if std::fs::metadata(path).is_ok_and(|m| m.len() > 0) {return;}@@ -2435,17 +2434,17 @@ pub fn wait_for_file_content(path: &Path, timeout: std::time::Duration) {}panic!("File remained empty within {:?}: {}",- timeout,+ BG_TIMEOUT,path.display());}/// Wait for a file to have at least `expected_lines` lines, polling with exponential backoff./// Use when a background process writes multiple lines sequentially.-pub fn wait_for_file_lines(path: &Path, expected_lines: usize, timeout: std::time::Duration) {+pub fn wait_for_file_lines(path: &Path, expected_lines: usize) {let start = std::time::Instant::now();let mut attempt = 0;- while start.elapsed() < timeout {+ while start.elapsed() < BG_TIMEOUT {if let Ok(content) = std::fs::read_to_string(path) {let line_count = content.lines().count();if line_count >= expected_lines {@@ -2461,7 +2460,7 @@ pub fn wait_for_file_lines(path: &Path, expected_lines: usize, timeout: std::timpanic!("File did not reach {} lines within {:?} (got {}): {}",expected_lines,- timeout,+ BG_TIMEOUT,actual,path.display());@@ -2469,11 +2468,11 @@ pub fn wait_for_file_lines(path: &Path, expected_lines: usize, timeout: std::tim/// Wait for a file to contain valid JSON, polling with exponential backoff./// Use when a background process writes JSON that may be partially written.-pub fn wait_for_valid_json(path: &Path, timeout: std::time::Duration) -> serde_json::Value {+pub fn wait_for_valid_json(path: &Path) -> serde_json::Value {let start = std::time::Instant::now();let mut attempt = 0;let mut last_error = String::new();- while start.elapsed() < timeout {+ while start.elapsed() < BG_TIMEOUT {if let Ok(content) = std::fs::read_to_string(path) {match serde_json::from_str(&content) {Ok(json) => return json,@@ -2485,7 +2484,7 @@ pub fn wait_for_valid_json(path: &Path, timeout: std::time::Duration) -> serde_j}panic!("File did not contain valid JSON within {:?}: {}\nLast error: {}",- timeout,+ BG_TIMEOUT,path.display(),last_error);diff --git a/tests/integration_tests/bare_repository.rs b/tests/integration_tests/bare_repository.rsindex f30b98769..3ad6aa7ff 100644--- a/tests/integration_tests/bare_repository.rs+++ b/tests/integration_tests/bare_repository.rs@@ -7,7 +7,6 @@ use rstest::rstest;use std::fs;use std::path::{Path, PathBuf};use std::process::Command;-use std::time::Duration;#[test]fn test_bare_repo_list_worktrees() {@@ -408,7 +407,7 @@ fn test_bare_repo_background_logs_location() {// Wait for background process to create log file (poll instead of fixed sleep)// The key test is that the path is correct, not that content was written (background processes are flaky in tests)let log_path = test.bare_repo_path().join("wt-logs/feature-remove.log");- wait_for_file(&log_path, Duration::from_secs(5));+ wait_for_file(&log_path);// Verify it's NOT in the worktree's .git directory (which doesn't exist for linked worktrees)let wrong_path = main_worktree.join(".git/wt-logs/feature-remove.log");diff --git a/tests/integration_tests/e2e_shell_post_start.rs b/tests/integration_tests/e2e_shell_post_start.rsindex 1adad0a54..cf65f8a21 100644--- a/tests/integration_tests/e2e_shell_post_start.rs+++ b/tests/integration_tests/e2e_shell_post_start.rs@@ -8,7 +8,6 @@ use crate::common::{};use rstest::rstest;use std::fs;-use std::time::Duration;/// Test that post-start background commands work with shell integration#[rstest]@@ -91,7 +90,7 @@ approved-commands = ["sleep 0.05 && echo 'Background task done' > bg_marker.txt"// Wait for background command to complete AND flush content (allow plenty of margin on CI)let marker_file = worktree_path.join("bg_marker.txt");- wait_for_file_content(marker_file.as_path(), Duration::from_secs(2));+ wait_for_file_content(marker_file.as_path());let content = fs::read_to_string(&marker_file).unwrap();assert!(@@ -159,14 +158,8 @@ approved-commands = [.unwrap().join("repo.parallel-test");- wait_for_file(- worktree_path.join("task1.txt").as_path(),- Duration::from_secs(2),- );- wait_for_file(- worktree_path.join("task2.txt").as_path(),- Duration::from_secs(2),- );+ wait_for_file(worktree_path.join("task1.txt").as_path());+ wait_for_file(worktree_path.join("task2.txt").as_path());}/// Test that post-create commands block before shell returns@@ -287,7 +280,7 @@ approved-commands = ["sleep 0.05 && echo 'Fish background done' > fish_bg.txt"]// Wait for background command AND flush content (allow plenty of margin on CI)let worktree_path = repo.root_path().parent().unwrap().join("repo.fish-bg-test");let marker_file = worktree_path.join("fish_bg.txt");- wait_for_file_content(marker_file.as_path(), Duration::from_secs(2));+ wait_for_file_content(marker_file.as_path());let content = fs::read_to_string(&marker_file).unwrap();assert!(diff --git a/tests/integration_tests/post_start_commands.rs b/tests/integration_tests/post_start_commands.rsindex d97b715b4..25028e76f 100644--- a/tests/integration_tests/post_start_commands.rs+++ b/tests/integration_tests/post_start_commands.rs@@ -8,7 +8,7 @@ use insta_cmd::assert_cmd_snapshot;use rstest::rstest;use std::fs;use std::thread;-use std::time::Duration;+use std::time::Duration; // Used for SLEEP_FOR_ABSENCE_CHECKuse tempfile::TempDir;/// Wait duration when checking file absence (testing command did NOT run).@@ -554,7 +554,7 @@ approved-commands = ["cat > context.json"]// Find the worktree and wait for valid JSON (polls until cat finishes writing)let worktree_path = repo.root_path().parent().unwrap().join("repo.bg-json");let json_file = worktree_path.join("context.json");- let json = wait_for_valid_json(&json_file, Duration::from_secs(5));+ let json = wait_for_valid_json(&json_file);assert_eq!(json["branch"].as_str(),@@ -607,7 +607,7 @@ approved-commands = ["sleep 0.1 && echo 'Background task done' > background.txt"// Wait for the background command to completelet output_file = worktree_path.join("background.txt");- wait_for_file(output_file.as_path(), Duration::from_secs(5));+ wait_for_file(output_file.as_path());}#[rstest]@@ -641,14 +641,8 @@ approved-commands = [// Wait for both background commandslet worktree_path = repo.root_path().parent().unwrap().join("repo.feature");- wait_for_file(- worktree_path.join("task1.txt").as_path(),- Duration::from_secs(5),- );- wait_for_file(- worktree_path.join("task2.txt").as_path(),- Duration::from_secs(5),- );+ wait_for_file(worktree_path.join("task1.txt").as_path());+ wait_for_file(worktree_path.join("task2.txt").as_path());}#[rstest]@@ -684,11 +678,8 @@ approved-commands = ["Post-create command should have completed before wt exits");- // Wait for background command with generous timeout for slow CI systems- wait_for_file(- worktree_path.join("server.txt").as_path(),- Duration::from_secs(5),- );+ // Wait for background command+ wait_for_file(worktree_path.join("server.txt").as_path());}#[rstest]@@ -730,7 +721,7 @@ approved-commands = ["echo 'stdout output' && echo 'stderr output' >&2"]let worktree_path = repo.root_path().parent().unwrap().join("repo.feature");let git_common_dir = resolve_git_common_dir(&worktree_path);let log_dir = git_common_dir.join("wt-logs");- wait_for_file_count(&log_dir, "log", 1, Duration::from_secs(5));+ wait_for_file_count(&log_dir, "log", 1);// Find the log filelet log_files: Vec<_> = fs::read_dir(&log_dir)@@ -749,7 +740,7 @@ approved-commands = ["echo 'stdout output' && echo 'stderr output' >&2"]// Wait for content to be written (background command might still be writing)let log_file = &log_files[0];- wait_for_file_content(log_file, Duration::from_secs(5));+ wait_for_file_content(log_file);let log_contents = fs::read_to_string(log_file).unwrap();@@ -819,7 +810,7 @@ approved-commands = [let worktree_path = repo.root_path().parent().unwrap().join("repo.feature");let git_common_dir = resolve_git_common_dir(&worktree_path);let log_dir = git_common_dir.join("wt-logs");- wait_for_file_count(&log_dir, "log", 3, Duration::from_secs(5));+ wait_for_file_count(&log_dir, "log", 3);let log_files: Vec<_> = fs::read_dir(&log_dir).unwrap()@@ -829,7 +820,7 @@ approved-commands = [// Wait for content to be flushed in each log file before readingfor entry in &log_files {- wait_for_file_content(&entry.path(), Duration::from_secs(5));+ wait_for_file_content(&entry.path());}// Read all log files and verify no cross-contamination@@ -903,10 +894,7 @@ approved-commands = ["echo 'Background task' > background.txt"]);// Wait for background command to complete- wait_for_file(- worktree_path.join("background.txt").as_path(),- Duration::from_secs(5),- );+ wait_for_file(worktree_path.join("background.txt").as_path());}#[rstest]@@ -930,7 +918,7 @@ approved-commands = ["echo 'line1\nline2\nline3' | grep line2 > filtered.txt"]// Wait for background command to create the file AND flush contentlet worktree_path = repo.root_path().parent().unwrap().join("repo.feature");let filtered_file = worktree_path.join("filtered.txt");- wait_for_file_content(filtered_file.as_path(), Duration::from_secs(5));+ wait_for_file_content(filtered_file.as_path());let contents = fs::read_to_string(&filtered_file).unwrap();assert_snapshot!(contents, @"line2");@@ -974,7 +962,7 @@ approved-commands = ["""// Wait for background command to write all 3 lines (not just the first)let worktree_path = repo.root_path().parent().unwrap().join("repo.feature");let output_file = worktree_path.join("multiline.txt");- wait_for_file_lines(output_file.as_path(), 3, Duration::from_secs(5));+ wait_for_file_lines(output_file.as_path(), 3);let contents = fs::read_to_string(&output_file).unwrap();assert_snapshot!(contents, @"@@ -1067,7 +1055,7 @@ approved-commands = ["echo 'POST-START-RAN' > post_start_marker.txt"]// Wait for background post-start command to completelet worktree_path = repo.root_path().parent().unwrap().join("repo.feature");let marker_file = worktree_path.join("post_start_marker.txt");- wait_for_file(marker_file.as_path(), Duration::from_secs(5));+ wait_for_file(marker_file.as_path());// Remove the marker file to detect if post-start runs againfs::remove_file(&marker_file).unwrap();diff --git a/tests/integration_tests/remove.rs b/tests/integration_tests/remove.rsindex 56acbeae2..313c5e559 100644--- a/tests/integration_tests/remove.rs+++ b/tests/integration_tests/remove.rs@@ -5,7 +5,7 @@ use crate::common::{};use insta_cmd::assert_cmd_snapshot;use rstest::rstest;-use std::time::Duration;+use std::time::Duration; // For absence checks (SLEEP_FOR_ABSENCE_CHECK pattern)#[rstest]fn test_remove_already_on_default(repo: TestRepo) {@@ -1066,7 +1066,7 @@ approved-commands = ["echo 'hook ran' > {}"].unwrap();// Wait for the hook to create the marker file- wait_for_file(&marker_file, Duration::from_secs(5));+ wait_for_file(&marker_file);// Marker file SHOULD exist - pre-remove hooks run before background removal startsassert!(diff --git a/tests/integration_tests/user_hooks.rs b/tests/integration_tests/user_hooks.rsindex d67c33b9b..accca3ad7 100644--- a/tests/integration_tests/user_hooks.rs+++ b/tests/integration_tests/user_hooks.rs@@ -16,6 +16,8 @@ use std::fs;use std::thread;use std::time::Duration;+// Note: Duration is still imported for SLEEP_FOR_ABSENCE_CHECK (testing command did NOT run)+/// Wait duration when checking file absence (testing command did NOT run).const SLEEP_FOR_ABSENCE_CHECK: Duration = Duration::from_millis(500);@@ -188,7 +190,7 @@ bg = "echo 'USER_POST_START_RAN' > user_bg_marker.txt"// Wait for background hook to complete and write contentlet worktree_path = repo.root_path().parent().unwrap().join("repo.feature");let marker_file = worktree_path.join("user_bg_marker.txt");- wait_for_file_content(&marker_file, Duration::from_secs(5));+ wait_for_file_content(&marker_file);let contents = fs::read_to_string(&marker_file).unwrap();assert!(@@ -346,7 +348,7 @@ long = "sh -c 'echo start >> hook.log; sleep 30; echo done >> hook.log'"// Wait until hook writes "start" to hook.log (verifies the hook is running)let hook_log = repo.root_path().join("hook.log");- wait_for_file_content(&hook_log, Duration::from_secs(5));+ wait_for_file_content(&hook_log);// Send SIGINT to wt's process group (wt's PID == its PGID since it's the leader)// This simulates real Ctrl-C which sends SIGINT to the foreground process group@@ -407,7 +409,7 @@ long = "sh -c 'echo start >> hook.log; sleep 30; echo done >> hook.log'"// Wait until hook writes "start" to hook.log (verifies the hook is running)let hook_log = repo.root_path().join("hook.log");- wait_for_file_content(&hook_log, Duration::from_secs(5));+ wait_for_file_content(&hook_log);// Send SIGTERM to wt's process group (wt's PID == its PGID since it's the leader)let wt_pgid = Pid::from_raw(child.id() as i32);@@ -689,11 +691,8 @@ approved-commands = ["echo 'PROJECT_POST_START' > project_bg.txt"]let worktree_path = repo.root_path().parent().unwrap().join("repo.feature");// Wait for both background commands- wait_for_file(&worktree_path.join("user_bg.txt"), Duration::from_secs(5));- wait_for_file(- &worktree_path.join("project_bg.txt"),- Duration::from_secs(5),- );+ wait_for_file(&worktree_path.join("user_bg.txt"));+ wait_for_file(&worktree_path.join("project_bg.txt"));// Both should have runassert!(@@ -750,7 +749,7 @@ fn test_standalone_hook_post_start(repo: TestRepo) {// Hook spawns in background - wait for marker filelet marker = repo.root_path().join("hook_ran.txt");- wait_for_file_content(&marker, Duration::from_secs(5));+ wait_for_file_content(&marker);let content = fs::read_to_string(&marker).unwrap();assert!(content.contains("STANDALONE_POST_START"));}@@ -864,7 +863,7 @@ fn test_concurrent_hook_single_failure(repo: TestRepo) {// Wait for log file to be created and contain outputlet log_dir = resolve_git_common_dir(repo.root_path()).join("wt-logs");- wait_for_file_count(&log_dir, "log", 1, Duration::from_secs(5));+ wait_for_file_count(&log_dir, "log", 1);// Find and read the log filelet log_file = fs::read_dir(&log_dir)@@ -874,7 +873,7 @@ fn test_concurrent_hook_single_failure(repo: TestRepo) {.expect("Should have a log file");// Wait for content to be written (command runs async)- wait_for_file_content(&log_file.path(), Duration::from_secs(5));+ wait_for_file_content(&log_file.path());let log_content = fs::read_to_string(log_file.path()).unwrap();// Verify the hook actually ran and wrote output (not just that file was created)@@ -909,7 +908,7 @@ second = "echo SECOND_OUTPUT"// Wait for both log files to be createdlet log_dir = resolve_git_common_dir(repo.root_path()).join("wt-logs");- wait_for_file_count(&log_dir, "log", 2, Duration::from_secs(5));+ wait_for_file_count(&log_dir, "log", 2);// Collect log files and their contentslet log_files: Vec<_> = fs::read_dir(&log_dir)@@ -921,7 +920,7 @@ second = "echo SECOND_OUTPUT"// Wait for content in both log filesfor log_file in &log_files {- wait_for_file_content(&log_file.path(), Duration::from_secs(5));+ wait_for_file_content(&log_file.path());}// Collect all log contents@@ -978,8 +977,8 @@ user = "echo 'USER_HOOK' > user_hook_ran.txt"let user_marker = repo.root_path().join("user_hook_ran.txt");let project_marker = repo.root_path().join("project_hook_ran.txt");- wait_for_file_content(&user_marker, Duration::from_secs(5));- wait_for_file_content(&project_marker, Duration::from_secs(5));+ wait_for_file_content(&user_marker);+ wait_for_file_content(&project_marker);let user_content = fs::read_to_string(&user_marker).unwrap();let project_content = fs::read_to_string(&project_marker).unwrap();@@ -1006,7 +1005,7 @@ fn test_concurrent_hook_post_switch(repo: TestRepo) {// Hook spawns in background - wait for marker filelet marker = repo.root_path().join("hook_ran.txt");- wait_for_file_content(&marker, Duration::from_secs(5));+ wait_for_file_content(&marker);let content = fs::read_to_string(&marker).unwrap();assert!(content.contains("POST_SWITCH"));}@@ -1039,7 +1038,7 @@ second = "echo 'SECOND' > second.txt"let first_marker = repo.root_path().join("first.txt");let second_marker = repo.root_path().join("second.txt");- wait_for_file_content(&first_marker, Duration::from_secs(5));+ wait_for_file_content(&first_marker);// Fixed sleep for absence check - second hook should NOT have runthread::sleep(SLEEP_FOR_ABSENCE_CHECK);