diff --git a/README.md b/README.md index db948e9..8a74fc8 100644 --- a/README.md +++ b/README.md @@ -90,12 +90,15 @@ The following is valid: +Commands that are allowed to exit with a non-zero status should be appended with `|| true` +so that cmdrun knows that it is intentional. + ```rust - + ``` ```diff - + ``` ```console diff --git a/src/cmdrun.rs b/src/cmdrun.rs index 3b2fea9..16a2d72 100644 --- a/src/cmdrun.rs +++ b/src/cmdrun.rs @@ -172,7 +172,13 @@ impl CmdRun { // eprintln!("command: {}", command); // eprintln!("stdout: {:?}", stdout); // eprintln!("stderr: {:?}", stderr); - - Ok(stdout) + match output.status.code() { + None => Ok(format!("**cmdrun error**: '{command}' was ended before completing.")), + Some(0) => Ok(stdout), + Some(code) => { + Ok(format!("**cmdrun error**: The following command returned a nonzero exit code ({0}):\n\n $ {1}\n\nIf you don't consider it a failure, consider making it return 0 instead.\nFor example, by appending ` || true`.\n\nstdout (what would be put into book):\n```\n{2}\n```\nstderr (helpful for debugging):\n```\n{3}\n```", + code, command, String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr))) + } + } } } diff --git a/tests/regression.rs b/tests/regression.rs index 5638f3c..9034271 100644 --- a/tests/regression.rs +++ b/tests/regression.rs @@ -73,6 +73,7 @@ fn check_all_regressions_dirs() { entries, vec![ "bash_call", + "err_messages", "inline_call", "py_factorial", "py_fibonacci", @@ -90,3 +91,4 @@ add_dir!(py_factorial); add_dir!(py_fibonacci); add_dir!(rust_call); add_dir!(shell); +add_dir!(err_messages); diff --git a/tests/regression/err_messages/a.rs b/tests/regression/err_messages/a.rs new file mode 100644 index 0000000..ce913f2 --- /dev/null +++ b/tests/regression/err_messages/a.rs @@ -0,0 +1,3 @@ +fn main() { + println!("I'm from `a.rs`"); +} diff --git a/tests/regression/err_messages/b.rs b/tests/regression/err_messages/b.rs new file mode 100644 index 0000000..4908ef8 --- /dev/null +++ b/tests/regression/err_messages/b.rs @@ -0,0 +1,3 @@ +fn main() { + println!("I'm from `b.rs`"); +} diff --git a/tests/regression/err_messages/input.md b/tests/regression/err_messages/input.md new file mode 100644 index 0000000..8ad21b9 --- /dev/null +++ b/tests/regression/err_messages/input.md @@ -0,0 +1,7 @@ +# Error Messages + +The output of a helpful command that returns 1 and I would still like its output. + + +The output from a simple typo that I need to correct. + diff --git a/tests/regression/err_messages/input_win.md b/tests/regression/err_messages/input_win.md new file mode 120000 index 0000000..fbc24e1 --- /dev/null +++ b/tests/regression/err_messages/input_win.md @@ -0,0 +1 @@ +input.md \ No newline at end of file diff --git a/tests/regression/err_messages/output.md b/tests/regression/err_messages/output.md new file mode 100644 index 0000000..efae52a --- /dev/null +++ b/tests/regression/err_messages/output.md @@ -0,0 +1,39 @@ +# Error Messages + +The output of a helpful command that returns 1 and I would still like its output. +**cmdrun error**: The following command returned a nonzero exit code (1): + + $ diff a.rs b.rs + +If you don't consider it a failure, consider making it return 0 instead. +For example, by appending ` || true`. + +stdout (what would be put into book): +``` +2c2 +< println!("I'm from `a.rs`"); +--- +> println!("I'm from `b.rs`"); + +``` +stderr (helpful for debugging): +``` + +``` +The output from a simple typo that I need to correct. +**cmdrun error**: The following command returned a nonzero exit code (127): + + $ eco simple typo + +If you don't consider it a failure, consider making it return 0 instead. +For example, by appending ` || true`. + +stdout (what would be put into book): +``` + +``` +stderr (helpful for debugging): +``` +sh: 1: eco: not found + +``` \ No newline at end of file diff --git a/tests/regression/err_messages/output_win.md b/tests/regression/err_messages/output_win.md new file mode 120000 index 0000000..8f47d6a --- /dev/null +++ b/tests/regression/err_messages/output_win.md @@ -0,0 +1 @@ +output.md \ No newline at end of file diff --git a/tests/regression/py_readme/input.md b/tests/regression/py_readme/input.md index 2121466..9a85d91 100644 --- a/tests/regression/py_readme/input.md +++ b/tests/regression/py_readme/input.md @@ -7,11 +7,11 @@ ```rust - + ``` ```diff - + ``` ```console