|
| 1 | +use std::fs; |
1 | 2 | use std::fs::File; |
2 | | -use std::io::Write; |
| 3 | +use std::io::{Read, Write}; |
| 4 | +use std::path::PathBuf; |
3 | 5 |
|
4 | 6 | #[cfg(feature = "junit")] use junit_report::{ReportBuilder, TestCaseBuilder, TestSuiteBuilder}; |
5 | 7 | #[cfg(feature = "junit")] use strip_ansi_escapes; |
6 | 8 | use serde_json::Value; |
7 | 9 | use tracing::debug; |
| 10 | +use xrust::{Error, ErrorKind, Item, Node, SequenceTrait}; |
| 11 | +use xrust::parser::ParseError; |
| 12 | +use xrust::parser::xml::parse; |
| 13 | +use xrust::transform::context::StaticContextBuilder; |
| 14 | +use xrust::trees::smite::RNode; |
| 15 | +use xrust::xslt::from_document; |
8 | 16 |
|
9 | 17 | #[cfg(feature = "junit")] use pact_verifier::{interaction_mismatch_output, MismatchResult}; |
10 | 18 | use pact_verifier::verification_result::VerificationExecutionResult; |
11 | 19 |
|
| 20 | +mod xml; |
| 21 | + |
| 22 | +const XSLT: &str = include_str!("verification-report.xsl"); |
| 23 | + |
| 24 | +fn node_from_str(s: &str) -> Result<RNode, Error> { |
| 25 | + parse(RNode::new_document(), s, Some(|_: &_| Err(ParseError::MissingNameSpace))) |
| 26 | +} |
| 27 | + |
| 28 | +fn apply_xslt(xml_str: &str, xslt: Option<&String>) -> anyhow::Result<String> { |
| 29 | + let doc = parse(RNode::new_document(), xml_str, Some(|_: &_| Err(ParseError::MissingNameSpace)))?; |
| 30 | + |
| 31 | + let mut s = String::new(); |
| 32 | + let xslt_doc = if let Some(xslt) = xslt { |
| 33 | + let mut f = File::open(xslt)?; |
| 34 | + f.read_to_string(&mut s)?; |
| 35 | + s.as_str() |
| 36 | + } else { |
| 37 | + XSLT |
| 38 | + }; |
| 39 | + let xslt_doc = parse(RNode::new_document(), xslt_doc, Some(|_: &_| Err(ParseError::MissingNameSpace)))?; |
| 40 | + |
| 41 | + let mut static_context = StaticContextBuilder::new() |
| 42 | + .message(|_| Ok(())) |
| 43 | + .fetcher(|_| Err(Error::new(ErrorKind::NotImplemented, "not implemented"))) |
| 44 | + .parser(|_| Err::<RNode, Error>(Error::new(ErrorKind::NotImplemented, "not implemented"))) |
| 45 | + .build(); |
| 46 | + |
| 47 | + let mut ctxt = from_document(xslt_doc, None, node_from_str, |_| Ok(String::new()))?; |
| 48 | + ctxt.context(vec![Item::Node(doc)], 0); |
| 49 | + ctxt.result_document(RNode::new_document()); |
| 50 | + let seq = ctxt.evaluate(&mut static_context)?; |
| 51 | + |
| 52 | + Ok(seq.to_xml()) |
| 53 | +} |
| 54 | + |
12 | 55 | pub(crate) fn write_json_report(result: &VerificationExecutionResult, file_name: &str) -> anyhow::Result<()> { |
13 | 56 | debug!("Writing JSON result of the verification to '{file_name}'"); |
14 | 57 | let mut f = File::create(file_name)?; |
@@ -67,3 +110,41 @@ pub(crate) fn write_junit_report(result: &VerificationExecutionResult, file_name |
67 | 110 | report.write_xml(&mut f)?; |
68 | 111 | Ok(()) |
69 | 112 | } |
| 113 | + |
| 114 | +pub(crate) fn write_html_report( |
| 115 | + result: &VerificationExecutionResult, |
| 116 | + file_name: &str, |
| 117 | + provider: &str, |
| 118 | + xslt: Option<&String> |
| 119 | +) -> anyhow::Result<()> { |
| 120 | + let path = PathBuf::from(file_name); |
| 121 | + let parent = if let Some(parent) = path.parent() { |
| 122 | + parent |
| 123 | + } else { |
| 124 | + return Err(anyhow::anyhow!("No parent directory found for {}", file_name)); |
| 125 | + }; |
| 126 | + fs::create_dir_all(parent)?; |
| 127 | + |
| 128 | + let filename = if let Some(filename) = path.file_name() { |
| 129 | + filename |
| 130 | + } else { |
| 131 | + return Err(anyhow::anyhow!("Failed to get file name of '{}'", path.display())); |
| 132 | + }; |
| 133 | + |
| 134 | + let xml_path = if let Some(_extension) = path.extension() { |
| 135 | + path.with_extension("xml") |
| 136 | + } else { |
| 137 | + parent.join(filename).with_extension("xml") |
| 138 | + }; |
| 139 | + |
| 140 | + let xml_str = xml::to_xml_string(result, provider)?; |
| 141 | + |
| 142 | + debug!("Writing XML report of the verification to '{}'", xml_path.display()); |
| 143 | + File::create(&xml_path)?.write_all(xml_str.as_bytes())?; |
| 144 | + |
| 145 | + debug!("Writing HTML report of the verification to '{file_name}'"); |
| 146 | + let html = apply_xslt(&xml_str, xslt)?; |
| 147 | + File::create(file_name)?.write_all(html.as_bytes())?; |
| 148 | + |
| 149 | + Ok(()) |
| 150 | +} |
0 commit comments