completely rewrote test suite for top level src files and all builtin files

This commit is contained in:
2026-03-06 23:42:14 -05:00
parent 42b4120055
commit b137c38e92
44 changed files with 5909 additions and 582 deletions

View File

@@ -75,3 +75,162 @@ pub fn cd(node: Node) -> ShResult<()> {
state::set_status(0);
Ok(())
}
#[cfg(test)]
pub mod tests {
use std::env;
use std::fs;
use tempfile::TempDir;
use crate::state;
use crate::testutil::{TestGuard, test_input};
// ===================== Basic Navigation =====================
#[test]
fn cd_simple() {
let _g = TestGuard::new();
let old_dir = env::current_dir().unwrap();
let temp_dir = TempDir::new().unwrap();
test_input(format!("cd {}", temp_dir.path().display())).unwrap();
let new_dir = env::current_dir().unwrap();
assert_ne!(old_dir, new_dir);
assert_eq!(new_dir.display().to_string(), temp_dir.path().display().to_string());
}
#[test]
fn cd_no_args_goes_home() {
let _g = TestGuard::new();
let temp_dir = TempDir::new().unwrap();
unsafe { env::set_var("HOME", temp_dir.path()) };
test_input("cd").unwrap();
let cwd = env::current_dir().unwrap();
assert_eq!(cwd.display().to_string(), temp_dir.path().display().to_string());
}
#[test]
fn cd_relative_path() {
let _g = TestGuard::new();
let temp_dir = TempDir::new().unwrap();
let sub = temp_dir.path().join("child");
fs::create_dir(&sub).unwrap();
test_input(format!("cd {}", temp_dir.path().display())).unwrap();
test_input("cd child").unwrap();
let cwd = env::current_dir().unwrap();
assert_eq!(cwd.display().to_string(), sub.display().to_string());
}
// ===================== Environment =====================
#[test]
fn cd_sets_pwd_env() {
let _g = TestGuard::new();
let temp_dir = TempDir::new().unwrap();
test_input(format!("cd {}", temp_dir.path().display())).unwrap();
let pwd = env::var("PWD").unwrap();
assert_eq!(pwd, env::current_dir().unwrap().display().to_string());
}
#[test]
fn cd_status_zero_on_success() {
let _g = TestGuard::new();
let temp_dir = TempDir::new().unwrap();
test_input(format!("cd {}", temp_dir.path().display())).unwrap();
assert_eq!(state::get_status(), 0);
}
// ===================== Error Cases =====================
#[test]
fn cd_nonexistent_dir_fails() {
let _g = TestGuard::new();
let result = test_input("cd /nonexistent_path_that_does_not_exist_xyz");
assert!(result.is_err());
}
#[test]
fn cd_file_not_directory_fails() {
let _g = TestGuard::new();
let temp_dir = TempDir::new().unwrap();
let file_path = temp_dir.path().join("afile.txt");
fs::write(&file_path, "hello").unwrap();
let result = test_input(format!("cd {}", file_path.display()));
assert!(result.is_err());
}
// ===================== Multiple cd =====================
#[test]
fn cd_multiple_times() {
let _g = TestGuard::new();
let dir_a = TempDir::new().unwrap();
let dir_b = TempDir::new().unwrap();
test_input(format!("cd {}", dir_a.path().display())).unwrap();
assert_eq!(
env::current_dir().unwrap().display().to_string(),
dir_a.path().display().to_string()
);
test_input(format!("cd {}", dir_b.path().display())).unwrap();
assert_eq!(
env::current_dir().unwrap().display().to_string(),
dir_b.path().display().to_string()
);
}
#[test]
fn cd_nested_subdirectories() {
let _g = TestGuard::new();
let temp_dir = TempDir::new().unwrap();
let deep = temp_dir.path().join("a").join("b").join("c");
fs::create_dir_all(&deep).unwrap();
test_input(format!("cd {}", deep.display())).unwrap();
assert_eq!(
env::current_dir().unwrap().display().to_string(),
deep.display().to_string()
);
}
// ===================== Autocmd Integration =====================
#[test]
fn cd_fires_post_change_dir_autocmd() {
let guard = TestGuard::new();
let temp_dir = TempDir::new().unwrap();
test_input("autocmd post-change-dir 'echo cd-hook-fired'").unwrap();
guard.read_output();
test_input(format!("cd {}", temp_dir.path().display())).unwrap();
let out = guard.read_output();
assert!(out.contains("cd-hook-fired"));
}
#[test]
fn cd_fires_pre_change_dir_autocmd() {
let guard = TestGuard::new();
let temp_dir = TempDir::new().unwrap();
test_input("autocmd pre-change-dir 'echo pre-cd'").unwrap();
guard.read_output();
test_input(format!("cd {}", temp_dir.path().display())).unwrap();
let out = guard.read_output();
assert!(out.contains("pre-cd"));
}
}