Skip to content

Commit

Permalink
Add basic integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dfaust committed Oct 25, 2024
1 parent c601b48 commit 88973e1
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions notify-debouncer-full/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ rstest.workspace = true
serde.workspace = true
deser-hjson.workspace = true
rand.workspace = true
tempfile.workspace = true
23 changes: 23 additions & 0 deletions notify-debouncer-full/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ mod tests {

use pretty_assertions::assert_eq;
use rstest::rstest;
use tempfile::tempdir;
use testing::TestCase;
use time::MockTime;

Expand Down Expand Up @@ -886,4 +887,26 @@ mod tests {
);
}
}

#[test]
fn integration() -> Result<(), Box<dyn std::error::Error>> {
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(())
}
}
1 change: 1 addition & 0 deletions notify-debouncer-mini/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ notify.workspace = true
notify-types.workspace = true
crossbeam-channel = { workspace = true, optional = true }
log.workspace = true
tempfile.workspace = true
36 changes: 36 additions & 0 deletions notify-debouncer-mini/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,39 @@ pub fn new_debouncer<F: DebounceEventHandler>(
let config = Config::default().with_timeout(timeout);
new_debouncer_opt::<F, RecommendedWatcher>(config, event_handler)
}

#[cfg(test)]
mod tests {
use super::*;
use notify::RecursiveMode;
use std::fs;
use tempfile::tempdir;

#[test]
fn integration() -> Result<(), Box<dyn std::error::Error>> {
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(())
}
}
27 changes: 27 additions & 0 deletions notify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ where

#[cfg(test)]
mod tests {
use std::{fs, time::Duration};

use tempfile::tempdir;

use super::*;

#[test]
Expand All @@ -408,4 +412,27 @@ mod tests {
assert_debug_impl!(RecursiveMode);
assert_debug_impl!(WatcherKind);
}

#[test]
fn integration() -> std::result::Result<(), Box<dyn std::error::Error>> {
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(())
}
}

0 comments on commit 88973e1

Please sign in to comment.