testing: allow testing of commands that expect stdin

This commit is contained in:
Daniel Ploch 2024-01-19 14:23:43 -05:00 committed by Daniel Ploch
parent b4b1a21e70
commit deaea0732a
2 changed files with 43 additions and 6 deletions

View file

@ -329,8 +329,15 @@ impl Ui {
}
}
pub fn can_prompt() -> bool {
io::stdout().is_terminal()
|| env::var("JJ_INTERACTIVE")
.map(|v| v == "1")
.unwrap_or(false)
}
pub fn prompt(&mut self, prompt: &str) -> io::Result<String> {
if !io::stdout().is_terminal() {
if !Self::can_prompt() {
return Err(io::Error::new(
io::ErrorKind::Unsupported,
"Cannot prompt for input since the output is not connected to a terminal",
@ -360,7 +367,7 @@ impl Ui {
choices: &[impl AsRef<str>],
default: Option<&str>,
) -> io::Result<String> {
if !io::stdout().is_terminal() {
if !Self::can_prompt() {
if let Some(default) = default {
// Choose the default automatically without waiting.
writeln!(self.stdout(), "{prompt}: {default}")?;

View file

@ -141,15 +141,45 @@ impl TestEnvironment {
cmd
}
/// Run a `jj` command, check that it was successful, and return its
/// `(stdout, stderr)`.
pub fn jj_cmd_ok(&self, current_dir: &Path, args: &[&str]) -> (String, String) {
let assert = self.jj_cmd(current_dir, args).assert().success();
pub fn write_stdin(&self, cmd: &mut assert_cmd::Command, stdin: &str) {
cmd.env("JJ_INTERACTIVE", "1");
cmd.write_stdin(stdin);
}
pub fn jj_cmd_stdin(
&self,
current_dir: &Path,
args: &[&str],
stdin: &str,
) -> assert_cmd::Command {
let mut cmd = self.jj_cmd(current_dir, args);
self.write_stdin(&mut cmd, stdin);
cmd
}
fn get_ok(&self, mut cmd: assert_cmd::Command) -> (String, String) {
let assert = cmd.assert().success();
let stdout = self.normalize_output(&get_stdout_string(&assert));
let stderr = self.normalize_output(&get_stderr_string(&assert));
(stdout, stderr)
}
/// Run a `jj` command, check that it was successful, and return its
/// `(stdout, stderr)`.
pub fn jj_cmd_ok(&self, current_dir: &Path, args: &[&str]) -> (String, String) {
self.get_ok(self.jj_cmd(current_dir, args))
}
pub fn jj_cmd_stdin_ok(
&self,
current_dir: &Path,
args: &[&str],
stdin: &str,
) -> (String, String) {
self.get_ok(self.jj_cmd_stdin(current_dir, args, stdin))
}
/// Run a `jj` command, check that it was successful, and return its stdout
pub fn jj_cmd_success(&self, current_dir: &Path, args: &[&str]) -> String {
if self.debug_allow_stderr {