Skip to content

Commit ff4d625

Browse files
committed
Save color configuration in a thread local variable
1 parent c7e859f commit ff4d625

File tree

5 files changed

+12
-46
lines changed

5 files changed

+12
-46
lines changed

googletest/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,3 @@ rustversion = "1.0.14"
4141
[dev-dependencies]
4242
indoc = "2"
4343
quickcheck = "1.0.3"
44-
serial_test = "2.0.0"

googletest/src/matcher_support/summarize_diff.rs

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use crate::matcher_support::edit_distance;
1818
#[rustversion::since(1.70)]
1919
use std::io::IsTerminal;
20-
use std::{borrow::Cow, fmt::Display};
20+
use std::{borrow::Cow, cell::Cell, fmt::Display};
2121

2222
/// Returns a string describing how the expected and actual lines differ.
2323
///
@@ -81,9 +81,9 @@ pub(crate) fn create_diff_reversed(
8181
}
8282

8383
// Produces the header, with or without coloring depending on
84-
// stdout_supports_color()
84+
// USE_COLOR
8585
fn summary_header() -> Cow<'static, str> {
86-
if stdout_supports_color() {
86+
if USE_COLOR.with(Cell::get) {
8787
format!(
8888
"Difference(-{ACTUAL_ONLY_STYLE}actual{RESET_ALL} / +{EXPECTED_ONLY_STYLE}expected{RESET_ALL}):"
8989
).into()
@@ -294,6 +294,10 @@ impl<'a> Default for Buffer<'a> {
294294
}
295295
}
296296

297+
thread_local! {
298+
pub(crate) static USE_COLOR: Cell<bool> = Cell::new(stdout_supports_color());
299+
}
300+
297301
#[rustversion::since(1.70)]
298302
fn stdout_supports_color() -> bool {
299303
#[allow(clippy::incompatible_msrv)]
@@ -389,14 +393,14 @@ impl SummaryBuilder {
389393
}
390394

391395
fn reset_ansi(&mut self) {
392-
if !self.last_ansi_style.is_empty() && stdout_supports_color() {
396+
if !self.last_ansi_style.is_empty() && USE_COLOR.with(Cell::get) {
393397
self.summary.push_str(RESET_ALL);
394398
self.last_ansi_style = "";
395399
}
396400
}
397401

398402
fn set_ansi(&mut self, ansi_style: &'static str) {
399-
if !stdout_supports_color() || self.last_ansi_style == ansi_style {
403+
if !USE_COLOR.with(Cell::get) || self.last_ansi_style == ansi_style {
400404
return;
401405
}
402406
if !self.last_ansi_style.is_empty() {
@@ -412,7 +416,6 @@ mod tests {
412416
use super::*;
413417
use crate::{matcher_support::edit_distance::Mode, prelude::*};
414418
use indoc::indoc;
415-
use serial_test::{parallel, serial};
416419
use std::fmt::Write;
417420

418421
// Make a long text with each element of the iterator on one line.
@@ -428,13 +431,11 @@ mod tests {
428431
}
429432

430433
#[test]
431-
#[parallel]
432434
fn create_diff_smaller_than_one_line() -> Result<()> {
433435
verify_that!(create_diff("One", "Two", Mode::Exact), eq(""))
434436
}
435437

436438
#[test]
437-
#[parallel]
438439
fn create_diff_exact_same() -> Result<()> {
439440
let expected = indoc! {"
440441
One
@@ -451,7 +452,6 @@ mod tests {
451452
}
452453

453454
#[test]
454-
#[parallel]
455455
fn create_diff_multiline_diff() -> Result<()> {
456456
let expected = indoc! {"
457457
prefix
@@ -484,13 +484,11 @@ mod tests {
484484
}
485485

486486
#[test]
487-
#[parallel]
488487
fn create_diff_exact_unrelated() -> Result<()> {
489488
verify_that!(create_diff(&build_text(1..500), &build_text(501..1000), Mode::Exact), eq(""))
490489
}
491490

492491
#[test]
493-
#[parallel]
494492
fn create_diff_exact_small_difference() -> Result<()> {
495493
verify_that!(
496494
create_diff(&build_text(1..50), &build_text(1..51), Mode::Exact),
@@ -508,27 +506,9 @@ mod tests {
508506
)
509507
}
510508

511-
// Test with color enabled.
512-
513-
struct ForceColor;
514-
515-
fn force_color() -> ForceColor {
516-
std::env::set_var("FORCE_COLOR", "1");
517-
std::env::remove_var("NO_COLOR");
518-
ForceColor
519-
}
520-
521-
impl Drop for ForceColor {
522-
fn drop(&mut self) {
523-
std::env::remove_var("FORCE_COLOR");
524-
std::env::set_var("NO_COLOR", "1");
525-
}
526-
}
527-
528509
#[test]
529-
#[serial]
530510
fn create_diff_exact_small_difference_with_color() -> Result<()> {
531-
let _keep = force_color();
511+
USE_COLOR.with(|cell| cell.set(true));
532512

533513
verify_that!(
534514
create_diff(&build_text(1..50), &build_text(1..51), Mode::Exact),
@@ -547,9 +527,9 @@ mod tests {
547527
}
548528

549529
#[test]
550-
#[serial]
551530
fn create_diff_exact_difference_with_inline_color() -> Result<()> {
552-
let _keep = force_color();
531+
USE_COLOR.with(|cell| cell.set(true));
532+
553533
let actual = indoc!(
554534
"There is a home in Nouvelle Orleans
555535
They say, it is the rising sons

googletest/src/matchers/display_matcher.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ impl<T: Debug + Display + Copy, InnerMatcher: for<'a> Matcher<&'a str>> Matcher<
6868
mod tests {
6969
use crate::prelude::*;
7070
use indoc::indoc;
71-
use serial_test::parallel;
7271
use std::fmt::{Debug, Display, Error, Formatter};
7372

7473
#[test]
@@ -103,7 +102,6 @@ mod tests {
103102
}
104103

105104
#[test]
106-
#[parallel]
107105
fn display_displays_error_message_with_explanation_from_inner_matcher() -> Result<()> {
108106
let result = verify_that!("123\n234", displays_as(eq("123\n345")));
109107

googletest/src/matchers/eq_matcher.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ fn to_display_output(string: &str) -> Option<String> {
135135
mod tests {
136136
use crate::prelude::*;
137137
use indoc::indoc;
138-
use serial_test::parallel;
139138

140139
#[test]
141140
fn eq_matches_string_reference_with_string_reference() -> Result<()> {
@@ -160,7 +159,6 @@ mod tests {
160159
}
161160

162161
#[test]
163-
#[parallel]
164162
fn eq_struct_debug_diff() -> Result<()> {
165163
#[derive(Debug, PartialEq)]
166164
struct Strukt {
@@ -190,7 +188,6 @@ mod tests {
190188
}
191189

192190
#[test]
193-
#[parallel]
194191
fn eq_vec_debug_diff() -> Result<()> {
195192
let result = verify_that!(vec![1, 2, 3], eq(&vec![1, 3, 4]));
196193
verify_that!(
@@ -213,7 +210,6 @@ mod tests {
213210
}
214211

215212
#[test]
216-
#[parallel]
217213
fn eq_vec_debug_diff_length_mismatch() -> Result<()> {
218214
let result = verify_that!(vec![1, 2, 3, 4, 5], eq(&vec![1, 3, 5]));
219215
verify_that!(
@@ -237,7 +233,6 @@ mod tests {
237233
}
238234

239235
#[test]
240-
#[parallel]
241236
fn eq_debug_diff_common_lines_omitted() -> Result<()> {
242237
let result = verify_that!((1..50).collect::<Vec<_>>(), eq(&(3..52).collect::<Vec<_>>()));
243238
verify_that!(
@@ -262,7 +257,6 @@ mod tests {
262257
}
263258

264259
#[test]
265-
#[parallel]
266260
fn eq_debug_diff_5_common_lines_not_omitted() -> Result<()> {
267261
let result = verify_that!((1..8).collect::<Vec<_>>(), eq(&(3..10).collect::<Vec<_>>()));
268262
verify_that!(
@@ -287,7 +281,6 @@ mod tests {
287281
}
288282

289283
#[test]
290-
#[parallel]
291284
fn eq_debug_diff_start_common_lines_omitted() -> Result<()> {
292285
let result = verify_that!((1..50).collect::<Vec<_>>(), eq(&(1..52).collect::<Vec<_>>()));
293286
verify_that!(
@@ -309,7 +302,6 @@ mod tests {
309302
}
310303

311304
#[test]
312-
#[parallel]
313305
fn eq_debug_diff_end_common_lines_omitted() -> Result<()> {
314306
let result = verify_that!((1..52).collect::<Vec<_>>(), eq(&(3..52).collect::<Vec<_>>()));
315307
verify_that!(
@@ -348,7 +340,6 @@ mod tests {
348340
}
349341

350342
#[test]
351-
#[parallel]
352343
fn match_explanation_contains_diff_of_strings_if_more_than_one_line() -> Result<()> {
353344
let result = verify_that!(
354345
indoc!(

googletest/src/matchers/str_matcher.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,6 @@ mod tests {
574574
use crate::matcher::MatcherResult;
575575
use crate::prelude::*;
576576
use indoc::indoc;
577-
use serial_test::parallel;
578577

579578
#[test]
580579
fn matches_string_reference_with_equal_string_reference() -> Result<()> {
@@ -953,7 +952,6 @@ mod tests {
953952
}
954953

955954
#[test]
956-
#[parallel]
957955
fn match_explanation_for_starts_with_ignores_trailing_lines_in_actual_string() -> Result<()> {
958956
let result = verify_that!(
959957
indoc!(

0 commit comments

Comments
 (0)