From 7da4e184563b92ebb432924bb6bfc0c36fade726 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Fri, 7 Jul 2023 23:36:55 +0200 Subject: [PATCH] logger rust test success --- Cargo.toml | 2 +- src/logger/mod.rs | 18 +++++++++++--- tests/test_logger_struct.rs | 47 ++++++++++++++++++++----------------- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 70bfb17..5a02e8a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,4 +27,4 @@ env_logger = "0.10.0" gag = "1.0.0" log = { version = "0.4.19", features = ["max_level_trace", "release_max_level_trace"] } pyo3 = "0.18.1" -testing_logger = "0.1.1" +regex = "1.9.1" diff --git a/src/logger/mod.rs b/src/logger/mod.rs index 3889f4f..4c4123d 100644 --- a/src/logger/mod.rs +++ b/src/logger/mod.rs @@ -15,10 +15,14 @@ //// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// use std::{ fmt, + io::Write, sync::atomic::{AtomicBool, Ordering}, }; -use env_logger::{Env, Target}; +use env_logger::{ + fmt::{Formatter, Style}, + Env, Target, WriteStyle, +}; use log::{debug, error, info, trace, warn, Level}; use pyo3::prelude::*; @@ -76,14 +80,22 @@ impl Logger { /// ## initializes the logger to log to a target /// /// Will enable the logger to be used. - pub fn init_target(test: bool, target: Target) { + pub fn init_specialized(test: bool, color: bool, target: Target) { // only init if no init has been performed yet if INITIALIZED.load(Ordering::Relaxed) { warn!("trying to reinitialize the logger, ignoring"); return; } else { let env = Env::default().filter_or(LOGGER_ENV_KEY, DEFAULT_LOG_LEVEL.to_string()); - env_logger::Builder::from_env(env).is_test(test).target(target).init(); + env_logger::Builder::from_env(env) + .is_test(test) + .target(target) + .write_style(if color { + WriteStyle::Auto + } else { + WriteStyle::Never + }) + .init(); INITIALIZED.store(true, Ordering::Relaxed); } } diff --git a/tests/test_logger_struct.rs b/tests/test_logger_struct.rs index 2e81598..a63b500 100644 --- a/tests/test_logger_struct.rs +++ b/tests/test_logger_struct.rs @@ -5,8 +5,8 @@ /// /// Source: [users.rust-lang.org](https://users.rust-lang.org/t/how-to-test-functions-that-use- /// println/67188/5) -macro_rules! assert_stdout_eq { - ($test:expr, $expected:literal) => {{ +macro_rules! get_stdout_for { + ($test:expr) => {{ use gag::BufferRedirect; use std::io::Read; @@ -18,7 +18,7 @@ macro_rules! assert_stdout_eq { buf.read_to_string(&mut output).unwrap(); drop(buf); - assert_eq!(&output, $expected); + output }}; } @@ -28,9 +28,12 @@ macro_rules! assert_stdout_eq { mod test_logger_struct { use libpt::logger::*; + use regex::Regex; + fn setup() { // we don't want to log messages during our tests! - Logger::init_target(false, env_logger::Target::Stdout) + Logger::init_specialized(false, false, env_logger::Target::Stdout); + println!() } /// ## Tests for basic logging @@ -43,27 +46,29 @@ mod test_logger_struct { /// - [`Logger::warn`] /// - [`Logger::error`] #[test] - #[ignore] fn test_log_basic() { std::env::set_var(LOGGER_ENV_KEY, "Trace"); setup(); let l = Logger::new(); - l.error("HELP"); - assert_stdout_eq!(l.trace("hello world"), "\u{1b}[0m\u{1b}[38;5;8m[\u{1b}[0m2023-07-07T1\ - 8:59:03Z \u{1b}[0m\u{1b}[36mTRACE\u{1b}[0m libpt::logger\u{1b}[0m\u{1b}[38;5;8m]\u{1b}[\ - 0m hello world\n"); - assert_stdout_eq!(l.debug("hello world"), "\u{1b}[0m\u{1b}[38;5;8m[\u{1b} [0m2023-07-07T1\ - 8:59:03Z \u{1b}[0m\u{1b}[34mDEBUG\u{1b}[0m libpt::logger\u{1b}[0m\u{1b}[38;5;8m]\u{1b}[\ - 0m hello world\n"); - assert_stdout_eq!(l.info("hello world"), "\u{1b}[0m\u{1b}[38;5;8m[\u{1b} [0m2023-07-07T1\ - 8:59:03Z \u{1b}[0m\u{1b}[34mINFO\u{1b}[0m libpt::logger\u{1b}[0m\u{1b}[38;5;8m]\u{1b}[\ - 0m hello world\n"); - assert_stdout_eq!(l.warn("hello world"), "\u{1b}[0m\u{1b}[38;5;8m[\u{1b} [0m2023-07-07T1\ - 8:59:03Z \u{1b}[0m\u{1b}[34mWARN\u{1b}[0m libpt::logger\u{1b}[0m\u{1b}[38;5;8m]\u{1b}[\ - 0m hello world\n"); - assert_stdout_eq!(l.error("hello world"), "\u{1b}[0m\u{1b}[38;5;8m[\u{1b} [0m2023-07-07T1\ - 8:59:03Z \u{1b}[0m\u{1b}[34mERROR\u{1b}[0m libpt::logger\u{1b}[0m\u{1b}[38;5;8m]\u{1b}[\ - 0m hello world\n"); + let trace_out = get_stdout_for!(l.trace("MSG")); + let debug_out = get_stdout_for!(l.debug("MSG")); + let info_out = get_stdout_for!(l.info("MSG")); + let warn_out = get_stdout_for!(l.warn("MSG")); + let error_out = get_stdout_for!(l.error("MSG")); + let combined = format!( + "{}{}{}{}{}", + trace_out, debug_out, info_out, warn_out, error_out + ); + print!("{}", combined); + + // too long, so i split into two lines. + let regex = Regex::new(concat!( + r"(?m)\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z ", + r"(TRACE|DEBUG|INFO|WARN|ERROR) +libpt::logger\] MSG" + )) + .unwrap(); + + assert_eq!(regex.captures_iter(&combined).count(), 5); } #[test]