dir-odt-to-pdf/tests/test_log_timed_macro.rs
Jesús Pérex 852df37ffc refactor(tests): improve test logging setup and organization
- Create unit test and integration tests for logs in separate tests
- Centralize temporary log file creation in common.rs
- Remove redundant logging initialization code from common.rs
- Update all test files to handle their own logging initialization
- Add detailed debug logging throughout test execution
- Add log file content display for better test debugging
- Standardize error handling across test files

Changes:
- Create setup_test_log_file() helper in common.rs
- Remove setup_test_logging() and setup_temp_log() functions
- Update test files to use common setup_test_log_file():
  - test_log_detail_macro.rs
  - test_log_timed_macro.rs
  - test_different_log_levels.rs
  - test_init_logging_with_file.rs
  - test_init_logging_append_mode.rs

This change improves test maintainability by:
1. Reducing code duplication
2. Making test behavior more explicit
3. Improving test output for debugging
4. Following single responsibility principle
2025-05-27 00:59:44 +01:00

43 lines
1.2 KiB
Rust

use dir_odt_to_pdf::error::{ProcessError, Result};
use dir_odt_to_pdf::log_timed;
use dir_odt_to_pdf::logging::{LogConfig, init_logging};
use log::{Level, LevelFilter, debug, info};
use std::fs;
mod common;
#[test]
fn test_log_timed_macro() -> Result<()> {
// Create a temporary file for logging
let (_temp_dir, log_path) = common::setup_test_log_file();
// Initialize logging with Debug level
let config = LogConfig {
log_file: Some(log_path.clone()),
log_level: LevelFilter::Debug,
append_log: false,
};
// Initialize logging
init_logging(config)?;
debug!("Starting test_log_timed_macro test");
// Test the macro
info!("Testing log_timed macro...");
log_timed!(Level::Info, "test operation", {
std::thread::sleep(std::time::Duration::from_millis(10));
});
// Read and verify the log file
let content = fs::read_to_string(&log_path).map_err(ProcessError::Io)?;
println!("=== Log File Contents ===");
println!("{}", content);
println!("=======================");
assert!(content.contains("test operation"));
assert!(content.contains("completed in"));
assert!(content.contains("ms"));
debug!("Test completed successfully");
Ok(())
}