Skip to content

Commit

Permalink
Add simple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
passcod committed Dec 14, 2014
1 parent 9a9619b commit a14f028
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/Cargo.lock
.*.sw*
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ extern crate notify;
let (tx, rx) = channel();

// Select the recommended implementation for this platform
let watcher = notify::new(tx);

// Watch files!
watcher.watch(Path::new("/path/to/foo"));

// Receive events!
println!("{}", rx.recv());
match notify::new(tx) {
Ok(watcher) => {
// Watch files!
watcher.watch(Path::new("/path/to/foo"));

// Receive events!
println!("{}", rx.recv());
},
Err(e) => println!("Uh oh: {}", e)
}
```

## Platforms
Expand Down
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,24 @@ pub fn new(tx: Sender<Event>) -> Result<INotifyWatcher, Error> {
pub fn new(tx: Sender<Event>) -> Result<PollWatcher, Error> {
Watcher::new(tx)
}

#[test]
#[cfg(target_os = "linux")]
fn new_inotify() {
let (tx, rx) = channel();
let w: Result<INotifyWatcher, Error> = Watcher::new(tx);
match w {
Ok(_) => assert!(true),
Err(_) => assert!(false)
}
}

#[test]
fn new_poll() {
let (tx, rx) = channel();
let w: Result<PollWatcher, Error> = Watcher::new(tx);
match w {
Ok(_) => assert!(true),
Err(_) => assert!(false)
}
}
10 changes: 5 additions & 5 deletions src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl PollWatcher {
let watches = self.watches.clone();
let open = self.open.clone();
spawn(proc() {
let mtimes: HashMap<Path, u64> = HashMap::new();
let mut mtimes: HashMap<Path, u64> = HashMap::new();
loop {
if !(*open.read()) {
break
Expand All @@ -24,16 +24,16 @@ impl PollWatcher {
for watch in watches.read().iter() {
if !watch.exists() {
tx.send(Event {
path: Ok(watch.clone()),
op: Err(PathNotFound)
path: Some(watch.clone()),
op: Err(Error::PathNotFound)
});
continue
}

match watch.lstat() {
Err(e) => {
tx.send(Event {
path: Ok(watch.clone()),
path: Some(watch.clone()),
op: Err(Error::Io(e))
});
continue
Expand All @@ -44,7 +44,7 @@ impl PollWatcher {
Some(old) => {
if stat.modified > old {
tx.send(Event {
path: Ok(watch.clone()),
path: Some(watch.clone()),
op: Ok(op::WRITE)
});
continue
Expand Down

0 comments on commit a14f028

Please sign in to comment.