generated from PlexSheep/baserepo
logger rust test success
This commit is contained in:
parent
88de9b0fe0
commit
7da4e18456
|
@ -27,4 +27,4 @@ env_logger = "0.10.0"
|
||||||
gag = "1.0.0"
|
gag = "1.0.0"
|
||||||
log = { version = "0.4.19", features = ["max_level_trace", "release_max_level_trace"] }
|
log = { version = "0.4.19", features = ["max_level_trace", "release_max_level_trace"] }
|
||||||
pyo3 = "0.18.1"
|
pyo3 = "0.18.1"
|
||||||
testing_logger = "0.1.1"
|
regex = "1.9.1"
|
||||||
|
|
|
@ -15,10 +15,14 @@
|
||||||
//// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
|
//// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
|
||||||
use std::{
|
use std::{
|
||||||
fmt,
|
fmt,
|
||||||
|
io::Write,
|
||||||
sync::atomic::{AtomicBool, Ordering},
|
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 log::{debug, error, info, trace, warn, Level};
|
||||||
|
|
||||||
use pyo3::prelude::*;
|
use pyo3::prelude::*;
|
||||||
|
@ -76,14 +80,22 @@ impl Logger {
|
||||||
/// ## initializes the logger to log to a target
|
/// ## initializes the logger to log to a target
|
||||||
///
|
///
|
||||||
/// Will enable the logger to be used.
|
/// 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
|
// only init if no init has been performed yet
|
||||||
if INITIALIZED.load(Ordering::Relaxed) {
|
if INITIALIZED.load(Ordering::Relaxed) {
|
||||||
warn!("trying to reinitialize the logger, ignoring");
|
warn!("trying to reinitialize the logger, ignoring");
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
let env = Env::default().filter_or(LOGGER_ENV_KEY, DEFAULT_LOG_LEVEL.to_string());
|
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);
|
INITIALIZED.store(true, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,8 @@
|
||||||
///
|
///
|
||||||
/// Source: [users.rust-lang.org](https://users.rust-lang.org/t/how-to-test-functions-that-use-
|
/// Source: [users.rust-lang.org](https://users.rust-lang.org/t/how-to-test-functions-that-use-
|
||||||
/// println/67188/5)
|
/// println/67188/5)
|
||||||
macro_rules! assert_stdout_eq {
|
macro_rules! get_stdout_for {
|
||||||
($test:expr, $expected:literal) => {{
|
($test:expr) => {{
|
||||||
use gag::BufferRedirect;
|
use gag::BufferRedirect;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ macro_rules! assert_stdout_eq {
|
||||||
buf.read_to_string(&mut output).unwrap();
|
buf.read_to_string(&mut output).unwrap();
|
||||||
drop(buf);
|
drop(buf);
|
||||||
|
|
||||||
assert_eq!(&output, $expected);
|
output
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,9 +28,12 @@ macro_rules! assert_stdout_eq {
|
||||||
mod test_logger_struct {
|
mod test_logger_struct {
|
||||||
use libpt::logger::*;
|
use libpt::logger::*;
|
||||||
|
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
fn setup() {
|
fn setup() {
|
||||||
// we don't want to log messages during our tests!
|
// 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
|
/// ## Tests for basic logging
|
||||||
|
@ -43,27 +46,29 @@ mod test_logger_struct {
|
||||||
/// - [`Logger::warn`]
|
/// - [`Logger::warn`]
|
||||||
/// - [`Logger::error`]
|
/// - [`Logger::error`]
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_log_basic() {
|
fn test_log_basic() {
|
||||||
std::env::set_var(LOGGER_ENV_KEY, "Trace");
|
std::env::set_var(LOGGER_ENV_KEY, "Trace");
|
||||||
setup();
|
setup();
|
||||||
let l = Logger::new();
|
let l = Logger::new();
|
||||||
l.error("HELP");
|
let trace_out = get_stdout_for!(l.trace("MSG"));
|
||||||
assert_stdout_eq!(l.trace("hello world"), "\u{1b}[0m\u{1b}[38;5;8m[\u{1b}[0m2023-07-07T1\
|
let debug_out = get_stdout_for!(l.debug("MSG"));
|
||||||
8:59:03Z \u{1b}[0m\u{1b}[36mTRACE\u{1b}[0m libpt::logger\u{1b}[0m\u{1b}[38;5;8m]\u{1b}[\
|
let info_out = get_stdout_for!(l.info("MSG"));
|
||||||
0m hello world\n");
|
let warn_out = get_stdout_for!(l.warn("MSG"));
|
||||||
assert_stdout_eq!(l.debug("hello world"), "\u{1b}[0m\u{1b}[38;5;8m[\u{1b} [0m2023-07-07T1\
|
let error_out = get_stdout_for!(l.error("MSG"));
|
||||||
8:59:03Z \u{1b}[0m\u{1b}[34mDEBUG\u{1b}[0m libpt::logger\u{1b}[0m\u{1b}[38;5;8m]\u{1b}[\
|
let combined = format!(
|
||||||
0m hello world\n");
|
"{}{}{}{}{}",
|
||||||
assert_stdout_eq!(l.info("hello world"), "\u{1b}[0m\u{1b}[38;5;8m[\u{1b} [0m2023-07-07T1\
|
trace_out, debug_out, info_out, warn_out, error_out
|
||||||
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");
|
print!("{}", combined);
|
||||||
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}[\
|
// too long, so i split into two lines.
|
||||||
0m hello world\n");
|
let regex = Regex::new(concat!(
|
||||||
assert_stdout_eq!(l.error("hello world"), "\u{1b}[0m\u{1b}[38;5;8m[\u{1b} [0m2023-07-07T1\
|
r"(?m)\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z ",
|
||||||
8:59:03Z \u{1b}[0m\u{1b}[34mERROR\u{1b}[0m libpt::logger\u{1b}[0m\u{1b}[38;5;8m]\u{1b}[\
|
r"(TRACE|DEBUG|INFO|WARN|ERROR) +libpt::logger\] MSG"
|
||||||
0m hello world\n");
|
))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(regex.captures_iter(&combined).count(), 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Reference in New Issue