Skip to content

Commit d9ba01b

Browse files
committed
test(env): add tests for default fish config detection
1 parent c2c6c5f commit d9ba01b

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

  • crates/vite_global_cli/src/commands/env

crates/vite_global_cli/src/commands/env/doctor.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,70 @@ mod tests {
764764
assert!(result.is_none(), "Should return None when all paths are bypassed");
765765
}
766766

767+
#[cfg(not(windows))]
768+
struct ProfileEnvGuard {
769+
original_home: Option<std::ffi::OsString>,
770+
}
771+
772+
#[cfg(not(windows))]
773+
impl ProfileEnvGuard {
774+
fn new(home: &std::path::Path) -> Self {
775+
let guard = Self {
776+
original_home: std::env::var_os("HOME"),
777+
};
778+
unsafe {
779+
std::env::set_var("HOME", home);
780+
}
781+
guard
782+
}
783+
}
784+
785+
#[cfg(not(windows))]
786+
impl Drop for ProfileEnvGuard {
787+
fn drop(&mut self) {
788+
unsafe {
789+
match &self.original_home {
790+
Some(v) => std::env::set_var("HOME", v),
791+
None => std::env::remove_var("HOME"),
792+
}
793+
}
794+
}
795+
}
796+
797+
#[test]
798+
#[serial]
799+
#[cfg(not(windows))]
800+
fn test_check_profile_files_default_fish_config() {
801+
let temp = TempDir::new().unwrap();
802+
let fake_home = temp.path().join("home");
803+
std::fs::create_dir_all(&fake_home).unwrap();
804+
805+
let fish_dir = fake_home.join(".config/fish/conf.d");
806+
std::fs::create_dir_all(&fish_dir).unwrap();
807+
std::fs::write(fish_dir.join("vite-plus.fish"), "source \"$HOME/.vite-plus/env.fish\"\n")
808+
.unwrap();
809+
810+
let _guard = ProfileEnvGuard::new(&fake_home);
811+
812+
let result = check_profile_files("$HOME/.vite-plus");
813+
assert!(result.is_some(), "Should find vite-plus.fish in default fish conf.d");
814+
assert!(result.unwrap().contains("vite-plus.fish"));
815+
}
816+
817+
#[test]
818+
#[serial]
819+
#[cfg(not(windows))]
820+
fn test_check_profile_files_no_fish_config() {
821+
let temp = TempDir::new().unwrap();
822+
let fake_home = temp.path().join("home");
823+
std::fs::create_dir_all(&fake_home).unwrap();
824+
825+
let _guard = ProfileEnvGuard::new(&fake_home);
826+
827+
let result = check_profile_files("$HOME/.vite-plus");
828+
assert_eq!(result, None);
829+
}
830+
767831
#[test]
768832
fn test_abbreviate_home() {
769833
if let Ok(home) = std::env::var("HOME") {

0 commit comments

Comments
 (0)