Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,15 @@ The following is valid:

<!-- cmdrun python3 generate_table.py -->

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
<!-- cmdrun cat program.rs -->
<!-- cmdrun cat program.rs || true -->
```

```diff
<!-- cmdrun diff a.rs b.rs -->
<!-- cmdrun diff a.rs b.rs || true -->
```

```console
Expand Down
10 changes: 8 additions & 2 deletions src/cmdrun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
}
}
}
2 changes: 2 additions & 0 deletions tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ fn check_all_regressions_dirs() {
entries,
vec![
"bash_call",
"err_messages",
"inline_call",
"py_factorial",
"py_fibonacci",
Expand All @@ -90,3 +91,4 @@ add_dir!(py_factorial);
add_dir!(py_fibonacci);
add_dir!(rust_call);
add_dir!(shell);
add_dir!(err_messages);
3 changes: 3 additions & 0 deletions tests/regression/err_messages/a.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("I'm from `a.rs`");
}
3 changes: 3 additions & 0 deletions tests/regression/err_messages/b.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("I'm from `b.rs`");
}
7 changes: 7 additions & 0 deletions tests/regression/err_messages/input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Error Messages

The output of a helpful command that returns 1 and I would still like its output.
<!-- cmdrun diff a.rs b.rs -->

The output from a simple typo that I need to correct.
<!-- cmdrun eco simple typo -->
1 change: 1 addition & 0 deletions tests/regression/err_messages/input_win.md
39 changes: 39 additions & 0 deletions tests/regression/err_messages/output.md
Original file line number Diff line number Diff line change
@@ -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

```
1 change: 1 addition & 0 deletions tests/regression/err_messages/output_win.md
4 changes: 2 additions & 2 deletions tests/regression/py_readme/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<!-- cmdrun python3 generate_table.py -->

```rust
<!-- cmdrun cat program.rs -->
<!-- cmdrun cat program.rs || true -->
```

```diff
<!-- cmdrun diff a.rs b.rs -->
<!-- cmdrun diff a.rs b.rs || true -->
```

```console
Expand Down