dir-odt-to-pdf/tests/logging_writer_tests.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

51 lines
1.3 KiB
Rust

use dir_odt_to_pdf::error::{ProcessError, Result};
use dir_odt_to_pdf::logging::{LogConfig, LogWriter};
use log::LevelFilter;
use std::fs;
use std::io;
use tempfile::NamedTempFile;
#[test]
fn test_log_writer_file_output() -> io::Result<()> {
let temp_file = NamedTempFile::new()?;
let mut writer = LogWriter::new(Some(temp_file.reopen()?));
writer.write_all(b"test message\n")?;
writer.flush()?;
let content = fs::read_to_string(temp_file.path())?;
assert_eq!(content, "test message\n");
Ok(())
}
#[test]
fn test_log_writer_console_output() {
let mut writer = LogWriter::new(None);
assert!(writer.write_all(b"test message\n").is_ok());
assert!(writer.flush().is_ok());
}
#[test]
fn test_log_config_default() {
let config = LogConfig::default();
assert_eq!(config.log_level, LevelFilter::Info);
assert_eq!(config.log_file, None);
assert!(!config.append_log);
}
#[test]
fn test_log_writer() -> Result<()> {
let mut writer = LogWriter::new(None);
writer
.write_all(b"test message\n")
.map_err(ProcessError::Io)?;
writer.flush().map_err(ProcessError::Io)?;
Ok(())
}
#[test]
fn test_log_writer_methods() -> Result<()> {
let mut writer = LogWriter::new(None);
assert!(writer.write_all(b"test message\n").is_ok());
assert!(writer.flush().is_ok());
Ok(())
}