use aktrs::actor::{testkit::ActorTestKit, SpawnOptions}; use aktrs::fs; use aktrs::reexport::notify::RecursiveMode; use std::io::prelude::*; use std::thread; use std::time::Duration; use tempfile::NamedTempFile; #[aktrs::test] #[ignore] // XXX: FS watcher planned for deprecation fn create_file(mut t: ActorTestKit) -> aktrs::Result<()> { let d = tempfile::tempdir()?; let d_path = std::fs::canonicalize(d.path())?; // avoid events for the creation of the temp dir thread::sleep(Duration::from_millis(50)); let mut probe = t.create_anonymous_test_probe(); let h = probe.pid(); let cfg = fs::watcher::Config { debounce: Duration::from_millis(500) }; let mut watcher = t.spawn(fs::Watcher::actor(cfg, h), SpawnOptions::default()); let cmd = fs::watcher::Command::Watch(d_path.clone(), RecursiveMode::Recursive); t.block_on(watcher.tell(cmd))?; probe.expect_message(fs::watcher::Event::Watching(d_path.clone().to_path_buf())); let f = NamedTempFile::new_in(d.path())?; let f_path = std::fs::canonicalize(f.path())?; match probe.receive_message_within(Duration::from_millis(1000)) { fs::watcher::Event::Update(k, ps) if k.is_create() && ps == vec![f_path] => (), other => panic!("received an invalid event: {:?}", other), } Ok(()) } #[aktrs::test] #[ignore] // XXX: FS watcher planned for deprecation fn modify_file(mut t: ActorTestKit) -> aktrs::Result<()> { let d = tempfile::tempdir()?; let d_path = std::fs::canonicalize(d.path())?; let f = NamedTempFile::new_in(d.path())?; let f_path = std::fs::canonicalize(f.path())?; // avoid events for the creation of the temp dir/file thread::sleep(Duration::from_millis(50)); let mut probe = t.create_anonymous_test_probe(); let h = probe.pid(); let cfg = fs::watcher::Config { debounce: Duration::from_millis(500) }; let mut watcher = t.spawn(fs::Watcher::actor(cfg, h), SpawnOptions::default()); let cmd = fs::watcher::Command::Watch(d_path.clone(), RecursiveMode::Recursive); t.block_on(watcher.tell(cmd))?; probe.expect_message(fs::watcher::Event::Watching(d_path.clone().to_path_buf())); let mut f = f.into_file(); f.write_all(b"hello, world!")?; drop(f); match probe.receive_message_within(Duration::from_millis(1000)) { fs::watcher::Event::Update(k, ps) if k.is_modify() && ps == vec![f_path] => (), other => panic!("received an invalid event: {:?}", other), } Ok(()) } #[aktrs::test] #[ignore] // XXX: FS watcher planned for deprecation fn remove_file(mut t: ActorTestKit) -> aktrs::Result<()> { let d = tempfile::tempdir()?; let d_path = std::fs::canonicalize(d.path())?; let f = NamedTempFile::new_in(d.path())?; let f_path = std::fs::canonicalize(f.path())?; // avoid events for the creation of the temp dir/file thread::sleep(Duration::from_millis(50)); let mut probe = t.create_anonymous_test_probe(); let h = probe.pid(); let cfg = fs::watcher::Config { debounce: Duration::from_millis(500) }; let mut watcher = t.spawn(fs::Watcher::actor(cfg, h), SpawnOptions::default()); let cmd = fs::watcher::Command::Watch(d_path.clone(), RecursiveMode::Recursive); t.block_on(watcher.tell(cmd))?; probe.expect_message(fs::watcher::Event::Watching(d_path.clone().to_path_buf())); std::fs::remove_file(&f_path)?; match probe.receive_message_within(Duration::from_millis(1000)) { fs::watcher::Event::Update(k, ps) if k.is_remove() && ps == vec![f_path] => (), other => panic!("received an invalid event: {:?}", other), } Ok(()) }