Skip to content

Commit

Permalink
fix(pact_verifier_cli): strip ANSI escapes from JUnit output
Browse files Browse the repository at this point in the history
When coloured output is enabled, output strings contain ANSI escapes,
sequences of binary data that communicate with a terminal to control
colours. Remove these sequences when producing a JUnit report, because
the ESC bytecode (0x1B) is not a valid codepoint in XML documents.
  • Loading branch information
mjpieters committed May 22, 2024
1 parent 260a91d commit 0b759de
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions rust/Cargo.lock

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

5 changes: 3 additions & 2 deletions rust/pact_verifier_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ 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
multipart = ["pact_verifier/multipart"] # support for MIME multipart bodies
junit = ["dep:junit-report", "dep:strip-ansi-escapes"] # support for Junit format reports

[dependencies]
ansi_term = "0.12.1"
Expand All @@ -34,6 +34,7 @@ pact_verifier = { version = "~1.2.1", path = "../pact_verifier", default-feature
regex = "1.10.2"
reqwest = { version = "0.12.4", default-features = false, features = ["rustls-tls-native-roots", "blocking", "json"] }
serde_json = "1.0.108"
strip-ansi-escapes = { version = "0.2.0", optional = true }
time = "0.3.31"
tokio = { version = "1.35.1", features = ["full"] }
tracing = "0.1.40"
Expand Down
7 changes: 5 additions & 2 deletions rust/pact_verifier_cli/src/reports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fs::File;
use std::io::Write;

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

Expand All @@ -22,7 +23,8 @@ pub(crate) fn write_junit_report(result: &VerificationExecutionResult, file_name
let mut f = File::create(file_name)?;

let mut test_suite = TestSuiteBuilder::new(provider);
test_suite.set_system_out(result.output.join("\n").as_str());
let stripped_system_out = strip_ansi_escapes::strip_str(result.output.join("\n"));
test_suite.set_system_out(&stripped_system_out);
for interaction_result in &result.interaction_results {
let duration = time::Duration::try_from(interaction_result.duration).unwrap_or_default();
let test_case = match &interaction_result.result {
Expand All @@ -42,7 +44,8 @@ pub(crate) fn write_junit_report(result: &VerificationExecutionResult, file_name
"",
"Verification for interaction failed"
);
builder.set_system_out(output_buffer.join("\n").as_str());
let stripped_output = strip_ansi_escapes::strip_str(output_buffer.join("\n"));
builder.set_system_out(&stripped_output);
builder
},
MismatchResult::Error(error, _) => TestCaseBuilder::error(
Expand Down

0 comments on commit 0b759de

Please sign in to comment.