Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
);
}
}
92 changes: 85 additions & 7 deletions src/config/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,13 +470,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 @@ -506,14 +510,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 @@ -525,7 +559,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 @@ -541,4 +575,48 @@ loggers:
assert!(config.is_ok());
assert!(config::create_raw_config(config.unwrap()).is_ok());
}

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

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

// Verify nothing is manipulating the appender errors
assert!(errs.is_empty());

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

// 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