diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-07-15 11:34:16 -0400 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2020-07-15 12:28:10 -0400 |
commit | 5f4a1fdc089a81981a00461dbf242214c410d101 (patch) | |
tree | 178e26b7d345b03abd6d02b538d4b0a3f3f7cc7f /src/features | |
parent | f26b726fd8b55870def3d63a591ca2c4a3c132dc (diff) |
Refactor: unify test utility functions
Diffstat (limited to 'src/features')
-rw-r--r-- | src/features/diff_highlight.rs | 10 | ||||
-rw-r--r-- | src/features/diff_so_fancy.rs | 14 | ||||
-rw-r--r-- | src/features/line_numbers.rs | 16 | ||||
-rw-r--r-- | src/features/mod.rs | 120 | ||||
-rw-r--r-- | src/features/navigate.rs | 57 | ||||
-rw-r--r-- | src/features/side_by_side.rs | 10 |
6 files changed, 134 insertions, 93 deletions
diff --git a/src/features/diff_highlight.rs b/src/features/diff_highlight.rs index 03e9792f..ea7ac7c9 100644 --- a/src/features/diff_highlight.rs +++ b/src/features/diff_highlight.rs @@ -58,11 +58,15 @@ pub fn _make_feature(bold: bool) -> Vec<(String, OptionValueFunction)> { mod test_utils { use std::fs::remove_file; - use crate::features; + use crate::tests::integration_test_utils::integration_test_utils; #[test] fn test_diff_highlight_defaults() { - let opt = features::tests::make_options(&["--features", "diff-highlight"], None, None); + let opt = integration_test_utils::make_options_from_args_and_git_config( + &["--features", "diff-highlight"], + None, + None, + ); assert_eq!(opt.minus_style, "red"); assert_eq!(opt.minus_non_emph_style, "red"); assert_eq!(opt.minus_emph_style, "red reverse"); @@ -87,7 +91,7 @@ mod test_utils { "; let git_config_path = "delta__test_diff_highlight.gitconfig"; - let opt = features::tests::make_options( + let opt = integration_test_utils::make_options_from_args_and_git_config( &["--features", "diff-highlight"], Some(git_config_contents), Some(git_config_path), diff --git a/src/features/diff_so_fancy.rs b/src/features/diff_so_fancy.rs index d05d2e68..cdc0c359 100644 --- a/src/features/diff_so_fancy.rs +++ b/src/features/diff_so_fancy.rs @@ -54,11 +54,15 @@ pub fn make_feature() -> Vec<(String, OptionValueFunction)> { pub mod tests { use std::fs::remove_file; - use crate::features; + use crate::tests::integration_test_utils::integration_test_utils; #[test] fn test_diff_so_fancy_defaults() { - let opt = features::tests::make_options(&["--features", "diff-so-fancy"], None, None); + let opt = integration_test_utils::make_options_from_args_and_git_config( + &["--features", "diff-so-fancy"], + None, + None, + ); assert_eq!(opt.commit_style, "bold yellow"); assert_eq!(opt.commit_decoration_style, "none"); @@ -83,7 +87,7 @@ pub mod tests { "; let git_config_path = "delta__test_diff_so_fancy.gitconfig"; - let opt = features::tests::make_options( + let opt = integration_test_utils::make_options_from_args_and_git_config( &["--features", "diff-so-fancy some-other-feature"], Some(git_config_contents), Some(git_config_path), @@ -117,7 +121,7 @@ pub mod tests { "; let git_config_path = "delta__test_diff_so_fancy_obeys_feature_precedence_rules.gitconfig"; - let opt = features::tests::make_options( + let opt = integration_test_utils::make_options_from_args_and_git_config( &["--features", "decorations diff-so-fancy"], Some(git_config_contents), Some(git_config_path), @@ -127,7 +131,7 @@ pub mod tests { assert_eq!(opt.file_decoration_style, "bold yellow ul ol"); - let opt = features::tests::make_options( + let opt = integration_test_utils::make_options_from_args_and_git_config( &["--features", "diff-so-fancy decorations"], Some(git_config_contents), Some(git_config_path), diff --git a/src/features/line_numbers.rs b/src/features/line_numbers.rs index 8426d529..b1d9b5b0 100644 --- a/src/features/line_numbers.rs +++ b/src/features/line_numbers.rs @@ -284,7 +284,9 @@ pub mod tests { use console::strip_ansi_codes; use regex::Captures; - use crate::tests::integration_test_utils::integration_test_utils::{make_config, run_delta}; + use crate::tests::integration_test_utils::integration_test_utils::{ + make_config_from_args, run_delta, + }; use super::*; @@ -401,7 +403,7 @@ pub mod tests { #[test] fn test_two_minus_lines() { - let config = make_config(&[ + let config = make_config_from_args(&[ "--line-numbers", "--line-numbers-left-format", "{nm:^4}⋮", @@ -425,7 +427,7 @@ pub mod tests { #[test] fn test_two_plus_lines() { - let config = make_config(&[ + let config = make_config_from_args(&[ "--line-numbers", "--line-numbers-left-format", "{nm:^4}⋮", @@ -449,7 +451,7 @@ pub mod tests { #[test] fn test_one_minus_one_plus_line() { - let config = make_config(&[ + let config = make_config_from_args(&[ "--line-numbers", "--line-numbers-left-format", "{nm:^4}⋮", @@ -474,7 +476,7 @@ pub mod tests { #[test] fn test_repeated_placeholder() { - let config = make_config(&[ + let config = make_config_from_args(&[ "--line-numbers", "--line-numbers-left-format", "{nm:^4} {nm:^4}⋮", @@ -499,7 +501,7 @@ pub mod tests { #[test] fn test_five_digit_line_number() { - let config = make_config(&["--line-numbers"]); + let config = make_config_from_args(&["--line-numbers"]); let output = run_delta(FIVE_DIGIT_LINE_NUMBER_DIFF, &config); let output = strip_ansi_codes(&output); let mut lines = output.lines().skip(4); @@ -510,7 +512,7 @@ pub mod tests { #[test] fn test_unequal_digit_line_number() { - let config = make_config(&["--line-numbers"]); + let config = make_config_from_args(&["--line-numbers"]); let output = run_delta(UNEQUAL_DIGIT_DIFF, &config); let output = strip_ansi_codes(&output); let mut lines = output.lines().skip(4); diff --git a/src/features/mod.rs b/src/features/mod.rs index 30b3a7a5..58599ea6 100644 --- a/src/features/mod.rs +++ b/src/features/mod.rs @@ -89,15 +89,11 @@ pub mod side_by_side; #[cfg(test)] pub mod tests { use std::collections::HashSet; - use std::fs::{remove_file, File}; - use std::io::Write; - use std::path::Path; - - use itertools; + use std::fs::remove_file; use crate::cli; use crate::features::make_builtin_features; - use crate::git_config::GitConfig; + use crate::tests::integration_test_utils::integration_test_utils::make_options_from_args_and_git_config; #[test] fn test_builtin_features_have_flags_and_these_set_features() { @@ -120,7 +116,12 @@ pub mod tests { let git_config_path = "delta__test_builtin_feature_from_gitconfig.gitconfig"; assert_eq!( - make_options(&[], Some(git_config_contents), Some(git_config_path)).features, + make_options_from_args_and_git_config( + &[], + Some(git_config_contents), + Some(git_config_path) + ) + .features, "navigate" ); @@ -137,7 +138,7 @@ pub mod tests { "delta__test_features_on_command_line_replace_features_in_gitconfig.gitconfig"; assert_eq!( - make_options( + make_options_from_args_and_git_config( &["--features", "navigate raw"], Some(git_config_contents), Some(git_config_path), @@ -146,7 +147,7 @@ pub mod tests { "navigate raw" ); assert_eq!( - make_options( + make_options_from_args_and_git_config( &["--navigate", "--features", "raw"], Some(git_config_contents), Some(git_config_path), @@ -167,7 +168,7 @@ pub mod tests { let git_config_path = "delta__test_feature_flag_on_command_line_does_not_replace_features_in_gitconfig.gitconfig"; assert_eq!( - make_options( + make_options_from_args_and_git_config( &["--navigate", "--raw"], Some(git_config_contents), Some(git_config_path), @@ -196,7 +197,7 @@ pub mod tests { let git_config_path = "delta__test_feature_collection.gitconfig"; assert_eq!( - make_options( + make_options_from_args_and_git_config( &["--raw", "--features", "d a"], Some(git_config_contents), Some(git_config_path), @@ -224,7 +225,11 @@ pub mod tests { minus-style = blue "; let git_config_path = "delta__test_recursive_features.gitconfig"; - let opt = make_options(&["delta"], Some(git_config_contents), Some(git_config_path)); + let opt = make_options_from_args_and_git_config( + &["delta"], + Some(git_config_contents), + Some(git_config_path), + ); assert_eq!(opt.features, "feature-4 feature-2 feature-3 feature-1"); remove_file(git_config_path).unwrap(); @@ -240,17 +245,21 @@ pub mod tests { // First check that it doesn't default to blue, because that's going to be used to signal // that gitconfig has set the style. - assert_ne!(make_options(&[], None, None).minus_style, "blue"); + assert_ne!( + make_options_from_args_and_git_config(&[], None, None).minus_style, + "blue" + ); // Check that --minus-style is honored as we expect. assert_eq!( - make_options(&["--minus-style", "red"], None, None).minus_style, + make_options_from_args_and_git_config(&["--minus-style", "red"], None, None) + .minus_style, "red" ); // Check that gitconfig does not override a command line argument assert_eq!( - make_options( + make_options_from_args_and_git_config( &["--minus-style", "red"], Some(git_config_contents), Some(git_config_path), @@ -261,7 +270,12 @@ pub mod tests { // Finally, check that gitconfig is honored when not overridden by a command line argument. assert_eq!( - make_options(&[], Some(git_config_contents), Some(git_config_path)).minus_style, + make_options_from_args_and_git_config( + &[], + Some(git_config_contents), + Some(git_config_path) + ) + .minus_style, "blue" ); @@ -280,7 +294,7 @@ pub mod tests { let git_config_path = "delta__test_feature.gitconfig"; assert_eq!( - make_options( + make_options_from_args_and_git_config( &["--features", "my-feature"], Some(git_config_contents), Some(git_config_path), @@ -304,13 +318,18 @@ pub mod tests { // Without --features the main section takes effect assert_eq!( - make_options(&[], Some(git_config_contents), Some(git_config_path)).minus_style, + make_options_from_args_and_git_config( + &[], + Some(git_config_contents), + Some(git_config_path) + ) + .minus_style, "blue" ); // Event with --features the main section overrides the feature. assert_eq!( - make_options( + make_options_from_args_and_git_config( &["--features", "my-feature-1"], Some(git_config_contents), Some(git_config_path), @@ -336,7 +355,7 @@ pub mod tests { let git_config_path = "delta__test_multiple_features.gitconfig"; assert_eq!( - make_options( + make_options_from_args_and_git_config( &["--features", "my-feature-1 my-feature-2"], Some(git_config_contents), Some(git_config_path), @@ -346,7 +365,7 @@ pub mod tests { ); assert_eq!( - make_options( + make_options_from_args_and_git_config( &["--features", "my-feature-2 my-feature-1"], Some(git_config_contents), Some(git_config_path), @@ -369,12 +388,12 @@ pub mod tests { "; let git_config_path = "delta__test_invalid_features.gitconfig"; - let default = make_options(&[], None, None).minus_style; + let default = make_options_from_args_and_git_config(&[], None, None).minus_style; assert_ne!(default, "green"); assert_ne!(default, "yellow"); assert_eq!( - make_options( + make_options_from_args_and_git_config( &["--features", "my-feature-1"], Some(git_config_contents), Some(git_config_path), @@ -384,7 +403,7 @@ pub mod tests { ); assert_eq!( - make_options( + make_options_from_args_and_git_config( &["--features", "my-feature-x"], Some(git_config_contents), Some(git_config_path), @@ -394,7 +413,7 @@ pub mod tests { ); assert_eq!( - make_options( + make_options_from_args_and_git_config( &["--features", "my-feature-1 my-feature-x"], Some(git_config_contents), Some(git_config_path), @@ -404,7 +423,7 @@ pub mod tests { ); assert_eq!( - make_options( + make_options_from_args_and_git_config( &["--features", "my-feature-x my-feature-2 my-feature-x"], Some(git_config_contents), Some(git_config_path), @@ -426,20 +445,24 @@ pub mod tests { // Git config disabled: hard-coded delta default wins assert_eq!( - make_options(&[], None, None).whitespace_error_style, + make_options_from_args_and_git_config(&[], None, None).whitespace_error_style, "magenta reverse" ); // Unspecified by user: color.diff.whitespace wins assert_eq!( - make_options(&[], Some(git_config_contents), Some(git_config_path)) - .whitespace_error_style, + make_options_from_args_and_git_config( + &[], + Some(git_config_contents), + Some(git_config_path) + ) + .whitespace_error_style, "yellow dim ul magenta" ); // Command line argument wins assert_eq!( - make_options( + make_options_from_args_and_git_config( &["--whitespace-error-style", "red reverse"], Some(git_config_contents), Some(git_config_path) @@ -461,7 +484,7 @@ pub mod tests { // Command line argument wins assert_eq!( - make_options( + make_options_from_args_and_git_config( &["--whitespace-error-style", "red reverse"], Some(git_config_contents), Some(git_config_path) @@ -472,8 +495,12 @@ pub mod tests { // No command line argument or features; main [delta] section wins assert_eq!( - make_options(&[], Some(git_config_contents), Some(git_config_path)) - .whitespace_error_style, + make_options_from_args_and_git_config( + &[], + Some(git_config_contents), + Some(git_config_path) + ) + .whitespace_error_style, "blue reverse" ); @@ -486,7 +513,7 @@ pub mod tests { // // In this situation, the value from the feature is overridden. assert_eq!( - make_options( + make_options_from_args_and_git_config( &["--features", "my-whitespace-error-style-feature"], Some(git_config_contents), Some(git_config_path) @@ -497,29 +524,4 @@ pub mod tests { remove_file(git_config_path).unwrap(); } - - fn make_git_config(contents: &[u8], path: &str) -> GitConfig { - let path = Path::new(path); - let mut file = File::create(path).unwrap(); - file.write_all(contents).unwrap(); - GitConfig::from_path(&path) - } - - pub fn make_options( - args: &[&str], - git_config_contents: Option<&[u8]>, - git_config_path: Option<&str>, - ) -> cli::Opt { - let args: Vec<&str> = itertools::chain( - &["/dev/null", "/dev/null", "--24-bit-color", "always"], - 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)), - _ => None, - }; - cli::Opt::from_iter_and_git_config(args, &mut git_config) - } } diff --git a/src/features/navigate.rs b/src/features/navigate.rs index 1175b13b..35549f24 100644 --- a/src/features/navigate.rs +++ b/src/features/navigate.rs @@ -32,7 +32,7 @@ pub fn make_navigate_regexp(config: &Config) -> String { mod tests { use std::fs::remove_file; - use crate::features; + use crate::tests::integration_test_utils::integration_test_utils; #[test] fn test_navigate_with_overriden_key_in_main_section() { @@ -44,21 +44,35 @@ mod tests { let git_config_path = "delta__test_navigate_with_overriden_key_in_main_section.gitconfig"; assert_eq!( - features::tests::make_options(&[], None, None).file_modified_label, + integration_test_utils::make_options_from_args_and_git_config(&[], None, None) + .file_modified_label, "" ); assert_eq!( - features::tests::make_options(&["--features", "navigate"], None, None) - .file_modified_label, + integration_test_utils::make_options_from_args_and_git_config( + &["--features", "navigate"], + None, + None + ) + .file_modified_label, "Δ" ); assert_eq!( - features::tests::make_options(&["--navigate"], None, None).file_modified_label, + integration_test_utils::make_options_from_args_and_git_config( + &["--navigate"], + None, + None + ) + .file_modified_label, "Δ" ); assert_eq!( - features::tests::make_options(&[], Some(git_config_contents), Some(git_config_path)) - .file_modified_label, + integration_test_utils::make_options_from_args_and_git_config( + &[], + Some(git_config_contents), + Some(git_config_path) + ) + .file_modified_label, "modified: " ); @@ -78,17 +92,26 @@ mod tests { "delta__test_navigate_with_overriden_key_in_custom_navigate_section.gitconfig"; assert_eq!( - features::tests::make_options(&[], None, None).file_modified_label, + integration_test_utils::make_options_from_args_and_git_config(&[], None, None) + .file_modified_label, "" ); assert_eq!( - features::tests::make_options(&["--features", "navigate"], None, None) - .file_modified_label, + integration_test_utils::make_options_from_args_and_git_config( + &["--features", "navigate"], + None, + None + ) + .file_modified_label, "Δ" ); assert_eq!( - features::tests::make_options(&[], Some(git_config_contents), Some(git_config_path)) - .file_modified_label, + integration_test_utils::make_options_from_args_and_git_config( + &[], + Some(git_config_contents), + Some(git_config_path) + ) + .file_modified_label, "modified: " ); @@ -105,12 +128,16 @@ mod tests { let git_config_path = "delta__test_navigate_activated_by_custom_feature.gitconfig"; assert_eq!( - features::tests::make_options(&[], Some(git_config_contents), Some(git_config_path)) - .file_modified_label, + integration_test_utils::make_options_from_args_and_git_config( + &[], + Some(git_config_contents), + Some(git_config_path) + ) + .file_modified_label, "" ); assert_eq!( - features::tests::make_options( + integration_test_utils::make_options_from_args_and_git_config( &["--features", "my-navigate-feature"], Some(git_config_contents), Some(git_config_path) diff --git a/src/features/side_by_side.rs b/src/features/side_by_side.rs index ada02c71..eb224f16 100644 --- a/src/features/side_by_side.rs +++ b/src/features/side_by_side.rs @@ -444,11 +444,13 @@ pub mod tests { use console::strip_ansi_codes; use crate::features::line_numbers::tests::*; - use crate::tests::integration_test_utils::integration_test_utils::{make_config, run_delta}; + use crate::tests::integration_test_utils::integration_test_utils::{ + make_config_from_args, run_delta, + }; #[test] fn test_two_minus_lines() { - let config = make_config(&["--side-by-side", "--width", "40"]); + let config = make_config_from_args(&["--side-by-side", "--width", "40"]); let output = run_delta(TWO_MINUS_LINES_DIFF, &config); let mut lines = output.lines().skip(4); let (line_1, line_2) = (lines.next().unwrap(), lines.next().unwrap()); @@ -458,7 +460,7 @@ pub mod tests { #[test] fn test_two_plus_lines() { - let config = make_config(&["--side-by-side", "--width", "40"]); + let config = make_config_from_args(&["--side-by-side", "--width", "40"]); let output = run_delta(TWO_PLUS_LINES_DIFF, &config); let mut lines = output.lines().skip(4); let (line_1, line_2) = (lines.next().unwrap(), lines.next().unwrap()); @@ -468,7 +470,7 @@ pub mod tests { #[test] fn test_one_minus_one_plus_line() { - let config = make_config(&["--side-by-side", "--width", "40"]); + let config = make_config_from_args(&["--side-by-side", "--width", "40"]); let output = run_delta(ONE_MINUS_ONE_PLUS_LINE_DIFF, &config); let output = strip_ansi_codes(&output); let mut lines = output.lines().skip(4); |