simplify unit tests

don't require them to start the logger
This commit is contained in:
João Marcos 2025-05-05 20:58:08 -03:00
parent 07967927dd
commit afbf0aaa75
2 changed files with 14 additions and 8 deletions

View File

@ -271,7 +271,6 @@ pub fn build_archive_file_suggestion(path: &Path, suggested_extension: &str) ->
#[cfg(test)]
mod tests {
use super::*;
use crate::utils::logger::spawn_logger_thread;
#[test]
fn test_extensions_from_path() {
@ -286,7 +285,6 @@ mod tests {
#[test]
/// Test extension parsing for input/output files
fn test_separate_known_extensions_from_name() {
spawn_logger_thread();
assert_eq!(
separate_known_extensions_from_name("file".as_ref()),
("file".as_ref(), vec![])

View File

@ -125,10 +125,14 @@ mod logger_thread {
static SENDER: OnceLock<LogSender> = OnceLock::new();
#[track_caller]
fn setup_channel() -> LogReceiver {
let (tx, rx) = mpsc::channel();
SENDER.set(tx).expect("`setup_channel` should only be called once");
rx
fn setup_channel() -> Option<LogReceiver> {
let mut optional = None;
SENDER.get_or_init(|| {
let (tx, rx) = mpsc::channel();
optional = Some(rx);
tx
});
optional
}
#[track_caller]
@ -138,6 +142,9 @@ mod logger_thread {
#[track_caller]
pub(super) fn send_print_command(msg: PrintMessage) {
if cfg!(test) {
spawn_logger_thread();
}
get_sender()
.send(LoggerCommand::Print(msg))
.expect("Failed to send print command");
@ -170,8 +177,9 @@ mod logger_thread {
}
pub fn spawn_logger_thread() {
let log_receiver = setup_channel();
thread::spawn(move || run_logger(log_receiver));
if let Some(log_receiver) = setup_channel() {
thread::spawn(move || run_logger(log_receiver));
}
}
fn run_logger(log_receiver: LogReceiver) {