From dc314144fdd2ea4f86d7e951770b0763ea22be78 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Fri, 24 Feb 2023 10:56:06 +0200 Subject: Exit --- readme.org | 2 +- src/main.rs | 19 ++++++++++++++++++- tests/cli.rs | 32 +++++++++++++++++++++++++++----- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/readme.org b/readme.org index 3d87e5c..08fbac4 100644 --- a/readme.org +++ b/readme.org @@ -14,7 +14,7 @@ Pijul. * Roadmap - [X] interactive command prompt -- [ ] exit command +- [X] exit command - [ ] git fetch and git push - [ ] git init of both private and shared repos - [ ] help command diff --git a/src/main.rs b/src/main.rs index f5f96b4..93880de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,23 @@ fn read_stdin() -> Result> { fn main() -> Result<(), Box> { loop { prompt()?; - let _user_input = read_stdin()?; + let user_input = read_stdin()?; + + if user_input.len() == 0 { + // control-d or end of input. Needs to be specially handled before + // the match because this is identical to whitespace after the trim. + break; + } + + match user_input.trim() { + "" => {} + "exit" => { + break; + } + other_input => { + println!("Unknown input {}", other_input); + } + } } + Ok(()) } diff --git a/tests/cli.rs b/tests/cli.rs index a4d368f..ccf837b 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -4,22 +4,44 @@ use std::error::Error; fn spawn_interactive_process() -> Result> { let path = cargo_bin(env!("CARGO_PKG_NAME")); - let process = spawn(&path.display().to_string(), Some(3000))?; + let mut process = spawn(&path.display().to_string(), Some(3000))?; + expect_prompt(&mut process)?; Ok(process) } +fn expect_prompt(p: &mut PtySession) -> Result<(), Box> { + p.exp_string("> ")?; + Ok(()) +} + #[test] fn shows_a_prompt() -> Result<(), Box> { - let mut p = spawn_interactive_process()?; - p.exp_string("> ")?; + spawn_interactive_process()?; Ok(()) } #[test] fn does_nothing_after_receiving_whitespace_input() -> Result<(), Box> { let mut p = spawn_interactive_process()?; - p.exp_string("> ")?; p.send_line("")?; - p.exp_string("> ")?; + expect_prompt(&mut p)?; + p.send_line(" ")?; + expect_prompt(&mut p)?; + Ok(()) +} + +#[test] +fn quits_when_eof_is_sent() -> Result<(), Box> { + let mut p = spawn_interactive_process()?; + p.send_control('d')?; + p.exp_eof()?; + Ok(()) +} + +#[test] +fn quits_when_exit_command_is_sent() -> Result<(), Box> { + let mut p = spawn_interactive_process()?; + p.send_line("exit")?; + p.exp_eof()?; Ok(()) } -- cgit v1.2.3