feat(test): add tests
This commit is contained in:
47
tests/file_test.rs
Normal file
47
tests/file_test.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
// tests/file_test.rs
|
||||
use chrono::Utc;
|
||||
use rust_logger::model::{LogLevel, LogRecord};
|
||||
use rust_logger::outputs::LogOutput;
|
||||
use rust_logger::outputs::file::FileOutput;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
#[tokio::test] // 使用 tokio 的测试宏
|
||||
async fn test_file_output_write() {
|
||||
let dir = "test_logs";
|
||||
let prefix = "test-service";
|
||||
|
||||
// 1. 清理环境 (确保之前跑剩下的文件删掉)
|
||||
if Path::new(dir).exists() {
|
||||
fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
// 2. 初始化 Output
|
||||
let output = FileOutput::new(dir, prefix)
|
||||
.await
|
||||
.expect("Failed to create output");
|
||||
|
||||
// 3. 构造一条假日志
|
||||
let record = LogRecord {
|
||||
service_name: "file_test".into(),
|
||||
timestamp: Utc::now(),
|
||||
level: LogLevel::INFO,
|
||||
message: "Hello Integration Test File".into(),
|
||||
module: "tests".into(),
|
||||
trace_id: Some("123-abc".into()),
|
||||
};
|
||||
|
||||
// 4. 写入
|
||||
output.write(&record).await;
|
||||
|
||||
// 5. 验证文件是否存在且内容正确
|
||||
let mut entries = fs::read_dir(dir).unwrap();
|
||||
let entry = entries.next().unwrap().unwrap();
|
||||
let content = fs::read_to_string(entry.path()).unwrap();
|
||||
|
||||
assert!(content.contains("Hello Integration Test"));
|
||||
assert!(content.contains("[INFO]"));
|
||||
|
||||
// 6. 清理
|
||||
fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
Reference in New Issue
Block a user