#[cfg(unix)]
mod tests {
use regex::Regex;
use rexpect::error::Error as RexpectError;
use rexpect::session::{spawn_command, PtySession};
use std::io::Write;
use std::process::Command;
use tempfile::tempdir;
fn strip_until_alternate_screen_exit(text: &str) -> &str {
let ptn = Regex::new(r"\x1b\[\?1049l").unwrap();
if let Some(m) = ptn.find(text) {
&text[m.end()..]
} else {
text
}
}
fn run_app_with_cmd(cmd: Command) -> PtySession {
let mut proc = spawn_command(cmd, Some(1_000)).expect("error spawning process");
proc.exp_string("\x1b[?1049h").unwrap();
proc
}
fn get_cmd() -> Command {
Command::new(env!("CARGO_BIN_EXE_tere"))
}
fn get_cmd_no_first_run_prompt() -> Command {
let mut cmd = get_cmd();
cmd.args(["--history-file", ""]);
cmd
}
#[test]
fn basic_run() -> Result<(), RexpectError> {
let mut cmd = get_cmd_no_first_run_prompt();
let tmp = tempdir().expect("error creating temporary folder");
cmd.current_dir(tmp.path())
.env("PWD", tmp.path().as_os_str());
let mut proc = run_app_with_cmd(cmd);
proc.send("\x1b")?;
proc.writer.flush()?;
let output = proc.exp_eof()?;
let output = strip_until_alternate_screen_exit(&output);
assert_eq!(output, format!("{}\r\n", tmp.path().display()));
Ok(())
}
#[test]
fn output_on_exit_without_cd() -> Result<(), RexpectError> {
let mut proc = run_app_with_cmd(get_cmd_no_first_run_prompt());
proc.send_control('c')?;
proc.writer.flush()?;
let output = proc.exp_eof()?;
let output = strip_until_alternate_screen_exit(&output);
assert_eq!(output, "tere: Exited without changing folder\r\n");
Ok(())
}
#[test]
fn first_run_prompt_cancel() -> Result<(), RexpectError> {
let mut cmd = get_cmd();
let tmp = tempdir().expect("error creating temporary folder");
cmd.env("XDG_CACHE_HOME", tmp.path().as_os_str());
let mut proc = run_app_with_cmd(cmd);
proc.send("n")?;
proc.writer.flush()?;
let output = proc.exp_eof()?;
let ptn = Regex::new("It seems like you are running.*for the first time").unwrap();
assert!(ptn.find(&output).is_some());
assert_eq!(strip_until_alternate_screen_exit(&output), "Cancelled.\r\n");
Ok(())
}
#[test]
fn first_run_prompt_accept() -> Result<(), RexpectError> {
let mut cmd = get_cmd();
let tmp = tempdir().expect("error creating temporary folder");
cmd.env("XDG_CACHE_HOME", tmp.path().as_os_str())
.current_dir(tmp.path())
.env("PWD", tmp.path());
let mut proc = run_app_with_cmd(cmd);
proc.send("y")?;
proc.writer.flush()?;
proc.send("\x1b")?;
proc.writer.flush()?;
let output = proc.exp_eof()?;
let ptn = Regex::new("It seems like you are running.*for the first time").unwrap();
assert!(ptn.find(&output).is_some());
assert_eq!(
strip_until_alternate_screen_exit(&output),
format!("{}\r\n", tmp.path().display()),
);
assert!(tmp.path().join("tere").join("history.json").exists());
Ok(())
}
#[test]
fn skip_first_run_prompt() -> Result<(), RexpectError> {
let mut cmd = get_cmd();
let tmp = tempdir().expect("error creating temporary folder");
cmd.env("XDG_CACHE_HOME", tmp.path().as_os_str())
.current_dir(tmp.path())
.env("PWD", tmp.path())
.args(["--skip-first-run-prompt"]);
let mut proc = run_app_with_cmd(cmd);
proc.send("\x1b")?;
proc.writer.flush()?;
let output = proc.exp_eof()?;
let ptn = Regex::new("It seems like you are running.*for the first time").unwrap();
assert!(ptn.find(&output).is_none());
assert!(tmp.path().join("tere").join("history.json").exists());
Ok(())
}
}