From 88973e163b64ee23f2a4d5d87d4f6e64b84dffcd Mon Sep 17 00:00:00 2001 From: Daniel Faust Date: Wed, 7 Aug 2024 21:59:19 +0200 Subject: [PATCH] Add basic integration tests --- notify-debouncer-full/Cargo.toml | 1 + notify-debouncer-full/src/lib.rs | 23 ++++++++++++++++++++ notify-debouncer-mini/Cargo.toml | 1 + notify-debouncer-mini/src/lib.rs | 36 ++++++++++++++++++++++++++++++++ notify/src/lib.rs | 27 ++++++++++++++++++++++++ 5 files changed, 88 insertions(+) diff --git a/notify-debouncer-full/Cargo.toml b/notify-debouncer-full/Cargo.toml index 59a040b4..7d0e914f 100644 --- a/notify-debouncer-full/Cargo.toml +++ b/notify-debouncer-full/Cargo.toml @@ -34,3 +34,4 @@ rstest.workspace = true serde.workspace = true deser-hjson.workspace = true rand.workspace = true +tempfile.workspace = true diff --git a/notify-debouncer-full/src/lib.rs b/notify-debouncer-full/src/lib.rs index 3841eb99..a6624984 100644 --- a/notify-debouncer-full/src/lib.rs +++ b/notify-debouncer-full/src/lib.rs @@ -752,6 +752,7 @@ mod tests { use pretty_assertions::assert_eq; use rstest::rstest; + use tempfile::tempdir; use testing::TestCase; use time::MockTime; @@ -886,4 +887,26 @@ mod tests { ); } } + + #[test] + fn integration() -> Result<(), Box> { + let dir = tempdir()?; + + let (tx, rx) = std::sync::mpsc::channel(); + + let mut debouncer = new_debouncer(Duration::from_millis(10), None, tx)?; + + debouncer.watch(dir.path(), RecursiveMode::Recursive)?; + + fs::write(dir.path().join("file.txt"), b"Lorem ipsum")?; + + let events = rx + .recv_timeout(Duration::from_secs(10)) + .expect("no events received") + .expect("received an error"); + + assert!(!events.is_empty(), "received empty event list"); + + Ok(()) + } } diff --git a/notify-debouncer-mini/Cargo.toml b/notify-debouncer-mini/Cargo.toml index 37ec4723..e1f70788 100644 --- a/notify-debouncer-mini/Cargo.toml +++ b/notify-debouncer-mini/Cargo.toml @@ -25,3 +25,4 @@ notify.workspace = true notify-types.workspace = true crossbeam-channel = { workspace = true, optional = true } log.workspace = true +tempfile.workspace = true diff --git a/notify-debouncer-mini/src/lib.rs b/notify-debouncer-mini/src/lib.rs index 091d8291..2a165102 100644 --- a/notify-debouncer-mini/src/lib.rs +++ b/notify-debouncer-mini/src/lib.rs @@ -396,3 +396,39 @@ pub fn new_debouncer( let config = Config::default().with_timeout(timeout); new_debouncer_opt::(config, event_handler) } + +#[cfg(test)] +mod tests { + use super::*; + use notify::RecursiveMode; + use std::fs; + use tempfile::tempdir; + + #[test] + fn integration() -> Result<(), Box> { + let dir = tempdir()?; + + let (tx, rx) = std::sync::mpsc::channel(); + + let mut debouncer = new_debouncer(Duration::from_secs(1), tx)?; + + debouncer + .watcher() + .watch(dir.path(), RecursiveMode::Recursive)?; + + let file_path = dir.path().join("file.txt"); + fs::write(&file_path, b"Lorem ipsum")?; + + let events = rx + .recv_timeout(Duration::from_secs(10)) + .expect("no events received") + .expect("received an error"); + + assert_eq!( + events, + vec![DebouncedEvent::new(file_path, DebouncedEventKind::Any)] + ); + + Ok(()) + } +} diff --git a/notify/src/lib.rs b/notify/src/lib.rs index 30b3b50a..ef063730 100644 --- a/notify/src/lib.rs +++ b/notify/src/lib.rs @@ -383,6 +383,10 @@ where #[cfg(test)] mod tests { + use std::{fs, time::Duration}; + + use tempfile::tempdir; + use super::*; #[test] @@ -408,4 +412,27 @@ mod tests { assert_debug_impl!(RecursiveMode); assert_debug_impl!(WatcherKind); } + + #[test] + fn integration() -> std::result::Result<(), Box> { + let dir = tempdir()?; + + let (tx, rx) = std::sync::mpsc::channel(); + + let mut watcher = RecommendedWatcher::new(tx, Config::default())?; + + watcher.watch(dir.path(), RecursiveMode::Recursive)?; + + let file_path = dir.path().join("file.txt"); + fs::write(&file_path, b"Lorem ipsum")?; + + let event = rx + .recv_timeout(Duration::from_secs(10)) + .expect("no events received") + .expect("received an error"); + + assert_eq!(event.paths, vec![file_path]); + + Ok(()) + } }