Skip to content

Commit c713652

Browse files
committed
chore: add integration tests
1 parent 049e2cc commit c713652

12 files changed

+277
-2
lines changed

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,7 @@ required-features = ["file_appender", "rolling_file_appender", "size_trigger"]
108108
[[example]]
109109
name = "multi_logger_config"
110110
required-features = ["yaml_format", "config_parsing"]
111+
112+
[[example]]
113+
name = "color_control"
114+
required-features = ["yaml_format", "config_parsing"]

examples/color_control.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use log::{error, info};
2+
use log4rs;
3+
use serde_yaml;
4+
use std::env;
5+
6+
fn main() {
7+
let config_str = include_str!("sample_config.yml");
8+
let config = serde_yaml::from_str(config_str).unwrap();
9+
log4rs::init_raw_config(config).unwrap();
10+
11+
let no_color = match env::var("NO_COLOR") {
12+
Ok(no_color) => no_color,
13+
Err(_) => "0".to_string(),
14+
};
15+
let clicolor_force = match env::var("CLICOLOR_FORCE") {
16+
Ok(clicolor_force) => clicolor_force,
17+
Err(_) => "0".to_string(),
18+
};
19+
let cli_color = match env::var("CLICOLOR") {
20+
Ok(cli_color) => cli_color,
21+
Err(_) => "0".to_string(),
22+
};
23+
info!(
24+
"NO_COLOR: {}, CLICOLOR_FORCE: {}, CLICOLOR: {}",
25+
no_color, clicolor_force, cli_color
26+
);
27+
error!(
28+
"NO_COLOR: {}, CLICOLOR_FORCE: {}, CLICOLOR: {}",
29+
no_color, clicolor_force, cli_color
30+
);
31+
}

test_cfgs/malformed_appender.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
refresh_rate: 5 seconds
2+
3+
appenders:
4+
file:
5+
kind: file
6+
pah: "log/file.log"
7+
encoder:
8+
pattern: "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}"
9+
10+
root:
11+
level: info
12+
appenders:
13+
- file

test_cfgs/test.json

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"refresh_rate": "5 seconds",
3+
"appenders": {
4+
"stdout": {
5+
"kind": "console",
6+
"encoder": {
7+
"pattern": "{d(%+)(utc)} [{f}:{L}] {h({l})} {M}:{m}{n}"
8+
},
9+
"filters": [
10+
{
11+
"kind": "threshold",
12+
"level": "info"
13+
}
14+
]
15+
},
16+
"file": {
17+
"kind": "file",
18+
"path": "log/file.log",
19+
"encoder": {
20+
"pattern": "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}"
21+
}
22+
},
23+
"rollingfile": {
24+
"kind": "rolling_file",
25+
"path": "log/rolling_file.log",
26+
"encoder": {
27+
"pattern": "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}"
28+
},
29+
"policy": {
30+
"trigger": {
31+
"kind": "time",
32+
"interval": "1 minute"
33+
},
34+
"roller": {
35+
"kind": "fixed_window",
36+
"pattern": "log/old-rolling_file-{}.log",
37+
"base": 0,
38+
"count": 2
39+
}
40+
}
41+
}
42+
},
43+
"root": {
44+
"level": "info",
45+
"appenders": [
46+
"stdout",
47+
"file",
48+
"rollingfile"
49+
]
50+
}
51+
}

test_cfgs/test.toml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
refresh_rate = "5 seconds"
2+
3+
[appenders.stdout]
4+
kind = "console"
5+
6+
[appenders.stdout.encoder]
7+
pattern = "{d(%+)(utc)} [{f}:{L}] {h({l})} {M}:{m}{n}"
8+
9+
[[appenders.stdout.filters]]
10+
kind = "threshold"
11+
level = "info"
12+
13+
[appenders.file]
14+
kind = "file"
15+
path = "log/file.log"
16+
17+
[appenders.file.encoder]
18+
pattern = "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}"
19+
20+
[appenders.rollingfile]
21+
kind = "rolling_file"
22+
path = "log/rolling_file.log"
23+
24+
[appenders.rollingfile.encoder]
25+
pattern = "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}"
26+
27+
[appenders.rollingfile.policy.trigger]
28+
kind = "time"
29+
interval = "1 minute"
30+
31+
[appenders.rollingfile.policy.roller]
32+
kind = "fixed_window"
33+
pattern = "log/old-rolling_file-{}.log"
34+
base = 0
35+
count = 2
36+
37+
[root]
38+
level = "info"
39+
appenders = [ "stdout", "file", "rollingfile" ]

test_cfgs/test.yml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
refresh_rate: 5 seconds
2+
3+
appenders:
4+
stdout:
5+
kind: console
6+
encoder:
7+
pattern: "{d(%+)(utc)} [{f}:{L}] {h({l})} {M}:{m}{n}"
8+
filters:
9+
- kind: threshold
10+
level: info
11+
file:
12+
kind: file
13+
path: "log/file.log"
14+
encoder:
15+
pattern: "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}"
16+
rollingfile:
17+
kind: rolling_file
18+
path: "log/rolling_file.log"
19+
encoder:
20+
pattern: "[{d(%Y-%m-%dT%H:%M:%S%.6f)} {h({l}):<5.5} {M}] {m}{n}"
21+
policy:
22+
trigger:
23+
kind: time
24+
interval: 1 minute
25+
roller:
26+
kind: fixed_window
27+
pattern: "log/old-rolling_file-{}.log"
28+
base: 0
29+
count: 2
30+
31+
root:
32+
level: info
33+
appenders:
34+
- stdout
35+
- file
36+
- rollingfile

tests/color_control.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::process::Command;
22

33
fn execute_test(env_key: &str, env_val: &str) {
44
let mut child_proc = Command::new("cargo")
5-
.args(&["run", "--example", "compile_time_config"])
5+
.args(&["run", "--example", "color_control"])
66
.env(env_key, env_val)
77
.spawn()
88
.expect("Cargo command failed to start");
@@ -14,11 +14,39 @@ fn execute_test(env_key: &str, env_val: &str) {
1414

1515
// Maintaining as a single test to avoid blocking calls to the package cache
1616
#[test]
17-
fn test_no_color() {
17+
fn test_single_var() {
1818
let keys = vec!["NO_COLOR", "CLICOLOR_FORCE", "CLICOLOR"];
1919

2020
for key in keys {
2121
execute_test(key, "1");
2222
execute_test(key, "0");
2323
}
2424
}
25+
26+
#[test]
27+
fn test_no_color_vs_force() {
28+
let mut child_proc = Command::new("cargo")
29+
.args(&["run", "--example", "color_control"])
30+
.env("NO_COLOR", "1")
31+
.env("CLICOLOR_FORCE", "1")
32+
.spawn()
33+
.expect("Cargo command failed to start");
34+
35+
let ecode = child_proc.wait().expect("failed to wait on child");
36+
37+
assert!(ecode.success());
38+
}
39+
40+
#[test]
41+
fn test_no_color_vs_regular() {
42+
let mut child_proc = Command::new("cargo")
43+
.args(&["run", "--example", "color_control"])
44+
.env("NO_COLOR", "1")
45+
.env("CLICOLOR", "1")
46+
.spawn()
47+
.expect("Cargo command failed to start");
48+
49+
let ecode = child_proc.wait().expect("failed to wait on child");
50+
51+
assert!(ecode.success());
52+
}

tests/init_cfg_with_error_handler.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#[test]
2+
#[cfg(all(feature = "config_parsing", feature = "yaml_format"))]
3+
fn test_cfg_err_hdlr() {
4+
use std::{
5+
io::{self, Write},
6+
path::Path,
7+
};
8+
9+
let cfg = log4rs::config::load_config_file(
10+
Path::new("./test_cfgs/test.yml"),
11+
log4rs::config::Deserializers::default(),
12+
);
13+
assert!(cfg.is_ok());
14+
let cfg = cfg.unwrap();
15+
16+
let res = log4rs::config::init_config_with_err_handler(
17+
cfg,
18+
Box::new(|e: &anyhow::Error| {
19+
let _ = writeln!(io::stderr(), "log4rs: {}", e);
20+
}),
21+
);
22+
assert!(res.is_ok());
23+
}

tests/init_json_config.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#[test]
2+
#[cfg(all(feature = "config_parsing", feature = "json_format"))]
3+
fn test_init_json_cfg() {
4+
use log4rs;
5+
use std::path::Path;
6+
7+
assert!(log4rs::init_file(
8+
Path::new("./test_cfgs/test.json"),
9+
log4rs::config::Deserializers::default()
10+
)
11+
.is_ok());
12+
}

tests/init_malformed_appenders.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#[test]
2+
#[cfg(all(feature = "config_parsing", feature = "yaml_format"))]
3+
fn test_malformed_appenders() {
4+
use std::fs;
5+
6+
let config_str = fs::read_to_string("test_cfgs/malformed_appender.yml").unwrap();
7+
let cfg = ::serde_yaml::from_str::<log4rs::config::RawConfig>(&config_str);
8+
9+
assert!(cfg.is_ok());
10+
let cfg = cfg.unwrap();
11+
12+
let res = log4rs::config::create_raw_config(cfg);
13+
assert!(res.is_err());
14+
}

tests/init_toml_config.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#[test]
2+
#[cfg(all(feature = "config_parsing", feature = "toml_format"))]
3+
fn test_init_toml_cfg() {
4+
use log4rs;
5+
use std::path::Path;
6+
7+
assert!(log4rs::init_file(
8+
Path::new("./test_cfgs/test.toml"),
9+
log4rs::config::Deserializers::default()
10+
)
11+
.is_ok());
12+
}

tests/init_yaml_config.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#[test]
2+
#[cfg(all(feature = "config_parsing", feature = "yaml_format"))]
3+
fn test_init_yaml_cfg() {
4+
use log4rs;
5+
use std::path::Path;
6+
7+
assert!(log4rs::init_file(
8+
Path::new("./test_cfgs/test.yml"),
9+
log4rs::config::Deserializers::default()
10+
)
11+
.is_ok());
12+
}

0 commit comments

Comments
 (0)