use assert_cmd::Command;
use nix::sys::signal::{self, Signal};
use nix::unistd::Pid;
use predicates::prelude::*;
use std::os::unix::process::ExitStatusExt;
use std::process::Command as StdCommand;
#[test]
fn test_echo_functionality() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("zoro")?;
cmd.write_stdin("hello world\n");
cmd.assert()
.success() .stdout(predicate::str::contains("You typed hello world"));
Ok(())
}
#[test]
fn test_exit_on_eof() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("zoro")?;
let output = cmd.assert().success();
let stdout_str = String::from_utf8(output.get_output().stdout.clone())?;
assert!(stdout_str.contains("[zoro]> "));
assert_eq!(stdout_str.matches("[zoro]> ").count(), 1);
Ok(())
}
#[test]
fn test_graceful_shutdown_on_ctrl_c() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = StdCommand::new(env!("CARGO_BIN_EXE_zoro"));
let mut child = cmd.spawn()?;
let pid = Pid::from_raw(child.id() as i32);
std::thread::sleep(std::time::Duration::from_millis(100));
signal::kill(pid, Signal::SIGINT)?;
let status = child.wait()?;
assert_eq!(status.signal(), Some(Signal::SIGINT as i32));
Ok(())
}