oxedyne/fe2o3/fe2o3_jdat/tests/cfg_path_at.rs
3.0 KiB, 3 runs
created by r1870400018:75867, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! `cfg::path_at`, one location value resolved under a root already chosen (kcfg F1, 26 Oct 2026). |
| 2 | |
| 3 | use oxedyne_fe2o3_core::prelude::*; |
| 4 | use oxedyne_fe2o3_jdat::cfg; |
| 5 | |
| 6 | use std::path::{ |
| 7 | Path, |
| 8 | PathBuf, |
| 9 | }; |
| 10 | |
| 11 | |
| 12 | #[test] |
| 13 | fn path_at_absolute_rerooted() -> Outcome<()> { |
| 14 | let root = Path::new("/srv/app"); |
| 15 | assert_eq!(res!(cfg::path_at("/var/lib/x", root, None)), PathBuf::from("/var/lib/x")); |
| 16 | // NormalPath turns /a/b into ./a/b, so the result must be re-rooted, and stay absolute. |
| 17 | assert_eq!(res!(cfg::path_at("/var/../etc/./y/", root, None)), PathBuf::from("/etc/y")); |
| 18 | assert_eq!(res!(cfg::path_at("data/db", root, None)), PathBuf::from("/srv/app/data/db")); |
| 19 | assert_eq!(res!(cfg::path_at("./data/../www", root, None)), PathBuf::from("/srv/app/www")); |
| 20 | assert_eq!(res!(cfg::path_at(".", root, None)), PathBuf::from("/srv/app")); |
| 21 | Ok(()) |
| 22 | } |
| 23 | |
| 24 | #[test] |
| 25 | fn path_at_home_needs_home() -> Outcome<()> { |
| 26 | let root = Path::new("/srv/app"); |
| 27 | let home = Path::new("/home/runner"); |
| 28 | assert_eq!(res!(cfg::path_at("~/var/x", root, Some(home))), PathBuf::from("/home/runner/var/x")); |
| 29 | let e = match cfg::path_at("~/var/x", root, None) { |
| 30 | Ok(p) => return Err(err!("A '~/' value resolved to {:?} with no home.", p; Test)), |
| 31 | Err(e) => e, |
| 32 | }; |
| 33 | assert!(fmt!("{}", e).contains("~/var/x"), "{}", e); |
| 34 | // The home is a root like any other: it must be absolute and may not be escaped. |
| 35 | assert!(cfg::path_at("~/x", root, Some(Path::new("home/runner"))).is_err()); |
| 36 | assert!(cfg::path_at("~/../other", root, Some(home)).is_err()); |
| 37 | Ok(()) |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn path_at_bare_tilde_is_home() -> Outcome<()> { |
| 42 | // Kimberlite's installer writes `"work_dir": "~"`; it was a folder named `~` under the root (LA1, 20 Nov 2026). |
| 43 | let root = Path::new("/srv/app"); |
| 44 | let home = Path::new("/home/runner"); |
| 45 | assert_eq!(res!(cfg::path_at("~", root, Some(home))), PathBuf::from("/home/runner")); |
| 46 | assert!(cfg::path_at("~", root, None).is_err()); |
| 47 | // Only the whole value: `~x` and `x/~` stay names under the root. |
| 48 | assert_eq!(res!(cfg::path_at("~x", root, Some(home))), PathBuf::from("/srv/app/~x")); |
| 49 | assert_eq!(res!(cfg::path_at("x/~", root, Some(home))), PathBuf::from("/srv/app/x/~")); |
| 50 | Ok(()) |
| 51 | } |
| 52 | |
| 53 | #[test] |
| 54 | fn path_at_refuses_escape_empty_nul_env_relative_root() -> Outcome<()> { |
| 55 | let root = Path::new("/srv/app"); |
| 56 | for value in [ |
| 57 | "../etc", |
| 58 | "data/../../etc", |
| 59 | "", |
| 60 | "data\0x", |
| 61 | "{env:DB_DIR}", |
| 62 | "x/{file:dir.txt}", |
| 63 | "{file?:dir.txt}", |
| 64 | ] { |
| 65 | match cfg::path_at(value, root, None) { |
| 66 | Ok(p) => return Err(err!("{:?} resolved to {:?} and should be refused.", value, p; Test)), |
| 67 | Err(e) => if !value.is_empty() && !value.contains('\0') { |
| 68 | assert!(fmt!("{}", e).contains(value), "the error does not name {:?}: {}", value, e); |
| 69 | }, |
| 70 | } |
| 71 | } |
| 72 | assert!(cfg::path_at("data", Path::new("srv/app"), None).is_err()); |
| 73 | assert!(cfg::path_at("data", Path::new(""), None).is_err()); |
| 74 | Ok(()) |
| 75 | } |