Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions notify/src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,9 @@ mod tests {
let path = tmpdir.path().join("entry");
std::fs::File::create_new(&path).expect("Unable to create");

rx.sleep_until_parent_contains(&path);
rx.sleep_until_exists(&path);

rx.wait_ordered_exact([expected(&path).create_any()]);
}

Expand All @@ -713,7 +715,9 @@ mod tests {
let path = tmpdir.path().join("entry");
std::fs::create_dir(&path).expect("Unable to create");

rx.sleep_until_parent_contains(&path);
rx.sleep_until_exists(&path);

rx.wait_ordered_exact([expected(&path).create_any()]);
}

Expand All @@ -724,6 +728,8 @@ mod tests {
let path = tmpdir.path().join("entry");
std::fs::File::create_new(&path).expect("Unable to create");

rx.sleep_until_parent_contains(&path);

watcher.watch_recursively(&tmpdir);
std::fs::write(&path, b"123").expect("Unable to write");

Expand All @@ -741,10 +747,15 @@ mod tests {
let path = tmpdir.path().join("entry");
std::fs::File::create_new(&path).expect("Unable to create");

rx.sleep_until_parent_contains(&path);

watcher.watch_recursively(&tmpdir);

std::fs::remove_file(&path).expect("Unable to remove");

rx.sleep_while_exists(&path);
rx.sleep_while_parent_contains(&path);

rx.wait_ordered_exact([expected(&path).remove_any()]);
}

Expand All @@ -756,12 +767,18 @@ mod tests {
let new_path = tmpdir.path().join("new_entry");
std::fs::File::create_new(&path).expect("Unable to create");

rx.sleep_until_parent_contains(&path);

watcher.watch_recursively(&tmpdir);

std::fs::rename(&path, &new_path).expect("Unable to remove");

rx.sleep_while_exists(&path);
rx.sleep_until_exists(&new_path);

rx.sleep_while_parent_contains(&path);
rx.sleep_until_parent_contains(&new_path);

rx.wait_unordered_exact([
expected(&path).remove_any(),
expected(&new_path).create_any(),
Expand All @@ -776,13 +793,17 @@ mod tests {
let overwriting_file = tmpdir.path().join("overwriting_file");
std::fs::write(&overwritten_file, "123").expect("write1");

rx.sleep_until_parent_contains(&overwritten_file);

watcher.watch_nonrecursively(&tmpdir);

std::fs::File::create(&overwriting_file).expect("create");
std::fs::write(&overwriting_file, "321").expect("write2");
std::fs::rename(&overwriting_file, &overwritten_file).expect("rename");

rx.sleep_while_exists(&overwriting_file);
rx.sleep_while_parent_contains(&overwriting_file);

assert!(
rx.sleep_until(
|| std::fs::read_to_string(&overwritten_file).is_ok_and(|cnt| cnt == "321")
Expand Down
54 changes: 54 additions & 0 deletions notify/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,60 @@ impl Receiver {
self.timeout
)
}

/// Sleeps, while the parent directory of the provided path
/// does not contain the provided path in the [`std::fs::read_dir`] result.
///
/// Errors will be ignored
///
/// It is useful for the [`PollWatcher`], because on some file systems the directory
/// may contain a DirEntry after deletion
pub fn sleep_until_parent_contains(&self, path: impl AsRef<Path>) {
let path = path.as_ref();
let parent = path
.parent()
.expect("The path {path:?} does not have a parent");

assert!(
self.sleep_until(|| {
std::fs::read_dir(parent)
.into_iter()
.flatten()
.flatten()
.any(|r| r.path() == path)
}),
"the path {parent:?} has not contained an expected entry {:?} after timeout {:?}",
path.file_name(),
self.timeout
)
}

/// Sleeps, while the parent directory of the provided path
/// contains the provided path in the [`std::fs::read_dir`] result.
///
/// Errors will be ignored
///
/// It is useful for the [`PollWatcher`], because on some file systems the directory
/// may contain a DirEntry after deletion
pub fn sleep_while_parent_contains(&self, path: impl AsRef<Path>) {
let path = path.as_ref();
let parent = path
.parent()
.expect("The path {path:?} does not have a parent");

assert!(
self.sleep_until(|| {
!std::fs::read_dir(parent)
.into_iter()
.flatten()
.flatten()
.any(|r| r.path() == path)
}),
"the path {parent:?} has contained an expected entry {:?} yet after timeout {:?}",
path.file_name(),
self.timeout
)
}
}

/// Result of a `wait` call on a [`Receiver`]
Expand Down
Loading