use std::path::Path;
use std::time::{Duration, Instant};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WatchScope {
All,
Suite(String),
Test(String),
}
pub const DEBOUNCE: Duration = Duration::from_millis(300);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WatchNotice {
Quiet,
NewlyRed,
}
#[derive(Debug, Default)]
pub struct TestWatch {
scope: Option<WatchScope>,
due_at: Option<Instant>,
awaiting: bool,
red: bool,
}
impl TestWatch {
pub fn scope(&self) -> Option<&WatchScope> {
self.scope.as_ref()
}
pub fn is_watching(&self, scope: &WatchScope) -> bool {
self.scope.as_ref() == Some(scope)
}
pub fn toggle(&mut self, scope: WatchScope) -> bool {
if self.is_watching(&scope) {
self.clear();
return false;
}
*self = Self {
scope: Some(scope),
..Self::default()
};
true
}
pub fn clear(&mut self) {
*self = Self::default();
}
pub fn on_saved(&mut self, path: &Path, root: &Path, now: Instant) {
if self.scope.is_some() && path.starts_with(root) {
self.due_at = Some(now + DEBOUNCE);
}
}
pub fn take_due(&mut self, now: Instant, busy: bool) -> Option<WatchScope> {
if busy || self.due_at.is_none_or(|due| now < due) {
return None;
}
self.due_at = None;
self.awaiting = true;
self.scope.clone()
}
pub fn finished(&mut self, ok: Option<bool>) -> WatchNotice {
if !std::mem::take(&mut self.awaiting) {
return WatchNotice::Quiet;
}
let Some(ok) = ok else {
return WatchNotice::Quiet;
};
let was_red = std::mem::replace(&mut self.red, !ok);
if !ok && !was_red {
WatchNotice::NewlyRed
} else {
WatchNotice::Quiet
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn at(ms: u64) -> Instant {
thread_local!(static BASE: Instant = Instant::now());
BASE.with(|b| *b + Duration::from_millis(ms))
}
#[test]
fn toggling_the_same_scope_turns_it_off_and_another_replaces_it() {
let mut w = TestWatch::default();
assert!(w.toggle(WatchScope::Test("a::b".into())));
assert!(w.is_watching(&WatchScope::Test("a::b".into())));
assert!(w.toggle(WatchScope::Suite("a".into())));
assert_eq!(w.scope(), Some(&WatchScope::Suite("a".into())));
assert!(!w.toggle(WatchScope::Suite("a".into())));
assert_eq!(w.scope(), None);
}
#[test]
fn a_save_under_the_root_reruns_once_after_the_debounce() {
let root = Path::new("/w");
let mut w = TestWatch::default();
w.on_saved(Path::new("/w/src/a.rs"), root, at(0));
assert_eq!(w.take_due(at(100), false), None, "not watching: nothing");
w.toggle(WatchScope::All);
w.on_saved(Path::new("/elsewhere/a.rs"), root, at(0));
assert_eq!(w.take_due(at(1000), false), None, "outside the root");
w.on_saved(Path::new("/w/src/a.rs"), root, at(0));
w.on_saved(Path::new("/w/src/b.rs"), root, at(200));
assert_eq!(w.take_due(at(400), false), None, "the burst pushed it back");
assert_eq!(w.take_due(at(500), false), Some(WatchScope::All));
assert_eq!(w.take_due(at(600), false), None, "one rerun per burst");
}
#[test]
fn a_due_rerun_waits_for_a_busy_runner_instead_of_being_dropped() {
let mut w = TestWatch::default();
w.toggle(WatchScope::Test("t".into()));
w.on_saved(Path::new("/w/a.rs"), Path::new("/w"), at(0));
assert_eq!(w.take_due(at(500), true), None);
assert_eq!(
w.take_due(at(900), false),
Some(WatchScope::Test("t".into()))
);
}
#[test]
fn only_a_watched_run_turning_red_is_reported() {
let mut w = TestWatch::default();
w.toggle(WatchScope::All);
assert_eq!(
w.finished(Some(false)),
WatchNotice::Quiet,
"a run the user started"
);
let rerun = |w: &mut TestWatch, ms: u64| {
w.on_saved(Path::new("/w/a.rs"), Path::new("/w"), at(ms));
w.take_due(at(ms + 400), false).unwrap();
};
rerun(&mut w, 0);
assert_eq!(w.finished(Some(false)), WatchNotice::NewlyRed);
rerun(&mut w, 1000);
assert_eq!(
w.finished(Some(false)),
WatchNotice::Quiet,
"still red: no repeat"
);
rerun(&mut w, 2000);
assert_eq!(
w.finished(Some(true)),
WatchNotice::Quiet,
"green is silent"
);
rerun(&mut w, 3000);
assert_eq!(
w.finished(Some(false)),
WatchNotice::NewlyRed,
"red again after green"
);
rerun(&mut w, 4000);
assert_eq!(
w.finished(None),
WatchNotice::Quiet,
"a discovery is not a verdict"
);
}
}