Skip to content

Commit b56f3b3

Browse files
committed
chore: add config tests
1 parent 522a87e commit b56f3b3

File tree

6 files changed

+510
-16
lines changed

6 files changed

+510
-16
lines changed

src/config/file.rs

+103-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub enum FormatError {
9292
UnknownFormat,
9393
}
9494

95-
#[derive(Debug)]
95+
#[derive(Debug, PartialEq)]
9696
enum Format {
9797
#[cfg(feature = "yaml_format")]
9898
Yaml,
@@ -230,3 +230,105 @@ impl ConfigReloader {
230230
Ok(rate)
231231
}
232232
}
233+
234+
#[cfg(test)]
235+
mod test {
236+
use super::*;
237+
238+
#[test]
239+
fn test_from_path_all_formats() {
240+
#[cfg(feature = "yaml_format")]
241+
{
242+
assert_eq!(
243+
Format::from_path(Path::new("test.yml")).unwrap(),
244+
Format::Yaml
245+
);
246+
assert_eq!(
247+
Format::from_path(Path::new("test.yaml")).unwrap(),
248+
Format::Yaml
249+
);
250+
}
251+
252+
#[cfg(not(feature = "yaml_format"))]
253+
assert!(Format::from_path(Path::new("test.yml")).is_err());
254+
255+
#[cfg(feature = "json_format")]
256+
assert_eq!(
257+
Format::from_path(Path::new("test.json")).unwrap(),
258+
Format::Json
259+
);
260+
#[cfg(not(feature = "json_format"))]
261+
assert!(Format::from_path(Path::new("test.json")).is_err());
262+
263+
#[cfg(feature = "toml_format")]
264+
assert_eq!(
265+
Format::from_path(Path::new("test.toml")).unwrap(),
266+
Format::Toml
267+
);
268+
#[cfg(not(feature = "toml_format"))]
269+
assert!(Format::from_path(Path::new("test.toml")).is_err());
270+
271+
// Unsupported Type
272+
assert!(Format::from_path(Path::new("test.ini")).is_err());
273+
274+
// UnknownFormat
275+
assert!(Format::from_path(Path::new("test")).is_err());
276+
}
277+
278+
#[test]
279+
fn test_format_parse_all_formats() {
280+
#[cfg(feature = "yaml_format")]
281+
{
282+
let cfg = read_config(Path::new("./test_cfgs/test.yml")).unwrap();
283+
assert!(!cfg.is_empty());
284+
285+
let cfg = Format::Yaml.parse(&cfg);
286+
assert!(cfg.is_ok());
287+
288+
// No actions to test at this time.
289+
deserialize(&cfg.unwrap(), &Deserializers::default());
290+
}
291+
292+
#[cfg(feature = "json_format")]
293+
{
294+
let cfg = read_config(Path::new("./test_cfgs/test.json")).unwrap();
295+
assert!(!cfg.is_empty());
296+
297+
let cfg = Format::Json.parse(&cfg);
298+
assert!(cfg.is_ok());
299+
300+
// No actions to test at this time.
301+
deserialize(&cfg.unwrap(), &Deserializers::default());
302+
}
303+
304+
#[cfg(feature = "toml_format")]
305+
{
306+
let cfg = read_config(Path::new("./test_cfgs/test.toml")).unwrap();
307+
assert!(!cfg.is_empty());
308+
309+
let cfg = Format::Toml.parse(&cfg);
310+
assert!(cfg.is_ok());
311+
312+
// No actions to test at this time.
313+
deserialize(&cfg.unwrap(), &Deserializers::default());
314+
}
315+
}
316+
317+
#[test]
318+
fn test_load_cfg_all_formats() {
319+
#[cfg(feature = "yaml_format")]
320+
assert!(
321+
load_config_file(Path::new("./test_cfgs/test.yml"), Deserializers::default()).is_ok()
322+
);
323+
324+
#[cfg(feature = "json_format")]
325+
assert!(
326+
load_config_file(Path::new("./test_cfgs/test.json"), Deserializers::default()).is_ok()
327+
);
328+
329+
#[cfg(feature = "toml_format")]
330+
assert!(
331+
load_config_file(Path::new("./test_cfgs/test.toml"), Deserializers::default()).is_ok()
332+
);
333+
}
334+
}

src/config/raw.rs

+85-7
Original file line numberDiff line numberDiff line change
@@ -470,13 +470,17 @@ fn logger_additive_default() -> bool {
470470
#[cfg(test)]
471471
#[allow(unused_imports)]
472472
mod test {
473-
use std::fs;
473+
use crate::filter::FilterConfig;
474474

475475
use super::*;
476+
use anyhow::Error;
477+
use serde_test::{assert_de_tokens, assert_de_tokens_error, Token};
478+
use serde_value::{DeserializerError::UnknownField, Value};
479+
use std::{collections::BTreeMap, fs, vec};
476480

477481
#[test]
478482
#[cfg(all(feature = "yaml_format", feature = "threshold_filter"))]
479-
fn full_deserialize() {
483+
fn test_yaml_deserialize() {
480484
let cfg = r#"
481485
refresh_rate: 60 seconds
482486
@@ -506,14 +510,44 @@ loggers:
506510
"#;
507511
let config = ::serde_yaml::from_str::<RawConfig>(cfg).unwrap();
508512
let errors = config.appenders_lossy(&Deserializers::new()).1;
509-
println!("{:?}", errors);
510513
assert!(errors.is_empty());
514+
assert_eq!(config.refresh_rate().unwrap(), Duration::new(60, 0));
511515
}
512516

513517
#[test]
514-
#[cfg(feature = "yaml_format")]
515-
fn empty() {
516-
::serde_yaml::from_str::<RawConfig>("{}").unwrap();
518+
#[cfg(all(feature = "yaml_format", feature = "threshold_filter"))]
519+
fn test_bad_filter_and_appender_yaml() {
520+
let cfg = r#"
521+
refresh_rate: 60 seconds
522+
523+
appenders:
524+
console:
525+
kind: console
526+
filters:
527+
- kind: threshold
528+
leve: debug
529+
baz:
530+
kind: file
531+
pah: /tmp/baz.log
532+
encoder:
533+
pattern: "%m"
534+
535+
root:
536+
appenders:
537+
- console
538+
level: info
539+
540+
loggers:
541+
foo::bar::baz:
542+
level: warn
543+
appenders:
544+
- baz
545+
additive: false
546+
"#;
547+
let config = ::serde_yaml::from_str::<RawConfig>(cfg).unwrap();
548+
let errors = config.appenders_lossy(&Deserializers::new()).1;
549+
assert_eq!(errors.0.len(), 2);
550+
// TODO look for a way to check the errors
517551
}
518552

519553
#[cfg(windows)]
@@ -525,7 +559,7 @@ loggers:
525559

526560
#[test]
527561
#[cfg(feature = "yaml_format")]
528-
fn readme_sample_file_is_ok() {
562+
fn test_readme_sample_file_is_ok() {
529563
let readme = fs::read_to_string("./README.md").expect("README file exists");
530564
let sample_file = &readme[readme
531565
.find("log4rs.yaml:")
@@ -541,4 +575,48 @@ loggers:
541575
assert!(config.is_ok());
542576
assert!(config::create_raw_config(config.unwrap()).is_ok());
543577
}
578+
579+
#[test]
580+
#[cfg(feature = "yaml_format")]
581+
fn test_empty_cfg_is_valid() {
582+
::serde_yaml::from_str::<RawConfig>("{}").unwrap();
583+
}
584+
585+
#[test]
586+
fn test_appender_errors() {
587+
let errs = AppenderErrors { 0: vec![] };
588+
589+
// Verify nothing is manipulating the appender errors
590+
assert!(errs.is_empty());
591+
592+
let mut errs = AppenderErrors {
593+
0: vec![DeserializingConfigError::Appender(
594+
"example".to_owned(),
595+
anyhow!("test_appender_errors"),
596+
)],
597+
};
598+
599+
// Reports to stderr
600+
errs.handle();
601+
}
602+
603+
#[test]
604+
fn test_duration_deserialize() {
605+
let duration = Duration::new(5, 0);
606+
607+
assert_de_tokens(
608+
&duration,
609+
&[
610+
Token::Struct {
611+
name: "Duration",
612+
len: 2,
613+
},
614+
Token::Str("secs"),
615+
Token::U64(5),
616+
Token::Str("nanos"),
617+
Token::U64(0),
618+
Token::StructEnd,
619+
],
620+
);
621+
}
544622
}

0 commit comments

Comments
 (0)