1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#![cfg(not(miri))]
#![allow(missing_docs)]
#[cfg(test)]
mod tests {
use rlg::log::Log;
use rlg::log_format::LogFormat;
use rlg::log_level::LogLevel;
use std::sync::Mutex;
// Use a mutex to prevent race conditions when manipulating the default "RLG.log" file
static RLG_LOG_MUTEX: Mutex<()> = Mutex::new(());
#[tokio::test]
async fn test_log_file_open_error() {
let _guard = RLG_LOG_MUTEX.lock().unwrap();
// Remove RLG.log if it exists
let _ = std::fs::remove_file("RLG.log");
let _ = std::fs::remove_dir_all("RLG.log");
// Create a directory named "RLG.log" so that it cannot be opened as a file for appending
let _ = std::fs::create_dir("RLG.log");
let log = Log::default();
log.log();
// Cleanup
let _ = std::fs::remove_dir("RLG.log");
}
#[tokio::test]
async fn test_write_log_entry_open_error() {
let _guard = RLG_LOG_MUTEX.lock().unwrap();
// Remove RLG.log if it exists
let _ = std::fs::remove_file("RLG.log");
let _ = std::fs::remove_dir_all("RLG.log");
// Create a directory named "RLG.log" so that it cannot be opened as a file
let _ = std::fs::create_dir("RLG.log");
// Use fluent API instead of deprecated write_log_entry
let log = Log::build(LogLevel::INFO, "msg")
.component("proc")
.format(LogFormat::CLF);
log.log();
// Cleanup
let _ = std::fs::remove_dir("RLG.log");
}
}