| @@ -0,0 +1,141 @@ |
| 1 | +//! `louise test --watch`: run the tests, then again whenever a source |
| 2 | +//! changes (issue #316). With the kept results ([`crate::test_cache`]), |
| 3 | +//! a run after an edit runs only the tests the edit can affect. |
| 4 | +//! |
| 5 | +//! Watching is polling, with no dependency: every 300 ms, the size and |
| 6 | +//! modification time of every watched file. Watched are the package's |
| 7 | +//! `.lse` files under `src/` and `test/` (new ones included), its |
| 8 | +//! `louise.toml` and `louise.lock`, and every source the last run read |
| 9 | +//! from disk (path dependencies' too). Ctrl-C stops it. |
| 10 | + |
| 11 | +use std::collections::BTreeMap; |
| 12 | +use std::path::{Path, PathBuf}; |
| 13 | +use std::process::ExitCode; |
| 14 | +use std::time::{Duration, SystemTime}; |
| 15 | + |
| 16 | +/// Every watched file's size and modification time. |
| 17 | +#[derive(PartialEq, Debug, Default)] |
| 18 | +pub(crate) struct Snapshot(BTreeMap<PathBuf, Option<(u64, SystemTime)>>); |
| 19 | + |
| 20 | +/// The watched files now: those under `dirs` with an `.lse` extension, |
| 21 | +/// and `files`. |
| 22 | +pub(crate) fn snapshot(dirs: &[PathBuf], files: &[PathBuf]) -> Snapshot { |
| 23 | + let mut found = BTreeMap::new(); |
| 24 | + for dir in dirs { |
| 25 | + sources_in(dir, &mut found); |
| 26 | + } |
| 27 | + for file in files { |
| 28 | + found.insert(file.clone(), stamp(file)); |
| 29 | + } |
| 30 | + Snapshot(found) |
| 31 | +} |
| 32 | + |
| 33 | +fn stamp(path: &Path) -> Option<(u64, SystemTime)> { |
| 34 | + let meta = std::fs::metadata(path).ok()?; |
| 35 | + Some((meta.len(), meta.modified().ok()?)) |
| 36 | +} |
| 37 | + |
| 38 | +fn sources_in(dir: &Path, found: &mut BTreeMap<PathBuf, Option<(u64, SystemTime)>>) { |
| 39 | + let Ok(entries) = std::fs::read_dir(dir) else { |
| 40 | + return; |
| 41 | + }; |
| 42 | + for entry in entries.flatten() { |
| 43 | + let path = entry.path(); |
| 44 | + if path.is_dir() { |
| 45 | + sources_in(&path, found); |
| 46 | + } else if path.extension().is_some_and(|e| e == "lse") { |
| 47 | + found.insert(path.clone(), stamp(&path)); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/// The first file that changed, appeared or went away from `old` to `new`. |
| 53 | +pub(crate) fn first_change(old: &Snapshot, new: &Snapshot) -> Option<PathBuf> { |
| 54 | + new.0 |
| 55 | + .iter() |
| 56 | + .find(|(path, stamp)| old.0.get(*path) != Some(*stamp)) |
| 57 | + .map(|(path, _)| path.clone()) |
| 58 | + .or_else(|| old.0.keys().find(|p| !new.0.contains_key(*p)).cloned()) |
| 59 | +} |
| 60 | + |
| 61 | +/// The root of the package `path` is in (the directory with a |
| 62 | +/// `louise.toml`), if any. |
| 63 | +fn package_root(path: Option<&Path>) -> Option<PathBuf> { |
| 64 | + let start = std::path::absolute(path.unwrap_or(Path::new("."))).ok()?; |
| 65 | + start |
| 66 | + .ancestors() |
| 67 | + .find(|dir| dir.join(louise_package::MANIFEST).is_file()) |
| 68 | + .map(Path::to_owned) |
| 69 | +} |
| 70 | + |
| 71 | +/// Run `run` (the tests: its exit code, and the sources it read), then |
| 72 | +/// again after every change to what it is built from, until interrupted. |
| 73 | +pub(crate) fn watch( |
| 74 | + path: Option<&Path>, |
| 75 | + mut run: impl FnMut() -> (ExitCode, Vec<PathBuf>), |
| 76 | +) -> ExitCode { |
| 77 | + let (dirs, mut fixed) = match package_root(path) { |
| 78 | + Some(root) => ( |
| 79 | + vec![root.join("src"), root.join("test")], |
| 80 | + vec![ |
| 81 | + root.join(louise_package::MANIFEST), |
| 82 | + root.join(louise_package::LOCKFILE), |
| 83 | + ], |
| 84 | + ), |
| 85 | + None => (Vec::new(), path.map(Path::to_owned).into_iter().collect()), |
| 86 | + }; |
| 87 | + fixed.sort(); |
| 88 | + loop { |
| 89 | + let (_, sources) = run(); |
| 90 | + let mut files = fixed.clone(); |
| 91 | + files.extend(sources); |
| 92 | + let before = snapshot(&dirs, &files); |
| 93 | + let changed = loop { |
| 94 | + std::thread::sleep(Duration::from_millis(300)); |
| 95 | + if let Some(path) = first_change(&before, &snapshot(&dirs, &files)) { |
| 96 | + break path; |
| 97 | + } |
| 98 | + }; |
| 99 | + println!("\n--- {} changed: running again ---\n", changed.display()); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +#[cfg(test)] |
| 104 | +mod tests { |
| 105 | + use super::*; |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn a_change_an_addition_and_a_removal_are_seen() { |
| 109 | + let dir = std::env::temp_dir().join(format!("louise-watch-{}", std::process::id())); |
| 110 | + let _ = std::fs::remove_dir_all(&dir); |
| 111 | + std::fs::create_dir_all(dir.join("src/deep")).unwrap(); |
| 112 | + let a = dir.join("src/a.lse"); |
| 113 | + std::fs::write(&a, "1").unwrap(); |
| 114 | + std::fs::write(dir.join("src/notes.txt"), "ignored").unwrap(); |
| 115 | + let manifest = dir.join("louise.toml"); |
| 116 | + let dirs = [dir.join("src")]; |
| 117 | + let files = [manifest.clone()]; |
| 118 | + let start = snapshot(&dirs, &files); |
| 119 | + assert_eq!(first_change(&start, &snapshot(&dirs, &files)), None); |
| 120 | + std::fs::write(dir.join("src/notes.txt"), "still ignored").unwrap(); |
| 121 | + assert_eq!(first_change(&start, &snapshot(&dirs, &files)), None); |
| 122 | + std::fs::write(&a, "22").unwrap(); |
| 123 | + assert_eq!( |
| 124 | + first_change(&start, &snapshot(&dirs, &files)), |
| 125 | + Some(a.clone()) |
| 126 | + ); |
| 127 | + let added = dir.join("src/deep/b.lse"); |
| 128 | + std::fs::write(&added, "").unwrap(); |
| 129 | + let now = snapshot(&dirs, &files); |
| 130 | + assert_eq!(first_change(&snapshot(&dirs, &files), &now), None); |
| 131 | + std::fs::remove_file(&added).unwrap(); |
| 132 | + assert_eq!(first_change(&now, &snapshot(&dirs, &files)), Some(added)); |
| 133 | + let before = snapshot(&dirs, &files); |
| 134 | + std::fs::write(&manifest, "[package]").unwrap(); |
| 135 | + assert_eq!( |
| 136 | + first_change(&before, &snapshot(&dirs, &files)), |
| 137 | + Some(manifest) |
| 138 | + ); |
| 139 | + let _ = std::fs::remove_dir_all(&dir); |
| 140 | + } |
| 141 | +} |