Skip to content

Commit

Permalink
chore: add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bconn98 committed Mar 3, 2024
1 parent 049e2cc commit 8bbfba2
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
31 changes: 31 additions & 0 deletions examples/color_control.rs
Original file line number Diff line number Diff line change
@@ -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
);
}
13 changes: 13 additions & 0 deletions test_cfgs/malformed_appender.yml
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions test_cfgs/test.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
39 changes: 39 additions & 0 deletions test_cfgs/test.toml
Original file line number Diff line number Diff line change
@@ -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" ]
36 changes: 36 additions & 0 deletions test_cfgs/test.yml
Original file line number Diff line number Diff line change
@@ -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
32 changes: 30 additions & 2 deletions tests/color_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -14,11 +14,39 @@ 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 {
execute_test(key, "1");
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());
}
23 changes: 23 additions & 0 deletions tests/init_cfg_with_error_handler.rs
Original file line number Diff line number Diff line change
@@ -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());
}
12 changes: 12 additions & 0 deletions tests/init_json_config.rs
Original file line number Diff line number Diff line change
@@ -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());
}
14 changes: 14 additions & 0 deletions tests/init_malformed_appenders.rs
Original file line number Diff line number Diff line change
@@ -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::<log4rs::config::RawConfig>(&config_str);

assert!(cfg.is_ok());
let cfg = cfg.unwrap();

let res = log4rs::config::create_raw_config(cfg);
assert!(res.is_err());
}
12 changes: 12 additions & 0 deletions tests/init_toml_config.rs
Original file line number Diff line number Diff line change
@@ -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());
}
12 changes: 12 additions & 0 deletions tests/init_yaml_config.rs
Original file line number Diff line number Diff line change
@@ -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());
}

0 comments on commit 8bbfba2

Please sign in to comment.