diff options
author | Tim Oram <dev@mitmaro.ca> | 2021-06-14 09:55:20 -0230 |
---|---|---|
committer | Tim Oram <dev@mitmaro.ca> | 2021-06-15 09:11:07 -0230 |
commit | e9501a7164bf11ef69b75d8ffdc3d0434fae1207 (patch) | |
tree | 40c9115f49521deedd9e705825e6dc7bb66f2666 /src/process | |
parent | 57ef83e68a7c90dc36c30510407c7f0ad5dc4ba2 (diff) |
Refactor CLI and main function
This replaces the CLI argument parsing with a lighter library and drops
the need for the heavy Clap library. Most of the main function code is
also moved into a new core module, and additional lighter tests are
added. A build.rs file has also been added to provide a more detailed
version info output.
Diffstat (limited to 'src/process')
-rw-r--r-- | src/process/mod.rs | 12 | ||||
-rw-r--r-- | src/process/tests.rs | 8 | ||||
-rw-r--r-- | src/process/testutil.rs | 65 |
3 files changed, 10 insertions, 75 deletions
diff --git a/src/process/mod.rs b/src/process/mod.rs index a41f5f7..7d168cd 100644 --- a/src/process/mod.rs +++ b/src/process/mod.rs @@ -51,10 +51,10 @@ impl Process { } } - pub(crate) fn run(&mut self, mut modules: Modules<'_>) -> Result<Option<ExitStatus>> { + pub(crate) fn run(&mut self, mut modules: Modules<'_>) -> Result<ExitStatus> { if self.view_sender.start().is_err() { self.exit_status = Some(ExitStatus::StateError); - return Ok(self.exit_status); + return Ok(ExitStatus::StateError); } self.handle_process_result( @@ -92,7 +92,7 @@ impl Process { } } if self.view_sender.stop().is_err() { - return Ok(Some(ExitStatus::StateError)); + return Ok(ExitStatus::StateError); } if let Some(status) = self.exit_status { if status != ExitStatus::Kill { @@ -101,16 +101,16 @@ impl Process { } if self.view_sender.end().is_err() { - return Ok(Some(ExitStatus::StateError)); + return Ok(ExitStatus::StateError); } while let Some(handle) = self.threads.pop() { if handle.join().is_err() { - return Ok(Some(ExitStatus::StateError)); + return Ok(ExitStatus::StateError); } } - Ok(self.exit_status) + Ok(self.exit_status.unwrap_or(ExitStatus::Good)) } fn handle_process_result(&mut self, modules: &mut Modules<'_>, result: &ProcessResult) { diff --git a/src/process/tests.rs b/src/process/tests.rs index e48c744..c75d43b 100644 --- a/src/process/tests.rs +++ b/src/process/tests.rs @@ -32,7 +32,7 @@ fn window_too_small() { view, ); let modules = Modules::new(test_context.config); - assert_eq!(process.run(modules).unwrap().unwrap(), ExitStatus::Abort); + assert_eq!(process.run(modules).unwrap(), ExitStatus::Abort); }, ); } @@ -53,7 +53,7 @@ fn force_abort() { view, ); let modules = Modules::new(test_context.config); - assert_eq!(process.run(modules).unwrap().unwrap(), ExitStatus::Good); + assert_eq!(process.run(modules).unwrap(), ExitStatus::Good); shadow_rebase_file.load_file().unwrap(); assert!(shadow_rebase_file.is_empty()); }, @@ -76,7 +76,7 @@ fn force_rebase() { view, ); let modules = Modules::new(test_context.config); - assert_eq!(process.run(modules).unwrap().unwrap(), ExitStatus::Good); + assert_eq!(process.run(modules).unwrap(), ExitStatus::Good); shadow_rebase_file.load_file().unwrap(); assert_eq!(shadow_rebase_file.get_lines_owned(), vec![Line::new( "pick aaa comment" @@ -126,7 +126,7 @@ fn resize_window_size_okay() { view, ); let modules = Modules::new(test_context.config); - assert_eq!(process.run(modules).unwrap().unwrap(), ExitStatus::Abort); + assert_eq!(process.run(modules).unwrap(), ExitStatus::Abort); }, ); } diff --git a/src/process/testutil.rs b/src/process/testutil.rs index ae53adf..75758fe 100644 --- a/src/process/testutil.rs +++ b/src/process/testutil.rs @@ -12,7 +12,6 @@ use crate::{ process::{exit_status::ExitStatus, process_module::ProcessModule, process_result::ProcessResult, state::State}, todo_file::{line::Line, TodoFile}, view::{render_context::RenderContext, view_data::ViewData}, - Exit, }; pub struct TestContext<'t> { @@ -282,67 +281,3 @@ where C: for<'p> FnOnce(TestContext<'p>) { }); }); } - -fn format_exit_status(exit: &Result<ExitStatus, Exit>) -> String { - format!( - "Result({}, {})", - exit.as_ref() - .map_or_else(|_| String::from("None"), |e| { format!("{:?}", e) }), - exit.as_ref().map_or_else( - |e| { format!("Exit {{ Message({}), Status({:?}) }}", e.message, e.status) }, - |_| String::from("None") - ) - ) -} - -pub fn _assert_exit_status(actual: &Result<ExitStatus, Exit>, expected: &Result<ExitStatus, Exit>) { - if !match actual.as_ref() { - Ok(actual_exit_status) => { - if let Ok(expected_exit_status) = expected.as_ref() { - actual_exit_status == expected_exit_status - } - else { - false - } - }, - Err(actual_exit) => { - if let Err(expected_exit) = expected.as_ref() { - actual_exit.status == expected_exit.status && actual_exit.message == expected_exit.message - } - else { - false - } - }, - } { - panic!( - "{}", - vec![ - "\n", - "Exit result does not match", - "==========", - "Expected:", - format_exit_status(expected).as_str(), - "Actual:", - format_exit_status(actual).as_str(), - "==========\n" - ] - .join("\n") - ); - } -} - -#[macro_export] -macro_rules! assert_exit_status { - ($actual:expr, status = $status:expr) => { - crate::process::testutil::_assert_exit_status(&$actual, &Ok($status)) - }; - ($actual:expr, message = $message:expr, status = $status:expr) => { - crate::process::testutil::_assert_exit_status( - &$actual, - &Err(Exit { - message: String::from($message), - status: $status, - }), - ) - }; -} |