Skip to content

Commit 0485c2d

Browse files
committed
tests: app mod
This commit adds tests for the app mod functions, increasing patch hub tests coverage. The Logger initialization and Garbage Collector execution were wrapped in an `if !cfg!(test)` block. This change prevents double initialization panics and I/O errors during testing, ensuring that production logic does not interfere with the isolated test environment. Signed-off-by: JGBSouza <[email protected]>
1 parent ad93a74 commit 0485c2d

File tree

1 file changed

+97
-4
lines changed

1 file changed

+97
-4
lines changed

src/app/mod.rs

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ impl App {
7979

8080
let lore_api_client = BlockingLoreAPIClient::default();
8181

82-
// Initialize the logger before the app starts
83-
Logger::init_log_file(&config)?;
84-
Logger::info("patch-hub started");
85-
garbage_collector::collect_garbage(&config);
82+
// Initialize the logger before the app starts if not in a test
83+
if !cfg!(test) {
84+
Logger::init_log_file(&config).ok();
85+
Logger::info("patch-hub started");
86+
garbage_collector::collect_garbage(&config);
87+
}
8688

8789
Ok(App {
8890
current_screen: CurrentScreen::MailingListSelection,
@@ -438,3 +440,94 @@ impl App {
438440
app_can_run
439441
}
440442
}
443+
#[cfg(test)]
444+
mod tests {
445+
use super::*;
446+
use crate::app::config::Config;
447+
use std::sync::Once;
448+
use crate::infrastructure::{garbage_collector};
449+
450+
static INIT_LOGGER: Once = Once::new();
451+
452+
fn create_test_app() -> App {
453+
let config = Config::default();
454+
455+
INIT_LOGGER.call_once(|| {
456+
let _ = std::panic::catch_unwind(|| {
457+
Logger::init_log_file(&config).ok();
458+
Logger::info("Test started");
459+
garbage_collector::collect_garbage(&config);
460+
});
461+
});
462+
463+
App::new(config).unwrap()
464+
}
465+
466+
#[test]
467+
fn test_new_initial_state() {
468+
let app = create_test_app();
469+
470+
assert_eq!(app.current_screen, CurrentScreen::MailingListSelection);
471+
assert!(app.popup.is_none());
472+
assert!(app.latest_patchsets.is_none());
473+
assert!(app.details_actions.is_none());
474+
assert!(app.edit_config.is_none());
475+
}
476+
477+
#[test]
478+
fn test_set_current_screen() {
479+
let mut app = create_test_app();
480+
481+
assert_ne!(app.current_screen, CurrentScreen::EditConfig);
482+
app.set_current_screen(CurrentScreen::EditConfig);
483+
assert_eq!(app.current_screen, CurrentScreen::EditConfig);
484+
}
485+
486+
#[test]
487+
fn test_reset_latest_patchsets_when_none() {
488+
let mut app = create_test_app();
489+
490+
app.latest_patchsets = None;
491+
app.reset_latest_patchsets();
492+
assert!(app.latest_patchsets.is_none());
493+
}
494+
495+
#[test]
496+
fn test_reset_details_actions_when_none() {
497+
let mut app = create_test_app();
498+
499+
app.details_actions = None;
500+
app.reset_details_actions();
501+
assert!(app.details_actions.is_none());
502+
}
503+
504+
#[test]
505+
fn test_init_edit_config_sets_some() {
506+
let mut app = create_test_app();
507+
508+
assert!(app.edit_config.is_none());
509+
app.init_edit_config();
510+
assert!(app.edit_config.is_some());
511+
}
512+
513+
#[test]
514+
fn test_reset_edit_config_sets_none() {
515+
let mut app = create_test_app();
516+
517+
app.init_edit_config();
518+
assert!(app.edit_config.is_some());
519+
app.reset_edit_config();
520+
assert!(app.edit_config.is_none());
521+
}
522+
523+
#[test]
524+
fn test_consolidate_edit_config_with_none_does_not_change_config() {
525+
let mut app = create_test_app();
526+
527+
let before = app.config.page_size();
528+
app.edit_config = None;
529+
app.consolidate_edit_config();
530+
assert_eq!(app.config.page_size(), before);
531+
}
532+
533+
}

0 commit comments

Comments
 (0)