Add watcher tests
Browse files at this commitfa3bc69f94fdd2a14e21bf0573722fcafbc045baParent: 363e1d83 changed files +267 -1
- Cargo.lock +16 -0
- Cargo.toml +1 -0
- src/watcher.rs +250 -1
Cargo.lockmodified+16 -0
| @@ -2119,6 +2119,7 @@ dependencies = [ | ||
| 2119 | 2119 | "tracy-client", |
| 2120 | 2120 | "url", |
| 2121 | 2121 | "xcursor", |
| 2122 | + "xshell", | |
| 2122 | 2123 | "zbus", |
| 2123 | 2124 | ] |
| 2124 | 2125 | |
| @@ -4146,6 +4147,21 @@ version = "0.8.19" | ||
| 4146 | 4147 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 4147 | 4148 | checksum = "0fcb9cbac069e033553e8bb871be2fbdffcab578eb25bd0f7c508cedc6dcd75a" |
| 4148 | 4149 | |
| 4150 | +[[package]] | |
| 4151 | +name = "xshell" | |
| 4152 | +version = "0.2.5" | |
| 4153 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 4154 | +checksum = "ce2107fe03e558353b4c71ad7626d58ed82efaf56c54134228608893c77023ad" | |
| 4155 | +dependencies = [ | |
| 4156 | + "xshell-macros", | |
| 4157 | +] | |
| 4158 | + | |
| 4159 | +[[package]] | |
| 4160 | +name = "xshell-macros" | |
| 4161 | +version = "0.2.5" | |
| 4162 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 4163 | +checksum = "7e2c411759b501fb9501aac2b1b2d287a6e93e5bdcf13c25306b23e1b716dd0e" | |
| 4164 | + | |
| 4149 | 4165 | [[package]] |
| 4150 | 4166 | name = "yansi-term" |
| 4151 | 4167 | version = "0.1.2" |
Cargo.tomlmodified+1 -0
| @@ -94,6 +94,7 @@ features = [ | ||
| 94 | 94 | [dev-dependencies] |
| 95 | 95 | proptest = "1.4.0" |
| 96 | 96 | proptest-derive = "0.4.0" |
| 97 | +xshell = "0.2.5" | |
| 97 | 98 | |
| 98 | 99 | [features] |
| 99 | 100 | default = ["dbus", "xdp-gnome-screencast"] |
src/watcher.rsmodified+250 -1
| @@ -2,7 +2,7 @@ | ||
| 2 | 2 | |
| 3 | 3 | use std::path::PathBuf; |
| 4 | 4 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 5 | -use std::sync::Arc; | |
| 5 | +use std::sync::{mpsc, Arc}; | |
| 6 | 6 | use std::thread; |
| 7 | 7 | use std::time::Duration; |
| 8 | 8 | |
| @@ -20,6 +20,14 @@ impl Drop for Watcher { | ||
| 20 | 20 | |
| 21 | 21 | impl Watcher { |
| 22 | 22 | pub fn new(path: PathBuf, changed: SyncSender<()>) -> Self { |
| 23 | + Self::with_start_notification(path, changed, None) | |
| 24 | + } | |
| 25 | + | |
| 26 | + pub fn with_start_notification( | |
| 27 | + path: PathBuf, | |
| 28 | + changed: SyncSender<()>, | |
| 29 | + started: Option<mpsc::SyncSender<()>>, | |
| 30 | + ) -> Self { | |
| 23 | 31 | let should_stop = Arc::new(AtomicBool::new(false)); |
| 24 | 32 | |
| 25 | 33 | { |
| @@ -40,6 +48,10 @@ impl Watcher { | ||
| 40 | 48 | .and_then(|canon| Ok((canon.metadata()?.modified()?, canon))) |
| 41 | 49 | .ok(); |
| 42 | 50 | |
| 51 | + if let Some(started) = started { | |
| 52 | + let _ = started.send(()); | |
| 53 | + } | |
| 54 | + | |
| 43 | 55 | loop { |
| 44 | 56 | thread::sleep(Duration::from_millis(500)); |
| 45 | 57 | |
| @@ -72,3 +84,240 @@ impl Watcher { | ||
| 72 | 84 | Self { should_stop } |
| 73 | 85 | } |
| 74 | 86 | } |
| 87 | + | |
| 88 | +#[cfg(test)] | |
| 89 | +mod tests { | |
| 90 | + use std::error::Error; | |
| 91 | + use std::fs::File; | |
| 92 | + use std::io::Write; | |
| 93 | + use std::sync::atomic::AtomicU8; | |
| 94 | + use std::time::SystemTime; | |
| 95 | + | |
| 96 | + use calloop::channel::sync_channel; | |
| 97 | + use calloop::EventLoop; | |
| 98 | + use xshell::{cmd, Shell}; | |
| 99 | + | |
| 100 | + use super::*; | |
| 101 | + | |
| 102 | + fn check( | |
| 103 | + setup: impl FnOnce(&Shell) -> Result<(), Box<dyn Error>>, | |
| 104 | + change: impl FnOnce(&Shell) -> Result<(), Box<dyn Error>>, | |
| 105 | + ) { | |
| 106 | + let sh = Shell::new().unwrap(); | |
| 107 | + let temp_dir = sh.create_temp_dir().unwrap(); | |
| 108 | + sh.change_dir(temp_dir.path()); | |
| 109 | + // let dir = sh.create_dir("xshell").unwrap(); | |
| 110 | + // sh.change_dir(dir); | |
| 111 | + | |
| 112 | + let mut config_path = sh.current_dir(); | |
| 113 | + config_path.push("niri"); | |
| 114 | + config_path.push("config.kdl"); | |
| 115 | + | |
| 116 | + setup(&sh).unwrap(); | |
| 117 | + | |
| 118 | + let changed = AtomicU8::new(0); | |
| 119 | + | |
| 120 | + let mut event_loop = EventLoop::try_new().unwrap(); | |
| 121 | + let loop_handle = event_loop.handle(); | |
| 122 | + | |
| 123 | + let (tx, rx) = sync_channel(1); | |
| 124 | + let (started_tx, started_rx) = mpsc::sync_channel(1); | |
| 125 | + let _watcher = Watcher::with_start_notification(config_path.clone(), tx, Some(started_tx)); | |
| 126 | + loop_handle | |
| 127 | + .insert_source(rx, |_, _, _| { | |
| 128 | + changed.fetch_add(1, Ordering::SeqCst); | |
| 129 | + }) | |
| 130 | + .unwrap(); | |
| 131 | + started_rx.recv().unwrap(); | |
| 132 | + | |
| 133 | + // HACK: if we don't sleep, files might have the same mtime. | |
| 134 | + thread::sleep(Duration::from_millis(100)); | |
| 135 | + | |
| 136 | + change(&sh).unwrap(); | |
| 137 | + | |
| 138 | + event_loop | |
| 139 | + .dispatch(Duration::from_millis(750), &mut ()) | |
| 140 | + .unwrap(); | |
| 141 | + | |
| 142 | + assert_eq!(changed.load(Ordering::SeqCst), 1); | |
| 143 | + | |
| 144 | + // Verify that the watcher didn't break. | |
| 145 | + sh.write_file(&config_path, "c").unwrap(); | |
| 146 | + | |
| 147 | + event_loop | |
| 148 | + .dispatch(Duration::from_millis(750), &mut ()) | |
| 149 | + .unwrap(); | |
| 150 | + | |
| 151 | + assert_eq!(changed.load(Ordering::SeqCst), 2); | |
| 152 | + } | |
| 153 | + | |
| 154 | + #[test] | |
| 155 | + fn change_file() { | |
| 156 | + check( | |
| 157 | + |sh| { | |
| 158 | + sh.write_file("niri/config.kdl", "a")?; | |
| 159 | + Ok(()) | |
| 160 | + }, | |
| 161 | + |sh| { | |
| 162 | + sh.write_file("niri/config.kdl", "b")?; | |
| 163 | + Ok(()) | |
| 164 | + }, | |
| 165 | + ); | |
| 166 | + } | |
| 167 | + | |
| 168 | + #[test] | |
| 169 | + fn create_file() { | |
| 170 | + check( | |
| 171 | + |sh| { | |
| 172 | + sh.create_dir("niri")?; | |
| 173 | + Ok(()) | |
| 174 | + }, | |
| 175 | + |sh| { | |
| 176 | + sh.write_file("niri/config.kdl", "a")?; | |
| 177 | + Ok(()) | |
| 178 | + }, | |
| 179 | + ); | |
| 180 | + } | |
| 181 | + | |
| 182 | + #[test] | |
| 183 | + fn create_dir_and_file() { | |
| 184 | + check( | |
| 185 | + |_sh| Ok(()), | |
| 186 | + |sh| { | |
| 187 | + sh.write_file("niri/config.kdl", "a")?; | |
| 188 | + Ok(()) | |
| 189 | + }, | |
| 190 | + ); | |
| 191 | + } | |
| 192 | + | |
| 193 | + #[test] | |
| 194 | + fn change_linked_file() { | |
| 195 | + check( | |
| 196 | + |sh| { | |
| 197 | + sh.write_file("niri/config2.kdl", "a")?; | |
| 198 | + cmd!(sh, "ln -s config2.kdl niri/config.kdl").run()?; | |
| 199 | + Ok(()) | |
| 200 | + }, | |
| 201 | + |sh| { | |
| 202 | + sh.write_file("niri/config2.kdl", "b")?; | |
| 203 | + Ok(()) | |
| 204 | + }, | |
| 205 | + ); | |
| 206 | + } | |
| 207 | + | |
| 208 | + #[test] | |
| 209 | + fn change_file_in_linked_dir() { | |
| 210 | + check( | |
| 211 | + |sh| { | |
| 212 | + sh.write_file("niri2/config.kdl", "a")?; | |
| 213 | + cmd!(sh, "ln -s niri2 niri").run()?; | |
| 214 | + Ok(()) | |
| 215 | + }, | |
| 216 | + |sh| { | |
| 217 | + sh.write_file("niri2/config.kdl", "b")?; | |
| 218 | + Ok(()) | |
| 219 | + }, | |
| 220 | + ); | |
| 221 | + } | |
| 222 | + | |
| 223 | + #[test] | |
| 224 | + fn recreate_file() { | |
| 225 | + check( | |
| 226 | + |sh| { | |
| 227 | + sh.write_file("niri/config.kdl", "a")?; | |
| 228 | + Ok(()) | |
| 229 | + }, | |
| 230 | + |sh| { | |
| 231 | + sh.remove_path("niri/config.kdl")?; | |
| 232 | + sh.write_file("niri/config.kdl", "b")?; | |
| 233 | + Ok(()) | |
| 234 | + }, | |
| 235 | + ); | |
| 236 | + } | |
| 237 | + | |
| 238 | + #[test] | |
| 239 | + fn recreate_dir() { | |
| 240 | + check( | |
| 241 | + |sh| { | |
| 242 | + sh.write_file("niri/config.kdl", "a")?; | |
| 243 | + Ok(()) | |
| 244 | + }, | |
| 245 | + |sh| { | |
| 246 | + sh.remove_path("niri")?; | |
| 247 | + sh.write_file("niri/config.kdl", "b")?; | |
| 248 | + Ok(()) | |
| 249 | + }, | |
| 250 | + ); | |
| 251 | + } | |
| 252 | + | |
| 253 | + #[test] | |
| 254 | + fn swap_dir() { | |
| 255 | + check( | |
| 256 | + |sh| { | |
| 257 | + sh.write_file("niri/config.kdl", "a")?; | |
| 258 | + Ok(()) | |
| 259 | + }, | |
| 260 | + |sh| { | |
| 261 | + sh.write_file("niri2/config.kdl", "b")?; | |
| 262 | + sh.remove_path("niri")?; | |
| 263 | + cmd!(sh, "mv niri2 niri").run()?; | |
| 264 | + Ok(()) | |
| 265 | + }, | |
| 266 | + ); | |
| 267 | + } | |
| 268 | + | |
| 269 | + #[test] | |
| 270 | + fn swap_just_link() { | |
| 271 | + // NixOS setup: link path changes, mtime stays constant. | |
| 272 | + check( | |
| 273 | + |sh| { | |
| 274 | + let mut dir = sh.current_dir(); | |
| 275 | + dir.push("niri"); | |
| 276 | + sh.create_dir(&dir)?; | |
| 277 | + | |
| 278 | + let mut d2 = dir.clone(); | |
| 279 | + d2.push("config2.kdl"); | |
| 280 | + let mut c2 = File::create(d2).unwrap(); | |
| 281 | + write!(c2, "a")?; | |
| 282 | + c2.flush()?; | |
| 283 | + c2.set_modified(SystemTime::UNIX_EPOCH)?; | |
| 284 | + c2.sync_all()?; | |
| 285 | + drop(c2); | |
| 286 | + | |
| 287 | + let mut d3 = dir.clone(); | |
| 288 | + d3.push("config3.kdl"); | |
| 289 | + let mut c3 = File::create(d3).unwrap(); | |
| 290 | + write!(c3, "b")?; | |
| 291 | + c3.flush()?; | |
| 292 | + c3.set_modified(SystemTime::UNIX_EPOCH)?; | |
| 293 | + c3.sync_all()?; | |
| 294 | + drop(c3); | |
| 295 | + | |
| 296 | + cmd!(sh, "ln -s config2.kdl niri/config.kdl").run()?; | |
| 297 | + Ok(()) | |
| 298 | + }, | |
| 299 | + |sh| { | |
| 300 | + cmd!(sh, "unlink niri/config.kdl").run()?; | |
| 301 | + cmd!(sh, "ln -s config3.kdl niri/config.kdl").run()?; | |
| 302 | + Ok(()) | |
| 303 | + }, | |
| 304 | + ); | |
| 305 | + } | |
| 306 | + | |
| 307 | + #[test] | |
| 308 | + fn swap_dir_link() { | |
| 309 | + check( | |
| 310 | + |sh| { | |
| 311 | + sh.write_file("niri2/config.kdl", "a")?; | |
| 312 | + cmd!(sh, "ln -s niri2 niri").run()?; | |
| 313 | + Ok(()) | |
| 314 | + }, | |
| 315 | + |sh| { | |
| 316 | + sh.write_file("niri3/config.kdl", "b")?; | |
| 317 | + cmd!(sh, "unlink niri").run()?; | |
| 318 | + cmd!(sh, "ln -s niri3 niri").run()?; | |
| 319 | + Ok(()) | |
| 320 | + }, | |
| 321 | + ); | |
| 322 | + } | |
| 323 | +} |