merge_tools: leverage Display for printing exit status

This should handle signal exits better. format_tool_aborted() is inlined
because it is more or less a string literal now.
This commit is contained in:
Yuya Nishihara 2024-05-14 09:33:15 +09:00
parent c17a75624e
commit b0d17acb30
3 changed files with 10 additions and 20 deletions

View file

@ -116,7 +116,7 @@ pub enum ExternalToolError {
#[source]
source: std::io::Error,
},
#[error("{}", format_tool_aborted(.exit_status))]
#[error("Tool exited with {exit_status} (run with --debug to see the exact invocation)")]
ToolAborted { exit_status: ExitStatus },
#[error("I/O error")]
Io(#[source] std::io::Error),
@ -331,8 +331,7 @@ pub fn generate_diff(
if !exit_status.success() {
writeln!(
ui.warning_default(),
"{}",
format_tool_aborted(&exit_status)
"Tool exited with {exit_status} (run with --debug to see the exact invocation)",
)
.ok();
}
@ -340,17 +339,6 @@ pub fn generate_diff(
Ok(())
}
fn format_tool_aborted(exit_status: &ExitStatus) -> String {
let code = exit_status
.code()
.map(|c| c.to_string())
.unwrap_or_else(|| "<unknown>".to_string());
format!(
"Tool exited with a non-zero code (run with --debug to see the exact invocation). Exit \
code: {code}."
)
}
#[cfg(test)]
mod tests {
use super::*;

View file

@ -895,8 +895,8 @@ fn test_diff_external_tool() {
diff
"###);
insta::assert_snapshot!(stderr, @r###"
Warning: Tool exited with a non-zero code (run with --debug to see the exact invocation). Exit code: 1.
insta::assert_snapshot!(stderr.replace("exit code:", "exit status:"), @r###"
Warning: Tool exited with exit status: 1 (run with --debug to see the exact invocation)
"###);
// --tool=:builtin shouldn't be ignored

View file

@ -89,9 +89,10 @@ fn test_diffedit() {
// Nothing happens if the diff-editor exits with an error
std::fs::write(&edit_script, "rm file2\0fail").unwrap();
insta::assert_snapshot!(&test_env.jj_cmd_failure(&repo_path, &["diffedit"]), @r###"
let stderr = &test_env.jj_cmd_failure(&repo_path, &["diffedit"]);
insta::assert_snapshot!(stderr.replace("exit code:", "exit status:"), @r###"
Error: Failed to edit diff
Caused by: Tool exited with a non-zero code (run with --debug to see the exact invocation). Exit code: 1.
Caused by: Tool exited with exit status: 1 (run with --debug to see the exact invocation)
"###);
let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s"]);
insta::assert_snapshot!(stdout, @r###"
@ -435,9 +436,10 @@ fn test_diffedit_old_restore_interactive_tests() {
// Nothing happens if the diff-editor exits with an error
std::fs::write(&edit_script, "rm file2\0fail").unwrap();
insta::assert_snapshot!(&test_env.jj_cmd_failure(&repo_path, &["diffedit", "--from", "@-"]), @r###"
let stderr = test_env.jj_cmd_failure(&repo_path, &["diffedit", "--from", "@-"]);
insta::assert_snapshot!(stderr.replace("exit code:", "exit status:"), @r###"
Error: Failed to edit diff
Caused by: Tool exited with a non-zero code (run with --debug to see the exact invocation). Exit code: 1.
Caused by: Tool exited with exit status: 1 (run with --debug to see the exact invocation)
"###);
let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s"]);
insta::assert_snapshot!(stdout, @r###"