Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(core): add junit output parser #325

Merged
merged 1 commit into from
Oct 16, 2024
Merged
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
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,4 @@ walkdir = "2.5.0"
octocrab = "0.39.0"
bytes = "1.7.2"
tempfile = { workspace = true }
serde-xml-rs = "0.6.0"
99 changes: 99 additions & 0 deletions core/src/utils/junit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use serde::{Deserialize, Serialize};

use crate::types::errors::CoreError;

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct JunitOutput {
#[serde(rename = "time")]
pub time: f64,
#[serde(rename = "testsuite")]
pub testsuites: Vec<TestSuite>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TestSuite {
#[serde(rename = "name")]
pub name: String,
#[serde(rename = "time")]
pub time: f64,
#[serde(rename = "testcase")]
pub testcases: Vec<TestCase>,
#[serde(rename = "testsuite")]
pub testsuites: Option<Vec<TestSuite>>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TestCase {
#[serde(rename = "name")]
pub name: String,
#[serde(rename = "classname")]
pub classname: String,
#[serde(rename = "time", default)]
pub time: Option<f64>,
pub failure: Option<Failure>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Failure {
#[serde(rename = "message")]
pub message: String,
#[serde(rename = "type")]
pub failure_type: String,
#[serde(rename = "$value")]
pub content: Option<String>,
}

impl JunitOutput {
pub fn new_from_xml_str(xml_str: &str) -> Result<JunitOutput, CoreError> {
let parsed: JunitOutput = serde_xml_rs::from_str(xml_str)?;
Ok(parsed)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_junit_xml() {
const TEST_JUNIT_XML: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
<testsuites time="15.682687">
<testsuite name="Tests.Registration" time="6.605871">
<testcase name="testCase1" classname="Tests.Registration" time="2.113871" />
<testcase name="testCase2" classname="Tests.Registration" time="1.051" />
<testcase name="testCase3" classname="Tests.Registration" time="3.441" />
</testsuite>
<testsuite name="Tests.Authentication" time="9.076816">
<testsuite name="Tests.Authentication.Login" time="4.356">
<testcase name="testCase4" classname="Tests.Authentication.Login" time="2.244" />
<testcase name="testCase5" classname="Tests.Authentication.Login" time="0.781" />
<testcase name="testCase6" classname="Tests.Authentication.Login" time="1.331" />
</testsuite>
<testcase name="testCase7" classname="Tests.Authentication" time="2.508" />
<testcase name="testCase8" classname="Tests.Authentication" time="1.230816" />
<testcase name="testCase9" classname="Tests.Authentication" time="0.982">
<failure message="Assertion error message" type="AssertionError">
<!-- Call stack printed here -->
AAAA HELP WE FAILED
</failure>
</testcase>
</testsuite>
</testsuites>"#;

let parsed: JunitOutput = JunitOutput::new_from_xml_str(TEST_JUNIT_XML).unwrap();
println!("{:?}", parsed);

assert_eq!(parsed.testsuites.len(), 2);

assert_eq!(parsed.testsuites[0].testcases.len(), 3);
assert_eq!(parsed.testsuites[1].testcases.len(), 3);

let first_testsuite = parsed.testsuites[0].clone();
assert_eq!(first_testsuite.testcases.len(), 3);

// test the failure message
let second_testsuite = parsed.testsuites[1].clone();
let failure = second_testsuite.testcases[2].failure.clone();
assert_eq!(failure.unwrap().content.unwrap(), "AAAA HELP WE FAILED");
}
}
1 change: 1 addition & 0 deletions core/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod commit;
pub use commit::*;
pub mod json;
pub mod junit;
pub mod logging;
pub mod process;
pub mod serde;
Expand Down
1 change: 1 addition & 0 deletions friendshipper/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ k8s-openapi = { version = "0.20.0", features = ["v1_24"] }
notify = "6.1.1"
notify-debouncer-full = "0.3.1"
futures = "0.3.30"
serde-xml-rs = "0.6.0"

[target.'cfg(target_os = "macos")'.dependencies]
fix-path-env = { git = "https://github.com/tauri-apps/fix-path-env-rs" }
Expand Down
Loading