diff --git a/.travis.yml b/.travis.yml index 6028cd7e..83ca2503 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,86 @@ dist: xenial language: rust -cache: cargo -rust: - - stable - - beta -os: - - linux - - osx + +cache: + - directories: + - $HOME/.cargo + - target/debug/deps + - target/$TARGET/debug/deps +before_cache: + # Travis can't cache files that are not readable by "others" + - chmod -R a+r $HOME/.cargo + +matrix: + fast_finish: true + include: + + # Audit only + - env: CARGO_AUDIT=1 + + # Stable on windows first to get it going (very slow) + - os: windows + + # Linux + - os: linux + rust: nightly + - os: linux + rust: 1.26.1 + - os: linux + + # macOS + - os: osx + rust: nightly + - os: osx + rust: 1.26.1 + - os: osx + + # Other windowses later + - os: windows + rust: nightly + - os: windows + rust: 1.26.1 + + # Clippy only + - env: CARGO_CLIPPY=1 + + allow_failures: + - env: CARGO_CLIPPY=1 + +before_install: + - set -e + - | + if [ $TRAVIS_OS_NAME = windows ]; then + choco install windows-sdk-10.0 + fi + if [[ ! -z "$CARGO_AUDIT" ]]; then + which cargo-audit || cargo install --debug cargo-audit + elif [[ ! -z "$CARGO_CLIPPY" ]]; then + rustup component add clippy + fi +# --debug for faster build at the minimal expense of runtime speed + +script: + - | + if [[ ! -z "$CARGO_AUDIT" ]]; then + cargo check + cargo audit + elif [[ ! -z "$CARGO_CLIPPY" ]]; then + cargo clippy + else + cargo test + fi + +after_script: set +e + +branches: + only: + # release tags + - /^v\d+\.\d+\.\d+.*$/ + + # test branches + - /^try-/ + + # main branches + # - main + - next + - v4-legacy diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index 1bc90906..00000000 --- a/appveyor.yml +++ /dev/null @@ -1,23 +0,0 @@ -environment: - matrix: - - TARGET: x86_64-pc-windows-msvc - VCVARS: "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin\\amd64\\vcvars64.bat" - - TARGET: i686-pc-windows-msvc - VCVARS: "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin\\vcvars32.bat" - - # skip the gnu targets to reduce build time - #- TARGET: x86_64-pc-windows-gnu - #- TARGET: i686-pc-windows-gnu -install: - - ps: Start-FileDownload "https://static.rust-lang.org/dist/rust-nightly-${env:TARGET}.exe" - - rust-nightly-%TARGET%.exe /VERYSILENT /NORESTART /DIR="C:\Program Files (x86)\Rust" - - SET PATH=%PATH%;C:\Program Files (x86)\Rust\bin - - SET PATH=%PATH%;C:\MinGW\bin - - rustc -V - - cargo -V - -build: false - -test_script: - - call "%VCVARS%" - - cargo test --verbose diff --git a/examples/monitor_debounced.rs b/examples/monitor_debounced.rs index 6fc3541f..4fdd9e61 100644 --- a/examples/monitor_debounced.rs +++ b/examples/monitor_debounced.rs @@ -1,6 +1,6 @@ extern crate notify; -use notify::{RecommendedWatcher, Watcher, RecursiveMode}; +use notify::{RecommendedWatcher, RecursiveMode, Watcher}; use std::path::Path; use std::sync::mpsc::channel; use std::time::Duration; @@ -28,7 +28,9 @@ fn watch>(path: P) -> notify::Result<()> { } fn main() { - let path = std::env::args().nth(1).expect("Argument 1 needs to be a path"); + let path = std::env::args() + .nth(1) + .expect("Argument 1 needs to be a path"); println!("watching {}", path); if let Err(e) = watch(path) { println!("error: {:?}", e) diff --git a/examples/monitor_raw.rs b/examples/monitor_raw.rs index c3f6caf5..b63be2ae 100644 --- a/examples/monitor_raw.rs +++ b/examples/monitor_raw.rs @@ -1,6 +1,6 @@ extern crate notify; -use notify::{RecommendedWatcher, Watcher, RecursiveMode}; +use notify::{RecommendedWatcher, RecursiveMode, Watcher}; use std::path::Path; use std::sync::mpsc::channel; @@ -20,7 +20,11 @@ fn watch>(path: P) -> notify::Result<()> { // for example to handle I/O. loop { match rx.recv() { - Ok(notify::RawEvent{path: Some(path), op: Ok(op), cookie}) => println!("{:?} {:?} ({:?})", op, path, cookie), + Ok(notify::RawEvent { + path: Some(path), + op: Ok(op), + cookie, + }) => println!("{:?} {:?} ({:?})", op, path, cookie), Ok(event) => println!("broken event: {:?}", event), Err(e) => println!("watch error: {:?}", e), } @@ -28,7 +32,9 @@ fn watch>(path: P) -> notify::Result<()> { } fn main() { - let path = std::env::args().nth(1).expect("Argument 1 needs to be a path"); + let path = std::env::args() + .nth(1) + .expect("Argument 1 needs to be a path"); println!("watching {}", path); if let Err(e) = watch(path) { println!("error: {:?}", e) diff --git a/src/debounce/mod.rs b/src/debounce/mod.rs index 1927002c..53bed5b8 100644 --- a/src/debounce/mod.rs +++ b/src/debounce/mod.rs @@ -2,26 +2,30 @@ mod timer; -use super::{op, RawEvent, DebouncedEvent}; +use super::{op, DebouncedEvent, RawEvent}; use self::timer::WatchTimer; -use std::sync::mpsc; -use std::path::PathBuf; use std::collections::HashMap; +use std::path::PathBuf; +use std::sync::mpsc; use std::sync::{Arc, Mutex}; use std::time::Duration; -pub type OperationsBuffer = Arc, Option, Option)>>>; +pub type OperationsBuffer = + Arc, Option, Option)>>>; pub enum EventTx { - Raw { tx: mpsc::Sender }, + Raw { + tx: mpsc::Sender, + }, Debounced { tx: mpsc::Sender, debounce: Debounce, }, - DebouncedTx { tx: mpsc::Sender }, + DebouncedTx { + tx: mpsc::Sender, + }, } impl EventTx { @@ -30,7 +34,10 @@ impl EventTx { EventTx::Raw { ref tx } => { let _ = tx.send(event); } - EventTx::Debounced { ref tx, ref mut debounce } => { + EventTx::Debounced { + ref tx, + ref mut debounce, + } => { match (event.path, event.op, event.cookie) { (None, Ok(op::Op::RESCAN), None) => { let _ = tx.send(DebouncedEvent::Rescan); @@ -99,9 +106,12 @@ impl Debounce { // the last rename event might not be found in case the timer already fired // (https://github.com/passcod/notify/issues/101). if let Some(&mut (ref mut operation, ref mut from_path, ref mut timer_id)) = - op_buf.get_mut(self.rename_path.as_ref().unwrap()) { - if op != op::Op::RENAME || self.rename_cookie.is_none() || - self.rename_cookie != cookie { + op_buf.get_mut(self.rename_path.as_ref().unwrap()) + { + if op != op::Op::RENAME + || self.rename_cookie.is_none() + || self.rename_cookie != cookie + { if self.rename_path.as_ref().unwrap().exists() { match *operation { Some(op::Op::RENAME) if from_path.is_none() => { @@ -194,8 +204,8 @@ impl Debounce { } if op.contains(op::Op::CREATE) { - let &mut (ref mut operation, _, ref mut timer_id) = op_buf.entry(path.clone()) - .or_insert((None, None, None)); + let &mut (ref mut operation, _, ref mut timer_id) = + op_buf.entry(path.clone()).or_insert((None, None, None)); match *operation { // file can't be created twice Some(op::Op::CREATE) | @@ -229,8 +239,8 @@ impl Debounce { } if op.contains(op::Op::WRITE) { - let &mut (ref mut operation, _, ref mut timer_id) = op_buf.entry(path.clone()) - .or_insert((None, None, None)); + let &mut (ref mut operation, _, ref mut timer_id) = + op_buf.entry(path.clone()).or_insert((None, None, None)); match *operation { // keep create event / no need to emit NoticeWrite because // the file has just been created @@ -264,8 +274,8 @@ impl Debounce { } if op.contains(op::Op::CHMOD) { - let &mut (ref mut operation, _, ref mut timer_id) = op_buf.entry(path.clone()) - .or_insert((None, None, None)); + let &mut (ref mut operation, _, ref mut timer_id) = + op_buf.entry(path.clone()).or_insert((None, None, None)); match *operation { // keep create event Some(op::Op::CREATE) | @@ -295,9 +305,11 @@ impl Debounce { if op.contains(op::Op::RENAME) { // unwrap is safe because rename_path is Some - if self.rename_path.is_some() && self.rename_cookie.is_some() && - self.rename_cookie == cookie && - op_buf.contains_key(self.rename_path.as_ref().unwrap()) { + if self.rename_path.is_some() + && self.rename_cookie.is_some() + && self.rename_cookie == cookie + && op_buf.contains_key(self.rename_path.as_ref().unwrap()) + { // This is the second part of a rename operation, the old path is stored in the // rename_path variable. @@ -355,8 +367,8 @@ impl Debounce { self.rename_path = Some(path.clone()); self.rename_cookie = cookie; - let &mut (ref mut operation, _, ref mut timer_id) = op_buf.entry(path.clone()) - .or_insert((None, None, None)); + let &mut (ref mut operation, _, ref mut timer_id) = + op_buf.entry(path.clone()).or_insert((None, None, None)); match *operation { // keep create event / no need to emit NoticeRemove because // the file has just been created @@ -416,8 +428,8 @@ impl Debounce { } } - let &mut (ref mut operation, _, ref mut timer_id) = op_buf.entry(path.clone()) - .or_insert((None, None, None)); + let &mut (ref mut operation, _, ref mut timer_id) = + op_buf.entry(path.clone()).or_insert((None, None, None)); if remove_path.is_none() { match *operation { diff --git a/src/debounce/timer.rs b/src/debounce/timer.rs index 0c305551..003eb2f5 100644 --- a/src/debounce/timer.rs +++ b/src/debounce/timer.rs @@ -1,19 +1,19 @@ use super::super::{op, DebouncedEvent}; +use std::cmp::Ordering; +use std::collections::{BinaryHeap, HashSet}; +use std::path::PathBuf; use std::sync::mpsc; +use std::sync::{Arc, Condvar, Mutex}; use std::thread; use std::time::{Duration, Instant}; -use std::sync::{Arc, Condvar, Mutex}; -use std::collections::{BinaryHeap, HashSet}; -use std::path::PathBuf; -use std::cmp::Ordering; use debounce::OperationsBuffer; enum Action { Schedule(ScheduledEvent), Ignore(u64), - Stop + Stop, } #[derive(PartialEq, Eq)] @@ -46,11 +46,12 @@ struct ScheduleWorker { } impl ScheduleWorker { - fn new(trigger: Arc, - request_source: mpsc::Receiver, - tx: mpsc::Sender, - operations_buffer: OperationsBuffer) - -> ScheduleWorker { + fn new( + trigger: Arc, + request_source: mpsc::Receiver, + tx: mpsc::Sender, + operations_buffer: OperationsBuffer, + ) -> ScheduleWorker { ScheduleWorker { trigger: trigger, request_source: request_source, @@ -64,7 +65,7 @@ impl ScheduleWorker { fn drain_request_queue(&mut self) { loop { - match self.request_source.try_recv(){ + match self.request_source.try_recv() { Ok(Action::Schedule(event)) => self.schedule.push(event), Ok(Action::Ignore(ignore_id)) => { for &ScheduledEvent { ref id, .. } in &self.schedule { @@ -73,13 +74,12 @@ impl ScheduleWorker { break; } } - }, + } Err(mpsc::TryRecvError::Empty) => break, - Err(mpsc::TryRecvError::Disconnected) - | Ok(Action::Stop) => { + Err(mpsc::TryRecvError::Disconnected) | Ok(Action::Stop) => { self.stopped = true; break; - }, + } } } } @@ -99,7 +99,9 @@ impl ScheduleWorker { if let Some((op, from_path, _)) = op_buf.remove(&path) { let is_partial_rename = from_path.is_none(); if let Some(from_path) = from_path { - self.tx.send(DebouncedEvent::Rename(from_path, path.clone())).unwrap(); + self.tx + .send(DebouncedEvent::Rename(from_path, path.clone())) + .unwrap(); } let message = match op { Some(op::Op::CREATE) => Some(DebouncedEvent::Create(path)), @@ -176,10 +178,11 @@ pub struct WatchTimer { } impl WatchTimer { - pub fn new(tx: mpsc::Sender, - operations_buffer: OperationsBuffer, - delay: Duration) - -> WatchTimer { + pub fn new( + tx: mpsc::Sender, + operations_buffer: OperationsBuffer, + delay: Duration, + ) -> WatchTimer { let (schedule_tx, schedule_rx) = mpsc::channel(); let trigger = Arc::new(Condvar::new()); diff --git a/src/fsevent.rs b/src/fsevent.rs index 71bdf893..9dfd2585 100644 --- a/src/fsevent.rs +++ b/src/fsevent.rs @@ -13,6 +13,8 @@ #![allow(non_upper_case_globals, dead_code)] extern crate fsevent as fse; +use super::debounce::{Debounce, EventTx}; +use super::{op, DebouncedEvent, Error, RawEvent, RecursiveMode, Result, Watcher}; use fsevent_sys::core_foundation as cf; use fsevent_sys::fsevent as fs; use libc; @@ -23,12 +25,10 @@ use std::mem::transmute; use std::path::{Path, PathBuf}; use std::slice; use std::str::from_utf8; +use std::sync::mpsc::{channel, Receiver, Sender}; use std::sync::{Arc, Mutex}; -use std::sync::mpsc::{channel, Sender, Receiver}; use std::thread; use std::time::Duration; -use super::{Error, RawEvent, DebouncedEvent, op, Result, Watcher, RecursiveMode}; -use super::debounce::{Debounce, EventTx}; /// FSEvents-based `Watcher` implementation pub struct FsEventWatcher { @@ -132,9 +132,10 @@ impl FsEventWatcher { let mut to_remove = Vec::new(); for idx in 0..cf::CFArrayGetCount(self.paths) { let item = cf::CFArrayGetValueAtIndex(self.paths, idx); - if cf::CFStringCompare(item, cf_path, cf::kCFCompareCaseInsensitive) == - cf::kCFCompareEqualTo { - to_remove.push(idx); + if cf::CFStringCompare(item, cf_path, cf::kCFCompareCaseInsensitive) + == cf::kCFCompareEqualTo + { + to_remove.push(idx); } } @@ -154,19 +155,25 @@ impl FsEventWatcher { } // https://github.com/thibaudgg/rb-fsevent/blob/master/ext/fsevent_watch/main.c - fn append_path>(&mut self, path: P, recursive_mode: RecursiveMode) -> Result<()> { - if !path.as_ref().exists() { - return Err(Error::PathNotFound); - } + fn append_path>( + &mut self, + path: P, + recursive_mode: RecursiveMode, + ) -> Result<()> { + if !path.as_ref().exists() { + return Err(Error::PathNotFound); + } let str_path = path.as_ref().to_str().unwrap(); unsafe { let cf_path = cf::str_path_to_cfstring_ref(str_path); cf::CFArrayAppendValue(self.paths, cf_path); cf::CFRelease(cf_path); } - self.recursive_info.insert(path.as_ref().to_path_buf().canonicalize().unwrap(), - recursive_mode.is_recursive()); - Ok(()) + self.recursive_info.insert( + path.as_ref().to_path_buf().canonicalize().unwrap(), + recursive_mode.is_recursive(), + ); + Ok(()) } fn run(&mut self) -> Result<()> { @@ -194,13 +201,15 @@ impl FsEventWatcher { let cb = callback as *mut _; let stream = unsafe { - fs::FSEventStreamCreate(cf::kCFAllocatorDefault, - cb, - &stream_context, - self.paths, - self.since_when, - self.latency, - self.flags) + fs::FSEventStreamCreate( + cf::kCFAllocatorDefault, + cb, + &stream_context, + self.paths, + self.since_when, + self.latency, + self.flags, + ) }; // move into thread @@ -213,20 +222,25 @@ impl FsEventWatcher { unsafe { let cur_runloop = cf::CFRunLoopGetCurrent(); - fs::FSEventStreamScheduleWithRunLoop(stream, - cur_runloop, - cf::kCFRunLoopDefaultMode); + fs::FSEventStreamScheduleWithRunLoop( + stream, + cur_runloop, + cf::kCFRunLoopDefaultMode, + ); fs::FSEventStreamStart(stream); // the calling to CFRunLoopRun will be terminated by CFRunLoopStop call in drop() - rl_tx.send(cur_runloop as *mut libc::c_void as usize) + rl_tx + .send(cur_runloop as *mut libc::c_void as usize) .expect("Unable to send runloop to watcher"); cf::CFRunLoopRun(); fs::FSEventStreamStop(stream); fs::FSEventStreamInvalidate(stream); fs::FSEventStreamRelease(stream); } - done_tx.send(()).expect("error while signal run loop is done"); + done_tx + .send(()) + .expect("error while signal run loop is done"); }); // block until runloop has been set self.runloop = Some(rl_rx.recv().unwrap()); @@ -238,13 +252,13 @@ impl FsEventWatcher { #[allow(unused_variables)] #[doc(hidden)] pub unsafe extern "C" fn callback( - stream_ref: fs::FSEventStreamRef, - info: *mut libc::c_void, - num_events: libc::size_t, // size_t numEvents - event_paths: *const *const libc::c_char, // void *eventPaths - event_flags: *mut libc::c_void, // const FSEventStreamEventFlags eventFlags[] - event_ids: *mut libc::c_void, // const FSEventStreamEventId eventIds[] - ) { + stream_ref: fs::FSEventStreamRef, + info: *mut libc::c_void, + num_events: libc::size_t, // size_t numEvents + event_paths: *const *const libc::c_char, // void *eventPaths + event_flags: *mut libc::c_void, // const FSEventStreamEventFlags eventFlags[] + event_ids: *mut libc::c_void, // const FSEventStreamEventId eventIds[] +) { let num = num_events as usize; let e_ptr = event_flags as *mut u32; let i_ptr = event_ids as *mut u64; @@ -331,7 +345,6 @@ pub unsafe extern "C" fn callback( } } - impl Watcher for FsEventWatcher { fn new_raw(tx: Sender) -> Result { Ok(FsEventWatcher { @@ -415,9 +428,11 @@ fn test_fsevent_watcher_drop() { // if drop() works, this loop will quit after all Sender freed // otherwise will block forever for e in rx.iter() { - println!("debug => {:?} {:?}", - e.op.map(|e| e.bits()).unwrap_or(0), - e.path); + println!( + "debug => {:?} {:?}", + e.op.map(|e| e.bits()).unwrap_or(0), + e.path + ); } println!("in test: {} works", file!()); diff --git a/src/inotify.rs b/src/inotify.rs index 9641914f..084fecfb 100644 --- a/src/inotify.rs +++ b/src/inotify.rs @@ -148,9 +148,7 @@ impl EventLoop { let mut events = mio::Events::with_capacity(16); loop { // Wait for something to happen. - self.poll - .poll(&mut events, None) - .expect("poll failed"); + self.poll.poll(&mut events, None).expect("poll failed"); // Process whatever happened. for event in &events { diff --git a/src/lib.rs b/src/lib.rs index 60f21811..6f77e6d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -92,41 +92,41 @@ #[macro_use] extern crate bitflags; -#[cfg(target_os="linux")] +extern crate filetime; +#[cfg(target_os = "macos")] +extern crate fsevent_sys; +extern crate libc; +#[cfg(target_os = "linux")] extern crate mio; -#[cfg(target_os="linux")] +#[cfg(target_os = "linux")] extern crate mio_extras; -#[cfg(target_os="macos")] -extern crate fsevent_sys; -#[cfg(target_os="windows")] +#[cfg(target_os = "windows")] extern crate winapi; -extern crate libc; -extern crate filetime; pub use self::op::Op; -use std::io; -use std::path::{Path, PathBuf}; -use std::sync::mpsc::Sender; use std::convert::AsRef; -use std::fmt; use std::error::Error as StdError; +use std::fmt; +use std::io; +use std::path::{Path, PathBuf}; use std::result::Result as StdResult; +use std::sync::mpsc::Sender; use std::time::Duration; -#[cfg(target_os="macos")] +#[cfg(target_os = "macos")] pub use self::fsevent::FsEventWatcher; -#[cfg(target_os="linux")] +#[cfg(target_os = "linux")] pub use self::inotify::INotifyWatcher; -#[cfg(target_os="windows")] -pub use self::windows::ReadDirectoryChangesWatcher; pub use self::null::NullWatcher; pub use self::poll::PollWatcher; +#[cfg(target_os = "windows")] +pub use self::windows::ReadDirectoryChangesWatcher; -#[cfg(target_os="linux")] -pub mod inotify; -#[cfg(target_os="macos")] +#[cfg(target_os = "macos")] pub mod fsevent; -#[cfg(target_os="windows")] +#[cfg(target_os = "linux")] +pub mod inotify; +#[cfg(target_os = "windows")] pub mod windows; pub mod null; @@ -337,18 +337,21 @@ pub mod op { #[cfg(test)] mod op_test { - #[test] fn mixed_bitflags_form() { + #[test] + fn mixed_bitflags_form() { let op = super::op::Op::CHMOD | super::op::WRITE; assert!(op.contains(super::op::CHMOD)); assert!(op.contains(super::op::Op::WRITE)); } - #[test] fn new_bitflags_form() { + #[test] + fn new_bitflags_form() { let op = super::op::Op::CHMOD | super::op::Op::WRITE; assert!(op.contains(super::op::Op::WRITE)); } - #[test] fn old_bitflags_form() { + #[test] + fn old_bitflags_form() { let op = super::op::CHMOD | super::op::WRITE; assert!(op.contains(super::op::WRITE)); } @@ -448,12 +451,12 @@ pub enum DebouncedEvent { impl PartialEq for DebouncedEvent { fn eq(&self, other: &DebouncedEvent) -> bool { match (self, other) { - (&DebouncedEvent::NoticeWrite(ref a), &DebouncedEvent::NoticeWrite(ref b)) | - (&DebouncedEvent::NoticeRemove(ref a), &DebouncedEvent::NoticeRemove(ref b)) | - (&DebouncedEvent::Create(ref a), &DebouncedEvent::Create(ref b)) | - (&DebouncedEvent::Write(ref a), &DebouncedEvent::Write(ref b)) | - (&DebouncedEvent::Chmod(ref a), &DebouncedEvent::Chmod(ref b)) | - (&DebouncedEvent::Remove(ref a), &DebouncedEvent::Remove(ref b)) => a == b, + (&DebouncedEvent::NoticeWrite(ref a), &DebouncedEvent::NoticeWrite(ref b)) + | (&DebouncedEvent::NoticeRemove(ref a), &DebouncedEvent::NoticeRemove(ref b)) + | (&DebouncedEvent::Create(ref a), &DebouncedEvent::Create(ref b)) + | (&DebouncedEvent::Write(ref a), &DebouncedEvent::Write(ref b)) + | (&DebouncedEvent::Chmod(ref a), &DebouncedEvent::Chmod(ref b)) + | (&DebouncedEvent::Remove(ref a), &DebouncedEvent::Remove(ref b)) => a == b, (&DebouncedEvent::Rename(ref a1, ref a2), &DebouncedEvent::Rename(ref b1, ref b2)) => { (a1 == b1 && a2 == b2) } @@ -638,15 +641,20 @@ pub fn watcher(tx: Sender, delay: Duration) -> Result { event_tx.send(RawEvent { @@ -78,17 +84,21 @@ impl PollWatcher { } Ok(metadata) => { if !metadata.is_dir() { - let mtime = FileTime::from_last_modification_time(&metadata) - .seconds(); - match paths.insert(watch.clone(), - PathData { - mtime: mtime, - last_check: current_time, - }) { + let mtime = + FileTime::from_last_modification_time(&metadata).seconds(); + match paths.insert( + watch.clone(), + PathData { + mtime: mtime, + last_check: current_time, + }, + ) { None => { unreachable!(); } - Some(PathData { mtime: old_mtime, .. }) => { + Some(PathData { + mtime: old_mtime, .. + }) => { if mtime > old_mtime { event_tx.send(RawEvent { path: Some(watch.clone()), @@ -104,7 +114,8 @@ impl PollWatcher { .follow_links(true) .max_depth(depth) .into_iter() - .filter_map(|e| e.ok()) { + .filter_map(|e| e.ok()) + { let path = entry.path(); match entry.metadata() { @@ -119,11 +130,13 @@ impl PollWatcher { let mtime = FileTime::from_last_modification_time(&m) .seconds(); - match paths.insert(path.to_path_buf(), - PathData { - mtime: mtime, - last_check: current_time, - }) { + match paths.insert( + path.to_path_buf(), + PathData { + mtime: mtime, + last_check: current_time, + }, + ) { None => { event_tx.send(RawEvent { path: Some(path.to_path_buf()), @@ -131,7 +144,9 @@ impl PollWatcher { cookie: None, }); } - Some(PathData { mtime: old_mtime, .. }) => { + Some(PathData { + mtime: old_mtime, .. + }) => { if mtime > old_mtime { event_tx.send(RawEvent { path: Some(path.to_path_buf()), @@ -210,16 +225,20 @@ impl Watcher for PollWatcher { if !metadata.is_dir() { let mut paths = HashMap::new(); let mtime = FileTime::from_last_modification_time(&metadata).seconds(); - paths.insert(watch.clone(), - PathData { - mtime: mtime, - last_check: current_time, - }); - watches.insert(watch, - WatchData { - is_recursive: recursive_mode.is_recursive(), - paths: paths, - }); + paths.insert( + watch.clone(), + PathData { + mtime: mtime, + last_check: current_time, + }, + ); + watches.insert( + watch, + WatchData { + is_recursive: recursive_mode.is_recursive(), + paths: paths, + }, + ); } else { let mut paths = HashMap::new(); let depth = if recursive_mode.is_recursive() { @@ -231,7 +250,8 @@ impl Watcher for PollWatcher { .follow_links(true) .max_depth(depth) .into_iter() - .filter_map(|e| e.ok()) { + .filter_map(|e| e.ok()) + { let path = entry.path(); match entry.metadata() { @@ -244,19 +264,23 @@ impl Watcher for PollWatcher { } Ok(m) => { let mtime = FileTime::from_last_modification_time(&m).seconds(); - paths.insert(path.to_path_buf(), - PathData { - mtime: mtime, - last_check: current_time, - }); + paths.insert( + path.to_path_buf(), + PathData { + mtime: mtime, + last_check: current_time, + }, + ); } } } - watches.insert(watch, - WatchData { - is_recursive: recursive_mode.is_recursive(), - paths: paths, - }); + watches.insert( + watch, + WatchData { + is_recursive: recursive_mode.is_recursive(), + paths: paths, + }, + ); } } } @@ -265,7 +289,12 @@ impl Watcher for PollWatcher { } fn unwatch>(&mut self, path: P) -> Result<()> { - if (*self.watches).lock().unwrap().remove(path.as_ref()).is_some() { + if (*self.watches) + .lock() + .unwrap() + .remove(path.as_ref()) + .is_some() + { Ok(()) } else { Err(Error::WatchNotFound) diff --git a/src/windows.rs b/src/windows.rs index 8163bc31..4b28333f 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -11,10 +11,12 @@ use winapi::shared::minwindef::TRUE; use winapi::shared::winerror::ERROR_OPERATION_ABORTED; use winapi::um::fileapi; use winapi::um::handleapi::INVALID_HANDLE_VALUE; -use winapi::um::minwinbase::{OVERLAPPED, LPOVERLAPPED}; +use winapi::um::minwinbase::{LPOVERLAPPED, OVERLAPPED}; use winapi::um::winbase::{self, INFINITE, WAIT_OBJECT_0}; -use winapi::um::winnt::{self, HANDLE, FILE_NOTIFY_INFORMATION}; +use winapi::um::winnt::{self, FILE_NOTIFY_INFORMATION, HANDLE}; +use super::debounce::{Debounce, EventTx}; +use super::{op, DebouncedEvent, Error, Op, RawEvent, RecursiveMode, Result, Watcher}; use std::collections::HashMap; use std::env; use std::ffi::OsString; @@ -24,12 +26,10 @@ use std::os::windows::ffi::{OsStrExt, OsStringExt}; use std::path::{Path, PathBuf}; use std::ptr; use std::slice; +use std::sync::mpsc::{channel, Receiver, Sender}; use std::sync::{Arc, Mutex}; -use std::sync::mpsc::{channel, Sender, Receiver}; use std::thread; use std::time::Duration; -use super::{RawEvent, DebouncedEvent, Error, op, Op, Result, Watcher, RecursiveMode}; -use super::debounce::{Debounce, EventTx}; const BUF_SIZE: u32 = 16384; @@ -37,7 +37,7 @@ static mut COOKIE_COUNTER: u32 = 0; #[derive(Clone)] struct ReadData { - dir: PathBuf, // directory that is being watched + dir: PathBuf, // directory that is being watched file: Option, // if a file is being watched, this is its full path complete_sem: HANDLE, is_recursive: bool, @@ -76,12 +76,12 @@ struct ReadDirectoryChangesServer { } impl ReadDirectoryChangesServer { - fn start(event_tx: EventTx, - meta_tx: Sender, - cmd_tx: Sender>, - wakeup_sem: HANDLE) - -> Sender { - + fn start( + event_tx: EventTx, + meta_tx: Sender, + cmd_tx: Sender>, + wakeup_sem: HANDLE, + ) -> Sender { let (action_tx, action_rx) = channel(); // it is, in fact, ok to send the semaphore across threads let sem_temp = wakeup_sem as u64; @@ -144,8 +144,9 @@ impl ReadDirectoryChangesServer { fn add_watch(&mut self, path: PathBuf, is_recursive: bool) -> Result { // path must exist and be either a file or directory if !path.is_dir() && !path.is_file() { - return Err(Error::Generic("Input watch path is neither a file nor a directory." - .to_owned())); + return Err(Error::Generic( + "Input watch path is neither a file nor a directory.".to_owned(), + )); } let (watching_file, dir_target) = { @@ -157,24 +158,30 @@ impl ReadDirectoryChangesServer { } }; - let encoded_path: Vec = dir_target.as_os_str().encode_wide().chain(Some(0)).collect(); + let encoded_path: Vec = dir_target + .as_os_str() + .encode_wide() + .chain(Some(0)) + .collect(); let handle; unsafe { - handle = kernel32::CreateFileW(encoded_path.as_ptr(), - winnt::FILE_LIST_DIRECTORY, - winnt::FILE_SHARE_READ | winnt::FILE_SHARE_DELETE | - winnt::FILE_SHARE_WRITE, - ptr::null_mut(), - fileapi::OPEN_EXISTING, - winbase::FILE_FLAG_BACKUP_SEMANTICS | - winbase::FILE_FLAG_OVERLAPPED, - ptr::null_mut()); + handle = kernel32::CreateFileW( + encoded_path.as_ptr(), + winnt::FILE_LIST_DIRECTORY, + winnt::FILE_SHARE_READ | winnt::FILE_SHARE_DELETE | winnt::FILE_SHARE_WRITE, + ptr::null_mut(), + fileapi::OPEN_EXISTING, + winbase::FILE_FLAG_BACKUP_SEMANTICS | winbase::FILE_FLAG_OVERLAPPED, + ptr::null_mut(), + ); if handle == INVALID_HANDLE_VALUE { let err = if watching_file { - Err(Error::Generic("You attempted to watch a single file, but parent \ - directory could not be opened." - .to_owned())) + Err(Error::Generic( + "You attempted to watch a single file, but parent \ + directory could not be opened." + .to_owned(), + )) } else { // TODO: Call GetLastError for better error info? Err(Error::PathNotFound) @@ -194,7 +201,9 @@ impl ReadDirectoryChangesServer { unsafe { kernel32::CloseHandle(handle); } - return Err(Error::Generic("Failed to create semaphore for watch.".to_owned())); + return Err(Error::Generic( + "Failed to create semaphore for watch.".to_owned(), + )); } let rd = ReadData { dir: dir_target, @@ -227,7 +236,6 @@ fn stop_watch(ws: &WatchState, meta_tx: &Sender) { kernel32::WaitForSingleObjectEx(ws.complete_sem, INFINITE, TRUE); } kernel32::CloseHandle(ws.complete_sem); - } let _ = meta_tx.send(MetaEvent::SingleWatchComplete); } @@ -240,11 +248,13 @@ fn start_read(rd: &ReadData, event_tx: Arc>, handle: HANDLE) { data: rd.clone(), }); - let flags = - winnt::FILE_NOTIFY_CHANGE_FILE_NAME | winnt::FILE_NOTIFY_CHANGE_DIR_NAME | - winnt::FILE_NOTIFY_CHANGE_ATTRIBUTES | winnt::FILE_NOTIFY_CHANGE_SIZE | - winnt::FILE_NOTIFY_CHANGE_LAST_WRITE | winnt::FILE_NOTIFY_CHANGE_CREATION | - winnt::FILE_NOTIFY_CHANGE_SECURITY; + let flags = winnt::FILE_NOTIFY_CHANGE_FILE_NAME + | winnt::FILE_NOTIFY_CHANGE_DIR_NAME + | winnt::FILE_NOTIFY_CHANGE_ATTRIBUTES + | winnt::FILE_NOTIFY_CHANGE_SIZE + | winnt::FILE_NOTIFY_CHANGE_LAST_WRITE + | winnt::FILE_NOTIFY_CHANGE_CREATION + | winnt::FILE_NOTIFY_CHANGE_SECURITY; let monitor_subdir = if (&request.data.file).is_none() && request.data.is_recursive { 1 @@ -263,14 +273,16 @@ fn start_read(rd: &ReadData, event_tx: Arc>, handle: HANDLE) { // This is using an asynchronous call with a completion routine for receiving notifications // An I/O completion port would probably be more performant - let ret = winbase::ReadDirectoryChangesW(handle, - req_buf, - BUF_SIZE, - monitor_subdir, - flags, - &mut 0u32 as *mut u32, // not used for async reqs - &mut *overlapped as *mut OVERLAPPED, - Some(handle_event)); + let ret = winbase::ReadDirectoryChangesW( + handle, + req_buf, + BUF_SIZE, + monitor_subdir, + flags, + &mut 0u32 as *mut u32, // not used for async reqs + &mut *overlapped as *mut OVERLAPPED, + Some(handle_event), + ); if ret == 0 { // error reading. retransmute request memory to allow drop. @@ -295,9 +307,11 @@ fn send_pending_rename_event(event: Option, event_tx: &mut EventTx) { } } -unsafe extern "system" fn handle_event(error_code: u32, - _bytes_written: u32, - overlapped: LPOVERLAPPED) { +unsafe extern "system" fn handle_event( + error_code: u32, + _bytes_written: u32, + overlapped: LPOVERLAPPED, +) { let overlapped: Box = Box::from_raw(overlapped); let request: Box = Box::from_raw(overlapped.hEvent as *mut _); @@ -325,7 +339,10 @@ unsafe extern "system" fn handle_event(error_code: u32, let len = (*cur_entry).FileNameLength as usize / 2; let encoded_path: &[u16] = slice::from_raw_parts((*cur_entry).FileName.as_ptr(), len); // prepend root to get a full path - let path = request.data.dir.join(PathBuf::from(OsString::from_wide(encoded_path))); + let path = request + .data + .dir + .join(PathBuf::from(OsString::from_wide(encoded_path))); // if we are watching a single file, ignore the event unless the path is exactly // the watched file @@ -406,15 +423,18 @@ pub struct ReadDirectoryChangesWatcher { } impl ReadDirectoryChangesWatcher { - pub fn create(tx: Sender, - meta_tx: Sender) - -> Result { + pub fn create( + tx: Sender, + meta_tx: Sender, + ) -> Result { let (cmd_tx, cmd_rx) = channel(); let wakeup_sem = unsafe { kernel32::CreateSemaphoreW(ptr::null_mut(), 0, 1, ptr::null_mut()) }; if wakeup_sem == ptr::null_mut() || wakeup_sem == INVALID_HANDLE_VALUE { - return Err(Error::Generic("Failed to create wakeup semaphore.".to_owned())); + return Err(Error::Generic( + "Failed to create wakeup semaphore.".to_owned(), + )); } let event_tx = EventTx::Raw { tx: tx }; @@ -428,16 +448,19 @@ impl ReadDirectoryChangesWatcher { }) } - pub fn create_debounced(tx: Sender, - meta_tx: Sender, - delay: Duration) - -> Result { + pub fn create_debounced( + tx: Sender, + meta_tx: Sender, + delay: Duration, + ) -> Result { let (cmd_tx, cmd_rx) = channel(); let wakeup_sem = unsafe { kernel32::CreateSemaphoreW(ptr::null_mut(), 0, 1, ptr::null_mut()) }; if wakeup_sem == ptr::null_mut() || wakeup_sem == INVALID_HANDLE_VALUE { - return Err(Error::Generic("Failed to create wakeup semaphore.".to_owned())); + return Err(Error::Generic( + "Failed to create wakeup semaphore.".to_owned(), + )); } let event_tx = EventTx::Debounced { @@ -465,30 +488,31 @@ impl ReadDirectoryChangesWatcher { fn send_action_require_ack(&mut self, action: Action, pb: &PathBuf) -> Result<()> { match self.tx.send(action) { - Err(_) => Err(Error::Generic("Error sending to internal channel".to_owned())), + Err(_) => Err(Error::Generic( + "Error sending to internal channel".to_owned(), + )), Ok(_) => { // wake 'em up, we don't want to wait around for the ack self.wakeup_server(); match self.cmd_rx.recv() { - Err(_) => { - Err(Error::Generic("Error receiving from command channel".to_owned())) - } - Ok(ack_res) => { - match ack_res { - Err(e) => Err(Error::Generic(format!("Error in watcher: {:?}", e))), - Ok(ack_pb) => { - if pb.as_path() != ack_pb.as_path() { - Err(Error::Generic(format!("Expected ack for {:?} but got \ - ack for {:?}", - pb, - ack_pb))) - } else { - Ok(()) - } + Err(_) => Err(Error::Generic( + "Error receiving from command channel".to_owned(), + )), + Ok(ack_res) => match ack_res { + Err(e) => Err(Error::Generic(format!("Error in watcher: {:?}", e))), + Ok(ack_pb) => { + if pb.as_path() != ack_pb.as_path() { + Err(Error::Generic(format!( + "Expected ack for {:?} but got \ + ack for {:?}", + pb, ack_pb + ))) + } else { + Ok(()) } } - } + }, } } } @@ -517,8 +541,9 @@ impl Watcher for ReadDirectoryChangesWatcher { }; // path must exist and be either a file or directory if !pb.is_dir() && !pb.is_file() { - return Err(Error::Generic("Input watch path is neither a file nor a directory." - .to_owned())); + return Err(Error::Generic( + "Input watch path is neither a file nor a directory.".to_owned(), + )); } self.send_action_require_ack(Action::Watch(pb.clone(), recursive_mode), &pb) } @@ -530,7 +555,8 @@ impl Watcher for ReadDirectoryChangesWatcher { let p = try!(env::current_dir().map_err(Error::Io)); p.join(path) }; - let res = self.tx + let res = self + .tx .send(Action::Unwatch(pb)) .map_err(|_| Error::Generic("Error sending to internal channel".to_owned())); self.wakeup_server(); diff --git a/tests/debounce.rs b/tests/debounce.rs index 2734e495..51668d19 100644 --- a/tests/debounce.rs +++ b/tests/debounce.rs @@ -25,7 +25,7 @@ fn recv_events_debounced(rx: &mpsc::Receiver) -> Vec events.push(event), Err(mpsc::TryRecvError::Empty) => (), - Err(e) => panic!("unexpected channel err: {:?}", e) + Err(e) => panic!("unexpected channel err: {:?}", e), } thread::sleep(Duration::from_millis(50)); } @@ -39,51 +39,60 @@ fn create_file() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("file1"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::Create(tdir.mkpath("file1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![DebouncedEvent::Create(tdir.mkpath("file1")),] + ); } #[test] fn write_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1", - ]); + tdir.create_all(vec!["file1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.write("file1"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeWrite(tdir.mkpath("file1")), - DebouncedEvent::Write(tdir.mkpath("file1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeWrite(tdir.mkpath("file1")), + DebouncedEvent::Write(tdir.mkpath("file1")), + ] + ); } #[test] fn write_long_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1", - ]); + tdir.create_all(vec!["file1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); let wait = Duration::from_millis(DELAY_MS / 2); tdir.write("file1"); @@ -94,10 +103,13 @@ fn write_long_file() { thread::sleep(wait); tdir.write("file1"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeWrite(tdir.mkpath("file1")), - DebouncedEvent::Write(tdir.mkpath("file1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeWrite(tdir.mkpath("file1")), + DebouncedEvent::Write(tdir.mkpath("file1")), + ] + ); } // Linux: @@ -119,28 +131,33 @@ fn write_long_file() { fn modify_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1", - ]); + tdir.create_all(vec!["file1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.chmod("file1"); - if cfg!(target_os="windows") { + if cfg!(target_os = "windows") { // windows cannot distinguish between chmod and write - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeWrite(tdir.mkpath("file1")), - DebouncedEvent::Write(tdir.mkpath("file1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeWrite(tdir.mkpath("file1")), + DebouncedEvent::Write(tdir.mkpath("file1")), + ] + ); } else { - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::Chmod(tdir.mkpath("file1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![DebouncedEvent::Chmod(tdir.mkpath("file1")),] + ); } } @@ -148,44 +165,52 @@ fn modify_file() { fn delete_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1", - ]); + tdir.create_all(vec!["file1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.remove("file1"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), - DebouncedEvent::Remove(tdir.mkpath("file1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), + DebouncedEvent::Remove(tdir.mkpath("file1")), + ] + ); } #[test] fn rename_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1", - ]); + tdir.create_all(vec!["file1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.rename("file1", "file2"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), - DebouncedEvent::Rename(tdir.mkpath("file1"), tdir.mkpath("file2")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), + DebouncedEvent::Rename(tdir.mkpath("file1"), tdir.mkpath("file2")), + ] + ); } #[test] @@ -195,16 +220,20 @@ fn create_write_modify_file() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("file1"); tdir.write("file1"); tdir.chmod("file1"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::Create(tdir.mkpath("file1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![DebouncedEvent::Create(tdir.mkpath("file1")),] + ); } #[test] @@ -214,8 +243,11 @@ fn create_delete_file() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("file1"); sleep_macos(10); @@ -228,24 +260,28 @@ fn create_delete_file() { fn delete_create_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1", - ]); + tdir.create_all(vec!["file1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.remove("file1"); sleep_macos(10); tdir.create("file1"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), - DebouncedEvent::Write(tdir.mkpath("file1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), + DebouncedEvent::Write(tdir.mkpath("file1")), + ] + ); } #[test] @@ -255,15 +291,19 @@ fn create_rename_file() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("file1"); tdir.rename("file1", "file2"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::Create(tdir.mkpath("file2")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![DebouncedEvent::Create(tdir.mkpath("file2")),] + ); } #[test] @@ -273,8 +313,11 @@ fn create_rename_delete_file() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("file1"); sleep_macos(10); @@ -301,47 +344,52 @@ fn create_rename_delete_file() { fn create_rename_overwrite_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file2", - ]); + tdir.create_all(vec!["file2"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("file1"); tdir.rename("file1", "file2"); - if cfg!(target_os="windows") { - // Windows interprets a move that overwrites a file as a delete of the source file and a write to the file that is being overwritten - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeWrite(tdir.mkpath("file2")), - DebouncedEvent::Write(tdir.mkpath("file2")), - ]); - } else if cfg!(target_os="macos") { - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("file2")), - DebouncedEvent::Create(tdir.mkpath("file2")), // even though the file is being overwritten, that can't be detected - ]); + if cfg!(target_os = "windows") || cfg!(target_os = "macos") { + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("file2")), + DebouncedEvent::Create(tdir.mkpath("file2")), // even though the file is being overwritten, that can't be detected + ] + ); } else { - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::Create(tdir.mkpath("file2")), // even though the file is being overwritten, that can't be detected - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::Create(tdir.mkpath("file2")), // even though the file is being overwritten, that can't be detected + ] + ); } } // https://github.com/passcod/notify/issues/99 #[test] -fn create_rename_write_create() { // fsevents +fn create_rename_write_create() { + // fsevents let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("file1"); tdir.rename("file1", "file2"); @@ -349,22 +397,29 @@ fn create_rename_write_create() { // fsevents tdir.write("file2"); tdir.create("file3"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::Create(tdir.mkpath("file2")), - DebouncedEvent::Create(tdir.mkpath("file3")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::Create(tdir.mkpath("file2")), + DebouncedEvent::Create(tdir.mkpath("file3")), + ] + ); } // https://github.com/passcod/notify/issues/100 #[test] -fn create_rename_remove_create() { // fsevents +fn create_rename_remove_create() { + // fsevents let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("file1"); @@ -375,25 +430,30 @@ fn create_rename_remove_create() { // fsevents sleep_macos(10); tdir.create("file3"); - if cfg!(target_os="macos") { - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::Create(tdir.mkpath("file1")), - DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), - DebouncedEvent::NoticeRemove(tdir.mkpath("file2")), - // DebouncedEvent::Remove(tdir.mkpath("file1")), BUG: There should be a remove event for file1 - DebouncedEvent::Remove(tdir.mkpath("file2")), - DebouncedEvent::Create(tdir.mkpath("file3")), - ]); + if cfg!(target_os = "macos") { + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::Create(tdir.mkpath("file1")), + DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), + DebouncedEvent::NoticeRemove(tdir.mkpath("file2")), + // DebouncedEvent::Remove(tdir.mkpath("file1")), BUG: There should be a remove event for file1 + DebouncedEvent::Remove(tdir.mkpath("file2")), + DebouncedEvent::Create(tdir.mkpath("file3")), + ] + ); } else { - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::Create(tdir.mkpath("file3")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![DebouncedEvent::Create(tdir.mkpath("file3")),] + ); } } // https://github.com/passcod/notify/issues/101 #[test] -fn move_out_sleep_move_in() { // fsevents +fn move_out_sleep_move_in() { + // fsevents let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); tdir.create("watch_dir"); @@ -401,24 +461,31 @@ fn move_out_sleep_move_in() { // fsevents sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("watch_dir"), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("watch_dir"), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("watch_dir/file1"); tdir.rename("watch_dir/file1", "file1"); sleep(DELAY_MS + 10); tdir.rename("file1", "watch_dir/file2"); - if cfg!(target_os="macos") { - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::Create(tdir.mkpath("watch_dir/file1")), - DebouncedEvent::NoticeRemove(tdir.mkpath("watch_dir/file2")), - DebouncedEvent::Create(tdir.mkpath("watch_dir/file2")), - ]); + if cfg!(target_os = "macos") { + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::Create(tdir.mkpath("watch_dir/file1")), + DebouncedEvent::NoticeRemove(tdir.mkpath("watch_dir/file2")), + DebouncedEvent::Create(tdir.mkpath("watch_dir/file2")), + ] + ); } else { - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::Create(tdir.mkpath("watch_dir/file2")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![DebouncedEvent::Create(tdir.mkpath("watch_dir/file2")),] + ); } } @@ -435,14 +502,18 @@ fn move_repeatedly() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("watch_dir"), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("watch_dir"), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("watch_dir/file1"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::Create(tdir.mkpath("watch_dir/file1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![DebouncedEvent::Create(tdir.mkpath("watch_dir/file1")),] + ); for i in 1..300 { let from = format!("watch_dir/file{}", i); @@ -451,10 +522,13 @@ fn move_repeatedly() { if i % 10 == 0 { let from = format!("watch_dir/file{}", i - 9); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath(&from)), - DebouncedEvent::Rename(tdir.mkpath(&from), tdir.mkpath(&to)), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath(&from)), + DebouncedEvent::Rename(tdir.mkpath(&from), tdir.mkpath(&to)), + ] + ); } } } @@ -463,84 +537,99 @@ fn move_repeatedly() { fn write_rename_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1", - ]); + tdir.create_all(vec!["file1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.write("file1"); tdir.rename("file1", "file2"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeWrite(tdir.mkpath("file1")), - DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), - DebouncedEvent::Rename(tdir.mkpath("file1"), tdir.mkpath("file2")), - DebouncedEvent::Write(tdir.mkpath("file2")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeWrite(tdir.mkpath("file1")), + DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), + DebouncedEvent::Rename(tdir.mkpath("file1"), tdir.mkpath("file2")), + DebouncedEvent::Write(tdir.mkpath("file2")), + ] + ); } #[test] fn rename_write_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1", - ]); + tdir.create_all(vec!["file1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.rename("file1", "file2"); sleep_macos(10); tdir.write("file2"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), - DebouncedEvent::NoticeWrite(tdir.mkpath("file2")), // TODO not necessary - DebouncedEvent::Rename(tdir.mkpath("file1"), tdir.mkpath("file2")), - DebouncedEvent::Write(tdir.mkpath("file2")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), + DebouncedEvent::NoticeWrite(tdir.mkpath("file2")), // TODO not necessary + DebouncedEvent::Rename(tdir.mkpath("file1"), tdir.mkpath("file2")), + DebouncedEvent::Write(tdir.mkpath("file2")), + ] + ); } #[test] fn modify_rename_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1", - ]); + tdir.create_all(vec!["file1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.chmod("file1"); tdir.rename("file1", "file2"); - if cfg!(target_os="windows") { + if cfg!(target_os = "windows") { // windows cannot distinguish between chmod and write - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeWrite(tdir.mkpath("file1")), - DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), - DebouncedEvent::Rename(tdir.mkpath("file1"), tdir.mkpath("file2")), - DebouncedEvent::Write(tdir.mkpath("file2")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeWrite(tdir.mkpath("file1")), + DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), + DebouncedEvent::Rename(tdir.mkpath("file1"), tdir.mkpath("file2")), + DebouncedEvent::Write(tdir.mkpath("file2")), + ] + ); } else { - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), - DebouncedEvent::Rename(tdir.mkpath("file1"), tdir.mkpath("file2")), - DebouncedEvent::Chmod(tdir.mkpath("file2")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), + DebouncedEvent::Rename(tdir.mkpath("file1"), tdir.mkpath("file2")), + DebouncedEvent::Chmod(tdir.mkpath("file2")), + ] + ); } } @@ -548,34 +637,41 @@ fn modify_rename_file() { fn rename_modify_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1", - ]); + tdir.create_all(vec!["file1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.rename("file1", "file2"); sleep_macos(10); tdir.chmod("file2"); - if cfg!(target_os="windows") { + if cfg!(target_os = "windows") { // windows cannot distinguish between chmod and write - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), - DebouncedEvent::NoticeWrite(tdir.mkpath("file2")), // TODO unnecessary - DebouncedEvent::Rename(tdir.mkpath("file1"), tdir.mkpath("file2")), - DebouncedEvent::Write(tdir.mkpath("file2")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), + DebouncedEvent::NoticeWrite(tdir.mkpath("file2")), // TODO unnecessary + DebouncedEvent::Rename(tdir.mkpath("file1"), tdir.mkpath("file2")), + DebouncedEvent::Write(tdir.mkpath("file2")), + ] + ); } else { - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), - DebouncedEvent::Rename(tdir.mkpath("file1"), tdir.mkpath("file2")), - DebouncedEvent::Chmod(tdir.mkpath("file2")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), + DebouncedEvent::Rename(tdir.mkpath("file1"), tdir.mkpath("file2")), + DebouncedEvent::Chmod(tdir.mkpath("file2")), + ] + ); } } @@ -583,48 +679,56 @@ fn rename_modify_file() { fn rename_rename_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1", - ]); + tdir.create_all(vec!["file1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.rename("file1", "file2"); sleep_macos(10); tdir.rename("file2", "file3"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), - DebouncedEvent::Rename(tdir.mkpath("file1"), tdir.mkpath("file3")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), + DebouncedEvent::Rename(tdir.mkpath("file1"), tdir.mkpath("file3")), + ] + ); } #[test] fn write_delete_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1", - ]); + tdir.create_all(vec!["file1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.write("file1"); tdir.remove("file1"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeWrite(tdir.mkpath("file1")), - DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), - DebouncedEvent::Remove(tdir.mkpath("file1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeWrite(tdir.mkpath("file1")), + DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), + DebouncedEvent::Remove(tdir.mkpath("file1")), + ] + ); } #[test] @@ -634,14 +738,18 @@ fn create_directory() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("dir1"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::Create(tdir.mkpath("dir1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![DebouncedEvent::Create(tdir.mkpath("dir1")),] + ); } // https://github.com/passcod/notify/issues/124 @@ -652,8 +760,11 @@ fn create_directory_watch_subdirectories() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("dir1"); tdir.create("dir1/dir2"); @@ -662,17 +773,24 @@ fn create_directory_watch_subdirectories() { tdir.create("dir1/dir2/file1"); - if cfg!(target_os="linux") { - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::Create(tdir.mkpath("dir1")), - DebouncedEvent::Create(tdir.mkpath("dir1/dir2/file1")), - ]); - } else {/* */ - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::Create(tdir.mkpath("dir1")), - DebouncedEvent::Create(tdir.mkpath("dir1/dir2")), - DebouncedEvent::Create(tdir.mkpath("dir1/dir2/file1")), - ]); + if cfg!(target_os = "linux") { + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::Create(tdir.mkpath("dir1")), + DebouncedEvent::Create(tdir.mkpath("dir1/dir2/file1")), + ] + ); + } else { + /* */ + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::Create(tdir.mkpath("dir1")), + DebouncedEvent::Create(tdir.mkpath("dir1/dir2")), + DebouncedEvent::Create(tdir.mkpath("dir1/dir2/file1")), + ] + ); } } @@ -680,28 +798,33 @@ fn create_directory_watch_subdirectories() { fn modify_directory() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1", - ]); + tdir.create_all(vec!["dir1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.chmod("dir1"); - if cfg!(target_os="windows") { + if cfg!(target_os = "windows") { // windows cannot distinguish between chmod and write - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeWrite(tdir.mkpath("dir1")), - DebouncedEvent::Write(tdir.mkpath("dir1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeWrite(tdir.mkpath("dir1")), + DebouncedEvent::Write(tdir.mkpath("dir1")), + ] + ); } else { - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::Chmod(tdir.mkpath("dir1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![DebouncedEvent::Chmod(tdir.mkpath("dir1")),] + ); } } @@ -709,44 +832,52 @@ fn modify_directory() { fn delete_directory() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1", - ]); + tdir.create_all(vec!["dir1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.remove("dir1"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), - DebouncedEvent::Remove(tdir.mkpath("dir1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), + DebouncedEvent::Remove(tdir.mkpath("dir1")), + ] + ); } #[test] fn rename_directory() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1", - ]); + tdir.create_all(vec!["dir1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.rename("dir1", "dir2"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), - DebouncedEvent::Rename(tdir.mkpath("dir1"), tdir.mkpath("dir2")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), + DebouncedEvent::Rename(tdir.mkpath("dir1"), tdir.mkpath("dir2")), + ] + ); } #[test] @@ -756,15 +887,19 @@ fn create_modify_directory() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("dir1"); tdir.chmod("dir1"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::Create(tdir.mkpath("dir1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![DebouncedEvent::Create(tdir.mkpath("dir1")),] + ); } #[test] @@ -774,8 +909,11 @@ fn create_delete_directory() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("dir1"); sleep_macos(10); @@ -788,24 +926,28 @@ fn create_delete_directory() { fn delete_create_directory() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1", - ]); + tdir.create_all(vec!["dir1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.remove("dir1"); sleep_macos(10); tdir.create("dir1"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), - DebouncedEvent::Write(tdir.mkpath("dir1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), + DebouncedEvent::Write(tdir.mkpath("dir1")), + ] + ); } #[test] @@ -815,15 +957,19 @@ fn create_rename_directory() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("dir1"); tdir.rename("dir1", "dir2"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::Create(tdir.mkpath("dir2")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![DebouncedEvent::Create(tdir.mkpath("dir2")),] + ); } #[test] @@ -833,8 +979,11 @@ fn create_rename_delete_directory() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("dir1"); sleep_macos(10); @@ -849,34 +998,41 @@ fn create_rename_delete_directory() { #[cfg_attr(target_os = "windows", ignore)] fn create_rename_overwrite_directory() { // overwriting directories doesn't work on windows - if cfg!(target_os="windows") { + if cfg!(target_os = "windows") { panic!("cannot overwrite directory on windows"); } let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir2", - ]); + tdir.create_all(vec!["dir2"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("dir1"); tdir.rename("dir1", "dir2"); - if cfg!(target_os="macos") { - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("dir2")), // even though the directory is being overwritten, that can't be detected - DebouncedEvent::Create(tdir.mkpath("dir2")), // even though the directory is being overwritten, that can't be detected - ]); + if cfg!(target_os = "macos") { + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("dir2")), // even though the directory is being overwritten, that can't be detected + DebouncedEvent::Create(tdir.mkpath("dir2")), // even though the directory is being overwritten, that can't be detected + ] + ); } else { - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::Create(tdir.mkpath("dir2")), // even though the directory is being overwritten, that can't be detected - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::Create(tdir.mkpath("dir2")), // even though the directory is being overwritten, that can't be detected + ] + ); } } @@ -884,34 +1040,41 @@ fn create_rename_overwrite_directory() { fn modify_rename_directory() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1", - ]); + tdir.create_all(vec!["dir1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.chmod("dir1"); tdir.chmod("dir1"); // needed by macOS tdir.rename("dir1", "dir2"); - if cfg!(target_os="windows") { + if cfg!(target_os = "windows") { // windows cannot distinguish between chmod and write - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeWrite(tdir.mkpath("dir1")), - DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), - DebouncedEvent::Rename(tdir.mkpath("dir1"), tdir.mkpath("dir2")), - DebouncedEvent::Write(tdir.mkpath("dir2")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeWrite(tdir.mkpath("dir1")), + DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), + DebouncedEvent::Rename(tdir.mkpath("dir1"), tdir.mkpath("dir2")), + DebouncedEvent::Write(tdir.mkpath("dir2")), + ] + ); } else { - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), - DebouncedEvent::Rename(tdir.mkpath("dir1"), tdir.mkpath("dir2")), - DebouncedEvent::Chmod(tdir.mkpath("dir2")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), + DebouncedEvent::Rename(tdir.mkpath("dir1"), tdir.mkpath("dir2")), + DebouncedEvent::Chmod(tdir.mkpath("dir2")), + ] + ); } } @@ -919,15 +1082,16 @@ fn modify_rename_directory() { fn rename_modify_directory() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1", - ]); + tdir.create_all(vec!["dir1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.rename("dir1", "dir2"); sleep_macos(10); @@ -935,31 +1099,41 @@ fn rename_modify_directory() { let actual = recv_events_debounced(&rx); - if cfg!(target_os="windows") { + if cfg!(target_os = "windows") { // windows cannot distinguish between chmod and write - assert_eq!(actual, vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), - DebouncedEvent::NoticeWrite(tdir.mkpath("dir2")), // TODO unnecessary - DebouncedEvent::Rename(tdir.mkpath("dir1"), tdir.mkpath("dir2")), - DebouncedEvent::Write(tdir.mkpath("dir2")), - ]); - } else if cfg!(target_os="linux") { - assert_eq_any!(actual, vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), - DebouncedEvent::Rename(tdir.mkpath("dir1"), tdir.mkpath("dir2")), - DebouncedEvent::Chmod(tdir.mkpath("dir2")), - ], vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), - DebouncedEvent::Rename(tdir.mkpath("dir1"), tdir.mkpath("dir2")), - DebouncedEvent::Chmod(tdir.mkpath("dir2")), - DebouncedEvent::Chmod(tdir.mkpath("dir1")), // excessive chmod event - ]); + assert_eq!( + actual, + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), + DebouncedEvent::NoticeWrite(tdir.mkpath("dir2")), // TODO unnecessary + DebouncedEvent::Rename(tdir.mkpath("dir1"), tdir.mkpath("dir2")), + DebouncedEvent::Write(tdir.mkpath("dir2")), + ] + ); + } else if cfg!(target_os = "linux") { + assert_eq_any!( + actual, + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), + DebouncedEvent::Rename(tdir.mkpath("dir1"), tdir.mkpath("dir2")), + DebouncedEvent::Chmod(tdir.mkpath("dir2")), + ], + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), + DebouncedEvent::Rename(tdir.mkpath("dir1"), tdir.mkpath("dir2")), + DebouncedEvent::Chmod(tdir.mkpath("dir2")), + DebouncedEvent::Chmod(tdir.mkpath("dir1")), // excessive chmod event + ] + ); } else { - assert_eq!(actual, vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), - DebouncedEvent::Rename(tdir.mkpath("dir1"), tdir.mkpath("dir2")), - DebouncedEvent::Chmod(tdir.mkpath("dir2")), - ]); + assert_eq!( + actual, + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), + DebouncedEvent::Rename(tdir.mkpath("dir1"), tdir.mkpath("dir2")), + DebouncedEvent::Chmod(tdir.mkpath("dir2")), + ] + ); } } @@ -967,56 +1141,67 @@ fn rename_modify_directory() { fn rename_rename_directory() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1", - ]); + tdir.create_all(vec!["dir1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.rename("dir1", "dir2"); sleep_macos(10); tdir.rename("dir2", "dir3"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), - DebouncedEvent::Rename(tdir.mkpath("dir1"), tdir.mkpath("dir3")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), + DebouncedEvent::Rename(tdir.mkpath("dir1"), tdir.mkpath("dir3")), + ] + ); } #[test] fn modify_delete_directory() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1", - ]); + tdir.create_all(vec!["dir1"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.chmod("dir1"); tdir.chmod("dir1"); // needed by windows tdir.remove("dir1"); - if cfg!(target_os="windows") { + if cfg!(target_os = "windows") { // windows cannot distinguish between chmod and write - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeWrite(tdir.mkpath("dir1")), - DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), - DebouncedEvent::Remove(tdir.mkpath("dir1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeWrite(tdir.mkpath("dir1")), + DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), + DebouncedEvent::Remove(tdir.mkpath("dir1")), + ] + ); } else { - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), - DebouncedEvent::Remove(tdir.mkpath("dir1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("dir1")), + DebouncedEvent::Remove(tdir.mkpath("dir1")), + ] + ); } } @@ -1027,16 +1212,16 @@ fn modify_delete_directory() { fn move_in_directory_watch_subdirectories() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "watch_dir", - "dir1/dir2", - ]); + tdir.create_all(vec!["watch_dir", "dir1/dir2"]); sleep_macos(35_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("watch_dir"), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("watch_dir"), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.rename("dir1", "watch_dir/dir1"); @@ -1044,10 +1229,13 @@ fn move_in_directory_watch_subdirectories() { tdir.create("watch_dir/dir1/dir2/file1"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::Create(tdir.mkpath("watch_dir/dir1")), - DebouncedEvent::Create(tdir.mkpath("watch_dir/dir1/dir2/file1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::Create(tdir.mkpath("watch_dir/dir1")), + DebouncedEvent::Create(tdir.mkpath("watch_dir/dir1/dir2/file1")), + ] + ); } // https://github.com/passcod/notify/issues/129 @@ -1060,8 +1248,11 @@ fn rename_create_remove_temp_file() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_macos(10); tdir.rename("file1", "file2"); @@ -1069,10 +1260,13 @@ fn rename_create_remove_temp_file() { tdir.create("file1"); tdir.remove("file2"); - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), - DebouncedEvent::Create(tdir.mkpath("file1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), + DebouncedEvent::Create(tdir.mkpath("file1")), + ] + ); } #[test] @@ -1085,8 +1279,11 @@ fn rename_rename_remove_temp_file() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_macos(10); tdir.rename("file1", "file2"); @@ -1095,27 +1292,34 @@ fn rename_rename_remove_temp_file() { sleep_macos(10); tdir.remove("file2"); - if cfg!(target_os="windows") { - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), - DebouncedEvent::NoticeRemove(tdir.mkpath("file3")), - DebouncedEvent::NoticeWrite(tdir.mkpath("file1")), - DebouncedEvent::Rename(tdir.mkpath("file3"), tdir.mkpath("file1")), - DebouncedEvent::Write(tdir.mkpath("file1")), - ]); + if cfg!(target_os = "windows") { + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), + DebouncedEvent::NoticeRemove(tdir.mkpath("file3")), + DebouncedEvent::NoticeWrite(tdir.mkpath("file1")), + DebouncedEvent::Rename(tdir.mkpath("file3"), tdir.mkpath("file1")), + DebouncedEvent::Write(tdir.mkpath("file1")), + ] + ); } else { - assert_eq!(recv_events_debounced(&rx), vec![ - DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), - DebouncedEvent::NoticeRemove(tdir.mkpath("file3")), - DebouncedEvent::Rename(tdir.mkpath("file3"), tdir.mkpath("file1")), - ]); + assert_eq!( + recv_events_debounced(&rx), + vec![ + DebouncedEvent::NoticeRemove(tdir.mkpath("file1")), + DebouncedEvent::NoticeRemove(tdir.mkpath("file3")), + DebouncedEvent::Rename(tdir.mkpath("file3"), tdir.mkpath("file1")), + ] + ); } } #[test] fn watcher_terminates() { let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)).expect("failed to create debounced watcher"); + let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_millis(DELAY_MS)) + .expect("failed to create debounced watcher"); let thread = thread::spawn(move || { for e in rx.into_iter() { println!("{:?}", e); diff --git a/tests/event_path.rs b/tests/event_path.rs index 14cfabc1..e511412f 100644 --- a/tests/event_path.rs +++ b/tests/event_path.rs @@ -4,23 +4,23 @@ extern crate tempdir; mod utils; use notify::*; -use std::sync::mpsc; -use tempdir::TempDir; use std::env; use std::path::PathBuf; +use std::sync::mpsc; use std::sync::mpsc::Receiver; +use tempdir::TempDir; use utils::*; const NETWORK_PATH: &'static str = ""; // eg.: \\\\MY-PC\\Users\\MyName #[cfg(target_os = "windows")] -fn recv_events_simple(rx: &Receiver) -> Vec<(PathBuf, Op, Option)> { +fn recv_events_simple(rx: &Receiver) -> Vec<(PathBuf, Op, Option)> { recv_events(&rx) } #[cfg(target_os = "macos")] -fn recv_events_simple(rx: &Receiver) -> Vec<(PathBuf, Op, Option)> { +fn recv_events_simple(rx: &Receiver) -> Vec<(PathBuf, Op, Option)> { let mut events = Vec::new(); for (path, op, cookie) in inflate_events(recv_events(&rx)) { if op == (op::Op::CREATE | op::Op::WRITE) { @@ -33,7 +33,7 @@ fn recv_events_simple(rx: &Receiver) -> Vec<(PathBuf, Op, Option } #[cfg(target_os = "linux")] -fn recv_events_simple(rx: &Receiver) -> Vec<(PathBuf, Op, Option)> { +fn recv_events_simple(rx: &Receiver) -> Vec<(PathBuf, Op, Option)> { let mut events = recv_events(rx); events.retain(|&(_, op, _)| op != op::Op::CLOSE_WRITE); events @@ -42,7 +42,8 @@ fn recv_events_simple(rx: &Receiver) -> Vec<(PathBuf, Op, Option #[test] fn watch_relative() { // both of the following tests set the same environment variable, so they must not run in parallel - { // watch_relative_directory + { + // watch_relative_directory let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); tdir.create("dir1"); @@ -51,84 +52,109 @@ fn watch_relative() { env::set_current_dir(tdir.path()).expect("failed to change working directory"); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch("dir1", RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch("dir1", RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.create("dir1/file1"); if cfg!(target_os = "macos") { - assert_eq!(recv_events_simple(&rx), vec![ - (tdir.mkpath("dir1/file1"), op::Op::CREATE, None), // fsevents always returns canonicalized paths - ]); + assert_eq!( + recv_events_simple(&rx), + vec![ + (tdir.mkpath("dir1/file1"), op::Op::CREATE, None), // fsevents always returns canonicalized paths + ] + ); } else { - assert_eq!(recv_events_simple(&rx), vec![ - (tdir.path().join("dir1/file1"), op::Op::CREATE, None), - ]); + assert_eq!( + recv_events_simple(&rx), + vec![(tdir.path().join("dir1/file1"), op::Op::CREATE, None),] + ); } } - { // watch_relative_file + { + // watch_relative_file let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); tdir.create("file1"); env::set_current_dir(tdir.path()).expect("failed to change working directory"); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch("file1", RecursiveMode::Recursive).expect("failed to watch file"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch("file1", RecursiveMode::Recursive) + .expect("failed to watch file"); sleep_windows(100); tdir.write("file1"); if cfg!(target_os = "macos") { - assert_eq!(recv_events_simple(&rx), vec![ - (tdir.mkpath("file1"), op::Op::WRITE, None), // fsevents always returns canonicalized paths - ]); + assert_eq!( + recv_events_simple(&rx), + vec![ + (tdir.mkpath("file1"), op::Op::WRITE, None), // fsevents always returns canonicalized paths + ] + ); } else { - assert_eq!(recv_events_simple(&rx), vec![ - (tdir.path().join("file1"), op::Op::WRITE, None), - ]); + assert_eq!( + recv_events_simple(&rx), + vec![(tdir.path().join("file1"), op::Op::WRITE, None),] + ); } } - if cfg!(target_os = "windows") && !NETWORK_PATH.is_empty() - { // watch_relative_network_directory - let tdir = TempDir::new_in(NETWORK_PATH, "temp_dir").expect("failed to create temporary directory"); + if cfg!(target_os = "windows") && !NETWORK_PATH.is_empty() { + // watch_relative_network_directory + let tdir = TempDir::new_in(NETWORK_PATH, "temp_dir") + .expect("failed to create temporary directory"); tdir.create("dir1"); env::set_current_dir(tdir.path()).expect("failed to change working directory"); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch("dir1", RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch("dir1", RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.create("dir1/file1"); - assert_eq!(recv_events_simple(&rx), vec![ - (tdir.path().join("dir1/file1"), op::Op::CREATE, None), - ]); + assert_eq!( + recv_events_simple(&rx), + vec![(tdir.path().join("dir1/file1"), op::Op::CREATE, None),] + ); } - if cfg!(target_os = "windows") && !NETWORK_PATH.is_empty() - { // watch_relative_network_file - let tdir = TempDir::new_in(NETWORK_PATH, "temp_dir").expect("failed to create temporary directory"); + if cfg!(target_os = "windows") && !NETWORK_PATH.is_empty() { + // watch_relative_network_file + let tdir = TempDir::new_in(NETWORK_PATH, "temp_dir") + .expect("failed to create temporary directory"); tdir.create("file1"); env::set_current_dir(tdir.path()).expect("failed to change working directory"); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch("file1", RecursiveMode::Recursive).expect("failed to watch file"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch("file1", RecursiveMode::Recursive) + .expect("failed to watch file"); sleep_windows(100); tdir.write("file1"); - assert_eq!(recv_events_simple(&rx), vec![ - (tdir.path().join("file1"), op::Op::WRITE, None), - ]); + assert_eq!( + recv_events_simple(&rx), + vec![(tdir.path().join("file1"), op::Op::WRITE, None),] + ); } } @@ -141,21 +167,28 @@ fn watch_absolute_directory() { let watch_path = tdir.path().join("dir1"); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(&watch_path, RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(&watch_path, RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.create("dir1/file1"); if cfg!(target_os = "macos") { - assert_eq!(recv_events_simple(&rx), vec![ - (tdir.mkpath("dir1/file1"), op::Op::CREATE, None), // fsevents always returns canonicalized paths - ]); + assert_eq!( + recv_events_simple(&rx), + vec![ + (tdir.mkpath("dir1/file1"), op::Op::CREATE, None), // fsevents always returns canonicalized paths + ] + ); } else { - assert_eq!(recv_events_simple(&rx), vec![ - (watch_path.join("file1"), op::Op::CREATE, None), - ]); + assert_eq!( + recv_events_simple(&rx), + vec![(watch_path.join("file1"), op::Op::CREATE, None),] + ); } } @@ -166,21 +199,28 @@ fn watch_absolute_file() { let watch_path = tdir.path().join("file1"); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(&watch_path, RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(&watch_path, RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.write("file1"); if cfg!(target_os = "macos") { - assert_eq!(recv_events_simple(&rx), vec![ - (tdir.mkpath("file1"), op::Op::WRITE, None), // fsevents always returns canonicalized paths - ]); + assert_eq!( + recv_events_simple(&rx), + vec![ + (tdir.mkpath("file1"), op::Op::WRITE, None), // fsevents always returns canonicalized paths + ] + ); } else { - assert_eq!(recv_events_simple(&rx), vec![ - (watch_path, op::Op::WRITE, None), - ]); + assert_eq!( + recv_events_simple(&rx), + vec![(watch_path, op::Op::WRITE, None),] + ); } } @@ -188,48 +228,58 @@ fn watch_absolute_file() { #[cfg(target_os = "windows")] fn watch_absolute_network_directory() { if NETWORK_PATH.is_empty() { - return + return; } - let tdir = TempDir::new_in(NETWORK_PATH, "temp_dir").expect("failed to create temporary directory"); + let tdir = + TempDir::new_in(NETWORK_PATH, "temp_dir").expect("failed to create temporary directory"); tdir.create("dir1"); let watch_path = tdir.path().join("dir1"); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(&watch_path, RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(&watch_path, RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.create("dir1/file1"); - assert_eq!(recv_events_simple(&rx), vec![ - (watch_path.join("file1"), op::Op::CREATE, None), - ]); + assert_eq!( + recv_events_simple(&rx), + vec![(watch_path.join("file1"), op::Op::CREATE, None),] + ); } #[test] #[cfg(target_os = "windows")] fn watch_absolute_network_file() { if NETWORK_PATH.is_empty() { - return + return; } - let tdir = TempDir::new_in(NETWORK_PATH, "temp_dir").expect("failed to create temporary directory"); + let tdir = + TempDir::new_in(NETWORK_PATH, "temp_dir").expect("failed to create temporary directory"); tdir.create("file1"); let watch_path = tdir.path().join("file1"); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(&watch_path, RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(&watch_path, RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.write("file1"); - assert_eq!(recv_events_simple(&rx), vec![ - (watch_path, op::Op::WRITE, None), - ]); + assert_eq!( + recv_events_simple(&rx), + vec![(watch_path, op::Op::WRITE, None),] + ); } #[test] @@ -239,18 +289,26 @@ fn watch_canonicalized_directory() { sleep_macos(10); - let watch_path = tdir.path().canonicalize().expect("failed to canonicalize path").join("dir1"); + let watch_path = tdir + .path() + .canonicalize() + .expect("failed to canonicalize path") + .join("dir1"); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(&watch_path, RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(&watch_path, RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.create("dir1/file1"); - assert_eq!(recv_events_simple(&rx), vec![ - (watch_path.join("file1"), op::Op::CREATE, None), - ]); + assert_eq!( + recv_events_simple(&rx), + vec![(watch_path.join("file1"), op::Op::CREATE, None),] + ); } #[test] @@ -258,64 +316,90 @@ fn watch_canonicalized_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); tdir.create("file1"); - let watch_path = tdir.path().canonicalize().expect("failed to canonicalize path").join("file1"); + let watch_path = tdir + .path() + .canonicalize() + .expect("failed to canonicalize path") + .join("file1"); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(&watch_path, RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(&watch_path, RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.write("file1"); - assert_eq!(recv_events_simple(&rx), vec![ - (watch_path, op::Op::WRITE, None), - ]); + assert_eq!( + recv_events_simple(&rx), + vec![(watch_path, op::Op::WRITE, None),] + ); } #[test] #[cfg(target_os = "windows")] fn watch_canonicalized_network_directory() { if NETWORK_PATH.is_empty() { - return + return; } - let tdir = TempDir::new_in(NETWORK_PATH, "temp_dir").expect("failed to create temporary directory"); + let tdir = + TempDir::new_in(NETWORK_PATH, "temp_dir").expect("failed to create temporary directory"); tdir.create("dir1"); - let watch_path = tdir.path().canonicalize().expect("failed to canonicalize path").join("dir1"); + let watch_path = tdir + .path() + .canonicalize() + .expect("failed to canonicalize path") + .join("dir1"); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(&watch_path, RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(&watch_path, RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.create("dir1/file1"); - assert_eq!(recv_events_simple(&rx), vec![ - (watch_path.join("file1"), op::Op::CREATE, None), - ]); + assert_eq!( + recv_events_simple(&rx), + vec![(watch_path.join("file1"), op::Op::CREATE, None),] + ); } #[test] #[cfg(target_os = "windows")] fn watch_canonicalized_network_file() { if NETWORK_PATH.is_empty() { - return + return; } - let tdir = TempDir::new_in(NETWORK_PATH, "temp_dir").expect("failed to create temporary directory"); + let tdir = + TempDir::new_in(NETWORK_PATH, "temp_dir").expect("failed to create temporary directory"); tdir.create("file1"); - let watch_path = tdir.path().canonicalize().expect("failed to canonicalize path").join("file1"); + let watch_path = tdir + .path() + .canonicalize() + .expect("failed to canonicalize path") + .join("file1"); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(&watch_path, RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(&watch_path, RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.write("file1"); - assert_eq!(recv_events_simple(&rx), vec![ - (watch_path, op::Op::WRITE, None), - ]); + assert_eq!( + recv_events_simple(&rx), + vec![(watch_path, op::Op::WRITE, None),] + ); } diff --git a/tests/fsevents.rs b/tests/fsevents.rs index e45bb7c5..c9169342 100644 --- a/tests/fsevents.rs +++ b/tests/fsevents.rs @@ -22,8 +22,11 @@ mod timing_tests { sleep(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("file1"); tdir.remove("file1"); @@ -32,10 +35,13 @@ mod timing_tests { if actual == vec![(tdir.mkpath("file1"), op::CREATE | op::REMOVE, None)] { panic!("single CREATE | REMOVE event"); } else { - assert_eq!(actual, vec![ - (tdir.mkpath("file1"), op::CREATE, None), - (tdir.mkpath("file1"), op::CREATE | op::REMOVE, None), - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("file1"), op::CREATE, None), + (tdir.mkpath("file1"), op::CREATE | op::REMOVE, None), + ] + ); } } @@ -46,14 +52,18 @@ mod timing_tests { sleep(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("file1"); - assert_eq!(recv_events_with_timeout(&rx, 1.0), vec![ - (tdir.mkpath("file1"), op::CREATE, None), - ]); + assert_eq!( + recv_events_with_timeout(&rx, 1.0), + vec![(tdir.mkpath("file1"), op::CREATE, None),] + ); tdir.remove("file1"); @@ -61,9 +71,7 @@ mod timing_tests { if actual == vec![(tdir.mkpath("file1"), op::CREATE | op::REMOVE, None)] { panic!("excessive CREATE event"); } else { - assert_eq!(actual, vec![ - (tdir.mkpath("file1"), op::REMOVE, None), - ]); + assert_eq!(actual, vec![(tdir.mkpath("file1"), op::REMOVE, None),]); } } @@ -74,14 +82,18 @@ mod timing_tests { sleep(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("file1"); - assert_eq!(recv_events_with_timeout(&rx, 2.0), vec![ - (tdir.mkpath("file1"), op::CREATE, None), - ]); + assert_eq!( + recv_events_with_timeout(&rx, 2.0), + vec![(tdir.mkpath("file1"), op::CREATE, None),] + ); tdir.remove("file1"); @@ -89,9 +101,7 @@ mod timing_tests { if actual == vec![(tdir.mkpath("file1"), op::CREATE | op::REMOVE, None)] { panic!("excessive CREATE event"); } else { - assert_eq!(actual, vec![ - (tdir.mkpath("file1"), op::REMOVE, None), - ]); + assert_eq!(actual, vec![(tdir.mkpath("file1"), op::REMOVE, None),]); } } @@ -102,14 +112,18 @@ mod timing_tests { sleep(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("file1"); - assert_eq!(recv_events_with_timeout(&rx, 4.0), vec![ - (tdir.mkpath("file1"), op::CREATE, None), - ]); + assert_eq!( + recv_events_with_timeout(&rx, 4.0), + vec![(tdir.mkpath("file1"), op::CREATE, None),] + ); tdir.remove("file1"); @@ -117,9 +131,7 @@ mod timing_tests { if actual == vec![(tdir.mkpath("file1"), op::CREATE | op::REMOVE, None)] { panic!("excessive CREATE event"); } else { - assert_eq!(actual, vec![ - (tdir.mkpath("file1"), op::REMOVE, None), - ]); + assert_eq!(actual, vec![(tdir.mkpath("file1"), op::REMOVE, None),]); } } @@ -130,14 +142,18 @@ mod timing_tests { sleep(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("file1"); - assert_eq!(recv_events_with_timeout(&rx, 8.0), vec![ - (tdir.mkpath("file1"), op::CREATE, None), - ]); + assert_eq!( + recv_events_with_timeout(&rx, 8.0), + vec![(tdir.mkpath("file1"), op::CREATE, None),] + ); tdir.remove("file1"); @@ -145,9 +161,7 @@ mod timing_tests { if actual == vec![(tdir.mkpath("file1"), op::CREATE | op::REMOVE, None)] { panic!("excessive CREATE event"); } else { - assert_eq!(actual, vec![ - (tdir.mkpath("file1"), op::REMOVE, None), - ]); + assert_eq!(actual, vec![(tdir.mkpath("file1"), op::REMOVE, None),]); } } @@ -158,14 +172,18 @@ mod timing_tests { sleep(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("file1"); - assert_eq!(recv_events_with_timeout(&rx, 16.0), vec![ - (tdir.mkpath("file1"), op::CREATE, None), - ]); + assert_eq!( + recv_events_with_timeout(&rx, 16.0), + vec![(tdir.mkpath("file1"), op::CREATE, None),] + ); tdir.remove("file1"); @@ -173,9 +191,7 @@ mod timing_tests { if actual == vec![(tdir.mkpath("file1"), op::CREATE | op::REMOVE, None)] { panic!("excessive CREATE event"); } else { - assert_eq!(actual, vec![ - (tdir.mkpath("file1"), op::REMOVE, None), - ]); + assert_eq!(actual, vec![(tdir.mkpath("file1"), op::REMOVE, None),]); } } @@ -186,14 +202,18 @@ mod timing_tests { sleep(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("file1"); - assert_eq!(recv_events_with_timeout(&rx, 32.0), vec![ - (tdir.mkpath("file1"), op::CREATE, None), - ]); + assert_eq!( + recv_events_with_timeout(&rx, 32.0), + vec![(tdir.mkpath("file1"), op::CREATE, None),] + ); tdir.remove("file1"); @@ -201,9 +221,7 @@ mod timing_tests { if actual == vec![(tdir.mkpath("file1"), op::CREATE | op::REMOVE, None)] { panic!("excessive CREATE event"); } else { - assert_eq!(actual, vec![ - (tdir.mkpath("file1"), op::REMOVE, None), - ]); + assert_eq!(actual, vec![(tdir.mkpath("file1"), op::REMOVE, None),]); } } @@ -214,14 +232,18 @@ mod timing_tests { sleep(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.create("file1"); - assert_eq!(recv_events_with_timeout(&rx, 64.0), vec![ - (tdir.mkpath("file1"), op::CREATE, None), - ]); + assert_eq!( + recv_events_with_timeout(&rx, 64.0), + vec![(tdir.mkpath("file1"), op::CREATE, None),] + ); tdir.remove("file1"); @@ -229,9 +251,7 @@ mod timing_tests { if actual == vec![(tdir.mkpath("file1"), op::CREATE | op::REMOVE, None)] { panic!("excessive CREATE event"); } else { - assert_eq!(actual, vec![ - (tdir.mkpath("file1"), op::REMOVE, None), - ]); + assert_eq!(actual, vec![(tdir.mkpath("file1"), op::REMOVE, None),]); } } @@ -239,15 +259,16 @@ mod timing_tests { fn fsevents_rename_rename_file_0() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1a", - ]); + tdir.create_all(vec!["file1a"]); sleep(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.rename("file1a", "file1b"); tdir.rename("file1b", "file1c"); @@ -255,26 +276,30 @@ mod timing_tests { let actual = recv_events(&rx); let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 1); - assert_eq!(actual, vec![ - (tdir.mkpath("file1a"), op::CREATE | op::RENAME, None), - (tdir.mkpath("file1b"), op::RENAME, Some(cookies[0])), - (tdir.mkpath("file1c"), op::RENAME, Some(cookies[0])), - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("file1a"), op::CREATE | op::RENAME, None), + (tdir.mkpath("file1b"), op::RENAME, Some(cookies[0])), + (tdir.mkpath("file1c"), op::RENAME, Some(cookies[0])), + ] + ); } #[test] fn fsevents_rename_rename_file_10() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1a", - ]); + tdir.create_all(vec!["file1a"]); sleep(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.rename("file1a", "file1b"); sleep(10); @@ -283,27 +308,35 @@ mod timing_tests { let actual = recv_events(&rx); let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 2); - assert_eq!(actual, vec![ - (tdir.mkpath("file1a"), op::CREATE | op::RENAME, Some(cookies[0])), - (tdir.mkpath("file1b"), op::RENAME, Some(cookies[0])), - (tdir.mkpath("file1b"), op::RENAME, Some(cookies[1])), - (tdir.mkpath("file1c"), op::RENAME, Some(cookies[1])), - ]); + assert_eq!( + actual, + vec![ + ( + tdir.mkpath("file1a"), + op::CREATE | op::RENAME, + Some(cookies[0]) + ), + (tdir.mkpath("file1b"), op::RENAME, Some(cookies[0])), + (tdir.mkpath("file1b"), op::RENAME, Some(cookies[1])), + (tdir.mkpath("file1c"), op::RENAME, Some(cookies[1])), + ] + ); } #[test] fn fsevents_rename_rename_file_20() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1a", - ]); + tdir.create_all(vec!["file1a"]); sleep(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.rename("file1a", "file1b"); sleep(20); @@ -312,27 +345,35 @@ mod timing_tests { let actual = recv_events(&rx); let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 2); - assert_eq!(actual, vec![ - (tdir.mkpath("file1a"), op::CREATE | op::RENAME, Some(cookies[0])), - (tdir.mkpath("file1b"), op::RENAME, Some(cookies[0])), - (tdir.mkpath("file1b"), op::RENAME, Some(cookies[1])), - (tdir.mkpath("file1c"), op::RENAME, Some(cookies[1])), - ]); + assert_eq!( + actual, + vec![ + ( + tdir.mkpath("file1a"), + op::CREATE | op::RENAME, + Some(cookies[0]) + ), + (tdir.mkpath("file1b"), op::RENAME, Some(cookies[0])), + (tdir.mkpath("file1b"), op::RENAME, Some(cookies[1])), + (tdir.mkpath("file1c"), op::RENAME, Some(cookies[1])), + ] + ); } #[test] fn fsevents_rename_rename_back_file_0() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1a", - ]); + tdir.create_all(vec!["file1a"]); sleep(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.rename("file1a", "file1b"); tdir.rename("file1b", "file1a"); @@ -340,25 +381,33 @@ mod timing_tests { let actual = recv_events(&rx); let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 1); - assert_eq!(actual, vec![ - (tdir.mkpath("file1b"), op::RENAME, Some(cookies[0])), - (tdir.mkpath("file1a"), op::CREATE | op::RENAME, Some(cookies[0])), - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("file1b"), op::RENAME, Some(cookies[0])), + ( + tdir.mkpath("file1a"), + op::CREATE | op::RENAME, + Some(cookies[0]) + ), + ] + ); } #[test] fn fsevents_rename_rename_back_file_10() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1a", - ]); + tdir.create_all(vec!["file1a"]); sleep(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.rename("file1a", "file1b"); sleep(10); @@ -367,27 +416,39 @@ mod timing_tests { let actual = recv_events(&rx); let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 2); - assert_eq!(actual, vec![ - (tdir.mkpath("file1a"), op::CREATE | op::RENAME, Some(cookies[0])), - (tdir.mkpath("file1b"), op::RENAME, Some(cookies[0])), - (tdir.mkpath("file1b"), op::RENAME, Some(cookies[1])), - (tdir.mkpath("file1a"), op::CREATE | op::RENAME, Some(cookies[1])), - ]); + assert_eq!( + actual, + vec![ + ( + tdir.mkpath("file1a"), + op::CREATE | op::RENAME, + Some(cookies[0]) + ), + (tdir.mkpath("file1b"), op::RENAME, Some(cookies[0])), + (tdir.mkpath("file1b"), op::RENAME, Some(cookies[1])), + ( + tdir.mkpath("file1a"), + op::CREATE | op::RENAME, + Some(cookies[1]) + ), + ] + ); } #[test] fn fsevents_rename_rename_back_file_20() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1a", - ]); + tdir.create_all(vec!["file1a"]); sleep(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.rename("file1a", "file1b"); sleep(20); @@ -396,27 +457,39 @@ mod timing_tests { let actual = recv_events(&rx); let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 2); - assert_eq!(actual, vec![ - (tdir.mkpath("file1a"), op::CREATE | op::RENAME, Some(cookies[0])), - (tdir.mkpath("file1b"), op::RENAME, Some(cookies[0])), - (tdir.mkpath("file1b"), op::RENAME, Some(cookies[1])), - (tdir.mkpath("file1a"), op::CREATE | op::RENAME, Some(cookies[1])), - ]); + assert_eq!( + actual, + vec![ + ( + tdir.mkpath("file1a"), + op::CREATE | op::RENAME, + Some(cookies[0]) + ), + (tdir.mkpath("file1b"), op::RENAME, Some(cookies[0])), + (tdir.mkpath("file1b"), op::RENAME, Some(cookies[1])), + ( + tdir.mkpath("file1a"), + op::CREATE | op::RENAME, + Some(cookies[1]) + ), + ] + ); } #[test] fn fsevents_rename_rename_back_file_sleep() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1a", - ]); + tdir.create_all(vec!["file1a"]); sleep(40_000); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.rename("file1a", "file1b"); sleep(10); @@ -425,11 +498,14 @@ mod timing_tests { let actual = recv_events(&rx); let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 2); - assert_eq!(actual, vec![ - (tdir.mkpath("file1a"), op::RENAME, Some(cookies[0])), - (tdir.mkpath("file1b"), op::RENAME, Some(cookies[0])), - (tdir.mkpath("file1b"), op::RENAME, Some(cookies[1])), - (tdir.mkpath("file1a"), op::RENAME, Some(cookies[1])), - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("file1a"), op::RENAME, Some(cookies[0])), + (tdir.mkpath("file1b"), op::RENAME, Some(cookies[0])), + (tdir.mkpath("file1b"), op::RENAME, Some(cookies[1])), + (tdir.mkpath("file1a"), op::RENAME, Some(cookies[1])), + ] + ); } } diff --git a/tests/notify.rs b/tests/notify.rs index c2136e15..12280bf8 100644 --- a/tests/notify.rs +++ b/tests/notify.rs @@ -4,28 +4,32 @@ extern crate tempdir; mod utils; use notify::*; -use std::sync::mpsc; use std::path::PathBuf; +use std::sync::mpsc; use tempdir::TempDir; use utils::*; #[test] fn test_inflate_events() { - assert_eq!(inflate_events(vec![ - (PathBuf::from("file1"), op::Op::CREATE, None), - (PathBuf::from("file1"), op::Op::WRITE, None), - ]), vec![ - (PathBuf::from("file1"), op::Op::CREATE | op::Op::WRITE, None), - ]); - - assert_eq!(inflate_events(vec![ - (PathBuf::from("file1"), op::Op::RENAME, Some(1)), - (PathBuf::from("file1"), op::Op::RENAME, Some(2)), - ]), vec![ - (PathBuf::from("file1"), op::Op::RENAME, Some(1)), - (PathBuf::from("file1"), op::Op::RENAME, Some(2)), - ]); + assert_eq!( + inflate_events(vec![ + (PathBuf::from("file1"), op::Op::CREATE, None), + (PathBuf::from("file1"), op::Op::WRITE, None), + ]), + vec![(PathBuf::from("file1"), op::Op::CREATE | op::Op::WRITE, None),] + ); + + assert_eq!( + inflate_events(vec![ + (PathBuf::from("file1"), op::Op::RENAME, Some(1)), + (PathBuf::from("file1"), op::Op::RENAME, Some(2)), + ]), + vec![ + (PathBuf::from("file1"), op::Op::RENAME, Some(1)), + (PathBuf::from("file1"), op::Op::RENAME, Some(2)), + ] + ); } #[test] @@ -36,26 +40,34 @@ fn create_file() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.create("file1"); - if cfg!(target_os="macos") { - assert_eq!(inflate_events(recv_events(&rx)), vec![ - (tdir.mkpath("file1"), op::Op::CREATE, None), - ]); - } else if cfg!(target_os="windows") { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("file1"), op::Op::CREATE, None) - ]); + if cfg!(target_os = "macos") { + assert_eq!( + inflate_events(recv_events(&rx)), + vec![(tdir.mkpath("file1"), op::Op::CREATE, None),] + ); + } else if cfg!(target_os = "windows") { + assert_eq!( + recv_events(&rx), + vec![(tdir.mkpath("file1"), op::Op::CREATE, None)] + ); } else { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("file1"), op::Op::CREATE, None), - (tdir.mkpath("file1"), op::Op::CLOSE_WRITE, None) - ]); + assert_eq!( + recv_events(&rx), + vec![ + (tdir.mkpath("file1"), op::Op::CREATE, None), + (tdir.mkpath("file1"), op::Op::CLOSE_WRITE, None) + ] + ); } } @@ -63,33 +75,41 @@ fn create_file() { fn write_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1" - ]); + tdir.create_all(vec!["file1"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.write("file1"); - if cfg!(target_os="macos") { - assert_eq!(inflate_events(recv_events(&rx)), vec![ - (tdir.mkpath("file1"), op::Op::CREATE | op::Op::WRITE, None), // excessive create event - ]); - } else if cfg!(target_os="windows") { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("file1"), op::Op::WRITE, None) - ]); + if cfg!(target_os = "macos") { + assert_eq!( + inflate_events(recv_events(&rx)), + vec![ + (tdir.mkpath("file1"), op::Op::CREATE | op::Op::WRITE, None), // excessive create event + ] + ); + } else if cfg!(target_os = "windows") { + assert_eq!( + recv_events(&rx), + vec![(tdir.mkpath("file1"), op::Op::WRITE, None)] + ); } else { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("file1"), op::Op::WRITE, None), - (tdir.mkpath("file1"), op::Op::CLOSE_WRITE, None) - ]); + assert_eq!( + recv_events(&rx), + vec![ + (tdir.mkpath("file1"), op::Op::WRITE, None), + (tdir.mkpath("file1"), op::Op::CLOSE_WRITE, None) + ] + ); } } @@ -98,15 +118,16 @@ fn write_file() { fn modify_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1" - ]); + tdir.create_all(vec!["file1"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); @@ -114,20 +135,23 @@ fn modify_file() { sleep_macos(10); - if cfg!(target_os="windows") { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("file1"), op::Op::WRITE, None), - ]); + if cfg!(target_os = "windows") { + assert_eq!( + recv_events(&rx), + vec![(tdir.mkpath("file1"), op::Op::WRITE, None),] + ); panic!("windows cannot distinguish between chmod and write"); - } else if cfg!(target_os="macos") { - assert_eq!(inflate_events(recv_events(&rx)), vec![ - (tdir.mkpath("file1"), op::Op::CHMOD | op::Op::CREATE, None), - ]); + } else if cfg!(target_os = "macos") { + assert_eq!( + inflate_events(recv_events(&rx)), + vec![(tdir.mkpath("file1"), op::Op::CHMOD | op::Op::CREATE, None),] + ); panic!("macos cannot distinguish between chmod and create"); } else { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("file1"), op::Op::CHMOD, None), - ]); + assert_eq!( + recv_events(&rx), + vec![(tdir.mkpath("file1"), op::Op::CHMOD, None),] + ); } } @@ -135,28 +159,33 @@ fn modify_file() { fn delete_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1" - ]); + tdir.create_all(vec!["file1"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.remove("file1"); - if cfg!(target_os="macos") { - assert_eq!(inflate_events(recv_events(&rx)), vec![ - (tdir.mkpath("file1"), op::Op::CREATE | op::Op::REMOVE, None), // excessive create event - ]); + if cfg!(target_os = "macos") { + assert_eq!( + inflate_events(recv_events(&rx)), + vec![ + (tdir.mkpath("file1"), op::Op::CREATE | op::Op::REMOVE, None), // excessive create event + ] + ); } else { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("file1"), op::Op::REMOVE, None), - ]); + assert_eq!( + recv_events(&rx), + vec![(tdir.mkpath("file1"), op::Op::REMOVE, None),] + ); } } @@ -164,36 +193,47 @@ fn delete_file() { fn rename_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1a" - ]); + tdir.create_all(vec!["file1a"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.rename("file1a", "file1b"); - if cfg!(target_os="macos") { + if cfg!(target_os = "macos") { let actual = inflate_events(recv_events(&rx)); let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 1); - assert_eq!(actual, vec![ - (tdir.mkpath("file1a"), op::Op::CREATE | op::Op::RENAME, Some(cookies[0])), // excessive create event - (tdir.mkpath("file1b"), op::Op::RENAME, Some(cookies[0])) - ]); + assert_eq!( + actual, + vec![ + ( + tdir.mkpath("file1a"), + op::Op::CREATE | op::Op::RENAME, + Some(cookies[0]) + ), // excessive create event + (tdir.mkpath("file1b"), op::Op::RENAME, Some(cookies[0])) + ] + ); } else { let actual = recv_events(&rx); let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 1); - assert_eq!(actual, vec![ - (tdir.mkpath("file1a"), op::Op::RENAME, Some(cookies[0])), - (tdir.mkpath("file1b"), op::Op::RENAME, Some(cookies[0])) - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("file1a"), op::Op::RENAME, Some(cookies[0])), + (tdir.mkpath("file1b"), op::Op::RENAME, Some(cookies[0])) + ] + ); } } @@ -202,37 +242,51 @@ fn rename_file() { fn move_out_create_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "watch_dir/file1" - ]); + tdir.create_all(vec!["watch_dir/file1"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("watch_dir"), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("watch_dir"), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.rename("watch_dir/file1", "file1b"); tdir.create("watch_dir/file1"); - if cfg!(target_os="macos") { - assert_eq!(inflate_events(recv_events(&rx)), vec![ - (tdir.mkpath("watch_dir/file1"), op::Op::CREATE | op::Op::RENAME, None), // fsevent interprets a move_out as a rename event - ]); + if cfg!(target_os = "macos") { + assert_eq!( + inflate_events(recv_events(&rx)), + vec![ + ( + tdir.mkpath("watch_dir/file1"), + op::Op::CREATE | op::Op::RENAME, + None + ), // fsevent interprets a move_out as a rename event + ] + ); panic!("move event should be a remove event; fsevent conflates rename and create events"); - } else if cfg!(target_os="linux") { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("watch_dir/file1"), op::Op::REMOVE, None), - (tdir.mkpath("watch_dir/file1"), op::Op::CREATE, None), - (tdir.mkpath("watch_dir/file1"), op::Op::CLOSE_WRITE, None), - ]); + } else if cfg!(target_os = "linux") { + assert_eq!( + recv_events(&rx), + vec![ + (tdir.mkpath("watch_dir/file1"), op::Op::REMOVE, None), + (tdir.mkpath("watch_dir/file1"), op::Op::CREATE, None), + (tdir.mkpath("watch_dir/file1"), op::Op::CLOSE_WRITE, None), + ] + ); } else { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("watch_dir/file1"), op::Op::REMOVE, None), - (tdir.mkpath("watch_dir/file1"), op::Op::CREATE, None), - ]); + assert_eq!( + recv_events(&rx), + vec![ + (tdir.mkpath("watch_dir/file1"), op::Op::REMOVE, None), + (tdir.mkpath("watch_dir/file1"), op::Op::CREATE, None), + ] + ); } } @@ -244,8 +298,11 @@ fn create_write_modify_file() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); @@ -253,26 +310,37 @@ fn create_write_modify_file() { tdir.write("file1"); tdir.chmod("file1"); - if cfg!(target_os="windows") { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("file1"), op::Op::CREATE, None), - (tdir.mkpath("file1"), op::Op::WRITE, None), - (tdir.mkpath("file1"), op::Op::WRITE, None), - ]); + if cfg!(target_os = "windows") { + assert_eq!( + recv_events(&rx), + vec![ + (tdir.mkpath("file1"), op::Op::CREATE, None), + (tdir.mkpath("file1"), op::Op::WRITE, None), + (tdir.mkpath("file1"), op::Op::WRITE, None), + ] + ); panic!("windows cannot distinguish between chmod and write"); - } else if cfg!(target_os="macos") { - assert_eq!(inflate_events(recv_events(&rx)), vec![ - (tdir.mkpath("file1"), op::Op::CHMOD | op::Op::CREATE | op::Op::WRITE, None), - ]); + } else if cfg!(target_os = "macos") { + assert_eq!( + inflate_events(recv_events(&rx)), + vec![( + tdir.mkpath("file1"), + op::Op::CHMOD | op::Op::CREATE | op::Op::WRITE, + None + ),] + ); panic!("macos cannot distinguish between chmod and create"); - } else if cfg!(target_os="linux") { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("file1"), op::Op::CREATE, None), - (tdir.mkpath("file1"), op::Op::CLOSE_WRITE, None), - (tdir.mkpath("file1"), op::Op::WRITE, None), - (tdir.mkpath("file1"), op::Op::CLOSE_WRITE, None), - (tdir.mkpath("file1"), op::Op::CHMOD, None), - ]); + } else if cfg!(target_os = "linux") { + assert_eq!( + recv_events(&rx), + vec![ + (tdir.mkpath("file1"), op::Op::CREATE, None), + (tdir.mkpath("file1"), op::Op::CLOSE_WRITE, None), + (tdir.mkpath("file1"), op::Op::WRITE, None), + (tdir.mkpath("file1"), op::Op::CLOSE_WRITE, None), + (tdir.mkpath("file1"), op::Op::CHMOD, None), + ] + ); } } @@ -280,15 +348,16 @@ fn create_write_modify_file() { fn create_rename_overwrite_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1b", - ]); + tdir.create_all(vec!["file1b"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); @@ -297,27 +366,36 @@ fn create_rename_overwrite_file() { let actual = recv_events(&rx); - if cfg!(target_os="windows") { - // Windows interprets a move that overwrites a file as a delete of the source file and a write to the file that is being overwritten - assert_eq!(actual, vec![ - (tdir.mkpath("file1a"), op::Op::CREATE, None), - (tdir.mkpath("file1a"), op::Op::REMOVE, None), - (tdir.mkpath("file1b"), op::Op::WRITE, None) - ]); - } else if cfg!(target_os="macos") { - assert_eq!(inflate_events(actual), vec![ - (tdir.mkpath("file1a"), op::Op::CREATE | op::Op::RENAME, None), - (tdir.mkpath("file1b"), op::Op::CREATE | op::Op::RENAME, None) - ]); + if cfg!(target_os = "windows") { + assert_eq!( + actual, + vec![ + (tdir.mkpath("file1a"), op::Op::CREATE, None), + (tdir.mkpath("file1b"), op::Op::REMOVE, None), + (tdir.mkpath("file1a"), op::Op::RENAME, Some(1)), + (tdir.mkpath("file1b"), op::Op::RENAME, Some(1)) + ] + ); + } else if cfg!(target_os = "macos") { + assert_eq!( + inflate_events(actual), + vec![ + (tdir.mkpath("file1a"), op::Op::CREATE | op::Op::RENAME, None), + (tdir.mkpath("file1b"), op::Op::CREATE | op::Op::RENAME, None) + ] + ); } else { let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 1); - assert_eq!(actual, vec![ - (tdir.mkpath("file1a"), op::Op::CREATE, None), - (tdir.mkpath("file1a"), op::Op::CLOSE_WRITE, None), - (tdir.mkpath("file1a"), op::Op::RENAME, Some(cookies[0])), - (tdir.mkpath("file1b"), op::Op::RENAME, Some(cookies[0])) - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("file1a"), op::Op::CREATE, None), + (tdir.mkpath("file1a"), op::Op::CLOSE_WRITE, None), + (tdir.mkpath("file1a"), op::Op::RENAME, Some(cookies[0])), + (tdir.mkpath("file1b"), op::Op::RENAME, Some(cookies[0])) + ] + ); } } @@ -325,40 +403,47 @@ fn create_rename_overwrite_file() { fn rename_rename_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1a", - ]); + tdir.create_all(vec!["file1a"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.rename("file1a", "file1b"); tdir.rename("file1b", "file1c"); - if cfg!(target_os="macos") { + if cfg!(target_os = "macos") { let actual = inflate_events(recv_events(&rx)); let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 1); - assert_eq!(actual, vec![ - (tdir.mkpath("file1a"), op::Op::CREATE | op::Op::RENAME, None), - (tdir.mkpath("file1b"), op::Op::RENAME, Some(cookies[0])), - (tdir.mkpath("file1c"), op::Op::RENAME, Some(cookies[0])) - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("file1a"), op::Op::CREATE | op::Op::RENAME, None), + (tdir.mkpath("file1b"), op::Op::RENAME, Some(cookies[0])), + (tdir.mkpath("file1c"), op::Op::RENAME, Some(cookies[0])) + ] + ); } else { let actual = recv_events(&rx); let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 2); - assert_eq!(actual, vec![ - (tdir.mkpath("file1a"), op::Op::RENAME, Some(cookies[0])), - (tdir.mkpath("file1b"), op::Op::RENAME, Some(cookies[0])), - (tdir.mkpath("file1b"), op::Op::RENAME, Some(cookies[1])), - (tdir.mkpath("file1c"), op::Op::RENAME, Some(cookies[1])) - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("file1a"), op::Op::RENAME, Some(cookies[0])), + (tdir.mkpath("file1b"), op::Op::RENAME, Some(cookies[0])), + (tdir.mkpath("file1b"), op::Op::RENAME, Some(cookies[1])), + (tdir.mkpath("file1c"), op::Op::RENAME, Some(cookies[1])) + ] + ); } } @@ -369,22 +454,23 @@ fn create_directory() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.create("dir1"); - let actual = if cfg!(target_os="macos") { + let actual = if cfg!(target_os = "macos") { inflate_events(recv_events(&rx)) } else { recv_events(&rx) }; - assert_eq!(actual, vec![ - (tdir.mkpath("dir1"), op::Op::CREATE, None), - ]); + assert_eq!(actual, vec![(tdir.mkpath("dir1"), op::Op::CREATE, None),]); } // https://github.com/passcod/notify/issues/124 @@ -396,8 +482,11 @@ fn create_directory_watch_subdirectories() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); @@ -408,29 +497,38 @@ fn create_directory_watch_subdirectories() { tdir.create("dir1/dir2/file1"); - let actual = if cfg!(target_os="macos") { + let actual = if cfg!(target_os = "macos") { inflate_events(recv_events(&rx)) } else { recv_events(&rx) }; - if cfg!(target_os="linux") { - assert_eq!(actual, vec![ - (tdir.mkpath("dir1"), op::Op::CREATE, None), - (tdir.mkpath("dir1/dir2/file1"), op::Op::CREATE, None), - (tdir.mkpath("dir1/dir2/file1"), op::Op::CLOSE_WRITE, None), - ]); - } else if cfg!(target_os="windows") { - assert_eq!(actual, vec![ - (tdir.mkpath("dir1"), op::Op::CREATE, None), - (tdir.mkpath("dir1/dir2"), op::Op::CREATE, None), - (tdir.mkpath("dir1/dir2/file1"), op::Op::CREATE, None), - ]); + if cfg!(target_os = "linux") { + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1"), op::Op::CREATE, None), + (tdir.mkpath("dir1/dir2/file1"), op::Op::CREATE, None), + (tdir.mkpath("dir1/dir2/file1"), op::Op::CLOSE_WRITE, None), + ] + ); + } else if cfg!(target_os = "windows") { + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1"), op::Op::CREATE, None), + (tdir.mkpath("dir1/dir2"), op::Op::CREATE, None), + (tdir.mkpath("dir1/dir2/file1"), op::Op::CREATE, None), + ] + ); } else { - assert_eq!(actual, vec![ - (tdir.mkpath("dir1"), op::Op::CREATE, None), - (tdir.mkpath("dir1/dir2/file1"), op::Op::CREATE, None), - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1"), op::Op::CREATE, None), + (tdir.mkpath("dir1/dir2/file1"), op::Op::CREATE, None), + ] + ); } } @@ -439,36 +537,42 @@ fn create_directory_watch_subdirectories() { fn modify_directory() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1", - ]); + tdir.create_all(vec!["dir1"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.chmod("dir1"); - if cfg!(target_os="windows") { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("dir1"), op::Op::WRITE, None), - ]); + if cfg!(target_os = "windows") { + assert_eq!( + recv_events(&rx), + vec![(tdir.mkpath("dir1"), op::Op::WRITE, None),] + ); panic!("windows cannot distinguish between chmod and write"); - } else if cfg!(target_os="macos") { - assert_eq!(inflate_events(recv_events(&rx)), vec![ - (tdir.mkpath("dir1"), op::Op::CHMOD | op::Op::CREATE, None), - ]); + } else if cfg!(target_os = "macos") { + assert_eq!( + inflate_events(recv_events(&rx)), + vec![(tdir.mkpath("dir1"), op::Op::CHMOD | op::Op::CREATE, None),] + ); panic!("macos cannot distinguish between chmod and create"); } else { // TODO: emit chmod event only once - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("dir1"), op::Op::CHMOD, None), - (tdir.mkpath("dir1"), op::Op::CHMOD, None), - ]); + assert_eq!( + recv_events(&rx), + vec![ + (tdir.mkpath("dir1"), op::Op::CHMOD, None), + (tdir.mkpath("dir1"), op::Op::CHMOD, None), + ] + ); } } @@ -476,28 +580,33 @@ fn modify_directory() { fn delete_directory() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1", - ]); + tdir.create_all(vec!["dir1"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.remove("dir1"); - if cfg!(target_os="macos") { - assert_eq!(inflate_events(recv_events(&rx)), vec![ - (tdir.mkpath("dir1"), op::Op::CREATE | op::Op::REMOVE, None), // excessive create event - ]); + if cfg!(target_os = "macos") { + assert_eq!( + inflate_events(recv_events(&rx)), + vec![ + (tdir.mkpath("dir1"), op::Op::CREATE | op::Op::REMOVE, None), // excessive create event + ] + ); } else { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("dir1"), op::Op::REMOVE, None), - ]); + assert_eq!( + recv_events(&rx), + vec![(tdir.mkpath("dir1"), op::Op::REMOVE, None),] + ); } } @@ -505,36 +614,47 @@ fn delete_directory() { fn rename_directory() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1a", - ]); + tdir.create_all(vec!["dir1a"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.rename("dir1a", "dir1b"); - if cfg!(target_os="macos") { + if cfg!(target_os = "macos") { let actual = inflate_events(recv_events(&rx)); let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 1); - assert_eq!(actual, vec![ - (tdir.mkpath("dir1a"), op::Op::CREATE | op::Op::RENAME, Some(cookies[0])), // excessive create event - (tdir.mkpath("dir1b"), op::Op::RENAME, Some(cookies[0])), - ]); + assert_eq!( + actual, + vec![ + ( + tdir.mkpath("dir1a"), + op::Op::CREATE | op::Op::RENAME, + Some(cookies[0]) + ), // excessive create event + (tdir.mkpath("dir1b"), op::Op::RENAME, Some(cookies[0])), + ] + ); } else { let actual = recv_events(&rx); let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 1); - assert_eq!(actual, vec![ - (tdir.mkpath("dir1a"), op::Op::RENAME, Some(cookies[0])), - (tdir.mkpath("dir1b"), op::Op::RENAME, Some(cookies[0])), - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1a"), op::Op::RENAME, Some(cookies[0])), + (tdir.mkpath("dir1b"), op::Op::RENAME, Some(cookies[0])), + ] + ); } } @@ -543,31 +663,42 @@ fn rename_directory() { fn move_out_create_directory() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "watch_dir/dir1", - ]); + tdir.create_all(vec!["watch_dir/dir1"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("watch_dir"), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("watch_dir"), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.rename("watch_dir/dir1", "dir1b"); tdir.create("watch_dir/dir1"); - if cfg!(target_os="macos") { - assert_eq!(inflate_events(recv_events(&rx)), vec![ - (tdir.mkpath("watch_dir/dir1"), op::Op::CREATE | op::Op::RENAME, None), // fsevent interprets a move_out as a rename event - ]); + if cfg!(target_os = "macos") { + assert_eq!( + inflate_events(recv_events(&rx)), + vec![ + ( + tdir.mkpath("watch_dir/dir1"), + op::Op::CREATE | op::Op::RENAME, + None + ), // fsevent interprets a move_out as a rename event + ] + ); panic!("move event should be a remove event; fsevent conflates rename and create events"); } else { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("watch_dir/dir1"), op::Op::REMOVE, None), - (tdir.mkpath("watch_dir/dir1"), op::Op::CREATE, None), - ]); + assert_eq!( + recv_events(&rx), + vec![ + (tdir.mkpath("watch_dir/dir1"), op::Op::REMOVE, None), + (tdir.mkpath("watch_dir/dir1"), op::Op::CREATE, None), + ] + ); } } @@ -578,16 +709,16 @@ fn move_out_create_directory() { fn move_in_directory_watch_subdirectories() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "watch_dir", - "dir1/dir2", - ]); + tdir.create_all(vec!["watch_dir", "dir1/dir2"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("watch_dir"), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("watch_dir"), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); @@ -597,21 +728,44 @@ fn move_in_directory_watch_subdirectories() { tdir.create("watch_dir/dir1/dir2/file1"); - if cfg!(target_os="macos") { - assert_eq!(inflate_events(recv_events(&rx)), vec![ - (tdir.mkpath("watch_dir/dir1"), op::Op::CREATE | op::Op::RENAME, None), - ]); - } else if cfg!(target_os="linux") { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("watch_dir/dir1"), op::Op::CREATE, None), - (tdir.mkpath("watch_dir/dir1/dir2/file1"), op::Op::CREATE, None), - (tdir.mkpath("watch_dir/dir1/dir2/file1"), op::Op::CLOSE_WRITE, None), - ]); + if cfg!(target_os = "macos") { + assert_eq!( + inflate_events(recv_events(&rx)), + vec![( + tdir.mkpath("watch_dir/dir1"), + op::Op::CREATE | op::Op::RENAME, + None + ),] + ); + } else if cfg!(target_os = "linux") { + assert_eq!( + recv_events(&rx), + vec![ + (tdir.mkpath("watch_dir/dir1"), op::Op::CREATE, None), + ( + tdir.mkpath("watch_dir/dir1/dir2/file1"), + op::Op::CREATE, + None + ), + ( + tdir.mkpath("watch_dir/dir1/dir2/file1"), + op::Op::CLOSE_WRITE, + None + ), + ] + ); } else { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("watch_dir/dir1"), op::Op::CREATE, None), - (tdir.mkpath("watch_dir/dir1/dir2/file1"), op::Op::CREATE, None), - ]); + assert_eq!( + recv_events(&rx), + vec![ + (tdir.mkpath("watch_dir/dir1"), op::Op::CREATE, None), + ( + tdir.mkpath("watch_dir/dir1/dir2/file1"), + op::Op::CREATE, + None + ), + ] + ); } } @@ -619,42 +773,49 @@ fn move_in_directory_watch_subdirectories() { #[cfg_attr(target_os = "windows", ignore)] fn create_rename_overwrite_directory() { // overwriting directories doesn't work on windows - if cfg!(target_os="windows") { + if cfg!(target_os = "windows") { panic!("cannot overwrite directory on windows"); } let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1b", - ]); + tdir.create_all(vec!["dir1b"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.create("dir1a"); tdir.rename("dir1a", "dir1b"); - if cfg!(target_os="macos") { - assert_eq!(inflate_events(recv_events(&rx)), vec![ - (tdir.mkpath("dir1a"), op::Op::CREATE | op::Op::RENAME, None), - (tdir.mkpath("dir1b"), op::Op::CREATE | op::Op::RENAME, None), - ]); - } else if cfg!(target_os="linux") { + if cfg!(target_os = "macos") { + assert_eq!( + inflate_events(recv_events(&rx)), + vec![ + (tdir.mkpath("dir1a"), op::Op::CREATE | op::Op::RENAME, None), + (tdir.mkpath("dir1b"), op::Op::CREATE | op::Op::RENAME, None), + ] + ); + } else if cfg!(target_os = "linux") { let actual = recv_events(&rx); let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 1); - assert_eq!(actual, vec![ - (tdir.mkpath("dir1a"), op::Op::CREATE, None), - (tdir.mkpath("dir1a"), op::Op::RENAME, Some(cookies[0])), - (tdir.mkpath("dir1b"), op::Op::RENAME, Some(cookies[0])), - (tdir.mkpath("dir1b"), op::Op::CHMOD, None), - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1a"), op::Op::CREATE, None), + (tdir.mkpath("dir1a"), op::Op::RENAME, Some(cookies[0])), + (tdir.mkpath("dir1b"), op::Op::RENAME, Some(cookies[0])), + (tdir.mkpath("dir1b"), op::Op::CHMOD, None), + ] + ); } else { unimplemented!(); } @@ -664,39 +825,46 @@ fn create_rename_overwrite_directory() { fn rename_rename_directory() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1a", - ]); + tdir.create_all(vec!["dir1a"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.rename("dir1a", "dir1b"); tdir.rename("dir1b", "dir1c"); - if cfg!(target_os="macos") { + if cfg!(target_os = "macos") { let actual = inflate_events(recv_events(&rx)); let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 1); - assert_eq!(actual, vec![ - (tdir.mkpath("dir1a"), op::Op::CREATE | op::Op::RENAME, None), - (tdir.mkpath("dir1b"), op::Op::RENAME, Some(cookies[0])), - (tdir.mkpath("dir1c"), op::Op::RENAME, Some(cookies[0])), - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1a"), op::Op::CREATE | op::Op::RENAME, None), + (tdir.mkpath("dir1b"), op::Op::RENAME, Some(cookies[0])), + (tdir.mkpath("dir1c"), op::Op::RENAME, Some(cookies[0])), + ] + ); } else { let actual = recv_events(&rx); let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 2); - assert_eq!(actual, vec![ - (tdir.mkpath("dir1a"), op::Op::RENAME, Some(cookies[0])), - (tdir.mkpath("dir1b"), op::Op::RENAME, Some(cookies[0])), - (tdir.mkpath("dir1b"), op::Op::RENAME, Some(cookies[1])), - (tdir.mkpath("dir1c"), op::Op::RENAME, Some(cookies[1])), - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1a"), op::Op::RENAME, Some(cookies[0])), + (tdir.mkpath("dir1b"), op::Op::RENAME, Some(cookies[0])), + (tdir.mkpath("dir1b"), op::Op::RENAME, Some(cookies[1])), + (tdir.mkpath("dir1c"), op::Op::RENAME, Some(cookies[1])), + ] + ); } } diff --git a/tests/utils/mod.rs b/tests/utils/mod.rs index dc1e23b6..1d8b70e2 100644 --- a/tests/utils/mod.rs +++ b/tests/utils/mod.rs @@ -1,5 +1,5 @@ -use tempdir::TempDir; use notify::*; +use tempdir::TempDir; use std::fs; use std::io::Write; @@ -8,35 +8,42 @@ use std::sync::mpsc::{Receiver, TryRecvError}; use std::thread; use std::time::{Duration, Instant}; -#[cfg(not(target_os="windows"))] +#[cfg(not(target_os = "windows"))] use std::os::unix::fs::PermissionsExt; -#[cfg(not(target_os="windows"))] +#[cfg(not(target_os = "windows"))] const TIMEOUT_MS: u64 = 100; -#[cfg(target_os="windows")] +#[cfg(target_os = "windows")] const TIMEOUT_MS: u64 = 3000; // windows can take a while -pub fn recv_events_with_timeout(rx: &Receiver, timeout: Duration) -> Vec<(PathBuf, Op, Option)> { +pub fn recv_events_with_timeout( + rx: &Receiver, + timeout: Duration, +) -> Vec<(PathBuf, Op, Option)> { let start = Instant::now(); let mut evs = Vec::new(); while start.elapsed() < timeout { match rx.try_recv() { - Ok(RawEvent{path: Some(path), op: Ok(op), cookie}) => { + Ok(RawEvent { + path: Some(path), + op: Ok(op), + cookie, + }) => { evs.push((path, op, cookie)); - }, - Ok(RawEvent{path: None, ..}) => (), - Ok(RawEvent{op: Err(e), ..}) => panic!("unexpected event err: {:?}", e), + } + Ok(RawEvent { path: None, .. }) => (), + Ok(RawEvent { op: Err(e), .. }) => panic!("unexpected event err: {:?}", e), Err(TryRecvError::Empty) => (), - Err(e) => panic!("unexpected channel err: {:?}", e) + Err(e) => panic!("unexpected channel err: {:?}", e), } thread::sleep(Duration::from_millis(1)); } evs } -pub fn recv_events(rx: &Receiver) -> Vec<(PathBuf, Op, Option)> { +pub fn recv_events(rx: &Receiver) -> Vec<(PathBuf, Op, Option)> { recv_events_with_timeout(rx, Duration::from_millis(TIMEOUT_MS)) } @@ -51,11 +58,11 @@ pub fn inflate_events(input: Vec<(PathBuf, Op, Option)>) -> Vec<(PathBuf, O for (e_p, e_o, e_c) in input { let p = match path { Some(p) => p, - None => e_p.clone() + None => e_p.clone(), }; let c = match cookie { Some(c) => Some(c), - None => e_c + None => e_c, }; if p == e_p && c == e_c { ops |= e_o; @@ -122,7 +129,11 @@ pub trait TestHelpers { impl TestHelpers for TempDir { fn mkpath(&self, p: &str) -> PathBuf { - let mut path = self.path().canonicalize().expect("failed to canonicalize path").to_owned(); + let mut path = self + .path() + .canonicalize() + .expect("failed to canonicalize path") + .to_owned(); for part in p.split('/').collect::>() { if part != "." { path.push(part); @@ -133,10 +144,21 @@ impl TestHelpers for TempDir { fn create(&self, p: &str) { let path = self.mkpath(p); - if path.components().last().unwrap().as_os_str().to_str().unwrap().contains("dir") { + if path + .components() + .last() + .unwrap() + .as_os_str() + .to_str() + .unwrap() + .contains("dir") + { fs::create_dir_all(path).expect("failed to create directory"); } else { - let parent = path.parent().expect("failed to get parent directory").to_owned(); + let parent = path + .parent() + .expect("failed to get parent directory") + .to_owned(); if !parent.exists() { fs::create_dir_all(parent).expect("failed to create parent directory"); } @@ -156,25 +178,25 @@ impl TestHelpers for TempDir { fs::rename(&path_a, &path_b).expect("failed to rename file or directory"); } - #[cfg(not(target_os="windows"))] + #[cfg(not(target_os = "windows"))] fn chmod(&self, p: &str) { let path = self.mkpath(p); - let mut permissions = fs::metadata(&path).expect("failed to get metadata").permissions(); + let mut permissions = fs::metadata(&path) + .expect("failed to get metadata") + .permissions(); let u = (permissions.mode() / 100) % 10; let g = (permissions.mode() / 10) % 10; - let o = if permissions.mode() % 10 == 0 { - g - } else { - 0 - }; + let o = if permissions.mode() % 10 == 0 { g } else { 0 }; permissions.set_mode(u * 100 + g * 10 + o); fs::set_permissions(path, permissions).expect("failed to chmod file or directory"); } - #[cfg(target_os="windows")] + #[cfg(target_os = "windows")] fn chmod(&self, p: &str) { let path = self.mkpath(p); - let mut permissions = fs::metadata(&path).expect("failed to get metadata").permissions(); + let mut permissions = fs::metadata(&path) + .expect("failed to get metadata") + .permissions(); let r = permissions.readonly(); permissions.set_readonly(!r); fs::set_permissions(path, permissions).expect("failed to chmod file or directory"); diff --git a/tests/watcher.rs b/tests/watcher.rs index 07104305..8b8c9a40 100644 --- a/tests/watcher.rs +++ b/tests/watcher.rs @@ -4,23 +4,23 @@ extern crate tempdir; mod utils; use notify::*; +use std::env; use std::sync::mpsc; -use tempdir::TempDir; use std::thread; -use std::env; +use tempdir::TempDir; -#[cfg(all(feature = "manual_tests", target_os="linux"))] -use std::time::{Duration, Instant}; -#[cfg(all(feature = "manual_tests", target_os="linux"))] -use std::io::prelude::*; -#[cfg(all(feature = "manual_tests", target_os="linux"))] +#[cfg(all(feature = "manual_tests", target_os = "linux"))] use std::fs::File; +#[cfg(all(feature = "manual_tests", target_os = "linux"))] +use std::io::prelude::*; +#[cfg(all(feature = "manual_tests", target_os = "linux"))] +use std::time::{Duration, Instant}; use utils::*; const NETWORK_PATH: &'static str = ""; // eg.: \\\\MY-PC\\Users\\MyName -#[cfg(target_os="linux")] +#[cfg(target_os = "linux")] #[test] fn new_inotify() { let (tx, _) = mpsc::channel(); @@ -28,7 +28,7 @@ fn new_inotify() { assert!(w.is_ok()); } -#[cfg(target_os="macos")] +#[cfg(target_os = "macos")] #[test] fn new_fsevent() { let (tx, _) = mpsc::channel(); @@ -66,13 +66,15 @@ fn test_watcher_send() { thread::spawn(move || { watcher.watch(".", RecursiveMode::Recursive).unwrap(); - }).join().unwrap(); + }) + .join() + .unwrap(); } // if this test builds, it means RecommendedWatcher is Sync. #[test] fn test_watcher_sync() { - use std::sync::{ Arc, RwLock }; + use std::sync::{Arc, RwLock}; let (tx, _) = mpsc::channel(); @@ -82,25 +84,33 @@ fn test_watcher_sync() { thread::spawn(move || { let mut watcher = watcher.write().unwrap(); watcher.watch(".", RecursiveMode::Recursive).unwrap(); - }).join().unwrap(); + }) + .join() + .unwrap(); } #[test] fn watch_relative() { // both of the following tests set the same environment variable, so they must not run in parallel - { // watch_relative_directory + { + // watch_relative_directory let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); tdir.create("dir1"); env::set_current_dir(tdir.path()).expect("failed to change working directory"); let (tx, _) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch("dir1", RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch("dir1", RecursiveMode::Recursive) + .expect("failed to watch directory"); - watcher.unwatch("dir1").expect("failed to unwatch directory"); + watcher + .unwatch("dir1") + .expect("failed to unwatch directory"); - if cfg!(not(target_os="windows")) { + if cfg!(not(target_os = "windows")) { match watcher.unwatch("dir1") { Err(Error::WatchNotFound) => (), Err(e) => panic!("{:?}", e), @@ -108,19 +118,23 @@ fn watch_relative() { } } } - { // watch_relative_file + { + // watch_relative_file let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); tdir.create("file1"); env::set_current_dir(tdir.path()).expect("failed to change working directory"); let (tx, _) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch("file1", RecursiveMode::Recursive).expect("failed to watch file"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch("file1", RecursiveMode::Recursive) + .expect("failed to watch file"); watcher.unwatch("file1").expect("failed to unwatch file"); - if cfg!(not(target_os="windows")) { + if cfg!(not(target_os = "windows")) { match watcher.unwatch("file1") { Err(Error::WatchNotFound) => (), Err(e) => panic!("{:?}", e), @@ -128,20 +142,26 @@ fn watch_relative() { } } } - if cfg!(target_os = "windows") && !NETWORK_PATH.is_empty() - { // watch_relative_network_directory - let tdir = TempDir::new_in(NETWORK_PATH, "temp_dir").expect("failed to create temporary directory"); + if cfg!(target_os = "windows") && !NETWORK_PATH.is_empty() { + // watch_relative_network_directory + let tdir = TempDir::new_in(NETWORK_PATH, "temp_dir") + .expect("failed to create temporary directory"); tdir.create("dir1"); env::set_current_dir(tdir.path()).expect("failed to change working directory"); let (tx, _) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch("dir1", RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch("dir1", RecursiveMode::Recursive) + .expect("failed to watch directory"); - watcher.unwatch("dir1").expect("failed to unwatch directory"); + watcher + .unwatch("dir1") + .expect("failed to unwatch directory"); - if cfg!(not(target_os="windows")) { + if cfg!(not(target_os = "windows")) { match watcher.unwatch("dir1") { Err(Error::WatchNotFound) => (), Err(e) => panic!("{:?}", e), @@ -149,20 +169,24 @@ fn watch_relative() { } } } - if cfg!(target_os = "windows") && !NETWORK_PATH.is_empty() - { // watch_relative_network_file - let tdir = TempDir::new_in(NETWORK_PATH, "temp_dir").expect("failed to create temporary directory"); + if cfg!(target_os = "windows") && !NETWORK_PATH.is_empty() { + // watch_relative_network_file + let tdir = TempDir::new_in(NETWORK_PATH, "temp_dir") + .expect("failed to create temporary directory"); tdir.create("file1"); env::set_current_dir(tdir.path()).expect("failed to change working directory"); let (tx, _) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch("file1", RecursiveMode::Recursive).expect("failed to watch file"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch("file1", RecursiveMode::Recursive) + .expect("failed to watch file"); watcher.unwatch("file1").expect("failed to unwatch file"); - if cfg!(not(target_os="windows")) { + if cfg!(not(target_os = "windows")) { match watcher.unwatch("file1") { Err(Error::WatchNotFound) => (), Err(e) => panic!("{:?}", e), @@ -176,19 +200,25 @@ fn watch_relative() { #[cfg(target_os = "windows")] fn watch_absolute_network_directory() { if NETWORK_PATH.is_empty() { - return + return; } - let tdir = TempDir::new_in(NETWORK_PATH, "temp_dir").expect("failed to create temporary directory"); + let tdir = + TempDir::new_in(NETWORK_PATH, "temp_dir").expect("failed to create temporary directory"); tdir.create("dir1"); let (tx, _) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("dir1"), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("dir1"), RecursiveMode::Recursive) + .expect("failed to watch directory"); - watcher.unwatch(tdir.mkpath("dir1")).expect("failed to unwatch directory"); + watcher + .unwatch(tdir.mkpath("dir1")) + .expect("failed to unwatch directory"); - if cfg!(not(target_os="windows")) { + if cfg!(not(target_os = "windows")) { match watcher.unwatch(tdir.mkpath("dir1")) { Err(Error::WatchNotFound) => (), Err(e) => panic!("{:?}", e), @@ -201,21 +231,27 @@ fn watch_absolute_network_directory() { #[cfg(target_os = "windows")] fn watch_absolute_network_file() { if NETWORK_PATH.is_empty() { - return + return; } - let tdir = TempDir::new_in(NETWORK_PATH, "temp_dir").expect("failed to create temporary directory"); + let tdir = + TempDir::new_in(NETWORK_PATH, "temp_dir").expect("failed to create temporary directory"); tdir.create("file1"); env::set_current_dir(tdir.path()).expect("failed to change working directory"); let (tx, _) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("file1"), RecursiveMode::Recursive).expect("failed to watch file"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("file1"), RecursiveMode::Recursive) + .expect("failed to watch file"); - watcher.unwatch(tdir.mkpath("file1")).expect("failed to unwatch file"); + watcher + .unwatch(tdir.mkpath("file1")) + .expect("failed to unwatch file"); - if cfg!(not(target_os="windows")) { + if cfg!(not(target_os = "windows")) { match watcher.unwatch(tdir.mkpath("file1")) { Err(Error::WatchNotFound) => (), Err(e) => panic!("{:?}", e), @@ -225,21 +261,26 @@ fn watch_absolute_network_file() { } #[test] -#[cfg(all(feature = "manual_tests", target_os="linux"))] +#[cfg(all(feature = "manual_tests", target_os = "linux"))] // Test preparation: // 1. Run `sudo echo 10 > /proc/sys/fs/inotify/max_queued_events` // 2. Uncomment the lines near "test inotify_queue_overflow" in inotify watcher fn inotify_queue_overflow() { let mut max_queued_events = String::new(); - let mut f = File::open("/proc/sys/fs/inotify/max_queued_events").expect("failed to open max_queued_events"); - f.read_to_string(&mut max_queued_events).expect("failed to read max_queued_events"); + let mut f = File::open("/proc/sys/fs/inotify/max_queued_events") + .expect("failed to open max_queued_events"); + f.read_to_string(&mut max_queued_events) + .expect("failed to read max_queued_events"); assert_eq!(max_queued_events.trim(), "10"); let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); for i in 0..20 { let filename = format!("file{}", i); @@ -254,11 +295,14 @@ fn inotify_queue_overflow() { let mut rescan_found = false; while !rescan_found && start.elapsed().as_secs() < 5 { match rx.try_recv() { - Ok(RawEvent{op: Ok(op::Op::RESCAN), ..}) => rescan_found = true, - Ok(RawEvent{op: Err(e), ..}) => panic!("unexpected event err: {:?}", e), + Ok(RawEvent { + op: Ok(op::Op::RESCAN), + .. + }) => rescan_found = true, + Ok(RawEvent { op: Err(e), .. }) => panic!("unexpected event err: {:?}", e), Ok(e) => (), Err(mpsc::TryRecvError::Empty) => (), - Err(e) => panic!("unexpected channel err: {:?}", e) + Err(e) => panic!("unexpected channel err: {:?}", e), } thread::sleep(Duration::from_millis(10)); } @@ -273,8 +317,11 @@ fn watch_recursive_create_directory() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(&tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(&tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); @@ -284,34 +331,42 @@ fn watch_recursive_create_directory() { sleep_macos(100); - watcher.unwatch(&tdir.mkpath(".")).expect("failed to unwatch directory"); + watcher + .unwatch(&tdir.mkpath(".")) + .expect("failed to unwatch directory"); sleep_windows(100); tdir.create("dir1/file2"); - let actual = if cfg!(target_os="windows") { + let actual = if cfg!(target_os = "windows") { // Windows may sneak a write event in there let mut events = recv_events(&rx); events.retain(|&(_, op, _)| op != op::Op::WRITE); events - } else if cfg!(target_os="macos") { + } else if cfg!(target_os = "macos") { inflate_events(recv_events(&rx)) } else { recv_events(&rx) }; - if cfg!(target_os="linux") { - assert_eq!(actual, vec![ - (tdir.mkpath("dir1"), op::Op::CREATE, None), - (tdir.mkpath("dir1/file1"), op::Op::CREATE, None), - (tdir.mkpath("dir1/file1"), op::Op::CLOSE_WRITE, None) - ]); + if cfg!(target_os = "linux") { + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1"), op::Op::CREATE, None), + (tdir.mkpath("dir1/file1"), op::Op::CREATE, None), + (tdir.mkpath("dir1/file1"), op::Op::CLOSE_WRITE, None) + ] + ); } else { - assert_eq!(actual, vec![ - (tdir.mkpath("dir1"), op::Op::CREATE, None), - (tdir.mkpath("dir1/file1"), op::Op::CREATE, None) - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1"), op::Op::CREATE, None), + (tdir.mkpath("dir1/file1"), op::Op::CREATE, None) + ] + ); } } @@ -319,15 +374,16 @@ fn watch_recursive_create_directory() { fn watch_recursive_move() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1a", - ]); + tdir.create_all(vec!["dir1a"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); @@ -336,46 +392,59 @@ fn watch_recursive_move() { sleep(10); tdir.create("dir1b/file2"); - let actual = if cfg!(target_os="windows") { + let actual = if cfg!(target_os = "windows") { // Windows may sneak a write event in there let mut events = recv_events(&rx); events.retain(|&(_, op, _)| op != op::Op::WRITE); events - } else if cfg!(target_os="macos") { + } else if cfg!(target_os = "macos") { inflate_events(recv_events(&rx)) } else { recv_events(&rx) }; - if cfg!(target_os="macos") { + if cfg!(target_os = "macos") { let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 1); - assert_eq!(actual, vec![ - (tdir.mkpath("dir1a/file1"), op::Op::CREATE, None), - (tdir.mkpath("dir1a"), op::Op::CREATE | op::Op::RENAME, Some(cookies[0])), // excessive create event - (tdir.mkpath("dir1b"), op::Op::RENAME, Some(cookies[0])), - (tdir.mkpath("dir1b/file2"), op::Op::CREATE, None) - ]); - } else if cfg!(target_os="linux") { + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1a/file1"), op::Op::CREATE, None), + ( + tdir.mkpath("dir1a"), + op::Op::CREATE | op::Op::RENAME, + Some(cookies[0]) + ), // excessive create event + (tdir.mkpath("dir1b"), op::Op::RENAME, Some(cookies[0])), + (tdir.mkpath("dir1b/file2"), op::Op::CREATE, None) + ] + ); + } else if cfg!(target_os = "linux") { let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 1); - assert_eq!(actual, vec![ - (tdir.mkpath("dir1a/file1"), op::Op::CREATE, None), - (tdir.mkpath("dir1a/file1"), op::Op::CLOSE_WRITE, None), - (tdir.mkpath("dir1a"), op::Op::RENAME, Some(cookies[0])), - (tdir.mkpath("dir1b"), op::Op::RENAME, Some(cookies[0])), - (tdir.mkpath("dir1b/file2"), op::Op::CREATE, None), - (tdir.mkpath("dir1b/file2"), op::Op::CLOSE_WRITE, None) - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1a/file1"), op::Op::CREATE, None), + (tdir.mkpath("dir1a/file1"), op::Op::CLOSE_WRITE, None), + (tdir.mkpath("dir1a"), op::Op::RENAME, Some(cookies[0])), + (tdir.mkpath("dir1b"), op::Op::RENAME, Some(cookies[0])), + (tdir.mkpath("dir1b/file2"), op::Op::CREATE, None), + (tdir.mkpath("dir1b/file2"), op::Op::CLOSE_WRITE, None) + ] + ); } else { let cookies = extract_cookies(&actual); assert_eq!(cookies.len(), 1); - assert_eq!(actual, vec![ - (tdir.mkpath("dir1a/file1"), op::Op::CREATE, None), - (tdir.mkpath("dir1a"), op::Op::RENAME, Some(cookies[0])), - (tdir.mkpath("dir1b"), op::Op::RENAME, Some(cookies[0])), - (tdir.mkpath("dir1b/file2"), op::Op::CREATE, None) - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1a/file1"), op::Op::CREATE, None), + (tdir.mkpath("dir1a"), op::Op::RENAME, Some(cookies[0])), + (tdir.mkpath("dir1b"), op::Op::RENAME, Some(cookies[0])), + (tdir.mkpath("dir1b/file2"), op::Op::CREATE, None) + ] + ); } } @@ -384,16 +453,16 @@ fn watch_recursive_move() { fn watch_recursive_move_in() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "watch_dir", - "dir1a/dir1", - ]); + tdir.create_all(vec!["watch_dir", "dir1a/dir1"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(&tdir.mkpath("watch_dir"), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(&tdir.mkpath("watch_dir"), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); @@ -401,34 +470,59 @@ fn watch_recursive_move_in() { sleep(10); tdir.create("watch_dir/dir1b/dir1/file1"); - let actual = if cfg!(target_os="windows") { + let actual = if cfg!(target_os = "windows") { // Windows may sneak a write event in there let mut events = recv_events(&rx); events.retain(|&(_, op, _)| op != op::Op::WRITE); events - } else if cfg!(target_os="macos") { + } else if cfg!(target_os = "macos") { inflate_events(recv_events(&rx)) } else { recv_events(&rx) }; - if cfg!(target_os="macos") { - assert_eq!(actual, vec![ - (tdir.mkpath("watch_dir/dir1b"), op::Op::RENAME, None), // fsevent interprets a move_to as a rename event - (tdir.mkpath("watch_dir/dir1b/dir1/file1"), op::Op::CREATE, None) - ]); + if cfg!(target_os = "macos") { + assert_eq!( + actual, + vec![ + (tdir.mkpath("watch_dir/dir1b"), op::Op::RENAME, None), // fsevent interprets a move_to as a rename event + ( + tdir.mkpath("watch_dir/dir1b/dir1/file1"), + op::Op::CREATE, + None + ) + ] + ); panic!("move event should be a create event"); - } else if cfg!(target_os="linux") { - assert_eq!(actual, vec![ - (tdir.mkpath("watch_dir/dir1b"), op::Op::CREATE, None), - (tdir.mkpath("watch_dir/dir1b/dir1/file1"), op::Op::CREATE, None), - (tdir.mkpath("watch_dir/dir1b/dir1/file1"), op::Op::CLOSE_WRITE, None), - ]); + } else if cfg!(target_os = "linux") { + assert_eq!( + actual, + vec![ + (tdir.mkpath("watch_dir/dir1b"), op::Op::CREATE, None), + ( + tdir.mkpath("watch_dir/dir1b/dir1/file1"), + op::Op::CREATE, + None + ), + ( + tdir.mkpath("watch_dir/dir1b/dir1/file1"), + op::Op::CLOSE_WRITE, + None + ), + ] + ); } else { - assert_eq!(actual, vec![ - (tdir.mkpath("watch_dir/dir1b"), op::Op::CREATE, None), - (tdir.mkpath("watch_dir/dir1b/dir1/file1"), op::Op::CREATE, None), - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("watch_dir/dir1b"), op::Op::CREATE, None), + ( + tdir.mkpath("watch_dir/dir1b/dir1/file1"), + op::Op::CREATE, + None + ), + ] + ); } } @@ -437,15 +531,16 @@ fn watch_recursive_move_in() { fn watch_recursive_move_out() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "watch_dir/dir1a/dir1", - ]); + tdir.create_all(vec!["watch_dir/dir1a/dir1"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("watch_dir"), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("watch_dir"), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); @@ -454,34 +549,63 @@ fn watch_recursive_move_out() { sleep(10); tdir.create("dir1b/dir1/file2"); - let actual = if cfg!(target_os="windows") { + let actual = if cfg!(target_os = "windows") { // Windows may sneak a write event in there let mut events = recv_events(&rx); events.retain(|&(_, op, _)| op != op::Op::WRITE); events - } else if cfg!(target_os="macos") { + } else if cfg!(target_os = "macos") { inflate_events(recv_events(&rx)) } else { recv_events(&rx) }; - if cfg!(target_os="macos") { - assert_eq!(actual, vec![ - (tdir.mkpath("watch_dir/dir1a/dir1/file1"), op::Op::CREATE, None), - (tdir.mkpath("watch_dir/dir1a"), op::Op::CREATE | op::Op::RENAME, None) // excessive create event, fsevent interprets a move_out as a rename event - ]); + if cfg!(target_os = "macos") { + assert_eq!( + actual, + vec![ + ( + tdir.mkpath("watch_dir/dir1a/dir1/file1"), + op::Op::CREATE, + None + ), + ( + tdir.mkpath("watch_dir/dir1a"), + op::Op::CREATE | op::Op::RENAME, + None + ) // excessive create event, fsevent interprets a move_out as a rename event + ] + ); panic!("move event should be a remove event"); - } else if cfg!(target_os="linux") { - assert_eq!(actual, vec![ - (tdir.mkpath("watch_dir/dir1a/dir1/file1"), op::Op::CREATE, None), - (tdir.mkpath("watch_dir/dir1a/dir1/file1"), op::Op::CLOSE_WRITE, None), - (tdir.mkpath("watch_dir/dir1a"), op::Op::REMOVE, None), - ]); + } else if cfg!(target_os = "linux") { + assert_eq!( + actual, + vec![ + ( + tdir.mkpath("watch_dir/dir1a/dir1/file1"), + op::Op::CREATE, + None + ), + ( + tdir.mkpath("watch_dir/dir1a/dir1/file1"), + op::Op::CLOSE_WRITE, + None + ), + (tdir.mkpath("watch_dir/dir1a"), op::Op::REMOVE, None), + ] + ); } else { - assert_eq!(actual, vec![ - (tdir.mkpath("watch_dir/dir1a/dir1/file1"), op::Op::CREATE, None), - (tdir.mkpath("watch_dir/dir1a"), op::Op::REMOVE, None), - ]); + assert_eq!( + actual, + vec![ + ( + tdir.mkpath("watch_dir/dir1a/dir1/file1"), + op::Op::CREATE, + None + ), + (tdir.mkpath("watch_dir/dir1a"), op::Op::REMOVE, None), + ] + ); } } @@ -489,37 +613,40 @@ fn watch_recursive_move_out() { fn watch_nonrecursive() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1", - ]); + tdir.create_all(vec!["dir1"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::NonRecursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::NonRecursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.create("dir2"); sleep(10); - tdir.create_all(vec![ - "file0", - "dir1/file1", - "dir2/file2", - ]); - - if cfg!(target_os="linux") { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("dir2"), op::Op::CREATE, None), - (tdir.mkpath("file0"), op::Op::CREATE, None), - (tdir.mkpath("file0"), op::Op::CLOSE_WRITE, None) - ]); + tdir.create_all(vec!["file0", "dir1/file1", "dir2/file2"]); + + if cfg!(target_os = "linux") { + assert_eq!( + recv_events(&rx), + vec![ + (tdir.mkpath("dir2"), op::Op::CREATE, None), + (tdir.mkpath("file0"), op::Op::CREATE, None), + (tdir.mkpath("file0"), op::Op::CLOSE_WRITE, None) + ] + ); } else { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("dir2"), op::Op::CREATE, None), - (tdir.mkpath("file0"), op::Op::CREATE, None) - ]); + assert_eq!( + recv_events(&rx), + vec![ + (tdir.mkpath("dir2"), op::Op::CREATE, None), + (tdir.mkpath("file0"), op::Op::CREATE, None) + ] + ); } } @@ -527,34 +654,42 @@ fn watch_nonrecursive() { fn watch_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1", - ]); + tdir.create_all(vec!["file1"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(tdir.mkpath("file1"), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(tdir.mkpath("file1"), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.write("file1"); tdir.create("file2"); - if cfg!(target_os="macos") { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("file1"), op::Op::CREATE | op::Op::WRITE, None) // excessive write create - ]); - } else if cfg!(target_os="linux") { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("file1"), op::Op::WRITE, None), - (tdir.mkpath("file1"), op::Op::CLOSE_WRITE, None) - ]); + if cfg!(target_os = "macos") { + assert_eq!( + recv_events(&rx), + vec![ + (tdir.mkpath("file1"), op::Op::CREATE | op::Op::WRITE, None) // excessive write create + ] + ); + } else if cfg!(target_os = "linux") { + assert_eq!( + recv_events(&rx), + vec![ + (tdir.mkpath("file1"), op::Op::WRITE, None), + (tdir.mkpath("file1"), op::Op::CLOSE_WRITE, None) + ] + ); } else { - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("file1"), op::Op::WRITE, None) - ]); + assert_eq!( + recv_events(&rx), + vec![(tdir.mkpath("file1"), op::Op::WRITE, None)] + ); } } @@ -564,7 +699,9 @@ fn poll_watch_recursive_create_directory() { let (tx, rx) = mpsc::channel(); let mut watcher = PollWatcher::with_delay_ms(tx, 50).expect("failed to create poll watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep(1100); // PollWatcher has only a resolution of 1 second @@ -572,18 +709,23 @@ fn poll_watch_recursive_create_directory() { sleep(1100); // PollWatcher has only a resolution of 1 second - watcher.unwatch(tdir.mkpath(".")).expect("failed to unwatch directory"); + watcher + .unwatch(tdir.mkpath(".")) + .expect("failed to unwatch directory"); tdir.create("dir1/file2"); let mut actual = recv_events(&rx); actual.sort_by(|a, b| a.0.cmp(&b.0)); - assert_eq!(actual, vec![ - (tdir.mkpath("."), op::Op::WRITE, None), // parent directory gets modified - (tdir.mkpath("dir1"), op::Op::CREATE, None), - (tdir.mkpath("dir1/file1"), op::Op::CREATE, None) - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("."), op::Op::WRITE, None), // parent directory gets modified + (tdir.mkpath("dir1"), op::Op::CREATE, None), + (tdir.mkpath("dir1/file1"), op::Op::CREATE, None) + ] + ); } #[test] @@ -591,13 +733,13 @@ fn poll_watch_recursive_create_directory() { fn poll_watch_recursive_move() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1a", - ]); + tdir.create_all(vec!["dir1a"]); let (tx, rx) = mpsc::channel(); let mut watcher = PollWatcher::with_delay_ms(tx, 50).expect("failed to create poll watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep(1100); // PollWatcher has only a resolution of 1 second @@ -606,10 +748,13 @@ fn poll_watch_recursive_move() { let mut actual = recv_events(&rx); actual.sort_by(|a, b| a.0.cmp(&b.0)); - assert_eq!(actual, vec![ - (tdir.mkpath("dir1a"), op::Op::WRITE, None), // parent directory gets modified - (tdir.mkpath("dir1a/file1"), op::Op::CREATE, None), - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1a"), op::Op::WRITE, None), // parent directory gets modified + (tdir.mkpath("dir1a/file1"), op::Op::CREATE, None), + ] + ); sleep(1100); // PollWatcher has only a resolution of 1 second @@ -619,25 +764,31 @@ fn poll_watch_recursive_move() { actual = recv_events(&rx); actual.sort_by(|a, b| a.0.cmp(&b.0)); - if cfg!(target_os="windows") { - assert_eq!(actual, vec![ - (tdir.mkpath("."), op::Op::WRITE, None), // parent directory gets modified - (tdir.mkpath("dir1a"), op::Op::REMOVE, None), - (tdir.mkpath("dir1a/file1"), op::Op::REMOVE, None), - (tdir.mkpath("dir1b"), op::Op::CREATE, None), - (tdir.mkpath("dir1b"), op::Op::WRITE, None), // parent directory gets modified - (tdir.mkpath("dir1b/file1"), op::Op::CREATE, None), - (tdir.mkpath("dir1b/file2"), op::Op::CREATE, None), - ]); + if cfg!(target_os = "windows") { + assert_eq!( + actual, + vec![ + (tdir.mkpath("."), op::Op::WRITE, None), // parent directory gets modified + (tdir.mkpath("dir1a"), op::Op::REMOVE, None), + (tdir.mkpath("dir1a/file1"), op::Op::REMOVE, None), + (tdir.mkpath("dir1b"), op::Op::CREATE, None), + (tdir.mkpath("dir1b"), op::Op::WRITE, None), // parent directory gets modified + (tdir.mkpath("dir1b/file1"), op::Op::CREATE, None), + (tdir.mkpath("dir1b/file2"), op::Op::CREATE, None), + ] + ); } else { - assert_eq!(actual, vec![ - (tdir.mkpath("."), op::Op::WRITE, None), // parent directory gets modified - (tdir.mkpath("dir1a"), op::Op::REMOVE, None), - (tdir.mkpath("dir1a/file1"), op::Op::REMOVE, None), - (tdir.mkpath("dir1b"), op::Op::CREATE, None), - (tdir.mkpath("dir1b/file1"), op::Op::CREATE, None), - (tdir.mkpath("dir1b/file2"), op::Op::CREATE, None), - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("."), op::Op::WRITE, None), // parent directory gets modified + (tdir.mkpath("dir1a"), op::Op::REMOVE, None), + (tdir.mkpath("dir1a/file1"), op::Op::REMOVE, None), + (tdir.mkpath("dir1b"), op::Op::CREATE, None), + (tdir.mkpath("dir1b/file1"), op::Op::CREATE, None), + (tdir.mkpath("dir1b/file2"), op::Op::CREATE, None), + ] + ); } } @@ -646,14 +797,13 @@ fn poll_watch_recursive_move() { fn poll_watch_recursive_move_in() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "watch_dir", - "dir1a/dir1", - ]); + tdir.create_all(vec!["watch_dir", "dir1a/dir1"]); let (tx, rx) = mpsc::channel(); let mut watcher = PollWatcher::with_delay_ms(tx, 50).expect("failed to create poll watcher"); - watcher.watch(tdir.mkpath("watch_dir"), RecursiveMode::Recursive).expect("failed to watch directory"); + watcher + .watch(tdir.mkpath("watch_dir"), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep(1100); // PollWatcher has only a resolution of 1 second @@ -663,21 +813,35 @@ fn poll_watch_recursive_move_in() { let mut actual = recv_events(&rx); actual.sort_by(|a, b| a.0.cmp(&b.0)); - if cfg!(target_os="windows") { - assert_eq!(actual, vec![ - (tdir.mkpath("watch_dir"), op::Op::WRITE, None), // parent directory gets modified - (tdir.mkpath("watch_dir/dir1b"), op::Op::CREATE, None), - (tdir.mkpath("watch_dir/dir1b/dir1"), op::Op::CREATE, None), - (tdir.mkpath("watch_dir/dir1b/dir1"), op::Op::WRITE, None), // extra write event - (tdir.mkpath("watch_dir/dir1b/dir1/file1"), op::Op::CREATE, None), - ]); + if cfg!(target_os = "windows") { + assert_eq!( + actual, + vec![ + (tdir.mkpath("watch_dir"), op::Op::WRITE, None), // parent directory gets modified + (tdir.mkpath("watch_dir/dir1b"), op::Op::CREATE, None), + (tdir.mkpath("watch_dir/dir1b/dir1"), op::Op::CREATE, None), + (tdir.mkpath("watch_dir/dir1b/dir1"), op::Op::WRITE, None), // extra write event + ( + tdir.mkpath("watch_dir/dir1b/dir1/file1"), + op::Op::CREATE, + None + ), + ] + ); } else { - assert_eq!(actual, vec![ - (tdir.mkpath("watch_dir"), op::Op::WRITE, None), // parent directory gets modified - (tdir.mkpath("watch_dir/dir1b"), op::Op::CREATE, None), - (tdir.mkpath("watch_dir/dir1b/dir1"), op::Op::CREATE, None), - (tdir.mkpath("watch_dir/dir1b/dir1/file1"), op::Op::CREATE, None), - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("watch_dir"), op::Op::WRITE, None), // parent directory gets modified + (tdir.mkpath("watch_dir/dir1b"), op::Op::CREATE, None), + (tdir.mkpath("watch_dir/dir1b/dir1"), op::Op::CREATE, None), + ( + tdir.mkpath("watch_dir/dir1b/dir1/file1"), + op::Op::CREATE, + None + ), + ] + ); } } @@ -686,13 +850,13 @@ fn poll_watch_recursive_move_in() { fn poll_watch_recursive_move_out() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "watch_dir/dir1a/dir1", - ]); + tdir.create_all(vec!["watch_dir/dir1a/dir1"]); let (tx, rx) = mpsc::channel(); let mut watcher = PollWatcher::with_delay_ms(tx, 50).expect("failed to create poll watcher"); - watcher.watch(tdir.mkpath("watch_dir"), RecursiveMode::Recursive).expect("failed to watch directory"); + watcher + .watch(tdir.mkpath("watch_dir"), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep(1100); // PollWatcher has only a resolution of 1 second @@ -701,10 +865,17 @@ fn poll_watch_recursive_move_out() { let mut actual = recv_events(&rx); actual.sort_by(|a, b| a.0.cmp(&b.0)); - assert_eq!(actual, vec![ - (tdir.mkpath("watch_dir/dir1a/dir1"), op::Op::WRITE, None), // parent directory gets modified - (tdir.mkpath("watch_dir/dir1a/dir1/file1"), op::Op::CREATE, None), - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("watch_dir/dir1a/dir1"), op::Op::WRITE, None), // parent directory gets modified + ( + tdir.mkpath("watch_dir/dir1a/dir1/file1"), + op::Op::CREATE, + None + ), + ] + ); sleep(1100); // PollWatcher has only a resolution of 1 second @@ -714,65 +885,72 @@ fn poll_watch_recursive_move_out() { actual = recv_events(&rx); actual.sort_by(|a, b| a.0.cmp(&b.0)); - assert_eq!(actual, vec![ - (tdir.mkpath("watch_dir"), op::Op::WRITE, None), // parent directory gets modified - (tdir.mkpath("watch_dir/dir1a"), op::Op::REMOVE, None), - (tdir.mkpath("watch_dir/dir1a/dir1"), op::Op::REMOVE, None), - (tdir.mkpath("watch_dir/dir1a/dir1/file1"), op::Op::REMOVE, None), - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("watch_dir"), op::Op::WRITE, None), // parent directory gets modified + (tdir.mkpath("watch_dir/dir1a"), op::Op::REMOVE, None), + (tdir.mkpath("watch_dir/dir1a/dir1"), op::Op::REMOVE, None), + ( + tdir.mkpath("watch_dir/dir1a/dir1/file1"), + op::Op::REMOVE, + None + ), + ] + ); } #[test] fn poll_watch_nonrecursive() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1", - ]); + tdir.create_all(vec!["dir1"]); let (tx, rx) = mpsc::channel(); let mut watcher = PollWatcher::with_delay_ms(tx, 50).expect("failed to create poll watcher"); - watcher.watch(tdir.mkpath("."), RecursiveMode::NonRecursive).expect("failed to watch directory"); + watcher + .watch(tdir.mkpath("."), RecursiveMode::NonRecursive) + .expect("failed to watch directory"); sleep(1100); // PollWatcher has only a resolution of 1 second - tdir.create_all(vec![ - "file1", - "dir1/file1", - "dir2/file1", - ]); + tdir.create_all(vec!["file1", "dir1/file1", "dir2/file1"]); let mut actual = recv_events(&rx); actual.sort_by(|a, b| a.0.cmp(&b.0)); - assert_eq!(actual, vec![ - (tdir.mkpath("."), op::Op::WRITE, None), // parent directory gets modified - (tdir.mkpath("dir1"), op::Op::WRITE, None), // parent directory gets modified - (tdir.mkpath("dir2"), op::Op::CREATE, None), - (tdir.mkpath("file1"), op::Op::CREATE, None), - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("."), op::Op::WRITE, None), // parent directory gets modified + (tdir.mkpath("dir1"), op::Op::WRITE, None), // parent directory gets modified + (tdir.mkpath("dir2"), op::Op::CREATE, None), + (tdir.mkpath("file1"), op::Op::CREATE, None), + ] + ); } #[test] fn poll_watch_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1", - ]); + tdir.create_all(vec!["file1"]); let (tx, rx) = mpsc::channel(); let mut watcher = PollWatcher::with_delay_ms(tx, 50).expect("failed to create poll watcher"); - watcher.watch(tdir.mkpath("file1"), RecursiveMode::Recursive).expect("failed to watch directory"); + watcher + .watch(tdir.mkpath("file1"), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep(1100); // PollWatcher has only a resolution of 1 second tdir.write("file1"); tdir.create("file2"); - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("file1"), op::Op::WRITE, None) - ]); + assert_eq!( + recv_events(&rx), + vec![(tdir.mkpath("file1"), op::Op::WRITE, None)] + ); } #[test] @@ -783,8 +961,11 @@ fn watch_nonexisting() { sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(&tdir1.mkpath("."), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(&tdir1.mkpath("."), RecursiveMode::Recursive) + .expect("failed to watch directory"); let result = watcher.watch(&tdir2.mkpath("non_existing"), RecursiveMode::Recursive); assert!(result.is_err()); @@ -794,19 +975,24 @@ fn watch_nonexisting() { tdir1.create("file1"); - if cfg!(target_os="macos") { - assert_eq!(inflate_events(recv_events(&rx)), vec![ - (tdir1.mkpath("file1"), op::Op::CREATE, None), - ]); - } else if cfg!(target_os="windows") { - assert_eq!(recv_events(&rx), vec![ - (tdir1.mkpath("file1"), op::Op::CREATE, None) - ]); + if cfg!(target_os = "macos") { + assert_eq!( + inflate_events(recv_events(&rx)), + vec![(tdir1.mkpath("file1"), op::Op::CREATE, None),] + ); + } else if cfg!(target_os = "windows") { + assert_eq!( + recv_events(&rx), + vec![(tdir1.mkpath("file1"), op::Op::CREATE, None)] + ); } else { - assert_eq!(recv_events(&rx), vec![ - (tdir1.mkpath("file1"), op::Op::CREATE, None), - (tdir1.mkpath("file1"), op::Op::CLOSE_WRITE, None) - ]); + assert_eq!( + recv_events(&rx), + vec![ + (tdir1.mkpath("file1"), op::Op::CREATE, None), + (tdir1.mkpath("file1"), op::Op::CLOSE_WRITE, None) + ] + ); } } @@ -814,15 +1000,16 @@ fn watch_nonexisting() { fn unwatch_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1", - ]); + tdir.create_all(vec!["file1"]); sleep_macos(10); let (tx, _) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(&tdir.mkpath("file1"), RecursiveMode::Recursive).expect("failed to watch file"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(&tdir.mkpath("file1"), RecursiveMode::Recursive) + .expect("failed to watch file"); match watcher.unwatch(&tdir.mkpath("file1")) { Ok(_) => (), @@ -834,15 +1021,16 @@ fn unwatch_file() { fn unwatch_directory() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1", - ]); + tdir.create_all(vec!["dir1"]); sleep_macos(10); let (tx, _) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(&tdir.mkpath("dir1"), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(&tdir.mkpath("dir1"), RecursiveMode::Recursive) + .expect("failed to watch directory"); match watcher.unwatch(&tdir.mkpath("dir1")) { Ok(_) => (), @@ -858,7 +1046,8 @@ fn unwatch_nonexisting() { sleep_macos(10); let (tx, _) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); match watcher.unwatch(&tdir.mkpath("file1")) { Err(Error::WatchNotFound) => (), @@ -871,42 +1060,45 @@ fn unwatch_nonexisting() { fn self_delete_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1", - ]); + tdir.create_all(vec!["file1"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(&tdir.mkpath("file1"), RecursiveMode::Recursive).expect("failed to watch file"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(&tdir.mkpath("file1"), RecursiveMode::Recursive) + .expect("failed to watch file"); sleep_windows(100); tdir.remove("file1"); - let actual = if cfg!(target_os="macos") { + let actual = if cfg!(target_os = "macos") { inflate_events(recv_events(&rx)) } else { recv_events(&rx) }; - if cfg!(target_os="windows") { - assert_eq!(actual, vec![ - (tdir.mkpath("file1"), op::Op::REMOVE, None), - ]); - } else if cfg!(target_os="macos") { - assert_eq!(actual, vec![ - (tdir.mkpath("file1"), op::Op::CREATE | op::Op::REMOVE, None), - ]); + if cfg!(target_os = "windows") { + assert_eq!(actual, vec![(tdir.mkpath("file1"), op::Op::REMOVE, None),]); + } else if cfg!(target_os = "macos") { + assert_eq!( + actual, + vec![(tdir.mkpath("file1"), op::Op::CREATE | op::Op::REMOVE, None),] + ); } else { - assert_eq!(actual, vec![ - (tdir.mkpath("file1"), op::Op::CHMOD, None), - (tdir.mkpath("file1"), op::Op::REMOVE, None), - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("file1"), op::Op::CHMOD, None), + (tdir.mkpath("file1"), op::Op::REMOVE, None), + ] + ); } - if cfg!(not(any(target_os="windows", target_os="macos"))) { + if cfg!(not(any(target_os = "windows", target_os = "macos"))) { tdir.create("file1"); assert_eq!(recv_events(&rx), vec![]); @@ -924,57 +1116,58 @@ fn self_delete_file() { fn self_delete_directory() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1", - ]); + tdir.create_all(vec!["dir1"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(&tdir.mkpath("dir1"), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(&tdir.mkpath("dir1"), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.remove("dir1"); - let actual = if cfg!(target_os="macos") { + let actual = if cfg!(target_os = "macos") { inflate_events(recv_events(&rx)) } else { recv_events(&rx) }; - if cfg!(target_os="windows") { - assert_eq!(actual, vec![ - (tdir.mkpath("dir1"), Op::empty(), None), - ]); - } else if cfg!(target_os="macos") { - assert_eq!(actual, vec![ - (tdir.mkpath("dir1"), op::Op::CREATE | op::Op::REMOVE, None), - ]); + if cfg!(target_os = "windows") { + assert_eq!(actual, vec![(tdir.mkpath("dir1"), Op::empty(), None),]); + } else if cfg!(target_os = "macos") { + assert_eq!( + actual, + vec![(tdir.mkpath("dir1"), op::Op::CREATE | op::Op::REMOVE, None),] + ); } else { - assert_eq!(actual, vec![ - (tdir.mkpath("dir1"), op::Op::REMOVE, None), - ]); + assert_eq!(actual, vec![(tdir.mkpath("dir1"), op::Op::REMOVE, None),]); } tdir.create("dir1"); - let actual = if cfg!(target_os="macos") { + let actual = if cfg!(target_os = "macos") { inflate_events(recv_events(&rx)) } else { recv_events(&rx) }; - if cfg!(target_os="macos") { - assert_eq!(actual, vec![ - (tdir.mkpath("dir1"), op::Op::CREATE | op::Op::REMOVE, None), // excessive remove event - ]); + if cfg!(target_os = "macos") { + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1"), op::Op::CREATE | op::Op::REMOVE, None), // excessive remove event + ] + ); } else { assert_eq!(actual, vec![]); } - if cfg!(not(any(target_os="windows", target_os="macos"))) { + if cfg!(not(any(target_os = "windows", target_os = "macos"))) { match watcher.unwatch(&tdir.mkpath("dir1")) { Err(Error::WatchNotFound) => (), Err(e) => panic!("{:?}", e), @@ -988,73 +1181,86 @@ fn self_delete_directory() { fn self_rename_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "file1", - ]); + tdir.create_all(vec!["file1"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(&tdir.mkpath("file1"), RecursiveMode::Recursive).expect("failed to watch file"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(&tdir.mkpath("file1"), RecursiveMode::Recursive) + .expect("failed to watch file"); sleep_windows(100); tdir.rename("file1", "file2"); - let actual = if cfg!(target_os="macos") { + let actual = if cfg!(target_os = "macos") { inflate_events(recv_events(&rx)) } else { recv_events(&rx) }; - if cfg!(target_os="macos") { - assert_eq!(actual, vec![ - (tdir.mkpath("file1"), op::Op::CREATE | op::Op::RENAME, None), // excessive create event - ]); + if cfg!(target_os = "macos") { + assert_eq!( + actual, + vec![ + (tdir.mkpath("file1"), op::Op::CREATE | op::Op::RENAME, None), // excessive create event + ] + ); } else { - assert_eq!(actual, vec![ - (tdir.mkpath("file1"), op::Op::RENAME, None), - ]); + assert_eq!(actual, vec![(tdir.mkpath("file1"), op::Op::RENAME, None),]); } tdir.write("file2"); - let actual = if cfg!(target_os="macos") { + let actual = if cfg!(target_os = "macos") { inflate_events(recv_events(&rx)) } else { recv_events(&rx) }; - if cfg!(target_os="windows") { + if cfg!(target_os = "windows") { assert_eq!(actual, vec![]); panic!("windows back-end should update file watch path"); - } else if cfg!(target_os="macos") { + } else if cfg!(target_os = "macos") { // macos doesn't watch files, but paths assert_eq!(actual, vec![]); - } else if cfg!(target_os="linux") { - assert_eq!(actual, vec![ - (tdir.mkpath("file1"), op::Op::WRITE, None), // path doesn't get updated - (tdir.mkpath("file1"), op::Op::CLOSE_WRITE, None), // path doesn't get updated - ]); + } else if cfg!(target_os = "linux") { + assert_eq!( + actual, + vec![ + (tdir.mkpath("file1"), op::Op::WRITE, None), // path doesn't get updated + (tdir.mkpath("file1"), op::Op::CLOSE_WRITE, None), // path doesn't get updated + ] + ); } else { - assert_eq!(actual, vec![ + assert_eq!( + actual, + vec![ (tdir.mkpath("file1"), op::Op::WRITE, None), // path doesn't get updated - ]); + ] + ); } tdir.create("file1"); - if cfg!(target_os="macos") { + if cfg!(target_os = "macos") { // macos doesn't watch files, but paths - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("file1"), op::Op::CREATE | op::Op::RENAME, None), // excessive rename event - ]); + assert_eq!( + recv_events(&rx), + vec![ + (tdir.mkpath("file1"), op::Op::CREATE | op::Op::RENAME, None), // excessive rename event + ] + ); } else { assert_eq!(recv_events(&rx), vec![]); } - watcher.unwatch(&tdir.mkpath("file1")).expect("failed to unwatch file"); // use old path to unwatch + watcher + .unwatch(&tdir.mkpath("file1")) + .expect("failed to unwatch file"); // use old path to unwatch let result = watcher.unwatch(&tdir.mkpath("file1")); match result { @@ -1068,81 +1274,94 @@ fn self_rename_file() { fn self_rename_directory() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1", - ]); + tdir.create_all(vec!["dir1"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(&tdir.mkpath("dir1"), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(&tdir.mkpath("dir1"), RecursiveMode::Recursive) + .expect("failed to watch directory"); sleep_windows(100); tdir.rename("dir1", "dir2"); - let actual = if cfg!(target_os="macos") { + let actual = if cfg!(target_os = "macos") { inflate_events(recv_events(&rx)) } else { recv_events(&rx) }; - if cfg!(target_os="windows") { + if cfg!(target_os = "windows") { assert_eq!(actual, vec![]); - } else if cfg!(target_os="macos") { - assert_eq!(actual, vec![ - (tdir.mkpath("dir1"), op::Op::CREATE | op::Op::RENAME, None), // excessive create event - ]); + } else if cfg!(target_os = "macos") { + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1"), op::Op::CREATE | op::Op::RENAME, None), // excessive create event + ] + ); } else { - assert_eq!(actual, vec![ - (tdir.mkpath("dir1"), op::Op::RENAME, None), - ]); + assert_eq!(actual, vec![(tdir.mkpath("dir1"), op::Op::RENAME, None),]); } tdir.create("dir2/file1"); - let actual = if cfg!(target_os="macos") { + let actual = if cfg!(target_os = "macos") { inflate_events(recv_events(&rx)) } else { recv_events(&rx) }; - if cfg!(target_os="macos") { + if cfg!(target_os = "macos") { // macos doesn't watch files, but paths assert_eq!(actual, vec![]); - } else if cfg!(target_os="linux") { - assert_eq!(actual, vec![ - (tdir.mkpath("dir1/file1"), op::Op::CREATE, None), // path doesn't get updated - (tdir.mkpath("dir1/file1"), op::Op::CLOSE_WRITE, None) - ]); + } else if cfg!(target_os = "linux") { + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1/file1"), op::Op::CREATE, None), // path doesn't get updated + (tdir.mkpath("dir1/file1"), op::Op::CLOSE_WRITE, None) + ] + ); } else { - assert_eq!(actual, vec![ + assert_eq!( + actual, + vec![ (tdir.mkpath("dir1/file1"), op::Op::CREATE, None) // path doesn't get updated - ]); + ] + ); } tdir.create("dir1"); - let actual = if cfg!(target_os="macos") { + let actual = if cfg!(target_os = "macos") { inflate_events(recv_events(&rx)) } else { recv_events(&rx) }; - if cfg!(target_os="macos") { + if cfg!(target_os = "macos") { // macos doesn't watch files, but paths - assert_eq!(actual, vec![ - (tdir.mkpath("dir1"), op::Op::CREATE | op::Op::RENAME, None), // excessive rename event - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1"), op::Op::CREATE | op::Op::RENAME, None), // excessive rename event + ] + ); } else { assert_eq!(actual, vec![]); } - watcher.unwatch(&tdir.mkpath("dir1")).expect("failed to unwatch directory"); // use old path to unwatch + watcher + .unwatch(&tdir.mkpath("dir1")) + .expect("failed to unwatch directory"); // use old path to unwatch let result = watcher.unwatch(&tdir.mkpath("dir1")); - if cfg!(target_os="windows") { + if cfg!(target_os = "windows") { match result { Err(e) => panic!("{:?}", e), Ok(()) => (), @@ -1160,15 +1379,16 @@ fn self_rename_directory() { fn parent_rename_file() { let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1/file1", - ]); + tdir.create_all(vec!["dir1/file1"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(&tdir.mkpath("dir1/file1"), RecursiveMode::Recursive).expect("failed to watch file"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(&tdir.mkpath("dir1/file1"), RecursiveMode::Recursive) + .expect("failed to watch file"); sleep_windows(100); @@ -1178,41 +1398,50 @@ fn parent_rename_file() { tdir.write("dir2/file1"); - let actual = if cfg!(target_os="macos") { + let actual = if cfg!(target_os = "macos") { inflate_events(recv_events(&rx)) } else { recv_events(&rx) }; - if cfg!(target_os="macos") { + if cfg!(target_os = "macos") { // macos doesn't watch files, but paths assert_eq!(actual, vec![]); - } else if cfg!(target_os="linux") { - assert_eq!(actual, vec![ - (tdir.mkpath("dir1/file1"), op::Op::WRITE, None), // path doesn't get updated - (tdir.mkpath("dir1/file1"), op::Op::CLOSE_WRITE, None) - ]); + } else if cfg!(target_os = "linux") { + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1/file1"), op::Op::WRITE, None), // path doesn't get updated + (tdir.mkpath("dir1/file1"), op::Op::CLOSE_WRITE, None) + ] + ); } else { - assert_eq!(actual, vec![ + assert_eq!( + actual, + vec![ (tdir.mkpath("dir1/file1"), op::Op::WRITE, None) // path doesn't get updated - ]); + ] + ); } tdir.create("dir1/file1"); - if cfg!(target_os="macos") { + if cfg!(target_os = "macos") { // macos doesn't watch files, but paths - assert_eq!(recv_events(&rx), vec![ - (tdir.mkpath("dir1/file1"), op::Op::CREATE, None), - ]); + assert_eq!( + recv_events(&rx), + vec![(tdir.mkpath("dir1/file1"), op::Op::CREATE, None),] + ); } else { assert_eq!(recv_events(&rx), vec![]); } - watcher.unwatch(&tdir.mkpath("dir1/file1")).expect("failed to unwatch file"); // use old path to unwatch + watcher + .unwatch(&tdir.mkpath("dir1/file1")) + .expect("failed to unwatch file"); // use old path to unwatch let result = watcher.unwatch(&tdir.mkpath("dir1/file1")); - if cfg!(target_os="windows") { + if cfg!(target_os = "windows") { match result { Err(e) => panic!("{:?}", e), Ok(()) => (), @@ -1230,21 +1459,22 @@ fn parent_rename_file() { #[cfg_attr(target_os = "windows", ignore)] fn parent_rename_directory() { // removing the parent directory doesn't work on windows - if cfg!(target_os="windows") { + if cfg!(target_os = "windows") { panic!("cannot remove parent directory on windows"); } let tdir = TempDir::new("temp_dir").expect("failed to create temporary directory"); - tdir.create_all(vec![ - "dir1/watch_dir", - ]); + tdir.create_all(vec!["dir1/watch_dir"]); sleep_macos(10); let (tx, rx) = mpsc::channel(); - let mut watcher: RecommendedWatcher = Watcher::new_raw(tx).expect("failed to create recommended watcher"); - watcher.watch(&tdir.mkpath("dir1/watch_dir"), RecursiveMode::Recursive).expect("failed to watch directory"); + let mut watcher: RecommendedWatcher = + Watcher::new_raw(tx).expect("failed to create recommended watcher"); + watcher + .watch(&tdir.mkpath("dir1/watch_dir"), RecursiveMode::Recursive) + .expect("failed to watch directory"); tdir.rename("dir1", "dir2"); @@ -1252,44 +1482,57 @@ fn parent_rename_directory() { tdir.create("dir2/watch_dir/file1"); - let actual = if cfg!(target_os="macos") { + let actual = if cfg!(target_os = "macos") { inflate_events(recv_events(&rx)) } else { recv_events(&rx) }; - if cfg!(target_os="macos") { + if cfg!(target_os = "macos") { // macos doesn't watch files, but paths assert_eq!(actual, vec![]); - } else if cfg!(target_os="linux") { - assert_eq!(actual, vec![ - (tdir.mkpath("dir1/watch_dir/file1"), op::Op::CREATE, None), // path doesn't get updated - (tdir.mkpath("dir1/watch_dir/file1"), op::Op::CLOSE_WRITE, None), // path doesn't get updated - ]); + } else if cfg!(target_os = "linux") { + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1/watch_dir/file1"), op::Op::CREATE, None), // path doesn't get updated + ( + tdir.mkpath("dir1/watch_dir/file1"), + op::Op::CLOSE_WRITE, + None + ), // path doesn't get updated + ] + ); } else { - assert_eq!(actual, vec![ - (tdir.mkpath("dir1/watch_dir/file1"), op::Op::CREATE, None), // path doesn't get updated - ]); + assert_eq!( + actual, + vec![ + (tdir.mkpath("dir1/watch_dir/file1"), op::Op::CREATE, None), // path doesn't get updated + ] + ); } tdir.create("dir1/watch_dir"); - let actual = if cfg!(target_os="macos") { + let actual = if cfg!(target_os = "macos") { inflate_events(recv_events(&rx)) } else { recv_events(&rx) }; - if cfg!(target_os="macos") { + if cfg!(target_os = "macos") { // macos doesn't watch files, but paths - assert_eq!(actual, vec![ - (tdir.mkpath("dir1/watch_dir"), op::Op::CREATE, None), - ]); + assert_eq!( + actual, + vec![(tdir.mkpath("dir1/watch_dir"), op::Op::CREATE, None),] + ); } else { assert_eq!(actual, vec![]); } - watcher.unwatch(&tdir.mkpath("dir1/watch_dir")).expect("failed to unwatch directory"); // use old path to unwatch + watcher + .unwatch(&tdir.mkpath("dir1/watch_dir")) + .expect("failed to unwatch directory"); // use old path to unwatch let result = watcher.unwatch(&tdir.mkpath("dir1/watch_dir")); match result { diff --git a/tests/windows.rs b/tests/windows.rs index a55c2828..3cd7b51b 100644 --- a/tests/windows.rs +++ b/tests/windows.rs @@ -7,21 +7,19 @@ extern crate tempdir; mod windows_tests { use super::notify::*; use super::tempdir::TempDir; + use std::sync::mpsc; use std::thread; use std::time::{Duration, Instant}; - use std::sync::mpsc; fn wait_for_disconnect(rx: &mpsc::Receiver) { loop { match rx.try_recv() { Err(mpsc::TryRecvError::Disconnected) => break, Err(mpsc::TryRecvError::Empty) => (), - Ok(res) => { - match res.op { - Err(e) => panic!("unexpected err: {:?}: {:?}", e, res.path), - _ => (), - } - } + Ok(res) => match res.op { + Err(e) => panic!("unexpected err: {:?}: {:?}", e, res.path), + _ => (), + }, } thread::sleep(Duration::from_millis(10)); } @@ -60,14 +58,15 @@ mod windows_tests { wait_for_disconnect(&rx); - const TIMEOUT_MS: u64 = 60000; // give it PLENTY of time before we declare failure + const TIMEOUT_MS: u64 = 60000; // give it PLENTY of time before we declare failure let start = Instant::now(); let mut watchers_shutdown = 0; - while watchers_shutdown != dir_count && start.elapsed() < Duration::from_millis(TIMEOUT_MS) { + while watchers_shutdown != dir_count && start.elapsed() < Duration::from_millis(TIMEOUT_MS) + { if let Ok(actual) = meta_rx.try_recv() { match actual { - windows::MetaEvent::SingleWatchComplete => watchers_shutdown += 1, + windows::MetaEvent::SingleWatchComplete => watchers_shutdown += 1, _ => (), } }