From 8c7fe404e4f4a1482d6d2c2417a93fa0a7f0732c Mon Sep 17 00:00:00 2001 From: joseph-sentry Date: Fri, 18 Oct 2024 11:43:34 -0400 Subject: [PATCH 1/5] feat: make duration_seconds optional --- src/junit.rs | 11 ++++++----- src/testrun.rs | 20 ++++++++++---------- tests/no-time.xml | 22 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 15 deletions(-) create mode 100644 tests/no-time.xml diff --git a/src/junit.rs b/src/junit.rs index 4dadb40..93b32bf 100644 --- a/src/junit.rs +++ b/src/junit.rs @@ -61,11 +61,12 @@ fn populate( .name .ok_or_else(|| ParserError::new_err("No name found"))?; - let duration = match rel_attrs.time { - None => testsuite_time - .ok_or_else(|| ParserError::new_err("No time/duration found"))? - .parse()?, - Some(time_str) => time_str.parse()?, + let duration: Option = match rel_attrs.time { + Some(time_str) => Some(time_str.parse()?), + None => match testsuite_time { + Some(time_str) => Some(time_str.parse()?), + None => None, + }, }; Ok(Testrun { diff --git a/src/testrun.rs b/src/testrun.rs index 4eedb15..b98f424 100644 --- a/src/testrun.rs +++ b/src/testrun.rs @@ -83,7 +83,7 @@ pub struct Testrun { #[pyo3(get, set)] pub classname: String, #[pyo3(get, set)] - pub duration: f64, + pub duration: Option, #[pyo3(get, set)] pub outcome: Outcome, #[pyo3(get, set)] @@ -101,7 +101,7 @@ impl Testrun { Testrun { name: "".into(), classname: "".into(), - duration: 0.0, + duration: None, outcome: Outcome::Pass, testsuite: "".into(), failure_message: None, @@ -147,7 +147,7 @@ impl Testrun { fn new( name: String, classname: String, - duration: f64, + duration: Option, outcome: Outcome, testsuite: String, failure_message: Option, @@ -168,7 +168,7 @@ impl Testrun { fn __repr__(&self) -> String { format!( - "({}, {}, {}, {}, {}, {:?}, {:?})", + "({}, {}, {}, {:?}, {}, {:?}, {:?})", self.name, self.classname, self.outcome, @@ -279,7 +279,7 @@ mod tests { let t = Testrun { classname: "".to_string(), name: "".to_string(), - duration: 0.0, + duration: None, outcome: Outcome::Pass, testsuite: "pytest".to_string(), failure_message: None, @@ -294,7 +294,7 @@ mod tests { let t = Testrun { classname: "".to_string(), name: "".to_string(), - duration: 0.0, + duration: None, outcome: Outcome::Pass, testsuite: "".to_string(), failure_message: None, @@ -309,7 +309,7 @@ mod tests { let t = Testrun { classname: ".py".to_string(), name: "".to_string(), - duration: 0.0, + duration: None, outcome: Outcome::Pass, testsuite: "".to_string(), failure_message: None, @@ -324,7 +324,7 @@ mod tests { let t = Testrun { classname: "".to_string(), name: ".py".to_string(), - duration: 0.0, + duration: None, outcome: Outcome::Pass, testsuite: "".to_string(), failure_message: None, @@ -339,7 +339,7 @@ mod tests { let t = Testrun { classname: "".to_string(), name: "".to_string(), - duration: 0.0, + duration: None, outcome: Outcome::Pass, testsuite: "".to_string(), failure_message: Some(".py".to_string()), @@ -354,7 +354,7 @@ mod tests { let t = Testrun { classname: "".to_string(), name: "".to_string(), - duration: 0.0, + duration: None, outcome: Outcome::Pass, testsuite: "".to_string(), failure_message: Some(".py".to_string()), diff --git a/tests/no-time.xml b/tests/no-time.xml new file mode 100644 index 0000000..1d9f332 --- /dev/null +++ b/tests/no-time.xml @@ -0,0 +1,22 @@ + + + + + + + + + \ No newline at end of file From 497e2a09c58c6d5b232e754d405f4745a2ab61a9 Mon Sep 17 00:00:00 2001 From: joseph-sentry Date: Fri, 18 Oct 2024 11:43:58 -0400 Subject: [PATCH 2/5] test: add test for completely empty JUnit XML --- tests/testsuites.xml | 1 + 1 file changed, 1 insertion(+) create mode 100644 tests/testsuites.xml diff --git a/tests/testsuites.xml b/tests/testsuites.xml new file mode 100644 index 0000000..2823732 --- /dev/null +++ b/tests/testsuites.xml @@ -0,0 +1 @@ + \ No newline at end of file From 93a3b78d2f33472dfc6025fc201318afaba49256 Mon Sep 17 00:00:00 2001 From: joseph-sentry Date: Fri, 18 Oct 2024 11:45:53 -0400 Subject: [PATCH 3/5] test: actually test the empty file --- tests/test_junit.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_junit.py b/tests/test_junit.py index 4a6d792..f5ddf6a 100644 --- a/tests/test_junit.py +++ b/tests/test_junit.py @@ -262,6 +262,13 @@ def test_junit(self, filename, expected, check): ], ), ), + ( + "./tests/testsuites.xml", + ParsingInfo( + None, + [], + ), + ), ], ) def test_junit(self, filename, expected): From 0688cc22cd7b91a703a55b242f7ddae643e7fb45 Mon Sep 17 00:00:00 2001 From: joseph-sentry Date: Wed, 27 Nov 2024 09:32:38 -0500 Subject: [PATCH 4/5] fix: use map instead of and_then --- src/junit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/junit.rs b/src/junit.rs index 9b6481c..e28ec97 100644 --- a/src/junit.rs +++ b/src/junit.rs @@ -63,7 +63,7 @@ fn populate( .time .as_deref() .or(testsuite_time) - .and_then(|t| Some(t.parse().unwrap())); + .map(|t| t.parse().unwrap()); let mut t = Testrun { name, From 24435471969e929907d37c3ba217f3cd455fe70b Mon Sep 17 00:00:00 2001 From: joseph-sentry Date: Wed, 27 Nov 2024 10:20:03 -0500 Subject: [PATCH 5/5] fix: avoid unwrapping, if there's an error parsing just set it to None --- src/junit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/junit.rs b/src/junit.rs index e28ec97..012e0d4 100644 --- a/src/junit.rs +++ b/src/junit.rs @@ -63,7 +63,7 @@ fn populate( .time .as_deref() .or(testsuite_time) - .map(|t| t.parse().unwrap()); + .and_then(|t| t.parse().ok()); let mut t = Testrun { name,