Skip to content

Commit 41e9bf1

Browse files
committed
feat: UpdatePathsError into Error conversion
1 parent 79f2d0e commit 41e9bf1

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

notify/src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ impl StdError for UpdatePathsError {
181181
}
182182
}
183183

184+
impl From<UpdatePathsError> for Error {
185+
fn from(value: UpdatePathsError) -> Self {
186+
value.source
187+
}
188+
}
189+
184190
#[cfg(test)]
185191
mod tests {
186192
use super::*;

notify/src/lib.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,4 +668,52 @@ mod tests {
668668
}
669669
panic!("Did not receive the event of {b_file2:?}");
670670
}
671+
672+
#[test]
673+
fn update_paths_in_a_loop_with_errors() -> StdResult<(), Box<dyn std::error::Error>> {
674+
let dir = tempdir()?;
675+
let existing_dir_1 = dir.path().join("existing_dir_1");
676+
let not_existent_dir = dir.path().join("noт_existent_dir");
677+
let existing_dir_2 = dir.path().join("existing_dir_2");
678+
679+
fs::create_dir(&existing_dir_1)?;
680+
fs::create_dir(&existing_dir_2)?;
681+
682+
let mut paths_to_add = vec![
683+
PathOp::watch_recursive(existing_dir_1.clone()),
684+
PathOp::watch_recursive(not_existent_dir.clone()),
685+
PathOp::watch_recursive(existing_dir_2.clone()),
686+
];
687+
688+
let (tx, rx) = std::sync::mpsc::channel();
689+
let mut watcher = RecommendedWatcher::new(tx, Config::default())?;
690+
691+
while !paths_to_add.is_empty() {
692+
if let Err(e) = watcher.update_paths(std::mem::take(&mut paths_to_add)) {
693+
paths_to_add = e.remaining;
694+
}
695+
}
696+
697+
fs::create_dir(existing_dir_1.join("1"))?;
698+
fs::create_dir(&not_existent_dir)?;
699+
let waiting_path = existing_dir_2.join("1");
700+
fs::create_dir(&waiting_path)?;
701+
702+
for event in iter_with_timeout(&rx) {
703+
let path = event
704+
.paths
705+
.get(0)
706+
.unwrap_or_else(|| panic!("event must have a path: {event:?}"));
707+
assert!(
708+
path != &not_existent_dir,
709+
"unexpeced {:?} event",
710+
not_existent_dir
711+
);
712+
if path == &waiting_path {
713+
return Ok(());
714+
}
715+
}
716+
717+
panic!("Did not receive the event of {waiting_path:?}");
718+
}
671719
}

0 commit comments

Comments
 (0)