Skip to content

Commit

Permalink
Exit with non-zero code if at least one error was encountered (#784)
Browse files Browse the repository at this point in the history
Fixes #780
  • Loading branch information
ia0 authored Nov 4, 2024
1 parent 317f3c1 commit 25da386
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions rust/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Minor

- Exit with non-zero code if at least one error was encountered (fixes #780)
- Print model version with `--version`
- Change model from `standard_v2_0` to `standard_v2_1`

Expand Down
5 changes: 5 additions & 0 deletions rust/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,11 @@ async fn main() -> Result<()> {
print!("[");
}
let mut reorder = Reorder::default();
let mut errors = false;
while let Some(response) = result_receiver.recv().await {
reorder.push(response?);
while let Some(response) = reorder.pop() {
errors |= response.result.is_err();
if flags.format.json {
if reorder.next != 1 {
print!(",");
Expand All @@ -221,6 +223,9 @@ async fn main() -> Result<()> {
}
println!("]");
}
if errors {
std::process::exit(1);
}
Ok(())
}

Expand Down
17 changes: 17 additions & 0 deletions rust/cli/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,20 @@ info "Test against the test suites: $TEST_SUITES"
[ "$expected" = "$actual" ] || error "$file is detected as $actual"
done
)

info "Test exit code with at least one error"
test_error() {
files="$1"
expected="$2"
( set +e
actual="$(magika $files)"
code=$?
[ $code -eq 1 ] || error "invalid exit code for magika $files"
[ "$actual" = "$expected" ] || error "invalid output for magika $files"
)
}
test_error '/etc/shadow' "\
/etc/shadow: Permission denied (os error 13) (error)"
test_error 'non_existent src/main.rs' "\
non_existent: No such file or directory (os error 2) (error)
src/main.rs: Rust source (code)"

0 comments on commit 25da386

Please sign in to comment.