Skip to content

Commit

Permalink
test exit scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene-babichenko committed Nov 25, 2024
1 parent b8c512e commit 45faeb8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/cmd/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,15 @@ impl Cmd {
print!("{}", fixes[selection]);
Ok(())
}
Ok(None) => Ok(()),
Ok(None) => {
eprintln!("Cancelled.");
Ok(())
}
// Do not throw an error when Ctrl-C is pressed.
Err(dialoguer::Error::IO(e)) if e.kind() == io::ErrorKind::Interrupted => Ok(()),
Err(dialoguer::Error::IO(e)) if e.kind() == io::ErrorKind::Interrupted => {
eprintln!("Cancelled.");
Ok(())
}
Err(e) => Err(Error::Select(e)),
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,16 @@ fn nothing_to_fix(bash: (Session, NamedTempFile)) {
p.send_line("").unwrap();
p.expect("nothing to fix").unwrap();
}

#[rstest]
fn quit(bash: (Session, NamedTempFile)) {
let (mut p, _histfile) = bash;

p.set_expect_timeout(Some(Duration::from_secs(5)));

p.send_line("eco 'Hello, world!'").unwrap();
p.expect("command not found").unwrap();
p.send_line("fix").unwrap();
p.send_line("q").unwrap();
p.expect("Cancelled.").unwrap();
}
20 changes: 20 additions & 0 deletions tests/ui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::{io::Read, process::Command, thread::sleep, time::Duration};

use expectrl::{Session, Signal};

#[test]
fn ctrl_c() {
let mut cmd = Command::new(format!("{}/target/debug/fixit", env!("CARGO_MANIFEST_DIR")));
cmd.env("FIXIT_QUICK_ENABLE", "false");
cmd.arg("eco 'Hello, world!'");

let mut p = Session::spawn(cmd).unwrap();

sleep(Duration::from_secs(1));

p.get_process_mut().signal(Signal::SIGTERM).unwrap();

let mut buf = String::new();
p.read_to_string(&mut buf).unwrap();
buf.contains("Cacelled.");
}

0 comments on commit 45faeb8

Please sign in to comment.