Skip to content

Commit

Permalink
cargo2junit should indicate test failure via exit code for easy CI in…
Browse files Browse the repository at this point in the history
…tegration (#57)

* return non-zero exit code on test failure or error

* Also handle case where build completely fails

Co-authored-by: Franz Gregor <[email protected]>
Co-authored-by: John T Erickson <[email protected]>
  • Loading branch information
3 people authored May 18, 2022
1 parent 364708a commit 30b72cf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,21 @@ fn parse<T: BufRead>(
Ok(r)
}

fn determine_exit_code(report: &Report) -> Result<()> {
if report.testsuites().is_empty() {
Err(Error::new(ErrorKind::NotFound, "No test suite results were found.".to_owned()))
} else if report
.testsuites()
.iter()
.flat_map(|suite| suite.testcases().iter())
.any(|testcase| testcase.is_error() || testcase.is_failure())
{
Err(Error::new(ErrorKind::Other, "One or more tests failed.".to_owned()))
} else {
Ok(())
}
}

fn main() -> Result<()> {
let timestamp = OffsetDateTime::now_utc();
let stdin = std::io::stdin();
Expand All @@ -285,13 +300,14 @@ fn main() -> Result<()> {
.write_xml(&mut stdout)
.map_err(|e| Error::new(ErrorKind::Other, format!("{}", e)))?;
writeln!(stdout)?;
Ok(())

determine_exit_code(&report)
}

#[cfg(test)]
mod tests {
use super::SYSTEM_OUT_MAX_LEN;
use crate::parse;
use crate::{parse, determine_exit_code};
use junit_report::*;
use regex::Regex;
use std::io::*;
Expand Down Expand Up @@ -468,4 +484,26 @@ mod tests {
)
.expect("Could not parse test input");
}

#[test]
fn compile_fail() {
let report = parse_bytes(
include_bytes!("test_inputs/compile_fail.json"),
SYSTEM_OUT_MAX_LEN,
)
.expect("Could not parse test input");

assert!(determine_exit_code(&report).is_err());
}

#[test]
fn one_suite_no_tests() {
let report = parse_bytes(
include_bytes!("test_inputs/one_suite_no_tests.json"),
SYSTEM_OUT_MAX_LEN,
)
.expect("Could not parse test input");

assert!(determine_exit_code(&report).is_ok());
}
}
Empty file.
2 changes: 2 additions & 0 deletions src/test_inputs/one_suite_no_tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{ "type": "suite", "event": "started", "test_count": 0 }
{ "type": "suite", "event": "ok", "passed": 0, "failed": 0, "allowed_fail": 0, "ignored": 0, "measured": 0, "filtered_out": 0, "exec_time": 0.0000924 }

0 comments on commit 30b72cf

Please sign in to comment.