Add watch run unit test

This commit is contained in:
thePR0M3TH3AN
2025-05-20 15:18:49 -04:00
parent 121394cb4b
commit daeec03a60
5 changed files with 57 additions and 1 deletions

2
Cargo.lock generated
View File

@@ -691,7 +691,9 @@ dependencies = [
"ctrlc", "ctrlc",
"dirs 5.0.1", "dirs 5.0.1",
"glob", "glob",
"libc",
"libmarlin", "libmarlin",
"once_cell",
"predicates", "predicates",
"rusqlite", "rusqlite",
"serde", "serde",

View File

@@ -22,12 +22,15 @@ tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["fmt", "env-filter"] } tracing-subscriber = { version = "0.3", features = ["fmt", "env-filter"] }
walkdir = "2.5" walkdir = "2.5"
serde_json = { version = "1", optional = true } serde_json = { version = "1", optional = true }
once_cell = "1"
[dev-dependencies] [dev-dependencies]
assert_cmd = "2" assert_cmd = "2"
predicates = "3" predicates = "3"
tempfile = "3" tempfile = "3"
dirs = "5" dirs = "5"
once_cell = "1"
libc = "0.2"
[features] [features]
# Enable JSON output with `--features json` # Enable JSON output with `--features json`

View File

@@ -11,6 +11,17 @@ use std::thread;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use tracing::info; use tracing::info;
use once_cell::sync::Lazy;
use std::sync::Mutex;
#[allow(dead_code)]
static LAST_WATCHER_STATE: Lazy<Mutex<Option<WatcherState>>> = Lazy::new(|| Mutex::new(None));
#[allow(dead_code)]
pub fn last_watcher_state() -> Option<WatcherState> {
LAST_WATCHER_STATE.lock().unwrap().clone()
}
/// Commands related to file watching functionality /// Commands related to file watching functionality
#[derive(Subcommand, Debug)] #[derive(Subcommand, Debug)]
pub enum WatchCmd { pub enum WatchCmd {
@@ -84,7 +95,11 @@ pub fn run(cmd: &WatchCmd, _conn: &mut Connection, _format: super::Format) -> Re
} }
info!("Watcher run loop ended. Explicitly stopping watcher instance..."); info!("Watcher run loop ended. Explicitly stopping watcher instance...");
watcher.stop()?; watcher.stop()?;
{
let mut guard = LAST_WATCHER_STATE.lock().unwrap();
*guard = Some(watcher.status().state);
}
info!("Watcher instance fully stopped."); info!("Watcher instance fully stopped.");
Ok(()) Ok(())
} }

1
cli-bin/src/lib.rs Normal file
View File

@@ -0,0 +1 @@
pub mod cli;

View File

@@ -0,0 +1,35 @@
use std::thread;
use std::time::Duration;
use tempfile::tempdir;
use marlin_cli::cli::{watch, Format};
use marlin_cli::cli::watch::WatchCmd;
use libmarlin::watcher::WatcherState;
use libmarlin::{self as marlin, db};
use libc;
#[test]
fn watch_start_and_stop_quickly() {
let tmp = tempdir().unwrap();
let db_path = tmp.path().join("index.db");
std::env::set_var("MARLIN_DB_PATH", &db_path);
// create database
let _m = marlin::Marlin::open_default().unwrap();
let mut conn = db::open(&db_path).unwrap();
let path = tmp.path().to_path_buf();
let cmd = WatchCmd::Start { path: path.clone(), debounce_ms: 50 };
// send SIGINT shortly after watcher starts
let t = thread::spawn(|| {
thread::sleep(Duration::from_millis(200));
unsafe { libc::raise(libc::SIGINT) };
});
watch::run(&cmd, &mut conn, Format::Text).unwrap();
t.join().unwrap();
assert_eq!(watch::last_watcher_state(), Some(WatcherState::Stopped));
}