From e6f01275cf8ed34971585556a6db8fa56ad14f98 Mon Sep 17 00:00:00 2001 From: bconn98 Date: Mon, 1 Apr 2024 21:36:38 -0400 Subject: [PATCH 1/5] refactor: COLOR_MODE to OnceCell --- src/encode/writer/console.rs | 208 +++++++++++++++++++++++++++++------ 1 file changed, 173 insertions(+), 35 deletions(-) diff --git a/src/encode/writer/console.rs b/src/encode/writer/console.rs index 6209fb97..08f6e63f 100644 --- a/src/encode/writer/console.rs +++ b/src/encode/writer/console.rs @@ -2,36 +2,37 @@ //! //! Requires the `console_writer` feature. -use std::{fmt, io}; +use std::{env, fmt, io}; use crate::encode::{self, Style}; -use once_cell::sync::Lazy; - -static COLOR_MODE: Lazy = Lazy::new(|| { - let no_color = std::env::var("NO_COLOR") - .map(|var| var != "0") - .unwrap_or(false); - let clicolor_force = std::env::var("CLICOLOR_FORCE") - .map(|var| var != "0") - .unwrap_or(false); +use once_cell::sync::OnceCell; + +static COLOR_MODE: OnceCell = OnceCell::new(); + +fn set_color_mode( + no_color: Result, + clicolor_force: Result, + clicolor: Result, +) -> ColorMode { + let no_color = no_color.map(|var| var != "0").unwrap_or(false); + let clicolor_force = clicolor_force.map(|var| var != "0").unwrap_or(false); + if no_color { ColorMode::Never } else if clicolor_force { ColorMode::Always } else { - let clicolor = std::env::var("CLICOLOR") - .map(|var| var != "0") - .unwrap_or(true); + let clicolor = clicolor.map(|var| var != "0").unwrap_or(true); if clicolor { ColorMode::Auto } else { ColorMode::Never } } -}); +} /// The color output mode for a `ConsoleAppender` -#[derive(Clone, Copy, Default)] +#[derive(Clone, Copy, Default, Debug, PartialEq)] pub enum ColorMode { /// Print color only if the output is recognized as a console #[default] @@ -121,14 +122,14 @@ impl<'a> encode::Write for ConsoleWriterLock<'a> { #[cfg(unix)] mod imp { - use std::{fmt, io}; + use std::{env, fmt, io}; use crate::{ encode::{ self, writer::{ ansi::AnsiWriter, - console::{ColorMode, COLOR_MODE}, + console::{set_color_mode, ColorMode, COLOR_MODE}, }, Style, }, @@ -140,7 +141,13 @@ mod imp { impl Writer { pub fn stdout() -> Option { let writer = || Writer(AnsiWriter(StdWriter::stdout())); - match *COLOR_MODE { + let color_mode_init = { + let no_color = env::var("NO_COLOR"); + let clicolor_force = env::var("CLICOLOR_FORCE"); + let clicolor = env::var("CLICOLOR"); + set_color_mode(no_color, clicolor_force, clicolor) + }; + match COLOR_MODE.get_or_init(|| color_mode_init) { ColorMode::Auto => { if unsafe { libc::isatty(libc::STDOUT_FILENO) } != 1 { None @@ -155,7 +162,13 @@ mod imp { pub fn stderr() -> Option { let writer = || Writer(AnsiWriter(StdWriter::stderr())); - match *COLOR_MODE { + let color_mode_init = { + let no_color = env::var("NO_COLOR"); + let clicolor_force = env::var("CLICOLOR_FORCE"); + let clicolor = env::var("CLICOLOR"); + set_color_mode(no_color, clicolor_force, clicolor) + }; + match COLOR_MODE.get_or_init(|| color_mode_init) { ColorMode::Auto => { if unsafe { libc::isatty(libc::STDERR_FILENO) } != 1 { None @@ -227,7 +240,7 @@ mod imp { #[cfg(windows)] mod imp { use std::{ - fmt, + env, fmt, io::{self, Write}, mem, }; @@ -239,7 +252,7 @@ mod imp { use crate::{ encode::{ self, - writer::console::{ColorMode, COLOR_MODE}, + writer::console::{set_color_mode, ColorMode, COLOR_MODE}, Color, Style, }, priv_io::{StdWriter, StdWriterLock}, @@ -335,7 +348,13 @@ mod imp { inner: StdWriter::stdout(), }; - match *COLOR_MODE { + let color_mode_init = { + let no_color = env::var("NO_COLOR"); + let clicolor_force = env::var("CLICOLOR_FORCE"); + let clicolor = env::var("CLICOLOR"); + set_color_mode(no_color, clicolor_force, clicolor) + }; + match COLOR_MODE.get_or_init(|| color_mode_init) { ColorMode::Auto | ColorMode::Always => Some(writer), ColorMode::Never => None, } @@ -362,7 +381,13 @@ mod imp { inner: StdWriter::stdout(), }; - match *COLOR_MODE { + let color_mode_init = { + let no_color = env::var("NO_COLOR"); + let clicolor_force = env::var("CLICOLOR_FORCE"); + let clicolor = env::var("CLICOLOR"); + set_color_mode(no_color, clicolor_force, clicolor) + }; + match COLOR_MODE.get_or_init(|| color_mode_init) { ColorMode::Auto | ColorMode::Always => Some(writer), ColorMode::Never => None, } @@ -435,32 +460,145 @@ mod imp { #[cfg(test)] mod test { - use std::io::Write; - use super::*; use crate::encode::{Color, Style, Write as EncodeWrite}; + use std::{env::VarError, io::Write}; + // Unable to test the non locked Console as by definition, the unlocked + // console results in race conditions. Codecov tooling does not seem to + // see this test as coverage of the ConsoleWritterLock or WriterLock + // class, however, it should completely cover either. #[test] - fn basic() { + fn test_stdout_console_writer_lock() { let w = match ConsoleWriter::stdout() { Some(w) => w, None => return, }; let mut w = w.lock(); - w.write_all(b"normal ").unwrap(); + w.write(b"normal ").unwrap(); w.set_style( - Style::new() - .text(Color::Red) - .background(Color::Blue) - .intense(true), - ) - .unwrap(); + Style::new() + .text(Color::Red) + .background(Color::Blue) + .intense(true), + ) + .unwrap(); w.write_all(b"styled").unwrap(); - w.set_style(Style::new().text(Color::Green)).unwrap(); + w.set_style(&Style::new().text(Color::Green).intense(false)) + .unwrap(); w.write_all(b" styled2").unwrap(); w.set_style(&Style::new()).unwrap(); - w.write_all(b" normal\n").unwrap(); + w.write_fmt(format_args!(" {} \n", "normal")).unwrap(); w.flush().unwrap(); } + + #[test] + fn test_color_mode_default() { + let no_color = Err(VarError::NotPresent); + let clicolor_force = Err(VarError::NotPresent); + let clicolor = Err(VarError::NotPresent); + + let color_mode: OnceCell = OnceCell::new(); + assert_eq!( + color_mode.get_or_init(|| set_color_mode(no_color, clicolor_force, clicolor)), + &ColorMode::Auto + ); + } + + // Note that NO_COLOR has priority over all other fields + #[test] + fn test_no_color() { + let no_color = Ok("1".to_owned()); + let clicolor_force = Err(VarError::NotPresent); + let clicolor = Err(VarError::NotPresent); + + let mut color_mode: OnceCell = OnceCell::new(); + assert_eq!( + color_mode.get_or_init(|| set_color_mode(no_color, clicolor_force, clicolor)), + &ColorMode::Never + ); + + let no_color = Ok("1".to_owned()); + let clicolor_force = Ok("1".to_owned()); + let clicolor = Ok("1".to_owned()); + + let _ = color_mode.take(); // Clear the owned value + assert_eq!( + color_mode.get_or_init(|| set_color_mode(no_color, clicolor_force, clicolor)), + &ColorMode::Never + ); + } + + #[test] + fn test_cli_force() { + // CLICOLOR_FORCE is the only set field + let no_color = Err(VarError::NotPresent); + let clicolor_force = Ok("1".to_owned()); + let clicolor = Err(VarError::NotPresent); + + let mut color_mode: OnceCell = OnceCell::new(); + assert_eq!( + color_mode.get_or_init(|| set_color_mode(no_color, clicolor_force, clicolor)), + &ColorMode::Always + ); + + // Although NO_COLOR has priority, when set to 0 next in line + // is CLICOLOR_FORCE which maintains precedence over clicolor + // regardless of how it's set. Attempt both settings below + let no_color = Ok("0".to_owned()); + let clicolor_force = Ok("1".to_owned()); + let clicolor = Ok("1".to_owned()); + + let _ = color_mode.take(); // Clear the owned value + assert_eq!( + color_mode.get_or_init(|| set_color_mode(no_color, clicolor_force, clicolor)), + &ColorMode::Always + ); + + let no_color = Ok("0".to_owned()); + let clicolor_force = Ok("1".to_owned()); + let clicolor = Ok("0".to_owned()); + + let _ = color_mode.take(); // Clear the owned value + assert_eq!( + color_mode.get_or_init(|| set_color_mode(no_color, clicolor_force, clicolor)), + &ColorMode::Always + ); + } + + #[test] + fn test_cli_on() { + // CLICOLOR is the only set field + let no_color = Err(VarError::NotPresent); + let clicolor_force = Err(VarError::NotPresent); + let clicolor = Ok("1".to_owned()); + + let mut color_mode: OnceCell = OnceCell::new(); + assert_eq!( + color_mode.get_or_init(|| set_color_mode(no_color, clicolor_force, clicolor)), + &ColorMode::Auto + ); + + let no_color = Err(VarError::NotPresent); + let clicolor_force = Err(VarError::NotPresent); + let clicolor = Ok("0".to_owned()); + + let _ = color_mode.take(); // Clear the owned value + assert_eq!( + color_mode.get_or_init(|| set_color_mode(no_color, clicolor_force, clicolor)), + &ColorMode::Never + ); + + // CLICOLOR_FORCE is disabled + let no_color = Err(VarError::NotPresent); + let clicolor_force = Ok("0".to_owned()); + let clicolor = Ok("1".to_owned()); + + let _ = color_mode.take(); // Clear the owned value + assert_eq!( + color_mode.get_or_init(|| set_color_mode(no_color, clicolor_force, clicolor)), + &ColorMode::Auto + ); + } } From cfe8960f3ca3462eebd0c3fcfa14626361a677f7 Mon Sep 17 00:00:00 2001 From: bconn98 Date: Tue, 9 Jul 2024 21:59:56 -0400 Subject: [PATCH 2/5] refactor: set_color_mode => get_color_mode --- src/encode/writer/console.rs | 44 ++++++++++++++++++------------------ src/lib.rs | 2 +- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/encode/writer/console.rs b/src/encode/writer/console.rs index 08f6e63f..f8ae9c0e 100644 --- a/src/encode/writer/console.rs +++ b/src/encode/writer/console.rs @@ -9,7 +9,7 @@ use once_cell::sync::OnceCell; static COLOR_MODE: OnceCell = OnceCell::new(); -fn set_color_mode( +fn get_color_mode( no_color: Result, clicolor_force: Result, clicolor: Result, @@ -129,7 +129,7 @@ mod imp { self, writer::{ ansi::AnsiWriter, - console::{set_color_mode, ColorMode, COLOR_MODE}, + console::{get_color_mode, ColorMode, COLOR_MODE}, }, Style, }, @@ -145,7 +145,7 @@ mod imp { let no_color = env::var("NO_COLOR"); let clicolor_force = env::var("CLICOLOR_FORCE"); let clicolor = env::var("CLICOLOR"); - set_color_mode(no_color, clicolor_force, clicolor) + get_color_mode(no_color, clicolor_force, clicolor) }; match COLOR_MODE.get_or_init(|| color_mode_init) { ColorMode::Auto => { @@ -166,7 +166,7 @@ mod imp { let no_color = env::var("NO_COLOR"); let clicolor_force = env::var("CLICOLOR_FORCE"); let clicolor = env::var("CLICOLOR"); - set_color_mode(no_color, clicolor_force, clicolor) + get_color_mode(no_color, clicolor_force, clicolor) }; match COLOR_MODE.get_or_init(|| color_mode_init) { ColorMode::Auto => { @@ -252,7 +252,7 @@ mod imp { use crate::{ encode::{ self, - writer::console::{set_color_mode, ColorMode, COLOR_MODE}, + writer::console::{get_color_mode, ColorMode, COLOR_MODE}, Color, Style, }, priv_io::{StdWriter, StdWriterLock}, @@ -352,7 +352,7 @@ mod imp { let no_color = env::var("NO_COLOR"); let clicolor_force = env::var("CLICOLOR_FORCE"); let clicolor = env::var("CLICOLOR"); - set_color_mode(no_color, clicolor_force, clicolor) + get_color_mode(no_color, clicolor_force, clicolor) }; match COLOR_MODE.get_or_init(|| color_mode_init) { ColorMode::Auto | ColorMode::Always => Some(writer), @@ -385,7 +385,7 @@ mod imp { let no_color = env::var("NO_COLOR"); let clicolor_force = env::var("CLICOLOR_FORCE"); let clicolor = env::var("CLICOLOR"); - set_color_mode(no_color, clicolor_force, clicolor) + get_color_mode(no_color, clicolor_force, clicolor) }; match COLOR_MODE.get_or_init(|| color_mode_init) { ColorMode::Auto | ColorMode::Always => Some(writer), @@ -478,12 +478,12 @@ mod test { w.write(b"normal ").unwrap(); w.set_style( - Style::new() - .text(Color::Red) - .background(Color::Blue) - .intense(true), - ) - .unwrap(); + Style::new() + .text(Color::Red) + .background(Color::Blue) + .intense(true), + ) + .unwrap(); w.write_all(b"styled").unwrap(); w.set_style(&Style::new().text(Color::Green).intense(false)) .unwrap(); @@ -501,7 +501,7 @@ mod test { let color_mode: OnceCell = OnceCell::new(); assert_eq!( - color_mode.get_or_init(|| set_color_mode(no_color, clicolor_force, clicolor)), + color_mode.get_or_init(|| get_color_mode(no_color, clicolor_force, clicolor)), &ColorMode::Auto ); } @@ -515,7 +515,7 @@ mod test { let mut color_mode: OnceCell = OnceCell::new(); assert_eq!( - color_mode.get_or_init(|| set_color_mode(no_color, clicolor_force, clicolor)), + color_mode.get_or_init(|| get_color_mode(no_color, clicolor_force, clicolor)), &ColorMode::Never ); @@ -525,7 +525,7 @@ mod test { let _ = color_mode.take(); // Clear the owned value assert_eq!( - color_mode.get_or_init(|| set_color_mode(no_color, clicolor_force, clicolor)), + color_mode.get_or_init(|| get_color_mode(no_color, clicolor_force, clicolor)), &ColorMode::Never ); } @@ -539,7 +539,7 @@ mod test { let mut color_mode: OnceCell = OnceCell::new(); assert_eq!( - color_mode.get_or_init(|| set_color_mode(no_color, clicolor_force, clicolor)), + color_mode.get_or_init(|| get_color_mode(no_color, clicolor_force, clicolor)), &ColorMode::Always ); @@ -552,7 +552,7 @@ mod test { let _ = color_mode.take(); // Clear the owned value assert_eq!( - color_mode.get_or_init(|| set_color_mode(no_color, clicolor_force, clicolor)), + color_mode.get_or_init(|| get_color_mode(no_color, clicolor_force, clicolor)), &ColorMode::Always ); @@ -562,7 +562,7 @@ mod test { let _ = color_mode.take(); // Clear the owned value assert_eq!( - color_mode.get_or_init(|| set_color_mode(no_color, clicolor_force, clicolor)), + color_mode.get_or_init(|| get_color_mode(no_color, clicolor_force, clicolor)), &ColorMode::Always ); } @@ -576,7 +576,7 @@ mod test { let mut color_mode: OnceCell = OnceCell::new(); assert_eq!( - color_mode.get_or_init(|| set_color_mode(no_color, clicolor_force, clicolor)), + color_mode.get_or_init(|| get_color_mode(no_color, clicolor_force, clicolor)), &ColorMode::Auto ); @@ -586,7 +586,7 @@ mod test { let _ = color_mode.take(); // Clear the owned value assert_eq!( - color_mode.get_or_init(|| set_color_mode(no_color, clicolor_force, clicolor)), + color_mode.get_or_init(|| get_color_mode(no_color, clicolor_force, clicolor)), &ColorMode::Never ); @@ -597,7 +597,7 @@ mod test { let _ = color_mode.take(); // Clear the owned value assert_eq!( - color_mode.get_or_init(|| set_color_mode(no_color, clicolor_force, clicolor)), + color_mode.get_or_init(|| get_color_mode(no_color, clicolor_force, clicolor)), &ColorMode::Auto ); } diff --git a/src/lib.rs b/src/lib.rs index a80f390b..0079b623 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -177,7 +177,7 @@ //! For more examples see the [examples](https://github.com/estk/log4rs/tree/main/examples). //! -#![allow(where_clauses_object_safety, clippy::manual_non_exhaustive)] +#![allow(clippy::manual_non_exhaustive)] #![warn(missing_docs)] use std::{ From 4a4e7cfe40778ff3d413c3218d9793df8be30790 Mon Sep 17 00:00:00 2001 From: bconn98 Date: Fri, 12 Jul 2024 11:37:47 -0400 Subject: [PATCH 3/5] refactor: change args of get_color_mode to be Option wrapped to support test driven dependency injection --- src/encode/writer/console.rs | 112 ++++++++++++++++------------------- 1 file changed, 51 insertions(+), 61 deletions(-) diff --git a/src/encode/writer/console.rs b/src/encode/writer/console.rs index f8ae9c0e..2cddb11f 100644 --- a/src/encode/writer/console.rs +++ b/src/encode/writer/console.rs @@ -9,20 +9,34 @@ use once_cell::sync::OnceCell; static COLOR_MODE: OnceCell = OnceCell::new(); +/// Determine if coloration of logs should be enabled or disabled at runtime. +/// Arguments are passed as Option to support dependency injection in +/// unit testing. fn get_color_mode( - no_color: Result, - clicolor_force: Result, - clicolor: Result, + no_color: Option>, + clicolor_force: Option>, + clicolor: Option>, ) -> ColorMode { - let no_color = no_color.map(|var| var != "0").unwrap_or(false); - let clicolor_force = clicolor_force.map(|var| var != "0").unwrap_or(false); + let no_color = match no_color { + Some(result) => result, + None => env::var("NO_COLOR"), + }.map(|var| var != "0").unwrap_or(false); + + let clicolor_force = match clicolor_force { + Some(result) => result, + None => env::var("CLICOLOR_FORCE"), + }.map(|var| var != "0").unwrap_or(false); if no_color { ColorMode::Never } else if clicolor_force { ColorMode::Always } else { - let clicolor = clicolor.map(|var| var != "0").unwrap_or(true); + let clicolor = match clicolor { + Some(result) => result, + None => env::var("CLICOLOR"), + }.map(|var| var != "0").unwrap_or(true); + if clicolor { ColorMode::Auto } else { @@ -141,13 +155,7 @@ mod imp { impl Writer { pub fn stdout() -> Option { let writer = || Writer(AnsiWriter(StdWriter::stdout())); - let color_mode_init = { - let no_color = env::var("NO_COLOR"); - let clicolor_force = env::var("CLICOLOR_FORCE"); - let clicolor = env::var("CLICOLOR"); - get_color_mode(no_color, clicolor_force, clicolor) - }; - match COLOR_MODE.get_or_init(|| color_mode_init) { + match COLOR_MODE.get_or_init(|| get_color_mode(None, None, None)) { ColorMode::Auto => { if unsafe { libc::isatty(libc::STDOUT_FILENO) } != 1 { None @@ -162,13 +170,7 @@ mod imp { pub fn stderr() -> Option { let writer = || Writer(AnsiWriter(StdWriter::stderr())); - let color_mode_init = { - let no_color = env::var("NO_COLOR"); - let clicolor_force = env::var("CLICOLOR_FORCE"); - let clicolor = env::var("CLICOLOR"); - get_color_mode(no_color, clicolor_force, clicolor) - }; - match COLOR_MODE.get_or_init(|| color_mode_init) { + match COLOR_MODE.get_or_init(|| get_color_mode(None, None, None)) { ColorMode::Auto => { if unsafe { libc::isatty(libc::STDERR_FILENO) } != 1 { None @@ -348,13 +350,7 @@ mod imp { inner: StdWriter::stdout(), }; - let color_mode_init = { - let no_color = env::var("NO_COLOR"); - let clicolor_force = env::var("CLICOLOR_FORCE"); - let clicolor = env::var("CLICOLOR"); - get_color_mode(no_color, clicolor_force, clicolor) - }; - match COLOR_MODE.get_or_init(|| color_mode_init) { + match COLOR_MODE.get_or_init(|| get_color_mode(None, None, None)) { ColorMode::Auto | ColorMode::Always => Some(writer), ColorMode::Never => None, } @@ -381,13 +377,7 @@ mod imp { inner: StdWriter::stdout(), }; - let color_mode_init = { - let no_color = env::var("NO_COLOR"); - let clicolor_force = env::var("CLICOLOR_FORCE"); - let clicolor = env::var("CLICOLOR"); - get_color_mode(no_color, clicolor_force, clicolor) - }; - match COLOR_MODE.get_or_init(|| color_mode_init) { + match COLOR_MODE.get_or_init(|| get_color_mode(None, None, None)) { ColorMode::Auto | ColorMode::Always => Some(writer), ColorMode::Never => None, } @@ -495,9 +485,9 @@ mod test { #[test] fn test_color_mode_default() { - let no_color = Err(VarError::NotPresent); - let clicolor_force = Err(VarError::NotPresent); - let clicolor = Err(VarError::NotPresent); + let no_color = Some(Err(VarError::NotPresent)); + let clicolor_force = Some(Err(VarError::NotPresent)); + let clicolor = Some(Err(VarError::NotPresent)); let color_mode: OnceCell = OnceCell::new(); assert_eq!( @@ -509,9 +499,9 @@ mod test { // Note that NO_COLOR has priority over all other fields #[test] fn test_no_color() { - let no_color = Ok("1".to_owned()); - let clicolor_force = Err(VarError::NotPresent); - let clicolor = Err(VarError::NotPresent); + let no_color = Some(Ok("1".to_owned())); + let clicolor_force = Some(Err(VarError::NotPresent)); + let clicolor = Some(Err(VarError::NotPresent)); let mut color_mode: OnceCell = OnceCell::new(); assert_eq!( @@ -519,9 +509,9 @@ mod test { &ColorMode::Never ); - let no_color = Ok("1".to_owned()); - let clicolor_force = Ok("1".to_owned()); - let clicolor = Ok("1".to_owned()); + let no_color = Some(Ok("1".to_owned())); + let clicolor_force = Some(Ok("1".to_owned())); + let clicolor = Some(Ok("1".to_owned())); let _ = color_mode.take(); // Clear the owned value assert_eq!( @@ -533,9 +523,9 @@ mod test { #[test] fn test_cli_force() { // CLICOLOR_FORCE is the only set field - let no_color = Err(VarError::NotPresent); - let clicolor_force = Ok("1".to_owned()); - let clicolor = Err(VarError::NotPresent); + let no_color = Some(Err(VarError::NotPresent)); + let clicolor_force = Some(Ok("1".to_owned())); + let clicolor = Some(Err(VarError::NotPresent)); let mut color_mode: OnceCell = OnceCell::new(); assert_eq!( @@ -546,9 +536,9 @@ mod test { // Although NO_COLOR has priority, when set to 0 next in line // is CLICOLOR_FORCE which maintains precedence over clicolor // regardless of how it's set. Attempt both settings below - let no_color = Ok("0".to_owned()); - let clicolor_force = Ok("1".to_owned()); - let clicolor = Ok("1".to_owned()); + let no_color = Some(Ok("0".to_owned())); + let clicolor_force = Some(Ok("1".to_owned())); + let clicolor = Some(Ok("1".to_owned())); let _ = color_mode.take(); // Clear the owned value assert_eq!( @@ -556,9 +546,9 @@ mod test { &ColorMode::Always ); - let no_color = Ok("0".to_owned()); - let clicolor_force = Ok("1".to_owned()); - let clicolor = Ok("0".to_owned()); + let no_color = Some(Ok("0".to_owned())); + let clicolor_force = Some(Ok("1".to_owned())); + let clicolor = Some(Ok("0".to_owned())); let _ = color_mode.take(); // Clear the owned value assert_eq!( @@ -570,9 +560,9 @@ mod test { #[test] fn test_cli_on() { // CLICOLOR is the only set field - let no_color = Err(VarError::NotPresent); - let clicolor_force = Err(VarError::NotPresent); - let clicolor = Ok("1".to_owned()); + let no_color = Some(Err(VarError::NotPresent)); + let clicolor_force = Some(Err(VarError::NotPresent)); + let clicolor = Some(Ok("1".to_owned())); let mut color_mode: OnceCell = OnceCell::new(); assert_eq!( @@ -580,9 +570,9 @@ mod test { &ColorMode::Auto ); - let no_color = Err(VarError::NotPresent); - let clicolor_force = Err(VarError::NotPresent); - let clicolor = Ok("0".to_owned()); + let no_color = Some(Err(VarError::NotPresent)); + let clicolor_force = Some(Err(VarError::NotPresent)); + let clicolor = Some(Ok("0".to_owned())); let _ = color_mode.take(); // Clear the owned value assert_eq!( @@ -591,9 +581,9 @@ mod test { ); // CLICOLOR_FORCE is disabled - let no_color = Err(VarError::NotPresent); - let clicolor_force = Ok("0".to_owned()); - let clicolor = Ok("1".to_owned()); + let no_color = Some(Err(VarError::NotPresent)); + let clicolor_force = Some(Ok("0".to_owned())); + let clicolor = Some(Ok("1".to_owned())); let _ = color_mode.take(); // Clear the owned value assert_eq!( From 0b7cf7c5b8caaac03da31530fd7dd1edcf76b572 Mon Sep 17 00:00:00 2001 From: bconn98 Date: Fri, 12 Jul 2024 11:40:49 -0400 Subject: [PATCH 4/5] chore: format --- src/encode/writer/console.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/encode/writer/console.rs b/src/encode/writer/console.rs index 2cddb11f..32f7eb8f 100644 --- a/src/encode/writer/console.rs +++ b/src/encode/writer/console.rs @@ -20,12 +20,16 @@ fn get_color_mode( let no_color = match no_color { Some(result) => result, None => env::var("NO_COLOR"), - }.map(|var| var != "0").unwrap_or(false); + } + .map(|var| var != "0") + .unwrap_or(false); let clicolor_force = match clicolor_force { Some(result) => result, None => env::var("CLICOLOR_FORCE"), - }.map(|var| var != "0").unwrap_or(false); + } + .map(|var| var != "0") + .unwrap_or(false); if no_color { ColorMode::Never @@ -35,7 +39,9 @@ fn get_color_mode( let clicolor = match clicolor { Some(result) => result, None => env::var("CLICOLOR"), - }.map(|var| var != "0").unwrap_or(true); + } + .map(|var| var != "0") + .unwrap_or(true); if clicolor { ColorMode::Auto From 863c33618899964edfecf4688551d0e0c50cbc4f Mon Sep 17 00:00:00 2001 From: bconn98 Date: Fri, 12 Jul 2024 11:43:50 -0400 Subject: [PATCH 5/5] chore: clippy --- src/encode/writer/console.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/encode/writer/console.rs b/src/encode/writer/console.rs index 32f7eb8f..7b864bd2 100644 --- a/src/encode/writer/console.rs +++ b/src/encode/writer/console.rs @@ -142,7 +142,7 @@ impl<'a> encode::Write for ConsoleWriterLock<'a> { #[cfg(unix)] mod imp { - use std::{env, fmt, io}; + use std::{fmt, io}; use crate::{ encode::{