Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for using is_terminal and environment variables to determine whether to output ANSI colour sequences. #275

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,10 @@ displayed, we recommend setting those variables in the personal

### Configuration variable list

| Variable name | Description |
| ------------------- | -------------------------------------------------- |
| GTEST_RUST_NO_COLOR | If set to any value, disables ANSI output from the failure message. This is useful when the failure description is piped to a file or another process. |
| Variable name | Description |
| ------------- | ------------------------------------------------------- |
| NO_COLOR | Disables coloured output. See <https://no-color.org/>. |
| FORCE_COLOR | Forces colours even when the output is piped to a file. |

## Contributing Changes

Expand Down
3 changes: 2 additions & 1 deletion googletest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ authors = [
googletest_macro = { path = "../googletest_macro", version = "0.9.0" }
anyhow = { version = "1", optional = true }
num-traits = "0.2.15"
regex = "1.6.0"
proptest = { version = "1.2.0", optional = true }
regex = "1.6.0"
rustversion = "1.0.14"

[dev-dependencies]
indoc = "2"
Expand Down
2 changes: 2 additions & 0 deletions googletest/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[env]
NO_COLOR = "1"
82 changes: 26 additions & 56 deletions googletest/src/matcher_support/summarize_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@

#![doc(hidden)]

use std::borrow::Cow;
use std::fmt::{Display, Write};

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";
#[rustversion::since(1.70)]
use std::io::IsTerminal;
use std::{
borrow::Cow,
fmt::{Display, Write},
};

/// Returns a string describing how the expected and actual lines differ.
///
Expand Down Expand Up @@ -195,7 +194,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() {
if stdout_supports_colour() {
write!(
f,
"{}{}{}{}",
Expand All @@ -207,36 +206,29 @@ impl<'a> Display for StyledLine<'a> {
}
}

#[rustversion::since(1.70)]
fn stdout_supports_colour() -> bool {
match (is_env_var_set("NO_COLOR"), is_env_var_set("FORCE_COLOR")) {
(true, _) => false,
(false, true) => true,
(false, false) => std::io::stdout().is_terminal(),
}
}

#[rustversion::not(since(1.70))]
fn stdout_supports_colour() -> bool {
is_env_var_set("FORCE_COLOR")
}

fn is_env_var_set(var: &'static str) -> bool {
std::env::var(var).map(|s| !s.is_empty()).unwrap_or(false)
}

#[cfg(test)]
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<String>);

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.
Expand Down Expand Up @@ -277,30 +269,8 @@ mod tests {
}

#[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");
std::env::set_var("NO_COLOR", "1");

verify_that!(
create_diff(&build_text(1..50), &build_text(1..51), Mode::Exact),
Expand Down
6 changes: 3 additions & 3 deletions googletest/src/matchers/display_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
"
))))
)
Expand Down
10 changes: 5 additions & 5 deletions googletest/src/matchers/eq_deref_of_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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\",
}
"})))
)
Expand Down
72 changes: 34 additions & 38 deletions googletest/src/matchers/eq_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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\",
}
"})))
)
Expand All @@ -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,
]
"})))
)
Expand All @@ -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,
]
"})))
Expand All @@ -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,
]"})))
)
}
Expand All @@ -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,
]"})))
)
}
Expand All @@ -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,
]"})))
)
}
Expand All @@ -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,
]"})))
)
Expand Down Expand Up @@ -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
"
))))
Expand All @@ -380,9 +380,7 @@ mod tests {

verify_that!(
result,
err(displays_as(not(contains_substring(
"Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m):"
))))
err(displays_as(not(contains_substring("Difference(-actual / +expected):"))))
)
}

Expand All @@ -401,9 +399,7 @@ mod tests {

verify_that!(
result,
err(displays_as(not(contains_substring(
"Difference(-\x1B[1;31mactual\x1B[0m / +\x1B[1;32mexpected\x1B[0m):"
))))
err(displays_as(not(contains_substring("Difference(-actual / +expected):"))))
)
}
}
Loading