summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Knaack <davidkna@users.noreply.github.com>2021-01-19 23:23:27 +0100
committerGitHub <noreply@github.com>2021-01-19 23:23:27 +0100
commitd3002cf96177b4afca5a89a84ab99eb2a2dceec3 (patch)
treeb8f8d48a487fafee97deb76cf1064a358529f29c
parentcf82762f6ed1fed06825bdd9bdd7be5148ba5a85 (diff)
test: avoid setting $HOME (#2155)
* test: avoid setting $HOME * add comment to get_home * move everything to context.get_home
-rw-r--r--src/bug_report.rs8
-rw-r--r--src/context.rs10
-rw-r--r--src/modules/aws.rs2
-rw-r--r--src/modules/directory.rs35
-rw-r--r--src/modules/docker_context.rs2
-rw-r--r--src/modules/gcloud.rs2
-rw-r--r--src/modules/kubernetes.rs2
-rw-r--r--src/modules/openstack.rs2
8 files changed, 25 insertions, 38 deletions
diff --git a/src/bug_report.rs b/src/bug_report.rs
index 3e3575382..7dc1f9779 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 fb8979f11..8a362768a 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<PathBuf> {
+ 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<K: AsRef<str>>(&self, key: K) -> Option<String> {
if cfg!(test) {
diff --git a/src/modules/aws.rs b/src/modules/aws.rs
index f29eaba82..0898d9e2f 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 04673f416..ea35cc923 100644
--- a/src/modules/directory.rs
+++ b/src/modules/directory.rs
@@ -39,7 +39,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
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 e88c657df..c30a6bdf5 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<Module<'a>> {
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 a33daf014..1ce084f82 100644
--- a/src/modules/gcloud.rs
+++ b/src/modules/gcloud.rs
@@ -82,7 +82,7 @@ fn get_config_dir(context: &Context) -> Option<PathBuf> {
.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 c6ea6aa86..c4ff58c75 100644
--- a/src/modules/kubernetes.rs
+++ b/src/modules/kubernetes.rs
@@ -59,7 +59,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
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 5bb7a8b54..f46300895 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<Pro
// 1st = $PWD/clouds.yaml, 2nd = $HOME/.config/openstack/clouds.yaml, 3rd = /etc/openstack/clouds.yaml
let config = [
context.get_env("PWD").map(|pwd| pwd + "/clouds.yaml"),
- dirs_next::home_dir().map(|home| {
+ context.get_home().map(|home| {
home.join(".config/openstack/clouds.yaml")
.display()
.to_string()