Skip to content

Commit

Permalink
feat: Add crate feature for JUnit report output
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Jul 12, 2023
1 parent 7a25a81 commit b9e034b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
14 changes: 11 additions & 3 deletions rust/pact_verifier_cli/Cargo.toml
Expand Up @@ -13,16 +13,24 @@ exclude = [
"*.iml"
]

[features]
default = ["datetime", "xml", "plugins", "multipart", "junit"]
datetime = ["pact_models/datetime", "pact_verifier/datetime"] # Support for date/time matchers and expressions
xml = ["pact_models/xml", "pact_verifier/xml"] # support for matching XML documents
plugins = ["pact_verifier/plugins"]
multipart = ["pact_verifier/multipart"] # suport for MIME multipart bodies
junit = ["dep:junit-report"] # suport for MIME multipart bodies

[dependencies]
ansi_term = "0.12.1"
anyhow = "1.0.70"
clap = { version = "4.2.1", features = ["cargo", "env"] }
env_logger = "0.10.0"
junit-report = "0.8.2"
junit-report = { version = "0.8.2", optional = true }
log = "0.4.17"
maplit = "1.0.2"
pact_models = "~1.1.8"
pact_verifier = { version = "~1.0.1", path = "../pact_verifier" }
pact_models = { version = "~1.1.8", default-features = false }
pact_verifier = { version = "~1.0.1", path = "../pact_verifier", default-features = false }
regex = "1.7.3"
reqwest = { version = "0.11.16", default-features = false, features = ["rustls-tls-native-roots", "blocking", "json"] }
serde_json = "1.0.95"
Expand Down
2 changes: 1 addition & 1 deletion rust/pact_verifier_cli/src/args.rs
Expand Up @@ -77,7 +77,7 @@ pub(crate) fn setup_app() -> Command {
.long("junit")
.action(ArgAction::Set)
.value_parser(NonEmptyStringValueParser::new())
.help("Generate a JUnit XML report of the verification"))
.help("Generate a JUnit XML report of the verification (requires the junit feature)"))
.arg(Arg::new("no-colour")
.long("no-colour")
.action(ArgAction::SetTrue)
Expand Down
10 changes: 7 additions & 3 deletions rust/pact_verifier_cli/src/main.rs
Expand Up @@ -400,11 +400,15 @@ async fn handle_matches(matches: &ArgMatches) -> Result<(), i32> {
}
}

if let Some(junit_file) = matches.get_one::<String>("junit-file") {
if let Err(err) = reports::write_junit_report(&result, junit_file.as_str(), &provider_name) {
error!("Failed to write JUnit report to '{junit_file}' - {err}");
if let Some(_junit_file) = matches.get_one::<String>("junit-file") {
#[cfg(feature = "junit")]
if let Err(err) = reports::write_junit_report(&result, _junit_file.as_str(), &provider_name) {
error!("Failed to write JUnit report to '{_junit_file}' - {err}");
return Err(2)
}

#[cfg(not(feature = "junit"))]
warn!("junit feature is not enabled, ignoring junit-file option");
}

if result.result { Ok(()) } else { Err(1) }
Expand Down
5 changes: 3 additions & 2 deletions rust/pact_verifier_cli/src/reports.rs
@@ -1,11 +1,11 @@
use std::fs::File;
use std::io::Write;

use junit_report::{ReportBuilder, TestCaseBuilder, TestSuiteBuilder};
#[cfg(feature = "junit")] use junit_report::{ReportBuilder, TestCaseBuilder, TestSuiteBuilder};
use serde_json::Value;
use tracing::debug;

use pact_verifier::{interaction_mismatch_output, MismatchResult};
#[cfg(feature = "junit")] use pact_verifier::{interaction_mismatch_output, MismatchResult};
use pact_verifier::verification_result::VerificationExecutionResult;

pub(crate) fn write_json_report(result: &VerificationExecutionResult, file_name: &str) -> anyhow::Result<()> {
Expand All @@ -16,6 +16,7 @@ pub(crate) fn write_json_report(result: &VerificationExecutionResult, file_name:
Ok(())
}

#[cfg(feature = "junit")]
pub(crate) fn write_junit_report(result: &VerificationExecutionResult, file_name: &str, provider: &String) -> anyhow::Result<()> {
debug!("Writing JUnit result of the verification to '{file_name}'");
let mut f = File::create(file_name)?;
Expand Down
2 changes: 1 addition & 1 deletion rust/pact_verifier_cli/tests/cmd/main.stderr
Expand Up @@ -12,7 +12,7 @@ Logging options:
--full-log This emits human-readable, single-line logs for each event that occurs, with the current span context displayed before the formatted representation of the event.
--compact-log Emit logs optimized for short line lengths.
-j, --json <json-file> Generate a JSON report of the verification
-x, --junit <junit-file> Generate a JUnit XML report of the verification
-x, --junit <junit-file> Generate a JUnit XML report of the verification (requires the junit feature)
--no-colour Disables ANSI escape codes in the output [aliases: no-color]

Loading pacts options:
Expand Down

0 comments on commit b9e034b

Please sign in to comment.