use std::path::PathBuf;
use crate::ported::init::source;
use crate::ported::params::getsparam;
use crate::ported::zsh_h::{module, EMULATE_ZSH, EMULATION};
#[allow(unused_variables)]
pub fn setup_(m: *const module) -> i32 {
0 }
#[allow(unused_variables)]
pub fn features_(m: *const module, features: &mut Vec<String>) -> i32 {
1 }
#[allow(unused_variables)]
pub fn enables_(m: *const module, enables: &mut Option<Vec<i32>>) -> i32 {
0 }
pub fn check_dotfile(dotdir: &str, fname: &str) -> i32 {
let mut p = PathBuf::from(dotdir); p.push(fname); if p.exists() {
0
} else {
-1
} }
#[allow(unused_variables)]
pub fn boot_(m: *const module) -> i32 {
let mut dotdir: String = getsparam("ZDOTDIR").unwrap_or_default();
let spaths: Vec<String> = std::env::var("ZSH_SITESCRIPT_DIR")
.ok()
.into_iter()
.chain(std::env::var("ZSH_SCRIPT_DIR").ok())
.chain(std::iter::once("/etc/zsh".to_string()))
.collect();
if !EMULATION(EMULATE_ZSH) {
return 0; } else {}
if dotdir.is_empty() {
dotdir = getsparam("HOME") .unwrap_or_default();
if dotdir.is_empty() {
return 0; }
}
if check_dotfile(&dotdir, ".zshenv") == 0 || check_dotfile(&dotdir, ".zprofile") == 0 || check_dotfile(&dotdir, ".zshrc") == 0 || check_dotfile(&dotdir, ".zlogin") == 0
{
return 0; }
for sp in &spaths {
let buf = format!("{}/newuser", sp); if source(&buf) != SOURCE_NOT_FOUND {
break; }
}
0 }
#[allow(unused_variables)]
pub fn cleanup_(m: *const module) -> i32 {
0 }
#[allow(unused_variables)]
pub fn finish_(m: *const module) -> i32 {
0 }
const SOURCE_NOT_FOUND: i32 = 1;
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
#[test]
fn check_dotfile_returns_zero_when_file_exists() {
let _g = crate::test_util::global_state_lock();
let tmp = std::env::temp_dir();
let p = tmp.join("zshrs_test_dotfile_exists");
fs::write(&p, "").expect("write tmp");
assert_eq!(
check_dotfile(tmp.to_str().unwrap(), "zshrs_test_dotfile_exists"),
0
);
let _ = fs::remove_file(&p);
}
#[test]
fn check_dotfile_returns_minus_one_when_missing() {
let _g = crate::test_util::global_state_lock();
let tmp = std::env::temp_dir();
assert_eq!(
check_dotfile(tmp.to_str().unwrap(), "zshrs_test_definitely_nothere_xyz"),
-1
);
}
#[test]
fn module_entry_points_return_zero() {
let _g = crate::test_util::global_state_lock();
assert_eq!(setup_(std::ptr::null()), 0);
assert_eq!(cleanup_(std::ptr::null()), 0);
assert_eq!(finish_(std::ptr::null()), 0);
}
#[test]
fn newuser_corpus_check_dotfile_existing_returns_zero() {
let _g = crate::test_util::global_state_lock();
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join(".zshrc");
std::fs::File::create(&p).unwrap();
assert_eq!(check_dotfile(dir.path().to_str().unwrap(), ".zshrc"), 0,
"existing file = 0 per c:62");
}
#[test]
fn newuser_corpus_check_dotfile_missing_in_existing_dir() {
let _g = crate::test_util::global_state_lock();
let dir = tempfile::tempdir().unwrap();
assert_eq!(
check_dotfile(dir.path().to_str().unwrap(), "zshrs_no_such_file_xyz"),
-1,
"missing file = -1",
);
}
#[test]
fn newuser_corpus_check_dotfile_missing_dir_returns_neg_one() {
let _g = crate::test_util::global_state_lock();
assert_eq!(
check_dotfile("/never/exists/zshrs_xyz", ".zshrc"),
-1,
);
}
#[test]
fn newuser_corpus_boot_returns_zero() {
let _g = crate::test_util::global_state_lock();
assert_eq!(boot_(std::ptr::null()), 0);
}
#[test]
fn newuser_corpus_features_returns_one_no_features() {
let _g = crate::test_util::global_state_lock();
let mut features = Vec::new();
assert_eq!(features_(std::ptr::null(), &mut features), 1,
"newuser has no advertised features");
}
}