Skip to content

Commit

Permalink
test: continuing
Browse files Browse the repository at this point in the history
  • Loading branch information
bconn98 committed Mar 1, 2024
1 parent 43973fb commit 21f7736
Show file tree
Hide file tree
Showing 28 changed files with 1,413 additions and 853 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ thiserror = "1.0.15"
anyhow = "1.0.28"
derivative = "2.2"
once_cell = "1.17.1"
strum = "0.26.1"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", optional = true, features = ["handleapi", "minwindef", "processenv", "winbase", "wincon"] }
Expand Down
114 changes: 114 additions & 0 deletions src/append/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,117 @@ impl Deserialize for ConsoleAppenderDeserializer {
Ok(Box::new(appender.build()))
}
}

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

#[test]
fn test_append() {
use log::Level;

// Build a std out appender
let appender = ConsoleAppender::builder()
.tty_only(false)
.target(Target::Stdout)
.encoder(Box::new(PatternEncoder::new("{m}{n}")))
.build();

assert!(appender
.append(
&Record::builder()
.level(Level::Debug)
.target("target")
.module_path(Some("module_path"))
.file(Some("file"))
.line(Some(100))
.args(format_args!("{}", "message"))
.build()
)
.is_ok());

// No op, but test coverage :)
appender.flush();
}

#[test]
fn test_builder() {
// Build a std out appender
let _appender = ConsoleAppender::builder()
.tty_only(false)
.target(Target::Stdout)
.encoder(Box::new(PatternEncoder::new("{m}{n}")))
.build();

// Build a std err appender
let _appender = ConsoleAppender::builder()
.tty_only(false)
.target(Target::Stderr)
.encoder(Box::new(PatternEncoder::new("{m}{n}")))
.build();

// Build a default encoder appender
let _appender = ConsoleAppender::builder()
.tty_only(true)
.target(Target::Stderr)
.build();
}

#[test]
#[cfg(feature = "config_parsing")]
fn test_config_deser() {
use crate::{config::Deserializers, encode::EncoderConfig};
use serde_value::Value;
use std::collections::BTreeMap;
let deserializer = ConsoleAppenderDeserializer;

let targets = vec![ConfigTarget::Stdout, ConfigTarget::Stderr];

for target in targets {
let console_cfg = ConsoleAppenderConfig {
target: Some(target),
encoder: Some(EncoderConfig {
kind: "pattern".to_owned(),
config: Value::Map(BTreeMap::new()),
}),
tty_only: Some(true),
};
assert!(deserializer
.deserialize(console_cfg, &Deserializers::default())
.is_ok());
}
}

fn write_test(mut writer: WriterLock) {
use std::io::Write;

assert_eq!(writer.write(b"Write log\n").unwrap(), 10);
assert!(writer.set_style(&Style::new()).is_ok());
assert!(writer.write_all(b"Write All log\n").is_ok());
assert!(writer.write_fmt(format_args!("{} \n", "normal")).is_ok());
assert!(writer.flush().is_ok());
}

#[test]
fn test_tty() {
// Note that this fails in GitHub Actions and therefore does not
// show as covered.
let w = match ConsoleWriter::stdout() {
Some(w) => w,
None => return,
};

let tty = Writer::Tty(w);
assert!(tty.is_tty());

write_test(tty.lock());
}

#[test]
fn test_raw() {
let raw = Writer::Raw(StdWriter::stdout());
assert!(!raw.is_tty());
write_test(raw.lock());
}
}
61 changes: 57 additions & 4 deletions src/append/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ mod test {
use super::*;

#[test]
fn create_directories() {
fn test_create_directories() {
let tempdir = tempfile::tempdir().unwrap();

FileAppender::builder()
Expand All @@ -173,11 +173,64 @@ mod test {
}

#[test]
fn append_false() {
fn test_append_trait() {
use log::Level;

let tempdir = tempfile::tempdir().unwrap();
FileAppender::builder()
.append(false)
let appender = FileAppender::builder()
.build(tempdir.path().join("foo.log"))
.unwrap();

log_mdc::insert("foo", "bar");
let res = appender.append(
&Record::builder()
.level(Level::Debug)
.target("target")
.module_path(Some("module_path"))
.file(Some("file"))
.line(Some(100))
.args(format_args!("{}", "message"))
.build(),
);
assert!(res.is_ok());

appender.flush();
}

#[test]
fn test_append_builder() {
let append_choices = vec![true, false];
let tempdir = tempfile::tempdir().unwrap();

for do_append in append_choices {
// No actionable test
FileAppender::builder()
.append(do_append)
.build(tempdir.path().join("foo.log"))
.unwrap();
}
}

#[test]
#[cfg(feature = "config_parsing")]
fn test_config_deser() {
use crate::config::Deserializers;
use serde_value::Value;
use std::collections::BTreeMap;

let tempdir = tempfile::tempdir().unwrap();
let file_cfg = FileAppenderConfig {
path: tempdir.path().join("foo.log").to_str().unwrap().to_owned(),
encoder: Some(EncoderConfig {
kind: "pattern".to_owned(),
config: Value::Map(BTreeMap::new()),
}),
append: Some(true),
};

let deserializer = FileAppenderDeserializer;

let res = deserializer.deserialize(file_cfg, &Deserializers::default());
assert!(res.is_ok());
}
}
60 changes: 59 additions & 1 deletion src/append/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,15 @@ impl<'de> Deserialize<'de> for AppenderConfig {

#[cfg(test)]
mod test {
#[cfg(feature = "config_parsing")]
use super::*;

#[cfg(any(feature = "file_appender", feature = "rolling_file_appender"))]
use std::env::{set_var, var};

#[test]
#[cfg(any(feature = "file_appender", feature = "rolling_file_appender"))]
fn expand_env_vars_tests() {
fn test_expand_env_vars() {
set_var("HELLO_WORLD", "GOOD BYE");
#[cfg(not(target_os = "windows"))]
let test_cases = vec![
Expand Down Expand Up @@ -250,4 +253,59 @@ mod test {
assert_eq!(res, expected)
}
}

#[test]
#[cfg(feature = "config_parsing")]
fn test_config_deser() {
use std::collections::BTreeMap;

use serde_test::{assert_de_tokens, assert_de_tokens_error, Token};
use serde_value::Value;

use crate::filter::FilterConfig;

let appender = AppenderConfig {
kind: "file".to_owned(),
filters: vec![FilterConfig {
kind: "threshold".to_owned(),
config: Value::Map({
let mut map = BTreeMap::new();
map.insert(
Value::String("level".to_owned()),
Value::String("error".to_owned()),
);
map
}),
}],
config: Value::Map(BTreeMap::new()),
};

let mut cfg = vec![
Token::Struct {
name: "AppenderConfig",
len: 3,
},
Token::Str("kind"),
Token::Str("file"),
Token::Str("filters"),
Token::Seq { len: Some(1) },
Token::Struct {
name: "FilterConfig",
len: 2,
},
Token::Str("kind"),
Token::Str("threshold"),
Token::Str("level"),
Token::Str("error"),
Token::StructEnd,
Token::SeqEnd,
Token::StructEnd,
];

assert_de_tokens(&appender, &cfg);

// Intentional typo on expected field
cfg[1] = Token::Str("kid");
assert_de_tokens_error::<AppenderConfig>(&cfg, "missing field `kind`");
}
}
Loading

0 comments on commit 21f7736

Please sign in to comment.