#![cfg(feature = "logging")]
use stratify::logging::Error;
use stratify::logging::FileConfig;
use tracing_subscriber::filter::EnvFilter;
#[test]
fn flush_before_init_reports_not_initialized() {
let error = stratify::logging::flush().expect_err("nothing has been initialized");
assert!(
matches!(error, Error::NotInitialized),
"expected NotInitialized, got {error:?}"
);
}
#[test]
fn reload_filter_before_init_reports_not_initialized() {
let error = stratify::logging::reload_filter(EnvFilter::new("debug"))
.expect_err("nothing has been initialized");
assert!(
matches!(error, Error::NotInitialized),
"expected NotInitialized, got {error:?}"
);
}
#[test]
fn an_unusable_log_directory_reports_io() {
let builder = stratify::logging::builder().file(FileConfig::new("/proc/stratify-cannot-exist"));
let error = builder
.build()
.err()
.expect("the directory cannot be created");
assert!(matches!(error, Error::Io(_)), "expected Io, got {error:?}");
}
#[test]
fn distinct_failures_are_distinct_variants() {
let lifecycle = stratify::logging::flush().expect_err("not initialized");
let filesystem = stratify::logging::builder()
.file(FileConfig::new("/proc/stratify-cannot-exist"))
.build()
.err()
.expect("the directory cannot be created");
let describe = |error: &Error| match error {
Error::NotInitialized => "lifecycle",
Error::Io(_) => "filesystem",
_ => "other",
};
assert_eq!(describe(&lifecycle), "lifecycle");
assert_eq!(describe(&filesystem), "filesystem");
}
#[test]
fn error_messages_stay_human_readable() {
let error = stratify::logging::flush().expect_err("not initialized");
assert_eq!(error.to_string(), "logging is not initialized");
}