diff --git a/src/bug_report.rs b/src/bug_report.rs index 3e357538..7dc1f977 100644 --- a/src/bug_report.rs +++ b/src/bug_report.rs @@ -254,10 +254,10 @@ mod tests { #[test] #[cfg(not(windows))] fn test_get_config_path() { - env::set_var("HOME", "/test/home"); - let config_path = get_config_path("bash"); - assert_eq!("/test/home/.bashrc", config_path.unwrap().to_str().unwrap()); - env::remove_var("HOME"); + assert_eq!( + dirs_next::home_dir().unwrap().join(".bashrc"), + config_path.unwrap() + ); } } diff --git a/src/context.rs b/src/context.rs index fb8979f1..8a362768 100644 --- a/src/context.rs +++ b/src/context.rs @@ -3,6 +3,7 @@ use crate::module::Module; use crate::modules; use clap::ArgMatches; +use dirs_next::home_dir; use git2::{ErrorCode::UnbornBranch, Repository, RepositoryState}; use once_cell::sync::OnceCell; use std::collections::{HashMap, HashSet}; @@ -90,6 +91,15 @@ impl<'a> Context<'a> { } } + // Tries to retrieve home directory from a table in testing mode or else retrieves it from the os + pub fn get_home(&self) -> Option { + if cfg!(test) { + return self.get_env("HOME").map(PathBuf::from).or_else(home_dir); + } + + home_dir() + } + // Retrives a environment variable from the os or from a table if in testing mode pub fn get_env>(&self, key: K) -> Option { if cfg!(test) { diff --git a/src/modules/aws.rs b/src/modules/aws.rs index f29eaba8..0898d9e2 100644 --- a/src/modules/aws.rs +++ b/src/modules/aws.rs @@ -17,7 +17,7 @@ fn get_aws_region_from_config(context: &Context, aws_profile: Option<&str>) -> O .get_env("AWS_CONFIG_FILE") .and_then(|path| PathBuf::from_str(&path).ok()) .or_else(|| { - let mut home = dirs_next::home_dir()?; + let mut home = context.get_home()?; home.push(".aws/config"); Some(home) })?; diff --git a/src/modules/directory.rs b/src/modules/directory.rs index 04673f41..ea35cc92 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -39,7 +39,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { let current_dir = &get_current_dir(&context, &config); - let home_dir = dirs_next::home_dir().unwrap(); + let home_dir = context.get_home().unwrap(); log::debug!("Current directory: {:?}", current_dir); let repo = &context.get_repo().ok()?; @@ -486,18 +486,10 @@ mod tests { #[cfg(not(target_os = "windows"))] mod linux { use super::*; - use std::sync::atomic::{AtomicBool, Ordering}; - - // As tests are run in parallel we have to keep a lock on which of the - // two tests are currently running as they both modify `HOME` which can - // override the other value resulting in inconsistent runs which is why - // we only run one of these tests at once. - static LOCK: AtomicBool = AtomicBool::new(false); #[test] #[ignore] fn symlinked_subdirectory_git_repo_out_of_tree() -> io::Result<()> { - while LOCK.swap(true, Ordering::Acquire) {} let tmp_dir = TempDir::new_in(home_dir().unwrap().as_path())?; let repo_dir = tmp_dir.path().join("above-repo").join("rocket-controls"); let src_dir = repo_dir.join("src/meters/fuel-gauge"); @@ -506,37 +498,25 @@ mod tests { init_repo(&repo_dir)?; symlink(&src_dir, &symlink_dir)?; - // We can't mock `HOME` since dirs-next uses it which does not care about our mocking - let previous_home = home_dir().unwrap(); - - std::env::set_var("HOME", tmp_dir.path()); - - let actual = ModuleRenderer::new("directory").path(symlink_dir).collect(); + let actual = ModuleRenderer::new("directory") + .env("HOME", tmp_dir.path().to_str().unwrap()) + .path(symlink_dir) + .collect(); let expected = Some(format!("{} ", Color::Cyan.bold().paint("~/fuel-gauge"))); - std::env::set_var("HOME", previous_home.as_path()); - assert_eq!(expected, actual); - LOCK.store(false, Ordering::Release); - tmp_dir.close() } #[test] #[ignore] fn git_repo_in_home_directory_truncate_to_repo_true() -> io::Result<()> { - while LOCK.swap(true, Ordering::Acquire) {} let tmp_dir = TempDir::new_in(home_dir().unwrap().as_path())?; let dir = tmp_dir.path().join("src/fuel-gauge"); fs::create_dir_all(&dir)?; init_repo(&tmp_dir.path())?; - // We can't mock `HOME` since dirs-next uses it which does not care about our mocking - let previous_home = home_dir().unwrap(); - - std::env::set_var("HOME", tmp_dir.path()); - let actual = ModuleRenderer::new("directory") .config(toml::toml! { [directory] @@ -545,15 +525,12 @@ mod tests { truncation_length = 5 }) .path(dir) + .env("HOME", tmp_dir.path().to_str().unwrap()) .collect(); let expected = Some(format!("{} ", Color::Cyan.bold().paint("~/src/fuel-gauge"))); - std::env::set_var("HOME", previous_home.as_path()); - assert_eq!(expected, actual); - LOCK.store(false, Ordering::Release); - tmp_dir.close() } diff --git a/src/modules/docker_context.rs b/src/modules/docker_context.rs index e88c657d..c30a6bdf 100644 --- a/src/modules/docker_context.rs +++ b/src/modules/docker_context.rs @@ -28,7 +28,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { let docker_config = PathBuf::from( &context .get_env_os("DOCKER_CONFIG") - .unwrap_or(dirs_next::home_dir()?.join(".docker").into_os_string()), + .unwrap_or(context.get_home()?.join(".docker").into_os_string()), ) .join("config.json"); diff --git a/src/modules/gcloud.rs b/src/modules/gcloud.rs index a33daf01..1ce084f8 100644 --- a/src/modules/gcloud.rs +++ b/src/modules/gcloud.rs @@ -82,7 +82,7 @@ fn get_config_dir(context: &Context) -> Option { .get_env("CLOUDSDK_CONFIG") .and_then(|path| PathBuf::from_str(&path).ok()) .or_else(|| { - let mut home = dirs_next::home_dir()?; + let mut home = context.get_home()?; home.push(".config/gcloud"); Some(home) })?; diff --git a/src/modules/kubernetes.rs b/src/modules/kubernetes.rs index c6ea6aa8..c4ff58c7 100644 --- a/src/modules/kubernetes.rs +++ b/src/modules/kubernetes.rs @@ -59,7 +59,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { return None; }; - let default_config_file = dirs_next::home_dir()?.join(".kube").join("config"); + let default_config_file = context.get_home()?.join(".kube").join("config"); let kube_cfg = context .get_env("KUBECONFIG") diff --git a/src/modules/openstack.rs b/src/modules/openstack.rs index 5bb7a8b5..f4630089 100644 --- a/src/modules/openstack.rs +++ b/src/modules/openstack.rs @@ -14,7 +14,7 @@ fn get_osp_project_from_config(context: &Context, osp_cloud: &str) -> Option