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