Skip to content

Commit 63bc1d9

Browse files
committed
refactor(ui): extract external signal handling into testable helpers and add regression tests
1 parent ac29450 commit 63bc1d9

1 file changed

Lines changed: 67 additions & 8 deletions

File tree

‎src/ui.rs‎

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,34 @@ impl<'a> UI<'a> {
160160
}
161161

162162
fn setup_signal_handler(should_exit: Arc<AtomicBool>) {
163-
ctrlc::set_handler(move || {
164-
let mut stdout = io::stdout();
165-
Self::handle_external_signal(should_exit.as_ref(), &mut stdout, std::process::exit);
166-
})
163+
ctrlc::set_handler(Self::build_signal_handler(
164+
should_exit,
165+
io::stdout,
166+
std::process::exit,
167+
))
167168
.expect("Error setting Ctrl-C handler");
168169
}
169170

171+
fn build_signal_handler<W, MakeWriter, Exit, ExitResult>(
172+
should_exit: Arc<AtomicBool>,
173+
make_writer: MakeWriter,
174+
exit: Exit,
175+
) -> impl FnMut() + Send + 'static
176+
where
177+
W: io::Write,
178+
MakeWriter: Fn() -> W + Send + Sync + 'static,
179+
Exit: Fn(i32) -> ExitResult + Send + Sync + 'static,
180+
{
181+
let make_writer = Arc::new(make_writer);
182+
let exit = Arc::new(exit);
183+
move || {
184+
let mut writer = make_writer.as_ref()();
185+
Self::handle_external_signal(should_exit.as_ref(), &mut writer, |code| {
186+
exit.as_ref()(code)
187+
});
188+
}
189+
}
190+
170191
fn handle_external_signal<W: io::Write, F, T>(
171192
should_exit: &AtomicBool,
172193
writer: &mut W,
@@ -284,10 +305,8 @@ impl<'a> UI<'a> {
284305
fn handle_next(&mut self) {
285306
if let Some(index) = self.history_index {
286307
if index + 1 < self.history.len() {
287-
let target = index + 1;
288-
if self.play_history_commit(target) {
289-
return;
290-
}
308+
let _ = self.play_history_commit(index + 1);
309+
return;
291310
}
292311
}
293312

@@ -803,6 +822,7 @@ mod tests {
803822
use std::fs;
804823
use std::path::{Path, PathBuf};
805824
use std::sync::atomic::{AtomicU64, Ordering, Ordering as CounterOrdering};
825+
use std::sync::Mutex;
806826
use std::time::{SystemTime, UNIX_EPOCH};
807827

808828
struct TestRepo {
@@ -948,6 +968,20 @@ mod tests {
948968
event::KeyEvent::new(code, KeyModifiers::NONE)
949969
}
950970

971+
#[derive(Clone)]
972+
struct SharedWriter(Arc<Mutex<Vec<u8>>>);
973+
974+
impl io::Write for SharedWriter {
975+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
976+
self.0.lock().unwrap().extend_from_slice(buf);
977+
Ok(buf.len())
978+
}
979+
980+
fn flush(&mut self) -> io::Result<()> {
981+
Ok(())
982+
}
983+
}
984+
951985
fn apply_until_metadata_is_visible(ui: &mut UI<'_>) {
952986
while ui.engine.current_metadata().is_none() {
953987
assert!(ui.engine.manual_step(StepMode::Change));
@@ -1000,6 +1034,31 @@ mod tests {
10001034
assert!(!output.is_empty());
10011035
}
10021036

1037+
#[test]
1038+
fn build_signal_handler_invokes_external_signal_cleanup() {
1039+
let should_exit = Arc::new(AtomicBool::new(false));
1040+
let exit_code = Arc::new(Mutex::new(None));
1041+
let output = Arc::new(Mutex::new(Vec::new()));
1042+
1043+
let mut handler = UI::build_signal_handler(
1044+
should_exit.clone(),
1045+
{
1046+
let output = output.clone();
1047+
move || SharedWriter(output.clone())
1048+
},
1049+
{
1050+
let exit_code = exit_code.clone();
1051+
move |code| *exit_code.lock().unwrap() = Some(code)
1052+
},
1053+
);
1054+
1055+
handler();
1056+
1057+
assert!(should_exit.load(Ordering::SeqCst));
1058+
assert_eq!(*exit_code.lock().unwrap(), Some(0));
1059+
assert!(!output.lock().unwrap().is_empty());
1060+
}
1061+
10031062
#[test]
10041063
fn load_commit_tracks_history_navigation_and_truncates_future_entries() {
10051064
let mut ui = test_ui();

0 commit comments

Comments
 (0)