-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changed logger logic for enable reseting
- Loading branch information
Showing
5 changed files
with
216 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use test_env_helpers::*; | ||
|
||
#[cfg(test)] | ||
#[after_all] | ||
#[cfg(test)] | ||
mod tests { | ||
use logger_core::{init, log_debug, log_trace}; | ||
use rand::{distributions::Alphanumeric, Rng}; | ||
use std::fs::{read_dir, read_to_string, remove_dir_all}; | ||
const FILE_DIRECTORY: &str = "babushka-logs"; | ||
|
||
fn generate_random_string(length: usize) -> String { | ||
rand::thread_rng() | ||
.sample_iter(&Alphanumeric) | ||
.take(length) | ||
.map(char::from) | ||
.collect() | ||
} | ||
|
||
fn get_file_contents(file_name: &str) -> String { | ||
let files = read_dir(FILE_DIRECTORY).unwrap(); | ||
let file = files | ||
.into_iter() | ||
.find(|path| { | ||
path.as_ref() | ||
.unwrap() | ||
.path() | ||
.file_name() | ||
.unwrap() | ||
.to_str() | ||
.unwrap() | ||
.starts_with(file_name) | ||
}) | ||
.unwrap(); | ||
read_to_string(file.unwrap().path()).unwrap() | ||
} | ||
|
||
#[test] | ||
fn log_to_console_works_after_multiple_inits_diff_log_level() { | ||
let identifier = generate_random_string(10); | ||
init(Some(logger_core::Level::Trace), None); | ||
init(Some(logger_core::Level::Debug), None); | ||
// you should see in the console something like '2023-07-07T06:57:54.446236Z DEBUG logger_core: e49NaJ5J41 - foo' | ||
log_debug(identifier.clone(), "foo"); | ||
// make sure that something like '2023-07-07T06:57:54.446236Z DEBUG logger_core: e49NaJ5J41 - boo' does not appear | ||
log_trace(identifier, "boo"); | ||
} | ||
|
||
#[test] | ||
fn log_to_file_works_after_multiple_inits() { | ||
let identifier = generate_random_string(10); | ||
init(Some(logger_core::Level::Trace), Some(identifier.as_str())); | ||
init(Some(logger_core::Level::Debug), Some(identifier.as_str())); | ||
log_debug(identifier.clone(), "foo"); | ||
let contents = get_file_contents(identifier.as_str()); | ||
assert!( | ||
contents.contains(identifier.as_str()), | ||
"Contens: {}", | ||
contents | ||
); | ||
assert!(contents.contains("foo"), "Contens: {}", contents); | ||
} | ||
|
||
#[test] | ||
fn log_to_file_works_after_console_init() { | ||
let identifier = generate_random_string(10); | ||
init(Some(logger_core::Level::Trace), None); | ||
init(Some(logger_core::Level::Trace), Some(identifier.as_str())); | ||
let identifier = generate_random_string(10); | ||
init(Some(logger_core::Level::Debug), Some(identifier.as_str())); | ||
log_debug(identifier.clone(), "foo"); | ||
log_trace(identifier.clone(), "boo"); | ||
let contents = get_file_contents(identifier.as_str()); | ||
assert!( | ||
contents.contains(identifier.as_str()), | ||
"Contens: {}", | ||
contents | ||
); | ||
assert!(contents.contains("foo"), "Contens: {}", contents); | ||
assert_eq!(contents.contains("boo"), false); | ||
} | ||
|
||
#[test] | ||
fn log_to_file_disabled_when_console_init() { | ||
let identifier = generate_random_string(10); | ||
init(Some(logger_core::Level::Trace), Some(identifier.as_str())); | ||
init(Some(logger_core::Level::Trace), None); | ||
// you should see in the console something like '2023-07-07T06:57:54.446236Z TRACE logger_core: e49NaJ5J41 - foo' | ||
log_trace(identifier.clone(), "foo"); | ||
let contents = get_file_contents(identifier.as_str()); | ||
assert!(contents.is_empty()); | ||
} | ||
|
||
fn after_all() { | ||
remove_dir_all(FILE_DIRECTORY).expect("Cannot remove log directory") | ||
} | ||
} |