oxedyne/fe2o3/fe2o3_core/tests/config_home.rs
2.7 KiB, 2 runs
created by r1870400018:74709, 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 | use oxedyne_fe2o3_core::file::{ |
| 2 | app_config_dir_from, |
| 3 | config_home_from, |
| 4 | xdg_config_home_from, |
| 5 | CONFIG_VENDOR, |
| 6 | }; |
| 7 | |
| 8 | use std::{ |
| 9 | ffi::OsString, |
| 10 | path::PathBuf, |
| 11 | }; |
| 12 | |
| 13 | |
| 14 | fn os(s: &str) -> Option<OsString> { Some(OsString::from(s)) } |
| 15 | |
| 16 | #[test] |
| 17 | fn test_xdg_config_home_from_absolute_only() { |
| 18 | assert_eq!(xdg_config_home_from(os("/x/cfg")), Some(PathBuf::from("/x/cfg"))); |
| 19 | assert_eq!(xdg_config_home_from(os("rel/cfg")), None); |
| 20 | assert_eq!(xdg_config_home_from(os("")), None); |
| 21 | assert_eq!(xdg_config_home_from(None), None); |
| 22 | } |
| 23 | |
| 24 | #[test] |
| 25 | fn test_config_home_absolute_xdg_wins() { |
| 26 | let p = config_home_from(os("/x/cfg"), os("/home/u")).unwrap_or_default(); |
| 27 | assert_eq!(p, PathBuf::from("/x/cfg")); |
| 28 | } |
| 29 | |
| 30 | #[test] |
| 31 | fn test_config_home_relative_or_empty_xdg_falls_back_to_home() { |
| 32 | for xdg in [os("rel/cfg"), os(""), None] { |
| 33 | let p = config_home_from(xdg, os("/home/u")).unwrap_or_default(); |
| 34 | assert_eq!(p, PathBuf::from("/home/u/.config")); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | #[test] |
| 39 | fn test_config_home_neither_names_both() { |
| 40 | for home in [None, os("")] { |
| 41 | match config_home_from(os("rel"), home) { |
| 42 | Ok(p) => panic!("expected an error, got {:?}", p), |
| 43 | Err(e) => { |
| 44 | let s = e.to_string(); |
| 45 | assert!(s.contains("XDG_CONFIG_HOME"), "{}", s); |
| 46 | assert!(s.contains("HOME"), "{}", s); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | #[test] |
| 53 | fn test_app_config_dir_from_ends_vendor_app() { |
| 54 | assert_eq!(CONFIG_VENDOR, "oxedyne"); |
| 55 | let p = app_config_dir_from("ore", os("/x/cfg"), os("/home/u")).unwrap_or_default(); |
| 56 | assert_eq!(p, PathBuf::from("/x/cfg/oxedyne/ore")); |
| 57 | assert!(p.ends_with("oxedyne/ore")); |
| 58 | let p = app_config_dir_from("ore", None, os("/home/u")).unwrap_or_default(); |
| 59 | assert_eq!(p, PathBuf::from("/home/u/.config/oxedyne/ore")); |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn test_app_config_dir_from_refuses_bad_name() { |
| 64 | for app in ["", "a/b", "..", ".", "../ore", "/abs"] { |
| 65 | assert!(app_config_dir_from(app, os("/x/cfg"), os("/home/u")).is_err(), "{:?}", app); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// A relative HOME is refused, as a relative XDG_CONFIG_HOME is ignored: either |
| 70 | /// would put the configuration home, and the keys in it, under whatever the |
| 71 | /// current directory happens to be. |
| 72 | #[test] |
| 73 | fn test_config_home_refuses_a_relative_home() { |
| 74 | for home in [os("rel"), os("./x"), os(".")] { |
| 75 | assert!(config_home_from(None, home.clone()).is_err(), |
| 76 | "a relative HOME {:?} was accepted", home); |
| 77 | assert!(config_home_from(os("rel/cfg"), home.clone()).is_err(), |
| 78 | "a relative HOME {:?} behind a relative XDG_CONFIG_HOME was accepted", home); |
| 79 | } |
| 80 | assert!(config_home_from(os("/x/cfg"), os("rel")).is_ok(), "an absolute XDG_CONFIG_HOME still wins"); |
| 81 | } |