use crate::ported::config_h::{
GLOBAL_ZLOGIN, GLOBAL_ZLOGOUT, GLOBAL_ZPROFILE, GLOBAL_ZSHENV, GLOBAL_ZSHRC,
};
const DEBIAN_SYSCONFDIR: &str = "/etc/zsh";
pub fn global_rc_path(default: &str) -> String {
let Some(base) = default.strip_prefix("/etc/") else {
return default.to_string();
};
let debian = format!("{DEBIAN_SYSCONFDIR}/{base}");
if std::path::Path::new(&debian).exists() {
debian
} else {
default.to_string()
}
}
pub fn global_rc_chain() -> [String; 5] {
[
global_rc_path(GLOBAL_ZSHENV),
global_rc_path(GLOBAL_ZPROFILE),
global_rc_path(GLOBAL_ZSHRC),
global_rc_path(GLOBAL_ZLOGIN),
global_rc_path(GLOBAL_ZLOGOUT),
]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn non_etc_paths_pass_through_untouched() {
assert_eq!(
global_rc_path("/usr/local/etc/zshenv"),
"/usr/local/etc/zshenv"
);
assert_eq!(global_rc_path("zshenv"), "zshenv");
assert_eq!(global_rc_path(""), "");
}
#[test]
fn resolution_preserves_the_file_name() {
for default in global_rc_chain() {
let stem = default.rsplit('/').next().expect("non-empty path");
assert!(
default == format!("/etc/{stem}")
|| default == format!("{DEBIAN_SYSCONFDIR}/{stem}"),
"{default:?} is neither the upstream nor the Debian location"
);
}
}
#[test]
fn chain_is_in_init_c_source_order() {
let chain = global_rc_chain();
let names: Vec<&str> = chain
.iter()
.map(|p| p.rsplit('/').next().expect("non-empty path"))
.collect();
assert_eq!(names, ["zshenv", "zprofile", "zshrc", "zlogin", "zlogout"]);
}
}