From 2e5f2947f39123849b3ad97a5398444d03c73652 Mon Sep 17 00:00:00 2001 From: Jimmy Lu Date: Sat, 11 Jul 2015 02:49:23 -0400 Subject: [PATCH 1/3] Single file support for inotify --- Cargo.toml | 4 ++++ src/inotify/mod.rs | 40 ++++++++++++++++++------------------- tests/notify.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 tests/notify.rs diff --git a/Cargo.toml b/Cargo.toml index 2644d57e..27807770 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,3 +40,7 @@ version = "^0.2.11" [target.x86_64-apple-darwin.dependencies.fsevent-sys] version = "^0.1" + +[dev-dependencies] +tempfile = "^1.1.0" +tempdir = "^0.3.4" diff --git a/src/inotify/mod.rs b/src/inotify/mod.rs index a347f0bc..3a8525e6 100644 --- a/src/inotify/mod.rs +++ b/src/inotify/mod.rs @@ -131,28 +131,28 @@ impl Watcher for INotifyWatcher { } fn watch(&mut self, path: &Path) -> Result<(), Error> { - match Walker::new(path) { - Ok(d) => { - for dir in d { - match dir { - Ok(entry) => { - let path = entry.path(); - let meta = match metadata(&path) { - Ok(m) => m, - Err(e) => return Err(Error::Io(e)), - }; - - if meta.is_dir() { + let is_dir = match metadata(&path) { + Ok(m) => m.is_dir(), + Err(e) => return Err(Error::Io(e)), + }; + if is_dir { + match Walker::new(path) { + Ok(dir) => { + for entry in dir { + match entry { + Ok(entry) => { + let path = entry.path(); try!(self.add_watch(&path)); - } - }, - Err(e) => return Err(Error::Io(e)), + }, + Err(e) => return Err(Error::Io(e)), + } } - } - - self.add_watch(path) - }, - Err(e) => Err(Error::Io(e)) + self.add_watch(path) + }, + Err(e) => Err(Error::Io(e)) + } + } else { + self.add_watch(&path) } } diff --git a/tests/notify.rs b/tests/notify.rs new file mode 100644 index 00000000..ff7a881a --- /dev/null +++ b/tests/notify.rs @@ -0,0 +1,49 @@ +extern crate notify; +extern crate tempdir; +extern crate tempfile; + +use notify::*; +use std::io::Write; +use std::path::Path; +use std::sync::mpsc::{channel, Receiver}; +use tempdir::TempDir; +use tempfile::NamedTempFile; + +fn validate_recv(rx: Receiver, evs: Vec<(&Path, Op)>) { + for expected in evs { + let actual = rx.recv().unwrap(); + assert_eq!(actual.path.unwrap().as_path(), expected.0); + assert_eq!(actual.op.unwrap(), expected.1); + } + assert!(rx.try_recv().is_err()); +} + +#[test] +fn watch_single_file() { + let mut file = NamedTempFile::new().unwrap(); + let (tx, rx) = channel(); + let mut w: RecommendedWatcher = Watcher::new(tx).unwrap(); + w.watch(file.path()).unwrap(); + file.write_all(b"foo").unwrap(); + file.flush().unwrap(); + validate_recv(rx, vec![(file.path(), op::WRITE)]); +} + +#[test] +fn watch_dir() { + let dir = TempDir::new("dir").unwrap(); + let dir1 = TempDir::new_in(dir.path(), "dir1").unwrap(); + let dir2 = TempDir::new_in(dir.path(), "dir2").unwrap(); + let dir11 = TempDir::new_in(dir1.path(), "dir11").unwrap(); + let (tx, rx) = channel(); + let mut w: RecommendedWatcher = Watcher::new(tx).unwrap(); + w.watch(dir.path()).unwrap(); + let f111 = NamedTempFile::new_in(dir11.path()).unwrap(); + let f111_path = f111.path().to_owned(); + let f111_path = f111_path.as_path(); + let f21 = NamedTempFile::new_in(dir2.path()).unwrap(); + f111.close().unwrap(); + validate_recv(rx, vec![(f111_path, op::CREATE), + (f21.path(), op::CREATE), + (f111_path, op::REMOVE)]); +} From 420beec521388b3e3091d53c685bd9ab7092a305 Mon Sep 17 00:00:00 2001 From: Jimmy Lu Date: Sat, 11 Jul 2015 12:21:08 -0400 Subject: [PATCH 2/3] Fix and add tests for PollWatcher single file --- src/poll.rs | 2 ++ tests/notify.rs | 39 ++++++++++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/poll.rs b/src/poll.rs index d474483b..52c2b3f8 100644 --- a/src/poll.rs +++ b/src/poll.rs @@ -71,6 +71,8 @@ impl PollWatcher { } } } + + if !stat.is_dir() { continue } } } diff --git a/tests/notify.rs b/tests/notify.rs index ff7a881a..3e5c4971 100644 --- a/tests/notify.rs +++ b/tests/notify.rs @@ -5,7 +5,8 @@ extern crate tempfile; use notify::*; use std::io::Write; use std::path::Path; -use std::sync::mpsc::{channel, Receiver}; +use std::thread; +use std::sync::mpsc::{channel, Sender, Receiver}; use tempdir::TempDir; use tempfile::NamedTempFile; @@ -18,25 +19,28 @@ fn validate_recv(rx: Receiver, evs: Vec<(&Path, Op)>) { assert!(rx.try_recv().is_err()); } -#[test] -fn watch_single_file() { +fn validate_watch_single_file(ctor: F) where + F: Fn(Sender) -> Result, W: Watcher +{ let mut file = NamedTempFile::new().unwrap(); let (tx, rx) = channel(); - let mut w: RecommendedWatcher = Watcher::new(tx).unwrap(); + let mut w = ctor(tx).unwrap(); w.watch(file.path()).unwrap(); + thread::sleep_ms(1000); file.write_all(b"foo").unwrap(); file.flush().unwrap(); validate_recv(rx, vec![(file.path(), op::WRITE)]); } -#[test] -fn watch_dir() { +fn validate_watch_dir(ctor: F) where + F: Fn(Sender) -> Result, W: Watcher +{ let dir = TempDir::new("dir").unwrap(); let dir1 = TempDir::new_in(dir.path(), "dir1").unwrap(); let dir2 = TempDir::new_in(dir.path(), "dir2").unwrap(); let dir11 = TempDir::new_in(dir1.path(), "dir11").unwrap(); let (tx, rx) = channel(); - let mut w: RecommendedWatcher = Watcher::new(tx).unwrap(); + let mut w = ctor(tx).unwrap(); w.watch(dir.path()).unwrap(); let f111 = NamedTempFile::new_in(dir11.path()).unwrap(); let f111_path = f111.path().to_owned(); @@ -47,3 +51,24 @@ fn watch_dir() { (f21.path(), op::CREATE), (f111_path, op::REMOVE)]); } + +#[test] +fn watch_single_file_recommended() { + validate_watch_single_file(RecommendedWatcher::new); +} + +#[test] +fn watch_dir_recommended() { + validate_watch_dir(RecommendedWatcher::new); +} + +#[test] +fn watch_single_file_poll() { + validate_watch_single_file(PollWatcher::new); +} + +// FIXME +// #[test] +// fn watch_dir_poll() { +// validate_watch_dir(PollWatcher::new); +// } From 11c861263ed921c702ec9a31f9005daa17f60212 Mon Sep 17 00:00:00 2001 From: Jimmy Lu Date: Sat, 11 Jul 2015 12:21:51 -0400 Subject: [PATCH 3/3] Add Jimmy Lu to authors --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 27807770..22a89725 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ authors = [ "Michael Maurizi ", "Pierre Baillet ", "ShuYu Wang ", + "Jimmy Lu ", ] description = "Cross-platform filesystem notification library"