1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-06-07 11:00:47 +00:00

test: avoid setting $HOME (#2155)

* test: avoid setting $HOME

* add comment to get_home

* move everything to context.get_home
This commit is contained in:
David Knaack 2021-01-19 23:23:27 +01:00 committed by GitHub
parent cf82762f6e
commit d3002cf961
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 25 additions and 38 deletions

View File

@ -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()
);
}
}

View File

@ -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) {

View File

@ -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)
})?;

View File

@ -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()
}

View File

@ -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");

View File

@ -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)
})?;

View File

@ -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")

View File

@ -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()