niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·2d82fdd·Ivan Molodetskikh·1 year ago·2536 commits
| 1 | //! File modification watcher. |
| 2 | |
| 3 | use std::collections::HashMap; |
| 4 | use std::path::{Path, PathBuf}; |
| 5 | use std::sync::mpsc; |
| 6 | use std::time::{Duration, SystemTime}; |
| 7 | use std::{io, thread}; |
| 8 | |
| 9 | use niri_config::{Config, ConfigParseResult, ConfigPath}; |
| 10 | use smithay::reexports::calloop::channel::SyncSender; |
| 11 | |
| 12 | use crate::niri::State; |
| 13 | |
| 14 | const POLLING_INTERVAL: Duration = Duration::from_millis(500); |
| 15 | |
| 16 | pub struct Watcher { |
| 17 | load_config: mpsc::Sender<()>, |
| 18 | } |
| 19 | |
| 20 | struct WatcherInner { |
| 21 | /// The paths we're watching. |
| 22 | path: ConfigPath, |
| 23 | |
| 24 | /// Last observed props of the watched file. |
| 25 | last_props: Option<Props>, |
| 26 | |
| 27 | /// Last observed props for included files. |
| 28 | includes: HashMap<PathBuf, Option<Props>>, |
| 29 | } |
| 30 | |
| 31 | /// Properties of the watched file. |
| 32 | /// |
| 33 | /// Equality on this means the file did not change. |
| 34 | #[derive(Debug, PartialEq, Eq)] |
| 35 | struct Props { |
| 36 | /// Modification time of the watched file. |
| 37 | mtime: SystemTime, |
| 38 | |
| 39 | /// Canonical form of the watched path. |
| 40 | /// |
| 41 | /// We store the absolute path in addition to mtime to account for symlinked configs where the |
| 42 | /// symlink target may change without mtime. This is common on nix where everything is a |
| 43 | /// symlink to /nix/store, which keeps no mtime (= 1970-01-01). |
| 44 | canonical: PathBuf, |
| 45 | } |
| 46 | |
| 47 | #[derive(Debug, PartialEq, Eq)] |
| 48 | enum CheckResult { |
| 49 | Missing, |
| 50 | Unchanged, |
| 51 | Changed, |
| 52 | } |
| 53 | |
| 54 | impl Watcher { |
| 55 | pub fn new( |
| 56 | path: ConfigPath, |
| 57 | includes: Vec<PathBuf>, |
| 58 | mut process: impl FnMut(&ConfigPath) -> ConfigParseResult<Config, ()> + Send + 'static, |
| 59 | changed: SyncSender<Result<Config, ()>>, |
| 60 | ) -> Self { |
| 61 | let (load_config, load_config_rx) = mpsc::channel(); |
| 62 | |
| 63 | thread::Builder::new() |
| 64 | .name(format!("Filesystem Watcher for {path:?}")) |
| 65 | .spawn(move || { |
| 66 | let mut inner = WatcherInner::new(path, includes); |
| 67 | |
| 68 | loop { |
| 69 | let mut should_load = match load_config_rx.recv_timeout(POLLING_INTERVAL) { |
| 70 | Ok(()) => true, |
| 71 | Err(mpsc::RecvTimeoutError::Disconnected) => break, |
| 72 | Err(mpsc::RecvTimeoutError::Timeout) => false, |
| 73 | }; |
| 74 | |
| 75 | match inner.check() { |
| 76 | CheckResult::Missing => continue, |
| 77 | CheckResult::Unchanged => (), |
| 78 | CheckResult::Changed => { |
| 79 | trace!("config file changed"); |
| 80 | should_load = true; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if should_load { |
| 85 | let res = process(&inner.path); |
| 86 | |
| 87 | if let Err(err) = changed.send(res.config) { |
| 88 | warn!("error sending change notification: {err:?}"); |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | // There's a bit of time here between reading the config and reading |
| 93 | // properties of included files where an included file could change and |
| 94 | // remain unnoticed by the watcher. Not sure there's any good way around it |
| 95 | // though since we don't know the final set of includes until the config is |
| 96 | // parsed. |
| 97 | inner.set_includes(res.includes); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | debug!("exiting watcher thread for {:?}", inner.path); |
| 102 | }) |
| 103 | .unwrap(); |
| 104 | |
| 105 | Self { load_config } |
| 106 | } |
| 107 | |
| 108 | pub fn load_config(&self) { |
| 109 | let _ = self.load_config.send(()); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | impl Props { |
| 114 | fn from_path(path: &Path) -> io::Result<Self> { |
| 115 | let canonical = path.canonicalize()?; |
| 116 | let mtime = canonical.metadata()?.modified()?; |
| 117 | Ok(Self { mtime, canonical }) |
| 118 | } |
| 119 | |
| 120 | fn from_config_path(config_path: &ConfigPath) -> io::Result<Self> { |
| 121 | match config_path { |
| 122 | ConfigPath::Explicit(path) => Self::from_path(path), |
| 123 | ConfigPath::Regular { |
| 124 | user_path, |
| 125 | system_path, |
| 126 | } => Self::from_path(user_path).or_else(|_| Self::from_path(system_path)), |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | impl WatcherInner { |
| 132 | pub fn new(path: ConfigPath, includes: Vec<PathBuf>) -> Self { |
| 133 | let last_props = Props::from_config_path(&path).ok(); |
| 134 | |
| 135 | let mut rv = Self { |
| 136 | path, |
| 137 | last_props, |
| 138 | includes: HashMap::new(), |
| 139 | }; |
| 140 | rv.set_includes(includes); |
| 141 | rv |
| 142 | } |
| 143 | |
| 144 | pub fn check(&mut self) -> CheckResult { |
| 145 | if let Ok(new_props) = Props::from_config_path(&self.path) { |
| 146 | if self.last_props.as_ref() != Some(&new_props) { |
| 147 | self.last_props = Some(new_props); |
| 148 | CheckResult::Changed |
| 149 | } else { |
| 150 | for (path, last_props) in &mut self.includes { |
| 151 | let new_props = Props::from_path(path).ok(); |
| 152 | |
| 153 | // If an include goes missing while the main config file is unchanged, we |
| 154 | // consider that a change and reload. |
| 155 | if *last_props != new_props { |
| 156 | return CheckResult::Changed; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | CheckResult::Unchanged |
| 161 | } |
| 162 | } else { |
| 163 | CheckResult::Missing |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | fn set_includes(&mut self, includes: Vec<PathBuf>) { |
| 168 | self.includes = includes |
| 169 | .into_iter() |
| 170 | .map(|path| { |
| 171 | let props = Props::from_path(&path).ok(); |
| 172 | (path, props) |
| 173 | }) |
| 174 | .collect(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | pub fn setup(state: &mut State, config_path: &ConfigPath, includes: Vec<PathBuf>) { |
| 179 | // Parsing the config actually takes > 20 ms on my beefy machine, so let's do it on the |
| 180 | // watcher thread. |
| 181 | let process = |path: &ConfigPath| { |
| 182 | path.load().map_config_res(|res| { |
| 183 | res.map_err(|err| { |
| 184 | warn!("{err:?}"); |
| 185 | }) |
| 186 | }) |
| 187 | }; |
| 188 | |
| 189 | let (tx, rx) = calloop::channel::sync_channel(1); |
| 190 | state |
| 191 | .niri |
| 192 | .event_loop |
| 193 | .insert_source( |
| 194 | rx, |
| 195 | |event: calloop::channel::Event<Result<Config, ()>>, _, state| match event { |
| 196 | calloop::channel::Event::Msg(config) => { |
| 197 | let failed = config.is_err(); |
| 198 | state.reload_config(config); |
| 199 | state.ipc_config_loaded(failed); |
| 200 | } |
| 201 | calloop::channel::Event::Closed => (), |
| 202 | }, |
| 203 | ) |
| 204 | .unwrap(); |
| 205 | |
| 206 | let watcher = Watcher::new(config_path.clone(), includes, process, tx); |
| 207 | state.niri.config_file_watcher = Some(watcher); |
| 208 | } |
| 209 | |
| 210 | #[cfg(test)] |
| 211 | mod tests { |
| 212 | use std::error::Error; |
| 213 | use std::fs::{self, File, FileTimes}; |
| 214 | use std::io::Write; |
| 215 | |
| 216 | use xshell::{cmd, Shell, TempDir}; |
| 217 | |
| 218 | use super::*; |
| 219 | |
| 220 | type Result<T = (), E = Box<dyn Error>> = std::result::Result<T, E>; |
| 221 | |
| 222 | fn canon(config_path: &ConfigPath) -> &PathBuf { |
| 223 | match config_path { |
| 224 | ConfigPath::Explicit(path) => path, |
| 225 | ConfigPath::Regular { |
| 226 | user_path, |
| 227 | system_path, |
| 228 | } => { |
| 229 | if user_path.exists() { |
| 230 | user_path |
| 231 | } else { |
| 232 | system_path |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | enum TestPath<P> { |
| 239 | Explicit(P), |
| 240 | Regular { user_path: P, system_path: P }, |
| 241 | } |
| 242 | |
| 243 | impl<P: AsRef<Path>> TestPath<P> { |
| 244 | fn setup<Discard>( |
| 245 | self, |
| 246 | setup: impl FnOnce(&Shell) -> xshell::Result<Discard>, |
| 247 | ) -> TestSetup { |
| 248 | self.setup_any(|sh| { |
| 249 | _ = setup(sh)?; |
| 250 | Ok(()) |
| 251 | }) |
| 252 | } |
| 253 | |
| 254 | fn without_setup(self) -> TestSetup { |
| 255 | self.setup_any(|_| Ok(())).assert_initial_not_exists() |
| 256 | } |
| 257 | |
| 258 | fn setup_any(self, setup: impl FnOnce(&Shell) -> Result) -> TestSetup { |
| 259 | let sh = Shell::new().unwrap(); |
| 260 | let temp_dir = sh.create_temp_dir().unwrap(); |
| 261 | sh.change_dir(temp_dir.path()); |
| 262 | |
| 263 | let dir = sh.current_dir(); |
| 264 | let config_path = match self { |
| 265 | TestPath::Explicit(path) => ConfigPath::Explicit(dir.join(path)), |
| 266 | TestPath::Regular { |
| 267 | user_path, |
| 268 | system_path, |
| 269 | } => ConfigPath::Regular { |
| 270 | user_path: dir.join(user_path), |
| 271 | system_path: dir.join(system_path), |
| 272 | }, |
| 273 | }; |
| 274 | |
| 275 | setup(&sh).unwrap(); |
| 276 | |
| 277 | TestSetup { |
| 278 | sh, |
| 279 | config_path, |
| 280 | _temp_dir: temp_dir, |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | struct TestSetup { |
| 286 | sh: Shell, |
| 287 | config_path: ConfigPath, |
| 288 | _temp_dir: TempDir, |
| 289 | } |
| 290 | |
| 291 | impl TestSetup { |
| 292 | fn assert_initial_not_exists(self) -> Self { |
| 293 | let canon = canon(&self.config_path); |
| 294 | assert!(!canon.exists(), "initial should not exist"); |
| 295 | self |
| 296 | } |
| 297 | |
| 298 | fn assert_initial(self, expected: &str) -> Self { |
| 299 | let canon = canon(&self.config_path); |
| 300 | assert!(canon.exists(), "initial should exist at {canon:?}"); |
| 301 | let actual = fs::read_to_string(canon).unwrap(); |
| 302 | assert_eq!(actual, expected, "initial file contents do not match"); |
| 303 | self |
| 304 | } |
| 305 | |
| 306 | fn run(self, body: impl FnOnce(&Shell, &mut TestUtil) -> Result) -> Result { |
| 307 | let TestSetup { |
| 308 | sh, config_path, .. |
| 309 | } = self; |
| 310 | |
| 311 | let includes = config_path.load().includes; |
| 312 | let mut test = TestUtil { |
| 313 | watcher: WatcherInner::new(config_path, includes), |
| 314 | }; |
| 315 | |
| 316 | // don't trigger before we start |
| 317 | test.assert_unchanged(); |
| 318 | // pass_time() inside assert_unchanged() ensures that mtime |
| 319 | // isn't the same as the initial time |
| 320 | |
| 321 | body(&sh, &mut test)?; |
| 322 | |
| 323 | // nothing should trigger after the test runs |
| 324 | test.assert_unchanged(); |
| 325 | |
| 326 | Ok(()) |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | struct TestUtil { |
| 331 | watcher: WatcherInner, |
| 332 | } |
| 333 | |
| 334 | impl TestUtil { |
| 335 | // Ensures that mtime is different between writes in the tests. |
| 336 | fn pass_time(&self) { |
| 337 | thread::sleep(Duration::from_millis(50)); |
| 338 | } |
| 339 | |
| 340 | fn assert_unchanged(&mut self) { |
| 341 | let res = self.watcher.check(); |
| 342 | |
| 343 | // This may be Missing or Unchanged, both are fine. |
| 344 | assert_ne!( |
| 345 | res, |
| 346 | CheckResult::Changed, |
| 347 | "watcher should not have noticed any changes" |
| 348 | ); |
| 349 | |
| 350 | self.pass_time(); |
| 351 | } |
| 352 | |
| 353 | fn assert_changed_to(&mut self, expected: &str) { |
| 354 | let res = self.watcher.check(); |
| 355 | assert_eq!( |
| 356 | res, |
| 357 | CheckResult::Changed, |
| 358 | "watcher should have noticed a change, but it didn't" |
| 359 | ); |
| 360 | |
| 361 | let new_path = canon(&self.watcher.path); |
| 362 | let actual = fs::read_to_string(new_path).unwrap(); |
| 363 | assert_eq!(actual, expected, "wrong file contents"); |
| 364 | |
| 365 | self.watcher.set_includes(Config::load(new_path).includes); |
| 366 | |
| 367 | self.pass_time(); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | #[test] |
| 372 | fn change_file() -> Result { |
| 373 | TestPath::Explicit("niri/config.kdl") |
| 374 | .setup(|sh| sh.write_file("niri/config.kdl", "a")) |
| 375 | .assert_initial("a") |
| 376 | .run(|sh, test| { |
| 377 | sh.write_file("niri/config.kdl", "b")?; |
| 378 | test.assert_changed_to("b"); |
| 379 | |
| 380 | Ok(()) |
| 381 | }) |
| 382 | } |
| 383 | |
| 384 | #[test] |
| 385 | fn overwrite_but_dont_change_file() -> Result { |
| 386 | TestPath::Explicit("niri/config.kdl") |
| 387 | .setup(|sh| sh.write_file("niri/config.kdl", "a")) |
| 388 | .assert_initial("a") |
| 389 | .run(|sh, test| { |
| 390 | sh.write_file("niri/config.kdl", "a")?; |
| 391 | test.assert_changed_to("a"); |
| 392 | |
| 393 | Ok(()) |
| 394 | }) |
| 395 | } |
| 396 | |
| 397 | #[test] |
| 398 | fn touch_file() -> Result { |
| 399 | TestPath::Explicit("niri/config.kdl") |
| 400 | .setup(|sh| sh.write_file("niri/config.kdl", "a")) |
| 401 | .assert_initial("a") |
| 402 | .run(|sh, test| { |
| 403 | cmd!(sh, "touch niri/config.kdl").run()?; |
| 404 | test.assert_changed_to("a"); |
| 405 | |
| 406 | Ok(()) |
| 407 | }) |
| 408 | } |
| 409 | |
| 410 | #[test] |
| 411 | fn create_file() -> Result { |
| 412 | TestPath::Explicit("niri/config.kdl") |
| 413 | .setup(|sh| sh.create_dir("niri")) |
| 414 | .assert_initial_not_exists() |
| 415 | .run(|sh, test| { |
| 416 | sh.write_file("niri/config.kdl", "a")?; |
| 417 | test.assert_changed_to("a"); |
| 418 | |
| 419 | Ok(()) |
| 420 | }) |
| 421 | } |
| 422 | |
| 423 | #[test] |
| 424 | fn create_dir_and_file() -> Result { |
| 425 | TestPath::Explicit("niri/config.kdl") |
| 426 | .without_setup() |
| 427 | .run(|sh, test| { |
| 428 | sh.write_file("niri/config.kdl", "a")?; |
| 429 | test.assert_changed_to("a"); |
| 430 | |
| 431 | Ok(()) |
| 432 | }) |
| 433 | } |
| 434 | |
| 435 | #[test] |
| 436 | fn change_linked_file() -> Result { |
| 437 | TestPath::Explicit("niri/config.kdl") |
| 438 | .setup(|sh| { |
| 439 | sh.write_file("niri/config2.kdl", "a")?; |
| 440 | cmd!(sh, "ln -sf config2.kdl niri/config.kdl").run() |
| 441 | }) |
| 442 | .assert_initial("a") |
| 443 | .run(|sh, test| { |
| 444 | sh.write_file("niri/config2.kdl", "b")?; |
| 445 | test.assert_changed_to("b"); |
| 446 | |
| 447 | Ok(()) |
| 448 | }) |
| 449 | } |
| 450 | |
| 451 | #[test] |
| 452 | fn change_file_in_linked_dir() -> Result { |
| 453 | TestPath::Explicit("niri/config.kdl") |
| 454 | .setup(|sh| { |
| 455 | sh.write_file("niri2/config.kdl", "a")?; |
| 456 | cmd!(sh, "ln -s niri2 niri").run() |
| 457 | }) |
| 458 | .assert_initial("a") |
| 459 | .run(|sh, test| { |
| 460 | sh.write_file("niri2/config.kdl", "b")?; |
| 461 | test.assert_changed_to("b"); |
| 462 | |
| 463 | Ok(()) |
| 464 | }) |
| 465 | } |
| 466 | |
| 467 | #[test] |
| 468 | fn remove_file() -> Result { |
| 469 | TestPath::Explicit("niri/config.kdl") |
| 470 | .setup(|sh| sh.write_file("niri/config.kdl", "a")) |
| 471 | .assert_initial("a") |
| 472 | .run(|sh, test| { |
| 473 | sh.remove_path("niri/config.kdl")?; |
| 474 | test.assert_unchanged(); |
| 475 | |
| 476 | Ok(()) |
| 477 | }) |
| 478 | } |
| 479 | |
| 480 | #[test] |
| 481 | fn remove_dir() -> Result { |
| 482 | TestPath::Explicit("niri/config.kdl") |
| 483 | .setup(|sh| sh.write_file("niri/config.kdl", "a")) |
| 484 | .assert_initial("a") |
| 485 | .run(|sh, test| { |
| 486 | sh.remove_path("niri")?; |
| 487 | test.assert_unchanged(); |
| 488 | |
| 489 | Ok(()) |
| 490 | }) |
| 491 | } |
| 492 | |
| 493 | #[test] |
| 494 | fn recreate_file() -> Result { |
| 495 | TestPath::Explicit("niri/config.kdl") |
| 496 | .setup(|sh| sh.write_file("niri/config.kdl", "a")) |
| 497 | .assert_initial("a") |
| 498 | .run(|sh, test| { |
| 499 | sh.remove_path("niri/config.kdl")?; |
| 500 | sh.write_file("niri/config.kdl", "b")?; |
| 501 | test.assert_changed_to("b"); |
| 502 | |
| 503 | Ok(()) |
| 504 | }) |
| 505 | } |
| 506 | |
| 507 | #[test] |
| 508 | fn recreate_dir() -> Result { |
| 509 | TestPath::Explicit("niri/config.kdl") |
| 510 | .setup(|sh| { |
| 511 | sh.write_file("niri/config.kdl", "a")?; |
| 512 | Ok(()) |
| 513 | }) |
| 514 | .assert_initial("a") |
| 515 | .run(|sh, test| { |
| 516 | sh.remove_path("niri")?; |
| 517 | sh.write_file("niri/config.kdl", "b")?; |
| 518 | test.assert_changed_to("b"); |
| 519 | |
| 520 | Ok(()) |
| 521 | }) |
| 522 | } |
| 523 | |
| 524 | #[test] |
| 525 | fn swap_dir() -> Result { |
| 526 | TestPath::Explicit("niri/config.kdl") |
| 527 | .setup(|sh| sh.write_file("niri/config.kdl", "a")) |
| 528 | .assert_initial("a") |
| 529 | .run(|sh, test| { |
| 530 | sh.write_file("niri2/config.kdl", "b")?; |
| 531 | sh.remove_path("niri")?; |
| 532 | cmd!(sh, "mv niri2 niri").run()?; |
| 533 | test.assert_changed_to("b"); |
| 534 | |
| 535 | Ok(()) |
| 536 | }) |
| 537 | } |
| 538 | |
| 539 | #[test] |
| 540 | fn swap_dir_link() -> Result { |
| 541 | TestPath::Explicit("niri/config.kdl") |
| 542 | .setup(|sh| { |
| 543 | sh.write_file("niri2/config.kdl", "a")?; |
| 544 | cmd!(sh, "ln -s niri2 niri").run() |
| 545 | }) |
| 546 | .assert_initial("a") |
| 547 | .run(|sh, test| { |
| 548 | sh.write_file("niri3/config.kdl", "b")?; |
| 549 | sh.remove_path("niri")?; |
| 550 | cmd!(sh, "ln -s niri3 niri").run()?; |
| 551 | test.assert_changed_to("b"); |
| 552 | |
| 553 | Ok(()) |
| 554 | }) |
| 555 | } |
| 556 | |
| 557 | #[test] |
| 558 | fn change_included_file() -> Result { |
| 559 | TestPath::Explicit("niri/config.kdl") |
| 560 | .setup(|sh| { |
| 561 | sh.write_file("niri/config.kdl", "include \"colors.kdl\"")?; |
| 562 | sh.write_file("niri/colors.kdl", "// Colors") |
| 563 | }) |
| 564 | .assert_initial("include \"colors.kdl\"") |
| 565 | .run(|sh, test| { |
| 566 | sh.write_file("niri/colors.kdl", "// Updated colors")?; |
| 567 | test.assert_changed_to("include \"colors.kdl\""); |
| 568 | |
| 569 | Ok(()) |
| 570 | }) |
| 571 | } |
| 572 | |
| 573 | #[test] |
| 574 | fn remove_included_file() -> Result { |
| 575 | TestPath::Explicit("niri/config.kdl") |
| 576 | .setup(|sh| { |
| 577 | sh.write_file("niri/config.kdl", "include \"colors.kdl\"")?; |
| 578 | sh.write_file("niri/colors.kdl", "// Colors") |
| 579 | }) |
| 580 | .assert_initial("include \"colors.kdl\"") |
| 581 | .run(|sh, test| { |
| 582 | sh.remove_path("niri/colors.kdl")?; |
| 583 | test.assert_changed_to("include \"colors.kdl\""); |
| 584 | |
| 585 | Ok(()) |
| 586 | }) |
| 587 | } |
| 588 | |
| 589 | #[test] |
| 590 | fn nested_includes() -> Result { |
| 591 | TestPath::Explicit("niri/config.kdl") |
| 592 | .setup(|sh| { |
| 593 | sh.write_file("niri/config.kdl", "include \"a.kdl\"")?; |
| 594 | sh.write_file("niri/a.kdl", "include \"b.kdl\"")?; |
| 595 | sh.write_file("niri/b.kdl", "// b content") |
| 596 | }) |
| 597 | .assert_initial("include \"a.kdl\"") |
| 598 | .run(|sh, test| { |
| 599 | sh.write_file("niri/b.kdl", "// updated b")?; |
| 600 | test.assert_changed_to("include \"a.kdl\""); |
| 601 | |
| 602 | Ok(()) |
| 603 | }) |
| 604 | } |
| 605 | |
| 606 | #[test] |
| 607 | fn broken_include_still_gets_watched() -> Result { |
| 608 | TestPath::Explicit("niri/config.kdl") |
| 609 | .setup(|sh| { |
| 610 | sh.write_file("niri/config.kdl", "include \"colors.kdl\"")?; |
| 611 | sh.write_file("niri/colors.kdl", "broken") |
| 612 | }) |
| 613 | .assert_initial("include \"colors.kdl\"") |
| 614 | .run(|sh, test| { |
| 615 | sh.write_file("niri/colors.kdl", "// Fixed")?; |
| 616 | test.assert_changed_to("include \"colors.kdl\""); |
| 617 | |
| 618 | Ok(()) |
| 619 | }) |
| 620 | } |
| 621 | |
| 622 | // Important: On systems like NixOS, mtime is not kept for config files. |
| 623 | // So, this is testing that the watcher handles that correctly. |
| 624 | fn create_epoch(path: impl AsRef<Path>, content: &str) -> Result { |
| 625 | let mut file = File::create(path)?; |
| 626 | file.write_all(content.as_bytes())?; |
| 627 | file.set_times( |
| 628 | FileTimes::new() |
| 629 | .set_accessed(SystemTime::UNIX_EPOCH) |
| 630 | .set_modified(SystemTime::UNIX_EPOCH), |
| 631 | )?; |
| 632 | file.sync_all()?; |
| 633 | Ok(()) |
| 634 | } |
| 635 | |
| 636 | #[test] |
| 637 | fn swap_just_link() -> Result { |
| 638 | TestPath::Explicit("niri/config.kdl") |
| 639 | .setup_any(|sh| { |
| 640 | let dir = sh.current_dir().join("niri"); |
| 641 | |
| 642 | sh.create_dir(&dir)?; |
| 643 | |
| 644 | create_epoch(dir.join("config2.kdl"), "a")?; |
| 645 | create_epoch(dir.join("config3.kdl"), "b")?; |
| 646 | |
| 647 | cmd!(sh, "ln -s config2.kdl niri/config.kdl").run()?; |
| 648 | |
| 649 | Ok(()) |
| 650 | }) |
| 651 | .assert_initial("a") |
| 652 | .run(|sh, test| { |
| 653 | cmd!(sh, "ln -sf config3.kdl niri/config.kdl").run()?; |
| 654 | test.assert_changed_to("b"); |
| 655 | |
| 656 | Ok(()) |
| 657 | }) |
| 658 | } |
| 659 | |
| 660 | #[test] |
| 661 | fn swap_many_regular() -> Result { |
| 662 | TestPath::Regular { |
| 663 | user_path: "user-niri/config.kdl", |
| 664 | system_path: "system-niri/config.kdl", |
| 665 | } |
| 666 | .setup(|sh| sh.write_file("system-niri/config.kdl", "system config")) |
| 667 | .assert_initial("system config") |
| 668 | .run(|sh, test| { |
| 669 | sh.write_file("user-niri/config.kdl", "user config")?; |
| 670 | test.assert_changed_to("user config"); |
| 671 | |
| 672 | cmd!(sh, "touch system-niri/config.kdl").run()?; |
| 673 | test.assert_unchanged(); |
| 674 | |
| 675 | sh.remove_path("system-niri")?; |
| 676 | test.assert_unchanged(); |
| 677 | |
| 678 | sh.write_file("system-niri/config.kdl", "new system config")?; |
| 679 | test.assert_unchanged(); |
| 680 | |
| 681 | sh.remove_path("user-niri")?; |
| 682 | test.assert_changed_to("new system config"); |
| 683 | |
| 684 | sh.write_file("system-niri/config.kdl", "updated system config")?; |
| 685 | test.assert_changed_to("updated system config"); |
| 686 | |
| 687 | sh.write_file("user-niri/config.kdl", "new user config")?; |
| 688 | test.assert_changed_to("new user config"); |
| 689 | |
| 690 | Ok(()) |
| 691 | }) |
| 692 | } |
| 693 | |
| 694 | #[test] |
| 695 | fn swap_many_links_regular_like_nix() -> Result { |
| 696 | TestPath::Regular { |
| 697 | user_path: "user-niri/config.kdl", |
| 698 | system_path: "system-niri/config.kdl", |
| 699 | } |
| 700 | .setup_any(|sh| { |
| 701 | let store = sh.current_dir().join("store"); |
| 702 | |
| 703 | sh.create_dir(&store)?; |
| 704 | |
| 705 | create_epoch(store.join("gen1"), "gen 1")?; |
| 706 | create_epoch(store.join("gen2"), "gen 2")?; |
| 707 | create_epoch(store.join("gen3"), "gen 3")?; |
| 708 | |
| 709 | sh.create_dir("user-niri")?; |
| 710 | sh.create_dir("system-niri")?; |
| 711 | |
| 712 | Ok(()) |
| 713 | }) |
| 714 | .assert_initial_not_exists() |
| 715 | .run(|sh, test| { |
| 716 | let store = sh.current_dir().join("store"); |
| 717 | test.assert_unchanged(); |
| 718 | |
| 719 | cmd!(sh, "ln -s {store}/gen1 user-niri/config.kdl").run()?; |
| 720 | test.assert_changed_to("gen 1"); |
| 721 | |
| 722 | cmd!(sh, "ln -s {store}/gen2 system-niri/config.kdl").run()?; |
| 723 | test.assert_unchanged(); |
| 724 | |
| 725 | cmd!(sh, "unlink user-niri/config.kdl").run()?; |
| 726 | test.assert_changed_to("gen 2"); |
| 727 | |
| 728 | cmd!(sh, "ln -s {store}/gen3 user-niri/config.kdl").run()?; |
| 729 | test.assert_changed_to("gen 3"); |
| 730 | |
| 731 | cmd!(sh, "ln -sf {store}/gen1 system-niri/config.kdl").run()?; |
| 732 | test.assert_unchanged(); |
| 733 | |
| 734 | cmd!(sh, "unlink system-niri/config.kdl").run()?; |
| 735 | test.assert_unchanged(); |
| 736 | |
| 737 | cmd!(sh, "ln -s {store}/gen1 system-niri/config.kdl").run()?; |
| 738 | test.assert_unchanged(); |
| 739 | |
| 740 | cmd!(sh, "unlink user-niri/config.kdl").run()?; |
| 741 | test.assert_changed_to("gen 1"); |
| 742 | |
| 743 | Ok(()) |
| 744 | }) |
| 745 | } |
| 746 | } |