-
-
Save AnthoniG/e53500ca3f2286941b9be30f9c2a825b to your computer and use it in GitHub Desktop.
Demo of [notify#181](https://github.com/passcod/notify/issues/181)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extern crate notify; | |
| use notify::{Watcher as _}; | |
| use std::fs; | |
| #[allow(unused_imports)] | |
| use std::io::{self, Read as _}; | |
| use std::sync::mpsc; | |
| use std::thread; | |
| use std::time::Duration; | |
| const WATCHER_DELAY: Duration = Duration::from_millis(100); | |
| fn main() -> io::Result<()> { | |
| let root = "tmp"; | |
| let from = &format!("{}/foo", root); | |
| let to = &format!("{}/bar", root); | |
| thread::sleep(Duration::from_secs(1)); | |
| if let Some(metadata) = fs::metadata(root).ok() { | |
| assert!(metadata.is_dir()); | |
| } else { | |
| println!("Creating directory: {}", root); | |
| fs::create_dir(root).ok(); | |
| } | |
| match fs::metadata(from) { | |
| Err(ref err) if err.kind() == io::ErrorKind::NotFound => { } | |
| _ => panic!("remove {} before running test", from), | |
| } | |
| match fs::metadata(to) { | |
| Err(ref err) if err.kind() == io::ErrorKind::NotFound => { } | |
| _ => panic!("remove {} before running test", to), | |
| } | |
| let (raw_sender, raw_receiver) = mpsc::channel(); | |
| let mut raw_watcher = notify::raw_watcher(raw_sender).unwrap(); | |
| let (db_sender, db_receiver) = mpsc::channel(); | |
| let mut db_watcher = notify::watcher(db_sender, WATCHER_DELAY).unwrap(); | |
| raw_watcher.watch(root, notify::RecursiveMode::Recursive).unwrap(); | |
| db_watcher.watch(root, notify::RecursiveMode::Recursive).unwrap(); | |
| let _handle = thread::spawn(|| for event in raw_receiver { | |
| println!("raw event: {:?}", &event); | |
| }); | |
| let _handle = thread::spawn(|| for event in db_receiver { | |
| println!("debounced event: {:?}", &event); | |
| }); | |
| println!("Writing file: {}", from); | |
| fs::write(from, "Hello")?; | |
| thread::sleep(Duration::from_secs(1)); | |
| println!("Renaming file: {} to {}", from, to); | |
| fs::rename(from, to)?; | |
| // Double-check everything happened as expected | |
| { | |
| println!("checking {} is gone", from); | |
| match fs::metadata(from) { | |
| Err(ref err) if err.kind() == io::ErrorKind::NotFound => { } | |
| _ => panic!("found {} even after renaming it?", from), | |
| } | |
| } | |
| { | |
| println!("checking {} is present", to); | |
| let _contents = { | |
| let mut c = String::new(); | |
| fs::File::open(to)?.read_to_string(&mut c)?; | |
| c | |
| }; | |
| { | |
| println!("checking {} has expected content", to); | |
| assert_eq!(_contents, "Hello"); | |
| } | |
| } | |
| thread::sleep(Duration::from_secs(1)); | |
| Ok(()) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| % rm -rf tmp && cargo run | |
| warning: unused manifest key: package.target | |
| Finished dev [unoptimized + debuginfo] target(s) in 0.21s | |
| Running `target/debug/ra-issue-734` | |
| Creating directory: tmp | |
| Writing file: tmp/foo | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp"), op: Ok(CREATE | REMOVE), cookie: None } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | WRITE), cookie: None } | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp") | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo") | |
| Renaming file: tmp/foo to tmp/bar | |
| checking tmp/foo is gone | |
| checking tmp/bar is present | |
| checking tmp/bar has expected content | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | RENAME | WRITE), cookie: Some(4124192) } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar"), op: Ok(REMOVE | RENAME), cookie: Some(4124192) } | |
| debounced event: NoticeRemove("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| debounced event: Remove("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| % rm -rf tmp && cargo run | |
| warning: unused manifest key: package.target | |
| Finished dev [unoptimized + debuginfo] target(s) in 0.05s | |
| Running `target/debug/ra-issue-734` | |
| Creating directory: tmp | |
| Writing file: tmp/foo | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp"), op: Ok(CREATE | REMOVE), cookie: None } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | RENAME | WRITE), cookie: None } | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp") | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo") | |
| Renaming file: tmp/foo to tmp/bar | |
| checking tmp/foo is gone | |
| checking tmp/bar is present | |
| checking tmp/bar has expected content | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(RENAME), cookie: Some(4124247) } | |
| debounced event: NoticeRemove("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo") | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar"), op: Ok(RENAME), cookie: Some(4124247) } | |
| debounced event: Rename("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo", "/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| % rm -rf tmp && cargo run | |
| warning: unused manifest key: package.target | |
| Finished dev [unoptimized + debuginfo] target(s) in 0.05s | |
| Running `target/debug/ra-issue-734` | |
| Creating directory: tmp | |
| Writing file: tmp/foo | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp"), op: Ok(CREATE | REMOVE), cookie: None } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | RENAME | WRITE), cookie: None } | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp") | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo") | |
| Renaming file: tmp/foo to tmp/bar | |
| checking tmp/foo is gone | |
| checking tmp/bar is present | |
| checking tmp/bar has expected content | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | RENAME | WRITE), cookie: Some(4124310) } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar"), op: Ok(REMOVE | RENAME), cookie: Some(4124310) } | |
| debounced event: NoticeRemove("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| debounced event: Remove("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| % rm -rf tmp && cargo run | |
| warning: unused manifest key: package.target | |
| Finished dev [unoptimized + debuginfo] target(s) in 0.05s | |
| Running `target/debug/ra-issue-734` | |
| Creating directory: tmp | |
| Writing file: tmp/foo | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp"), op: Ok(CREATE | REMOVE), cookie: None } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | RENAME | WRITE), cookie: None } | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp") | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo") | |
| Renaming file: tmp/foo to tmp/bar | |
| checking tmp/foo is gone | |
| checking tmp/bar is present | |
| checking tmp/bar has expected content | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | RENAME | WRITE), cookie: Some(4124371) } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar"), op: Ok(REMOVE | RENAME), cookie: Some(4124371) } | |
| debounced event: NoticeRemove("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| debounced event: Remove("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| % rm -rf tmp && cargo run | |
| warning: unused manifest key: package.target | |
| Finished dev [unoptimized + debuginfo] target(s) in 0.05s | |
| Running `target/debug/ra-issue-734` | |
| Creating directory: tmp | |
| Writing file: tmp/foo | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp"), op: Ok(CREATE | REMOVE), cookie: None } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | RENAME | WRITE), cookie: None } | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp") | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo") | |
| Renaming file: tmp/foo to tmp/bar | |
| checking tmp/foo is gone | |
| checking tmp/bar is present | |
| checking tmp/bar has expected content | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | RENAME | WRITE), cookie: Some(4124414) } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar"), op: Ok(REMOVE | RENAME), cookie: Some(4124414) } | |
| debounced event: NoticeRemove("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| debounced event: Remove("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| % rm -rf tmp && cargo run | |
| warning: unused manifest key: package.target | |
| Finished dev [unoptimized + debuginfo] target(s) in 0.05s | |
| Running `target/debug/ra-issue-734` | |
| Creating directory: tmp | |
| Writing file: tmp/foo | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp"), op: Ok(CREATE | REMOVE), cookie: None } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | RENAME | WRITE), cookie: None } | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp") | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo") | |
| Renaming file: tmp/foo to tmp/bar | |
| checking tmp/foo is gone | |
| checking tmp/bar is present | |
| checking tmp/bar has expected content | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | RENAME | WRITE), cookie: Some(4124463) } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar"), op: Ok(REMOVE | RENAME), cookie: Some(4124463) } | |
| debounced event: NoticeRemove("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| debounced event: Remove("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| % rm -rf tmp && cargo run | |
| warning: unused manifest key: package.target | |
| Finished dev [unoptimized + debuginfo] target(s) in 0.05s | |
| Running `target/debug/ra-issue-734` | |
| Creating directory: tmp | |
| Writing file: tmp/foo | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp"), op: Ok(CREATE | REMOVE), cookie: None } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | RENAME | WRITE), cookie: None } | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp") | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo") | |
| Renaming file: tmp/foo to tmp/bar | |
| checking tmp/foo is gone | |
| checking tmp/bar is present | |
| checking tmp/bar has expected content | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | RENAME | WRITE), cookie: Some(4124539) } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar"), op: Ok(REMOVE | RENAME), cookie: Some(4124539) } | |
| debounced event: NoticeRemove("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| debounced event: Remove("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| % rm -rf tmp && cargo run | |
| warning: unused manifest key: package.target | |
| Finished dev [unoptimized + debuginfo] target(s) in 0.05s | |
| Running `target/debug/ra-issue-734` | |
| Creating directory: tmp | |
| Writing file: tmp/foo | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp"), op: Ok(CREATE | REMOVE), cookie: None } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | RENAME | WRITE), cookie: None } | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp") | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo") | |
| Renaming file: tmp/foo to tmp/bar | |
| checking tmp/foo is gone | |
| checking tmp/bar is present | |
| checking tmp/bar has expected content | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | RENAME | WRITE), cookie: Some(4124582) } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar"), op: Ok(REMOVE | RENAME), cookie: Some(4124582) } | |
| debounced event: NoticeRemove("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| debounced event: Remove("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| % rm -rf tmp && cargo run | |
| warning: unused manifest key: package.target | |
| Finished dev [unoptimized + debuginfo] target(s) in 0.05s | |
| Running `target/debug/ra-issue-734` | |
| Creating directory: tmp | |
| Writing file: tmp/foo | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp"), op: Ok(CREATE | REMOVE), cookie: None } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | WRITE), cookie: None } | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp") | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo") | |
| Renaming file: tmp/foo to tmp/bar | |
| checking tmp/foo is gone | |
| checking tmp/bar is present | |
| checking tmp/bar has expected content | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | RENAME | WRITE), cookie: Some(4124682) } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar"), op: Ok(REMOVE | RENAME), cookie: Some(4124682) } | |
| debounced event: NoticeRemove("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| debounced event: Remove("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| % rm -rf tmp && cargo run | |
| warning: unused manifest key: package.target | |
| Finished dev [unoptimized + debuginfo] target(s) in 0.05s | |
| Running `target/debug/ra-issue-734` | |
| Creating directory: tmp | |
| Writing file: tmp/foo | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | RENAME | WRITE), cookie: None } | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo") | |
| Renaming file: tmp/foo to tmp/bar | |
| checking tmp/foo is gone | |
| checking tmp/bar is present | |
| checking tmp/bar has expected content | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | RENAME | WRITE), cookie: Some(4124980) } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar"), op: Ok(REMOVE | RENAME), cookie: Some(4124980) } | |
| debounced event: NoticeRemove("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| debounced event: Remove("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| % rm -rf tmp && cargo run | |
| warning: unused manifest key: package.target | |
| Finished dev [unoptimized + debuginfo] target(s) in 0.05s | |
| Running `target/debug/ra-issue-734` | |
| Creating directory: tmp | |
| Writing file: tmp/foo | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp"), op: Ok(CREATE), cookie: None } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | WRITE), cookie: None } | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp") | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo") | |
| Renaming file: tmp/foo to tmp/bar | |
| checking tmp/foo is gone | |
| checking tmp/bar is present | |
| checking tmp/bar has expected content | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/foo"), op: Ok(CREATE | RENAME | WRITE), cookie: Some(4125655) } | |
| raw event: RawEvent { path: Some("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar"), op: Ok(RENAME), cookie: Some(4125655) } | |
| debounced event: Create("/Users/fklock/Dev/Rust/ra-issue-734/tmp/bar") | |
| % |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment