Skip to content

Commit

Permalink
chore: add config tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bconn98 committed Mar 3, 2024
1 parent 049e2cc commit 78db60c
Show file tree
Hide file tree
Showing 7 changed files with 521 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ streaming-stats = "0.2.3"
humantime = "2.1"
tempfile = "3.8"
mock_instant = "0.3"
serde_test = "1.0.176"

[[example]]
name = "json_logger"
Expand Down
104 changes: 103 additions & 1 deletion src/config/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub enum FormatError {
UnknownFormat,
}

#[derive(Debug)]
#[derive(Debug, PartialEq)]
enum Format {
#[cfg(feature = "yaml_format")]
Yaml,
Expand Down Expand Up @@ -230,3 +230,105 @@ impl ConfigReloader {
Ok(rate)
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_from_path_all_formats() {
#[cfg(feature = "yaml_format")]
{
assert_eq!(
Format::from_path(Path::new("test.yml")).unwrap(),
Format::Yaml
);
assert_eq!(
Format::from_path(Path::new("test.yaml")).unwrap(),
Format::Yaml
);
}

#[cfg(not(feature = "yaml_format"))]
assert!(Format::from_path(Path::new("test.yml")).is_err());

#[cfg(feature = "json_format")]
assert_eq!(
Format::from_path(Path::new("test.json")).unwrap(),
Format::Json
);
#[cfg(not(feature = "json_format"))]
assert!(Format::from_path(Path::new("test.json")).is_err());

#[cfg(feature = "toml_format")]
assert_eq!(
Format::from_path(Path::new("test.toml")).unwrap(),
Format::Toml
);
#[cfg(not(feature = "toml_format"))]
assert!(Format::from_path(Path::new("test.toml")).is_err());

// Unsupported Type
assert!(Format::from_path(Path::new("test.ini")).is_err());

// UnknownFormat
assert!(Format::from_path(Path::new("test")).is_err());
}

#[test]
fn test_format_parse_all_formats() {
#[cfg(feature = "yaml_format")]
{
let cfg = read_config(Path::new("./test_cfgs/test.yml")).unwrap();
assert!(!cfg.is_empty());

let cfg = Format::Yaml.parse(&cfg);
assert!(cfg.is_ok());

// No actions to test at this time.
deserialize(&cfg.unwrap(), &Deserializers::default());
}

#[cfg(feature = "json_format")]
{
let cfg = read_config(Path::new("./test_cfgs/test.json")).unwrap();
assert!(!cfg.is_empty());

let cfg = Format::Json.parse(&cfg);
assert!(cfg.is_ok());

// No actions to test at this time.
deserialize(&cfg.unwrap(), &Deserializers::default());
}

#[cfg(feature = "toml_format")]
{
let cfg = read_config(Path::new("./test_cfgs/test.toml")).unwrap();
assert!(!cfg.is_empty());

let cfg = Format::Toml.parse(&cfg);
assert!(cfg.is_ok());

// No actions to test at this time.
deserialize(&cfg.unwrap(), &Deserializers::default());
}
}

#[test]
fn test_load_cfg_all_formats() {
#[cfg(feature = "yaml_format")]
assert!(
load_config_file(Path::new("./test_cfgs/test.yml"), Deserializers::default()).is_ok()
);

#[cfg(feature = "json_format")]
assert!(
load_config_file(Path::new("./test_cfgs/test.json"), Deserializers::default()).is_ok()
);

#[cfg(feature = "toml_format")]
assert!(
load_config_file(Path::new("./test_cfgs/test.toml"), Deserializers::default()).is_ok()
);
}
}
91 changes: 84 additions & 7 deletions src/config/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,13 +462,17 @@ fn logger_additive_default() -> bool {
#[cfg(test)]
#[allow(unused_imports)]
mod test {
use std::fs;
use crate::filter::FilterConfig;

use super::*;
use anyhow::Error;
use serde_test::{assert_de_tokens, assert_de_tokens_error, Token};
use serde_value::{DeserializerError::UnknownField, Value};
use std::{collections::BTreeMap, fs, vec};

#[test]
#[cfg(all(feature = "yaml_format", feature = "threshold_filter"))]
fn full_deserialize() {
fn test_yaml_deserialize() {
let cfg = r#"
refresh_rate: 60 seconds
Expand Down Expand Up @@ -498,14 +502,44 @@ loggers:
"#;
let config = ::serde_yaml::from_str::<RawConfig>(cfg).unwrap();
let errors = config.appenders_lossy(&Deserializers::new()).1;
println!("{:?}", errors);
assert!(errors.is_empty());
assert_eq!(config.refresh_rate().unwrap(), Duration::new(60, 0));
}

#[test]
#[cfg(feature = "yaml_format")]
fn empty() {
::serde_yaml::from_str::<RawConfig>("{}").unwrap();
#[cfg(all(feature = "yaml_format", feature = "threshold_filter"))]
fn test_bad_filter_and_appender_yaml() {
let cfg = r#"
refresh_rate: 60 seconds
appenders:
console:
kind: console
filters:
- kind: threshold
leve: debug
baz:
kind: file
pah: /tmp/baz.log
encoder:
pattern: "%m"
root:
appenders:
- console
level: info
loggers:
foo::bar::baz:
level: warn
appenders:
- baz
additive: false
"#;
let config = ::serde_yaml::from_str::<RawConfig>(cfg).unwrap();
let errors = config.appenders_lossy(&Deserializers::new()).1;
assert_eq!(errors.0.len(), 2);
// TODO look for a way to check the errors
}

#[cfg(windows)]
Expand All @@ -517,7 +551,7 @@ loggers:

#[test]
#[cfg(feature = "yaml_format")]
fn readme_sample_file_is_ok() {
fn test_readme_sample_file_is_ok() {
let readme = fs::read_to_string("./README.md").expect("README file exists");
let sample_file = &readme[readme
.find("log4rs.yaml:")
Expand All @@ -533,4 +567,47 @@ loggers:
assert!(config.is_ok());
assert!(config::create_raw_config(config.unwrap()).is_ok());
}

#[test]
#[cfg(feature = "yaml_format")]
fn test_empty_cfg() {
::serde_yaml::from_str::<RawConfig>("{}").unwrap();
}

#[test]
fn test_appender_errors() {
let errs = AppenderErrors { 0: vec![] };

assert!(errs.is_empty());

let mut errs = AppenderErrors {
0: vec![DeserializingConfigError::Appender(
"example".to_owned(),
anyhow!("test"),
)],
};

// Reports to stderr
errs.handle();
}

#[test]
fn test_duration_deserialize() {
let duration = Duration::new(5, 0);

assert_de_tokens(
&duration,
&[
Token::Struct {
name: "Duration",
len: 2,
},
Token::Str("secs"),
Token::U64(5),
Token::Str("nanos"),
Token::U64(0),
Token::StructEnd,
],
);
}
}
Loading

0 comments on commit 78db60c

Please sign in to comment.