From 8bbfba298648ff6de863ddcc1f1547f1ef3d6a13 Mon Sep 17 00:00:00 2001 From: bconn98 Date: Sat, 2 Mar 2024 21:24:55 -0500 Subject: [PATCH] chore: add integration tests --- Cargo.toml | 4 +++ examples/color_control.rs | 31 +++++++++++++++++ test_cfgs/malformed_appender.yml | 13 +++++++ test_cfgs/test.json | 51 ++++++++++++++++++++++++++++ test_cfgs/test.toml | 39 +++++++++++++++++++++ test_cfgs/test.yml | 36 ++++++++++++++++++++ tests/color_control.rs | 32 +++++++++++++++-- tests/init_cfg_with_error_handler.rs | 23 +++++++++++++ tests/init_json_config.rs | 12 +++++++ tests/init_malformed_appenders.rs | 14 ++++++++ tests/init_toml_config.rs | 12 +++++++ tests/init_yaml_config.rs | 12 +++++++ 12 files changed, 277 insertions(+), 2 deletions(-) create mode 100644 examples/color_control.rs create mode 100755 test_cfgs/malformed_appender.yml create mode 100644 test_cfgs/test.json create mode 100644 test_cfgs/test.toml create mode 100644 test_cfgs/test.yml create mode 100755 tests/init_cfg_with_error_handler.rs create mode 100755 tests/init_json_config.rs create mode 100755 tests/init_malformed_appenders.rs create mode 100755 tests/init_toml_config.rs create mode 100755 tests/init_yaml_config.rs diff --git a/Cargo.toml b/Cargo.toml index 8f7134ca..41d27cf9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -108,3 +108,7 @@ required-features = ["file_appender", "rolling_file_appender", "size_trigger"] [[example]] name = "multi_logger_config" required-features = ["yaml_format", "config_parsing"] + +[[example]] +name = "color_control" +required-features = ["yaml_format", "config_parsing"] diff --git a/examples/color_control.rs b/examples/color_control.rs new file mode 100644 index 00000000..54a17510 --- /dev/null +++ b/examples/color_control.rs @@ -0,0 +1,31 @@ +use log::{error, info}; +use log4rs; +use serde_yaml; +use std::env; + +fn main() { + let config_str = include_str!("sample_config.yml"); + let config = serde_yaml::from_str(config_str).unwrap(); + log4rs::init_raw_config(config).unwrap(); + + let no_color = match env::var("NO_COLOR") { + Ok(no_color) => no_color, + Err(_) => "0".to_string(), + }; + let clicolor_force = match env::var("CLICOLOR_FORCE") { + Ok(clicolor_force) => clicolor_force, + Err(_) => "0".to_string(), + }; + let cli_color = match env::var("CLICOLOR") { + Ok(cli_color) => cli_color, + Err(_) => "0".to_string(), + }; + info!( + "NO_COLOR: {}, CLICOLOR_FORCE: {}, CLICOLOR: {}", + no_color, clicolor_force, cli_color + ); + error!( + "NO_COLOR: {}, CLICOLOR_FORCE: {}, CLICOLOR: {}", + no_color, clicolor_force, cli_color + ); +} diff --git a/test_cfgs/malformed_appender.yml b/test_cfgs/malformed_appender.yml new file mode 100755 index 00000000..f9db7d57 --- /dev/null +++ b/test_cfgs/malformed_appender.yml @@ -0,0 +1,13 @@ +refresh_rate: 5 seconds + +appenders: + file: + kind: file + pah: "log/file.log" + encoder: + pattern: "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}" + +root: + level: info + appenders: + - file diff --git a/test_cfgs/test.json b/test_cfgs/test.json new file mode 100644 index 00000000..b93f08c9 --- /dev/null +++ b/test_cfgs/test.json @@ -0,0 +1,51 @@ +{ + "refresh_rate": "5 seconds", + "appenders": { + "stdout": { + "kind": "console", + "encoder": { + "pattern": "{d(%+)(utc)} [{f}:{L}] {h({l})} {M}:{m}{n}" + }, + "filters": [ + { + "kind": "threshold", + "level": "info" + } + ] + }, + "file": { + "kind": "file", + "path": "log/file.log", + "encoder": { + "pattern": "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}" + } + }, + "rollingfile": { + "kind": "rolling_file", + "path": "log/rolling_file.log", + "encoder": { + "pattern": "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}" + }, + "policy": { + "trigger": { + "kind": "time", + "interval": "1 minute" + }, + "roller": { + "kind": "fixed_window", + "pattern": "log/old-rolling_file-{}.log", + "base": 0, + "count": 2 + } + } + } + }, + "root": { + "level": "info", + "appenders": [ + "stdout", + "file", + "rollingfile" + ] + } +} diff --git a/test_cfgs/test.toml b/test_cfgs/test.toml new file mode 100644 index 00000000..dc1f10da --- /dev/null +++ b/test_cfgs/test.toml @@ -0,0 +1,39 @@ +refresh_rate = "5 seconds" + +[appenders.stdout] +kind = "console" + + [appenders.stdout.encoder] + pattern = "{d(%+)(utc)} [{f}:{L}] {h({l})} {M}:{m}{n}" + + [[appenders.stdout.filters]] + kind = "threshold" + level = "info" + +[appenders.file] +kind = "file" +path = "log/file.log" + + [appenders.file.encoder] + pattern = "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}" + +[appenders.rollingfile] +kind = "rolling_file" +path = "log/rolling_file.log" + + [appenders.rollingfile.encoder] + pattern = "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}" + +[appenders.rollingfile.policy.trigger] +kind = "time" +interval = "1 minute" + +[appenders.rollingfile.policy.roller] +kind = "fixed_window" +pattern = "log/old-rolling_file-{}.log" +base = 0 +count = 2 + +[root] +level = "info" +appenders = [ "stdout", "file", "rollingfile" ] diff --git a/test_cfgs/test.yml b/test_cfgs/test.yml new file mode 100644 index 00000000..47d18ccd --- /dev/null +++ b/test_cfgs/test.yml @@ -0,0 +1,36 @@ +refresh_rate: 5 seconds + +appenders: + stdout: + kind: console + encoder: + pattern: "{d(%+)(utc)} [{f}:{L}] {h({l})} {M}:{m}{n}" + filters: + - kind: threshold + level: info + file: + kind: file + path: "log/file.log" + encoder: + pattern: "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}" + rollingfile: + kind: rolling_file + path: "log/rolling_file.log" + encoder: + pattern: "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}" + policy: + trigger: + kind: time + interval: 1 minute + roller: + kind: fixed_window + pattern: "log/old-rolling_file-{}.log" + base: 0 + count: 2 + +root: + level: info + appenders: + - stdout + - file + - rollingfile diff --git a/tests/color_control.rs b/tests/color_control.rs index 344f032f..ab785e6b 100644 --- a/tests/color_control.rs +++ b/tests/color_control.rs @@ -2,7 +2,7 @@ use std::process::Command; fn execute_test(env_key: &str, env_val: &str) { let mut child_proc = Command::new("cargo") - .args(&["run", "--example", "compile_time_config"]) + .args(&["run", "--example", "color_control"]) .env(env_key, env_val) .spawn() .expect("Cargo command failed to start"); @@ -14,7 +14,7 @@ fn execute_test(env_key: &str, env_val: &str) { // Maintaining as a single test to avoid blocking calls to the package cache #[test] -fn test_no_color() { +fn test_single_var() { let keys = vec!["NO_COLOR", "CLICOLOR_FORCE", "CLICOLOR"]; for key in keys { @@ -22,3 +22,31 @@ fn test_no_color() { execute_test(key, "0"); } } + +#[test] +fn test_no_color_vs_force() { + let mut child_proc = Command::new("cargo") + .args(&["run", "--example", "color_control"]) + .env("NO_COLOR", "1") + .env("CLICOLOR_FORCE", "1") + .spawn() + .expect("Cargo command failed to start"); + + let ecode = child_proc.wait().expect("failed to wait on child"); + + assert!(ecode.success()); +} + +#[test] +fn test_no_color_vs_regular() { + let mut child_proc = Command::new("cargo") + .args(&["run", "--example", "color_control"]) + .env("NO_COLOR", "1") + .env("CLICOLOR", "1") + .spawn() + .expect("Cargo command failed to start"); + + let ecode = child_proc.wait().expect("failed to wait on child"); + + assert!(ecode.success()); +} diff --git a/tests/init_cfg_with_error_handler.rs b/tests/init_cfg_with_error_handler.rs new file mode 100755 index 00000000..3c231adb --- /dev/null +++ b/tests/init_cfg_with_error_handler.rs @@ -0,0 +1,23 @@ +#[test] +#[cfg(all(feature = "config_parsing", feature = "yaml_format"))] +fn test_cfg_with_error_handler() { + use std::{ + io::{self, Write}, + path::Path, + }; + + let cfg = log4rs::config::load_config_file( + Path::new("./test_cfgs/test.yml"), + log4rs::config::Deserializers::default(), + ); + assert!(cfg.is_ok()); + let cfg = cfg.unwrap(); + + let res = log4rs::config::init_config_with_err_handler( + cfg, + Box::new(|e: &anyhow::Error| { + let _ = writeln!(io::stderr(), "log4rs: {}", e); + }), + ); + assert!(res.is_ok()); +} diff --git a/tests/init_json_config.rs b/tests/init_json_config.rs new file mode 100755 index 00000000..7e6287c5 --- /dev/null +++ b/tests/init_json_config.rs @@ -0,0 +1,12 @@ +#[test] +#[cfg(all(feature = "config_parsing", feature = "json_format"))] +fn test_init_json_cfg() { + use log4rs; + use std::path::Path; + + assert!(log4rs::init_file( + Path::new("./test_cfgs/test.json"), + log4rs::config::Deserializers::default() + ) + .is_ok()); +} diff --git a/tests/init_malformed_appenders.rs b/tests/init_malformed_appenders.rs new file mode 100755 index 00000000..c6b9e36d --- /dev/null +++ b/tests/init_malformed_appenders.rs @@ -0,0 +1,14 @@ +#[test] +#[cfg(all(feature = "config_parsing", feature = "yaml_format"))] +fn test_malformed_appenders() { + use std::fs; + + let config_str = fs::read_to_string("test_cfgs/malformed_appender.yml").unwrap(); + let cfg = ::serde_yaml::from_str::(&config_str); + + assert!(cfg.is_ok()); + let cfg = cfg.unwrap(); + + let res = log4rs::config::create_raw_config(cfg); + assert!(res.is_err()); +} diff --git a/tests/init_toml_config.rs b/tests/init_toml_config.rs new file mode 100755 index 00000000..f5b576ca --- /dev/null +++ b/tests/init_toml_config.rs @@ -0,0 +1,12 @@ +#[test] +#[cfg(all(feature = "config_parsing", feature = "toml_format"))] +fn test_init_toml_cfg() { + use log4rs; + use std::path::Path; + + assert!(log4rs::init_file( + Path::new("./test_cfgs/test.toml"), + log4rs::config::Deserializers::default() + ) + .is_ok()); +} diff --git a/tests/init_yaml_config.rs b/tests/init_yaml_config.rs new file mode 100755 index 00000000..b2a5f4da --- /dev/null +++ b/tests/init_yaml_config.rs @@ -0,0 +1,12 @@ +#[test] +#[cfg(all(feature = "config_parsing", feature = "yaml_format"))] +fn test_init_yaml_cfg() { + use log4rs; + use std::path::Path; + + assert!(log4rs::init_file( + Path::new("./test_cfgs/test.yml"), + log4rs::config::Deserializers::default() + ) + .is_ok()); +}