Skip to content

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

feat: Add test helper to wait for file lines

Maximilian Roos72d7144d7705
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index 088249ac7..b9fa2cefc 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -925,6 +925,33 @@ pub fn wait_for_file_content(path: &Path, timeout: std::time::Duration) {
);
}
+/// 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) {
+ let start = std::time::Instant::now();
+ let mut attempt = 0;
+ while start.elapsed() < timeout {
+ if let Ok(content) = std::fs::read_to_string(path) {
+ let line_count = content.lines().count();
+ if line_count >= expected_lines {
+ return;
+ }
+ }
+ exponential_sleep(attempt);
+ attempt += 1;
+ }
+ let actual = std::fs::read_to_string(path)
+ .map(|c| c.lines().count())
+ .unwrap_or(0);
+ panic!(
+ "File did not reach {} lines within {:?} (got {}): {}",
+ expected_lines,
+ timeout,
+ actual,
+ path.display()
+ );
+}
+
#[cfg(test)]
mod tests {
use super::*;
diff --git a/tests/integration_tests/post_start_commands.rs b/tests/integration_tests/post_start_commands.rs
index 966e723b8..f0abbded5 100644
--- a/tests/integration_tests/post_start_commands.rs
+++ b/tests/integration_tests/post_start_commands.rs
@@ -1,6 +1,6 @@
use crate::common::{
TestRepo, make_snapshot_cmd, resolve_git_common_dir, setup_snapshot_settings, wait_for_file,
- wait_for_file_content, wait_for_file_count,
+ wait_for_file_content, wait_for_file_count, wait_for_file_lines,
};
use insta::assert_snapshot;
use insta_cmd::assert_cmd_snapshot;
@@ -668,10 +668,10 @@ approved-commands = ["""
&["--create", "feature"],
);
- // Wait for background command to create the file AND flush content
+ // Wait for background command to write all 3 lines (not just the first)
let worktree_path = repo.root_path().parent().unwrap().join("test-repo.feature");
let output_file = worktree_path.join("multiline.txt");
- wait_for_file_content(output_file.as_path(), Duration::from_secs(5));
+ wait_for_file_lines(output_file.as_path(), 3, Duration::from_secs(5));
let contents = fs::read_to_string(&output_file).unwrap();
assert_snapshot!(contents, @r"