diff --git a/cli/src/ui.rs b/cli/src/ui.rs index baaf4e265..9beced9cd 100644 --- a/cli/src/ui.rs +++ b/cli/src/ui.rs @@ -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 { - 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], default: Option<&str>, ) -> io::Result { - 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}")?; diff --git a/cli/tests/common/mod.rs b/cli/tests/common/mod.rs index fc77250b7..c9bad7ea6 100644 --- a/cli/tests/common/mod.rs +++ b/cli/tests/common/mod.rs @@ -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 {