diff options
-rw-r--r-- | readme.org | 2 | ||||
-rw-r--r-- | src/main.rs | 19 | ||||
-rw-r--r-- | tests/cli.rs | 32 |
3 files changed, 46 insertions, 7 deletions
@@ -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<String, Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> { 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<PtySession, Box<dyn Error>> { 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<dyn Error>> { + p.exp_string("> ")?; + Ok(()) +} + #[test] fn shows_a_prompt() -> Result<(), Box<dyn Error>> { - let mut p = spawn_interactive_process()?; - p.exp_string("> ")?; + spawn_interactive_process()?; Ok(()) } #[test] fn does_nothing_after_receiving_whitespace_input() -> Result<(), Box<dyn Error>> { 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<dyn Error>> { + let mut p = spawn_interactive_process()?; + p.send_control('d')?; + p.exp_eof()?; + Ok(()) +} + +#[test] +fn quits_when_exit_command_is_sent() -> Result<(), Box<dyn Error>> { + let mut p = spawn_interactive_process()?; + p.send_line("exit")?; + p.exp_eof()?; Ok(()) } |