use anyhow::Result;
use fs_usage_sys::FsUsageMonitorBuilder;
use std::thread;
use std::time::Duration;
#[test]
#[cfg(target_os = "macos")]
fn test_monitor_can_start_and_stop() -> Result<()> {
let mut monitor = FsUsageMonitorBuilder::new()
.watch_path("/tmp")
.exclude_process("mds")
.exclude_process("mdworker")
.build()?;
match monitor.start() {
Ok(_) => {
thread::sleep(Duration::from_millis(100));
monitor.stop()?;
}
Err(e) => {
let error_msg = e.to_string();
assert!(
error_msg.contains("must be run as root")
|| error_msg.contains("exited with code 1")
|| error_msg.contains("Failed to start fs_usage"),
"Expected permission error, got: {}",
error_msg
);
}
}
Ok(())
}
#[test]
#[cfg(target_os = "macos")]
#[ignore = "requires sudo/root permissions"]
fn test_monitor_lifecycle_with_sudo() -> Result<()> {
let mut monitor = FsUsageMonitorBuilder::new()
.watch_path("/tmp")
.exclude_process("mds")
.exclude_process("mdworker")
.build()?;
match monitor.start() {
Ok(_) => {}
Err(e) => {
let error_msg = e.to_string();
if error_msg.contains("Resource busy") || error_msg.contains("ktrace_start") {
eprintln!("Test skipped: Another fs_usage or ktrace process is already running");
return Ok(());
}
return Err(e);
}
}
thread::sleep(Duration::from_secs(1));
let events = monitor.events();
while events.try_recv().is_ok() {
}
monitor.stop()?;
Ok(())
}