Skip to content

Commit

Permalink
Prevent tests setting env vars from affecting other tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed Jan 8, 2021
1 parent ba0d36c commit dc26797
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
8 changes: 6 additions & 2 deletions src/git_config/git_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ impl GitConfig {
}

#[cfg(test)]
pub fn from_path(path: &Path) -> Self {
pub fn from_path(path: &Path, honor_env_var: bool) -> Self {
Self {
config: git2::Config::open(path).unwrap(),
config_from_env_var: parse_config_from_env_var(),
config_from_env_var: if honor_env_var {
parse_config_from_env_var()
} else {
HashMap::new()
},
repo: None,
enabled: true,
}
Expand Down
46 changes: 23 additions & 23 deletions src/options/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,13 @@ pub mod tests {

use crate::tests::integration_test_utils::integration_test_utils;

// TODO: the followig tests are collapsed into one since they all set the same env var and thus
// could affect each other if allowed to run concurrently.

#[test]
fn test_simple_string_env_var_overrides_git_config() {
fn test_env_var_overrides_git_config() {
// ----------------------------------------------------------------------------------------
// simple string
let git_config_contents = b"
[delta]
plus-style = blue
Expand All @@ -133,18 +138,17 @@ pub mod tests {
assert_eq!(opt.plus_style, "blue");

env::set_var("GIT_CONFIG_PARAMETERS", "'delta.plus-style=green'");
let opt = integration_test_utils::make_options_from_args_and_git_config(
let opt = integration_test_utils::make_options_from_args_and_git_config_honoring_env_var(
&[],
Some(git_config_contents),
Some(git_config_path),
);
assert_eq!(opt.plus_style, "green");

remove_file(git_config_path).unwrap();
}

#[test]
fn test_complex_string_env_var_overrides_git_config() {
// ----------------------------------------------------------------------------------------
// complex string
let git_config_contents = br##"
[delta]
minus-style = red bold ul "#ffeeee"
Expand All @@ -162,18 +166,17 @@ pub mod tests {
"GIT_CONFIG_PARAMETERS",
r##"'delta.minus-style=magenta italic ol "#aabbcc"'"##,
);
let opt = integration_test_utils::make_options_from_args_and_git_config(
let opt = integration_test_utils::make_options_from_args_and_git_config_honoring_env_var(
&[],
Some(git_config_contents),
Some(git_config_path),
);
assert_eq!(opt.minus_style, r##"magenta italic ol "#aabbcc""##,);

remove_file(git_config_path).unwrap();
}

#[test]
fn test_option_string_env_var_overrides_git_config() {
// ----------------------------------------------------------------------------------------
// option string
let git_config_contents = b"
[delta]
plus-style = blue
Expand All @@ -188,18 +191,17 @@ pub mod tests {
assert_eq!(opt.plus_style, "blue");

env::set_var("GIT_CONFIG_PARAMETERS", "'delta.plus-style=green'");
let opt = integration_test_utils::make_options_from_args_and_git_config(
let opt = integration_test_utils::make_options_from_args_and_git_config_honoring_env_var(
&[],
Some(git_config_contents),
Some(git_config_path),
);
assert_eq!(opt.plus_style, "green");

remove_file(git_config_path).unwrap();
}

#[test]
fn test_bool_env_var_overrides_git_config() {
// ----------------------------------------------------------------------------------------
// bool
let git_config_contents = b"
[delta]
side-by-side = true
Expand All @@ -214,18 +216,17 @@ pub mod tests {
assert_eq!(opt.side_by_side, true);

env::set_var("GIT_CONFIG_PARAMETERS", "'delta.side-by-side=false'");
let opt = integration_test_utils::make_options_from_args_and_git_config(
let opt = integration_test_utils::make_options_from_args_and_git_config_honoring_env_var(
&[],
Some(git_config_contents),
Some(git_config_path),
);
assert_eq!(opt.side_by_side, false);

remove_file(git_config_path).unwrap();
}

#[test]
fn test_int_env_var_overrides_git_config() {
// ----------------------------------------------------------------------------------------
// int
let git_config_contents = b"
[delta]
max-line-length = 1
Expand All @@ -240,33 +241,32 @@ pub mod tests {
assert_eq!(opt.max_line_length, 1);

env::set_var("GIT_CONFIG_PARAMETERS", "'delta.max-line-length=2'");
let opt = integration_test_utils::make_options_from_args_and_git_config(
let opt = integration_test_utils::make_options_from_args_and_git_config_honoring_env_var(
&[],
Some(git_config_contents),
Some(git_config_path),
);
assert_eq!(opt.max_line_length, 2);

remove_file(git_config_path).unwrap();
}

#[test]
fn test_float_env_var_overrides_git_config() {
// ----------------------------------------------------------------------------------------
// float
let git_config_contents = b"
[delta]
max-line-distance = 0.6
";
let git_config_path = "delta__test_float_env_var_overrides_git_config.gitconfig";

let opt = integration_test_utils::make_options_from_args_and_git_config(
let opt = integration_test_utils::make_options_from_args_and_git_config_honoring_env_var(
&[],
Some(git_config_contents),
Some(git_config_path),
);
assert_eq!(opt.max_line_distance, 0.6);

env::set_var("GIT_CONFIG_PARAMETERS", "'delta.max-line-distance=0.7'");
let opt = integration_test_utils::make_options_from_args_and_git_config(
let opt = integration_test_utils::make_options_from_args_and_git_config_honoring_env_var(
&[],
Some(git_config_contents),
Some(git_config_path),
Expand Down
23 changes: 20 additions & 3 deletions src/tests/integration_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,29 @@ pub mod integration_test_utils {
args: &[&str],
git_config_contents: Option<&[u8]>,
git_config_path: Option<&str>,
) -> cli::Opt {
_make_options_from_args_and_git_config(args, git_config_contents, git_config_path, false)
}

pub fn make_options_from_args_and_git_config_honoring_env_var(
args: &[&str],
git_config_contents: Option<&[u8]>,
git_config_path: Option<&str>,
) -> cli::Opt {
_make_options_from_args_and_git_config(args, git_config_contents, git_config_path, true)
}

fn _make_options_from_args_and_git_config(
args: &[&str],
git_config_contents: Option<&[u8]>,
git_config_path: Option<&str>,
honor_env_var: bool,
) -> cli::Opt {
let mut args: Vec<&str> = itertools::chain(&["/dev/null", "/dev/null"], args)
.map(|s| *s)
.collect();
let mut git_config = match (git_config_contents, git_config_path) {
(Some(contents), Some(path)) => Some(make_git_config(contents, path)),
(Some(contents), Some(path)) => Some(make_git_config(contents, path, honor_env_var)),
_ => {
args.push("--no-gitconfig");
None
Expand Down Expand Up @@ -52,11 +69,11 @@ pub mod integration_test_utils {
config::Config::from(make_options_from_args(args))
}

fn make_git_config(contents: &[u8], path: &str) -> GitConfig {
fn make_git_config(contents: &[u8], path: &str, honor_env_var: bool) -> GitConfig {
let path = Path::new(path);
let mut file = File::create(path).unwrap();
file.write_all(contents).unwrap();
GitConfig::from_path(&path)
GitConfig::from_path(&path, honor_env_var)
}

pub fn get_line_of_code_from_delta(
Expand Down

0 comments on commit dc26797

Please sign in to comment.