Skip to content

Commit 88973e1

Browse files
committed
Add basic integration tests
1 parent c601b48 commit 88973e1

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

notify-debouncer-full/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ rstest.workspace = true
3434
serde.workspace = true
3535
deser-hjson.workspace = true
3636
rand.workspace = true
37+
tempfile.workspace = true

notify-debouncer-full/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ mod tests {
752752

753753
use pretty_assertions::assert_eq;
754754
use rstest::rstest;
755+
use tempfile::tempdir;
755756
use testing::TestCase;
756757
use time::MockTime;
757758

@@ -886,4 +887,26 @@ mod tests {
886887
);
887888
}
888889
}
890+
891+
#[test]
892+
fn integration() -> Result<(), Box<dyn std::error::Error>> {
893+
let dir = tempdir()?;
894+
895+
let (tx, rx) = std::sync::mpsc::channel();
896+
897+
let mut debouncer = new_debouncer(Duration::from_millis(10), None, tx)?;
898+
899+
debouncer.watch(dir.path(), RecursiveMode::Recursive)?;
900+
901+
fs::write(dir.path().join("file.txt"), b"Lorem ipsum")?;
902+
903+
let events = rx
904+
.recv_timeout(Duration::from_secs(10))
905+
.expect("no events received")
906+
.expect("received an error");
907+
908+
assert!(!events.is_empty(), "received empty event list");
909+
910+
Ok(())
911+
}
889912
}

notify-debouncer-mini/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ notify.workspace = true
2525
notify-types.workspace = true
2626
crossbeam-channel = { workspace = true, optional = true }
2727
log.workspace = true
28+
tempfile.workspace = true

notify-debouncer-mini/src/lib.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,39 @@ pub fn new_debouncer<F: DebounceEventHandler>(
396396
let config = Config::default().with_timeout(timeout);
397397
new_debouncer_opt::<F, RecommendedWatcher>(config, event_handler)
398398
}
399+
400+
#[cfg(test)]
401+
mod tests {
402+
use super::*;
403+
use notify::RecursiveMode;
404+
use std::fs;
405+
use tempfile::tempdir;
406+
407+
#[test]
408+
fn integration() -> Result<(), Box<dyn std::error::Error>> {
409+
let dir = tempdir()?;
410+
411+
let (tx, rx) = std::sync::mpsc::channel();
412+
413+
let mut debouncer = new_debouncer(Duration::from_secs(1), tx)?;
414+
415+
debouncer
416+
.watcher()
417+
.watch(dir.path(), RecursiveMode::Recursive)?;
418+
419+
let file_path = dir.path().join("file.txt");
420+
fs::write(&file_path, b"Lorem ipsum")?;
421+
422+
let events = rx
423+
.recv_timeout(Duration::from_secs(10))
424+
.expect("no events received")
425+
.expect("received an error");
426+
427+
assert_eq!(
428+
events,
429+
vec![DebouncedEvent::new(file_path, DebouncedEventKind::Any)]
430+
);
431+
432+
Ok(())
433+
}
434+
}

notify/src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@ where
383383

384384
#[cfg(test)]
385385
mod tests {
386+
use std::{fs, time::Duration};
387+
388+
use tempfile::tempdir;
389+
386390
use super::*;
387391

388392
#[test]
@@ -408,4 +412,27 @@ mod tests {
408412
assert_debug_impl!(RecursiveMode);
409413
assert_debug_impl!(WatcherKind);
410414
}
415+
416+
#[test]
417+
fn integration() -> std::result::Result<(), Box<dyn std::error::Error>> {
418+
let dir = tempdir()?;
419+
420+
let (tx, rx) = std::sync::mpsc::channel();
421+
422+
let mut watcher = RecommendedWatcher::new(tx, Config::default())?;
423+
424+
watcher.watch(dir.path(), RecursiveMode::Recursive)?;
425+
426+
let file_path = dir.path().join("file.txt");
427+
fs::write(&file_path, b"Lorem ipsum")?;
428+
429+
let event = rx
430+
.recv_timeout(Duration::from_secs(10))
431+
.expect("no events received")
432+
.expect("received an error");
433+
434+
assert_eq!(event.paths, vec![file_path]);
435+
436+
Ok(())
437+
}
411438
}

0 commit comments

Comments
 (0)