-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from Yuhta/inotify-single-file
Single file support for inotify and poll
- Loading branch information
Showing
4 changed files
with
101 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ authors = [ | |
"Michael Maurizi <[email protected]>", | ||
"Pierre Baillet <[email protected]>", | ||
"ShuYu Wang <[email protected]>", | ||
"Jimmy Lu <[email protected]>", | ||
] | ||
|
||
description = "Cross-platform filesystem notification library" | ||
|
@@ -40,3 +41,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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,8 @@ impl PollWatcher { | |
} | ||
} | ||
} | ||
|
||
if !stat.is_dir() { continue } | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
extern crate notify; | ||
extern crate tempdir; | ||
extern crate tempfile; | ||
|
||
use notify::*; | ||
use std::io::Write; | ||
use std::path::Path; | ||
use std::thread; | ||
use std::sync::mpsc::{channel, Sender, Receiver}; | ||
use tempdir::TempDir; | ||
use tempfile::NamedTempFile; | ||
|
||
fn validate_recv(rx: Receiver<Event>, 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()); | ||
} | ||
|
||
fn validate_watch_single_file<F, W>(ctor: F) where | ||
F: Fn(Sender<Event>) -> Result<W, Error>, W: Watcher | ||
{ | ||
let mut file = NamedTempFile::new().unwrap(); | ||
let (tx, rx) = channel(); | ||
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)]); | ||
} | ||
|
||
fn validate_watch_dir<F, W>(ctor: F) where | ||
F: Fn(Sender<Event>) -> Result<W, Error>, 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 = ctor(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)]); | ||
} | ||
|
||
#[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); | ||
// } |