From e4aad8c1ef2d4df739a3805d039fb439150e482a Mon Sep 17 00:00:00 2001 From: Bastien Jacot-Guillarmod Date: Tue, 18 Jul 2023 17:27:59 +0200 Subject: [PATCH] Remove the feature of coloring the summarize diff. --- .../src/matcher_support/summarize_diff.rs | 77 ++----------------- googletest/src/matchers/display_matcher.rs | 6 +- .../src/matchers/eq_deref_of_matcher.rs | 10 +-- googletest/src/matchers/eq_matcher.rs | 68 ++++++++-------- googletest/src/matchers/str_matcher.rs | 62 +++++++-------- 5 files changed, 79 insertions(+), 144 deletions(-) diff --git a/googletest/src/matcher_support/summarize_diff.rs b/googletest/src/matcher_support/summarize_diff.rs index 9613ec6d..3428462b 100644 --- a/googletest/src/matcher_support/summarize_diff.rs +++ b/googletest/src/matcher_support/summarize_diff.rs @@ -16,13 +16,8 @@ use std::borrow::Cow; use std::fmt::{Display, Write}; -use nu_ansi_term::{Color, Style}; - use crate::matcher_support::edit_distance; -/// Environment variable controlling the usage of ansi color in difference -/// summary. -const NO_COLOR_VAR: &str = "GTEST_RUST_NO_COLOR"; /// Returns a string describing how the expected and actual lines differ. /// @@ -155,25 +150,24 @@ fn compress_common_lines(common_lines: Vec<&str>) -> String { } struct LineStyle { - ansi: Style, header: char, } impl LineStyle { fn extra_actual_style() -> Self { - Self { ansi: Style::new().fg(Color::Red).bold(), header: '-' } + Self { header: '-' } } fn extra_expected_style() -> Self { - Self { ansi: Style::new().fg(Color::Green).bold(), header: '+' } + Self { header: '+' } } fn comment_style() -> Self { - Self { ansi: Style::new().italic(), header: ' ' } + Self { header: ' ' } } fn unchanged_style() -> Self { - Self { ansi: Style::new(), header: ' ' } + Self { header: ' ' } } fn style(self, line: &str) -> StyledLine<'_> { @@ -188,18 +182,7 @@ struct StyledLine<'a> { impl<'a> Display for StyledLine<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - if std::env::var(NO_COLOR_VAR).is_err() { - write!( - f, - "{}{}{}{}", - self.style.header, - self.style.ansi.prefix(), - self.line, - self.style.ansi.suffix() - ) - } else { - write!(f, "{}{}", self.style.header, self.line) - } + write!(f, "{}{}", self.style.header, self.line) } } @@ -208,31 +191,6 @@ mod tests { use super::*; use crate::{matcher_support::edit_distance::Mode, prelude::*}; use indoc::indoc; - use serial_test::serial; - - #[must_use] - fn remove_var() -> TempVar { - let old_value = std::env::var(NO_COLOR_VAR); - std::env::remove_var(NO_COLOR_VAR); - TempVar(old_value.ok()) - } - - #[must_use] - fn set_var(var: &str) -> TempVar { - let old_value = std::env::var(NO_COLOR_VAR); - std::env::set_var(NO_COLOR_VAR, var); - TempVar(old_value.ok()) - } - struct TempVar(Option); - - impl Drop for TempVar { - fn drop(&mut self) { - match &self.0 { - Some(old_var) => std::env::set_var(NO_COLOR_VAR, old_var), - None => std::env::remove_var(NO_COLOR_VAR), - } - } - } // Make a long text with each element of the iterator on one line. // `collection` must contains at least one element. @@ -272,32 +230,9 @@ mod tests { verify_that!(create_diff(&build_text(1..500), &build_text(501..1000), Mode::Exact), eq("")) } - #[test] - #[serial] - fn create_diff_exact_small_difference() -> Result<()> { - let _cleanup = remove_var(); - - verify_that!( - create_diff(&build_text(1..50), &build_text(1..51), Mode::Exact), - eq(indoc! { - " - - Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m): - 1 - 2 - \x1B[3m<---- 45 common lines omitted ---->\x1B[0m - 48 - 49 - +\x1B[1;32m50\x1B[0m" - }) - ) - } #[test] - #[serial] - fn create_diff_exact_small_difference_no_color() -> Result<()> { - let _cleanup = set_var("NO_COLOR"); - + fn create_diff_exact_small_difference() -> Result<()> { verify_that!( create_diff(&build_text(1..50), &build_text(1..51), Mode::Exact), eq(indoc! { diff --git a/googletest/src/matchers/display_matcher.rs b/googletest/src/matchers/display_matcher.rs index 3c110acb..628769be 100644 --- a/googletest/src/matchers/display_matcher.rs +++ b/googletest/src/matchers/display_matcher.rs @@ -108,10 +108,10 @@ mod tests { err(displays_as(contains_substring(indoc!( " which displays as a string which isn't equal to \"123\\n345\" - Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m): + Difference(-actual / +expected): 123 - -\x1B[1;31m234\x1B[0m - +\x1B[1;32m345\x1B[0m + -234 + +345 " )))) ) diff --git a/googletest/src/matchers/eq_deref_of_matcher.rs b/googletest/src/matchers/eq_deref_of_matcher.rs index cb810ead..15409058 100644 --- a/googletest/src/matchers/eq_deref_of_matcher.rs +++ b/googletest/src/matchers/eq_deref_of_matcher.rs @@ -138,12 +138,12 @@ mod tests { " Actual: Strukt { int: 123, string: \"something\" }, which isn't equal to Strukt { int: 321, string: \"someone\" } - Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m): + Difference(-actual / +expected): Strukt { - -\x1B[1;31m int: 123,\x1B[0m - +\x1B[1;32m int: 321,\x1B[0m - -\x1B[1;31m string: \"something\",\x1B[0m - +\x1B[1;32m string: \"someone\",\x1B[0m + - int: 123, + + int: 321, + - string: \"something\", + + string: \"someone\", } "}))) ) diff --git a/googletest/src/matchers/eq_matcher.rs b/googletest/src/matchers/eq_matcher.rs index fc52ecb2..abd52ec2 100644 --- a/googletest/src/matchers/eq_matcher.rs +++ b/googletest/src/matchers/eq_matcher.rs @@ -178,12 +178,12 @@ mod tests { " Actual: Strukt { int: 123, string: \"something\" }, which isn't equal to Strukt { int: 321, string: \"someone\" } - Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m): + Difference(-actual / +expected): Strukt { - -\x1B[1;31m int: 123,\x1B[0m - +\x1B[1;32m int: 321,\x1B[0m - -\x1B[1;31m string: \"something\",\x1B[0m - +\x1B[1;32m string: \"someone\",\x1B[0m + - int: 123, + + int: 321, + - string: \"something\", + + string: \"someone\", } "}))) ) @@ -200,12 +200,12 @@ mod tests { Expected: is equal to [1, 3, 4] Actual: [1, 2, 3], which isn't equal to [1, 3, 4] - Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m): + Difference(-actual / +expected): [ 1, - -\x1B[1;31m 2,\x1B[0m + - 2, 3, - +\x1B[1;32m 4,\x1B[0m + + 4, ] "}))) ) @@ -222,12 +222,12 @@ mod tests { Expected: is equal to [1, 3, 5] Actual: [1, 2, 3, 4, 5], which isn't equal to [1, 3, 5] - Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m): + Difference(-actual / +expected): [ 1, - -\x1B[1;31m 2,\x1B[0m + - 2, 3, - -\x1B[1;31m 4,\x1B[0m + - 4, 5, ] "}))) @@ -241,17 +241,17 @@ mod tests { result, err(displays_as(contains_substring(indoc! { " - Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m): + Difference(-actual / +expected): [ - -\x1B[1;31m 1,\x1B[0m - -\x1B[1;31m 2,\x1B[0m + - 1, + - 2, 3, 4, - \x1B[3m<---- 43 common lines omitted ---->\x1B[0m + <---- 43 common lines omitted ----> 48, 49, - +\x1B[1;32m 50,\x1B[0m - +\x1B[1;32m 51,\x1B[0m + + 50, + + 51, ]"}))) ) } @@ -263,17 +263,17 @@ mod tests { result, err(displays_as(contains_substring(indoc! { " - Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m): + Difference(-actual / +expected): [ - -\x1B[1;31m 1,\x1B[0m - -\x1B[1;31m 2,\x1B[0m + - 1, + - 2, 3, 4, 5, 6, 7, - +\x1B[1;32m 8,\x1B[0m - +\x1B[1;32m 9,\x1B[0m + + 8, + + 9, ]"}))) ) } @@ -285,14 +285,14 @@ mod tests { result, err(displays_as(contains_substring(indoc! { " - Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m): + Difference(-actual / +expected): [ 1, - \x1B[3m<---- 46 common lines omitted ---->\x1B[0m + <---- 46 common lines omitted ----> 48, 49, - +\x1B[1;32m 50,\x1B[0m - +\x1B[1;32m 51,\x1B[0m + + 50, + + 51, ]"}))) ) } @@ -304,13 +304,13 @@ mod tests { result, err(displays_as(contains_substring(indoc! { " - Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m): + Difference(-actual / +expected): [ - -\x1B[1;31m 1,\x1B[0m - -\x1B[1;31m 2,\x1B[0m + - 1, + - 2, 3, 4, - \x1B[3m<---- 46 common lines omitted ---->\x1B[0m + <---- 46 common lines omitted ----> 51, ]"}))) ) @@ -357,8 +357,8 @@ mod tests { err(displays_as(contains_substring(indoc!( " First line - -\x1B[1;31mSecond line\x1B[0m - +\x1B[1;32mSecond lines\x1B[0m + -Second line + +Second lines Third line " )))) @@ -381,7 +381,7 @@ mod tests { verify_that!( result, err(displays_as(not(contains_substring( - "Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m):" + "Difference(-actual / +expected):" )))) ) } @@ -402,7 +402,7 @@ mod tests { verify_that!( result, err(displays_as(not(contains_substring( - "Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m):" + "Difference(-actual / +expected):" )))) ) } diff --git a/googletest/src/matchers/str_matcher.rs b/googletest/src/matchers/str_matcher.rs index c8a510e3..9bd51485 100644 --- a/googletest/src/matchers/str_matcher.rs +++ b/googletest/src/matchers/str_matcher.rs @@ -974,8 +974,8 @@ mod tests { err(displays_as(contains_substring(indoc!( " First line - -\x1B[1;31mSecond line\x1B[0m - +\x1B[1;32mSecond lines\x1B[0m + -Second line + +Second lines Third line " )))) @@ -1007,10 +1007,10 @@ mod tests { err(displays_as(contains_substring(indoc!( " First line - -\x1B[1;31mSecond line\x1B[0m - +\x1B[1;32mSecond lines\x1B[0m + -Second line + +Second lines Third line - \x1B[3m<---- remaining lines omitted ---->\x1B[0m + <---- remaining lines omitted ----> " )))) ) @@ -1040,9 +1040,9 @@ mod tests { err(displays_as(contains_substring(indoc!( " First line - -\x1B[1;31mSecond line\x1B[0m - +\x1B[1;32mSecond lines\x1B[0m - \x1B[3m<---- remaining lines omitted ---->\x1B[0m + -Second line + +Second lines + <---- remaining lines omitted ----> " )))) ) @@ -1072,11 +1072,11 @@ mod tests { result, err(displays_as(contains_substring(indoc!( " - Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m): - \x1B[3m<---- remaining lines omitted ---->\x1B[0m + Difference(-actual / +expected): + <---- remaining lines omitted ----> Second line - +\x1B[1;32mThird lines\x1B[0m - -\x1B[1;31mThird line\x1B[0m + +Third lines + -Third line Fourth line " )))) @@ -1109,13 +1109,13 @@ mod tests { result, err(displays_as(contains_substring(indoc!( " - Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m): - \x1B[3m<---- remaining lines omitted ---->\x1B[0m + Difference(-actual / +expected): + <---- remaining lines omitted ----> Second line - +\x1B[1;32mThird lines\x1B[0m - -\x1B[1;31mThird line\x1B[0m + +Third lines + -Third line Fourth line - \x1B[3m<---- remaining lines omitted ---->\x1B[0m" + <---- remaining lines omitted ---->" )))) ) } @@ -1146,16 +1146,16 @@ mod tests { result, err(displays_as(contains_substring(indoc!( " - Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m): - \x1B[3m<---- remaining lines omitted ---->\x1B[0m - +\x1B[1;32mline\x1B[0m - -\x1B[1;31mSecond line\x1B[0m + Difference(-actual / +expected): + <---- remaining lines omitted ----> + +line + -Second line Third line - +\x1B[1;32mFoorth line\x1B[0m - -\x1B[1;31mFourth line\x1B[0m - +\x1B[1;32mFifth\x1B[0m - -\x1B[1;31mFifth line\x1B[0m - \x1B[3m<---- remaining lines omitted ---->\x1B[0m + +Foorth line + -Fourth line + +Fifth + -Fifth line + <---- remaining lines omitted ----> " )))) ) @@ -1186,10 +1186,10 @@ mod tests { err(displays_as(contains_substring(indoc!( " First line - -\x1B[1;31mSecond line\x1B[0m - +\x1B[1;32mSecond lines\x1B[0m + -Second line + +Second lines Third line - -\x1B[1;31mFourth line\x1B[0m + -Fourth line " )))) ) @@ -1210,7 +1210,7 @@ mod tests { verify_that!( result, err(displays_as(not(contains_substring( - "Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m):" + "Difference(-actual / +expected):" )))) ) } @@ -1231,7 +1231,7 @@ mod tests { verify_that!( result, err(displays_as(not(contains_substring( - "Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m):" + "Difference(-actual / +expected):" )))) ) }