From 38485a9e3445b1e926da293448fd056ca78cf156 Mon Sep 17 00:00:00 2001 From: Andrey Cherkashin Date: Sun, 25 Apr 2021 14:29:24 -0700 Subject: [PATCH 1/3] feat(libtest): Add JUnit formatter --- library/test/src/cli.rs | 14 ++- library/test/src/console.rs | 3 +- library/test/src/formatters/junit.rs | 134 +++++++++++++++++++++++++++ library/test/src/formatters/mod.rs | 2 + library/test/src/options.rs | 2 + 5 files changed, 150 insertions(+), 5 deletions(-) create mode 100644 library/test/src/formatters/junit.rs diff --git a/library/test/src/cli.rs b/library/test/src/cli.rs index b7791b1b24d45..84874a2d2254a 100644 --- a/library/test/src/cli.rs +++ b/library/test/src/cli.rs @@ -95,8 +95,9 @@ fn optgroups() -> getopts::Options { "Configure formatting of output: pretty = Print verbose output; terse = Display one character per test; - json = Output a json document", - "pretty|terse|json", + json = Output a json document; + junit = Output a JUnit document", + "pretty|terse|json|junit", ) .optflag("", "show-output", "Show captured stdout of successful tests") .optopt( @@ -336,10 +337,15 @@ fn get_format( } OutputFormat::Json } - + Some("junit") => { + if !allow_unstable { + return Err("The \"junit\" format is only accepted on the nightly compiler".into()); + } + OutputFormat::Junit + } Some(v) => { return Err(format!( - "argument for --format must be pretty, terse, or json (was \ + "argument for --format must be pretty, terse, json or junit (was \ {})", v )); diff --git a/library/test/src/console.rs b/library/test/src/console.rs index 1721c3c14f9a9..9cfc7eaf4bcf4 100644 --- a/library/test/src/console.rs +++ b/library/test/src/console.rs @@ -10,7 +10,7 @@ use super::{ cli::TestOpts, event::{CompletedTest, TestEvent}, filter_tests, - formatters::{JsonFormatter, OutputFormatter, PrettyFormatter, TerseFormatter}, + formatters::{JsonFormatter, JunitFormatter, OutputFormatter, PrettyFormatter, TerseFormatter}, helpers::{concurrency::get_concurrency, metrics::MetricMap}, options::{Options, OutputFormat}, run_tests, @@ -277,6 +277,7 @@ pub fn run_tests_console(opts: &TestOpts, tests: Vec) -> io::Resu Box::new(TerseFormatter::new(output, opts.use_color(), max_name_len, is_multithreaded)) } OutputFormat::Json => Box::new(JsonFormatter::new(output)), + OutputFormat::Junit => Box::new(JunitFormatter::new(output)), }; let mut st = ConsoleTestState::new(opts)?; diff --git a/library/test/src/formatters/junit.rs b/library/test/src/formatters/junit.rs new file mode 100644 index 0000000000000..1d3c4ab604c3b --- /dev/null +++ b/library/test/src/formatters/junit.rs @@ -0,0 +1,134 @@ +use std::io::{self, prelude::Write}; +use std::time::Duration; + +use super::OutputFormatter; +use crate::{ + console::{ConsoleTestState, OutputLocation}, + test_result::TestResult, + time, + types::TestDesc, +}; + +pub struct JunitFormatter { + out: OutputLocation, + results: Vec<(TestDesc, TestResult, Duration)>, +} + +impl JunitFormatter { + pub fn new(out: OutputLocation) -> Self { + Self { out, results: Vec::new() } + } + + fn write_message(&mut self, s: &str) -> io::Result<()> { + assert!(!s.contains('\n')); + + self.out.write_all(s.as_ref()) + } +} + +impl OutputFormatter for JunitFormatter { + fn write_run_start(&mut self, _test_count: usize) -> io::Result<()> { + // We write xml header on run start + self.write_message(&"") + } + + fn write_test_start(&mut self, _desc: &TestDesc) -> io::Result<()> { + // We do not output anything on test start. + Ok(()) + } + + fn write_timeout(&mut self, _desc: &TestDesc) -> io::Result<()> { + // We do not output anything on test timeout. + Ok(()) + } + + fn write_result( + &mut self, + desc: &TestDesc, + result: &TestResult, + exec_time: Option<&time::TestExecTime>, + _stdout: &[u8], + _state: &ConsoleTestState, + ) -> io::Result<()> { + // Because testsuit node holds some of the information as attributes, we can't write it + // until all of the tests has ran. Instead of writting every result as they come in, we add + // them to a Vec and write them all at once when run is complete. + let duration = exec_time.map(|t| t.0.clone()).unwrap_or_default(); + self.results.push((desc.clone(), result.clone(), duration)); + Ok(()) + } + fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result { + self.write_message("")?; + + self.write_message(&*format!( + "", + state.failed, state.total, state.ignored + ))?; + for (desc, result, duration) in std::mem::replace(&mut self.results, Vec::new()) { + match result { + TestResult::TrIgnored => { /* no-op */ } + TestResult::TrFailed => { + self.write_message(&*format!( + "", + desc.name.as_slice(), + duration.as_secs() + ))?; + self.write_message("")?; + self.write_message("")?; + } + + TestResult::TrFailedMsg(ref m) => { + self.write_message(&*format!( + "", + desc.name.as_slice(), + duration.as_secs() + ))?; + self.write_message(&*format!("", m))?; + self.write_message("")?; + } + + TestResult::TrTimedFail => { + self.write_message(&*format!( + "", + desc.name.as_slice(), + duration.as_secs() + ))?; + self.write_message("")?; + self.write_message("")?; + } + + TestResult::TrBench(ref b) => { + self.write_message(&*format!( + "", + desc.name.as_slice(), + b.ns_iter_summ.sum + ))?; + } + + TestResult::TrOk | TestResult::TrAllowedFail => { + self.write_message(&*format!( + "", + desc.name.as_slice(), + duration.as_secs() + ))?; + } + } + } + self.write_message("")?; + self.write_message("")?; + self.write_message("")?; + self.write_message("")?; + + Ok(state.failed == 0) + } +} diff --git a/library/test/src/formatters/mod.rs b/library/test/src/formatters/mod.rs index 1fb840520a656..2e03581b3af3a 100644 --- a/library/test/src/formatters/mod.rs +++ b/library/test/src/formatters/mod.rs @@ -8,10 +8,12 @@ use crate::{ }; mod json; +mod junit; mod pretty; mod terse; pub(crate) use self::json::JsonFormatter; +pub(crate) use self::junit::JunitFormatter; pub(crate) use self::pretty::PrettyFormatter; pub(crate) use self::terse::TerseFormatter; diff --git a/library/test/src/options.rs b/library/test/src/options.rs index 8e7bd8de92499..baf36b5f1d85e 100644 --- a/library/test/src/options.rs +++ b/library/test/src/options.rs @@ -39,6 +39,8 @@ pub enum OutputFormat { Terse, /// JSON output Json, + /// JUnit output + Junit, } /// Whether ignored test should be run or not From 4b4d06ae829ef01de7cb4c0048db5c7577094ba5 Mon Sep 17 00:00:00 2001 From: Andrey Cherkashin <148123+andoriyu@users.noreply.github.com> Date: Mon, 26 Apr 2021 11:42:13 -0700 Subject: [PATCH 2/3] Update junit.rs --- library/test/src/formatters/junit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/test/src/formatters/junit.rs b/library/test/src/formatters/junit.rs index 1d3c4ab604c3b..0ec336889dee9 100644 --- a/library/test/src/formatters/junit.rs +++ b/library/test/src/formatters/junit.rs @@ -50,7 +50,7 @@ impl OutputFormatter for JunitFormatter { _stdout: &[u8], _state: &ConsoleTestState, ) -> io::Result<()> { - // Because testsuit node holds some of the information as attributes, we can't write it + // Because the testsuit node holds some of the information as attributes, we can't write it // until all of the tests has ran. Instead of writting every result as they come in, we add // them to a Vec and write them all at once when run is complete. let duration = exec_time.map(|t| t.0.clone()).unwrap_or_default(); From 9f83e2290a978ef448567e55548a192f8b8f1f69 Mon Sep 17 00:00:00 2001 From: Andrey Cherkashin Date: Fri, 30 Apr 2021 17:16:09 -0700 Subject: [PATCH 3/3] Better output for junit formatter --- library/test/src/formatters/junit.rs | 64 ++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/library/test/src/formatters/junit.rs b/library/test/src/formatters/junit.rs index 0ec336889dee9..ec66fc1219ff7 100644 --- a/library/test/src/formatters/junit.rs +++ b/library/test/src/formatters/junit.rs @@ -6,7 +6,7 @@ use crate::{ console::{ConsoleTestState, OutputLocation}, test_result::TestResult, time, - types::TestDesc, + types::{TestDesc, TestType}, }; pub struct JunitFormatter { @@ -70,13 +70,15 @@ impl OutputFormatter for JunitFormatter { state.failed, state.total, state.ignored ))?; for (desc, result, duration) in std::mem::replace(&mut self.results, Vec::new()) { + let (class_name, test_name) = parse_class_name(&desc); match result { TestResult::TrIgnored => { /* no-op */ } TestResult::TrFailed => { self.write_message(&*format!( - "", - desc.name.as_slice(), + class_name, + test_name, duration.as_secs() ))?; self.write_message("")?; @@ -85,9 +87,10 @@ impl OutputFormatter for JunitFormatter { TestResult::TrFailedMsg(ref m) => { self.write_message(&*format!( - "", - desc.name.as_slice(), + class_name, + test_name, duration.as_secs() ))?; self.write_message(&*format!("", m))?; @@ -96,9 +99,10 @@ impl OutputFormatter for JunitFormatter { TestResult::TrTimedFail => { self.write_message(&*format!( - "", - desc.name.as_slice(), + class_name, + test_name, duration.as_secs() ))?; self.write_message("")?; @@ -107,18 +111,18 @@ impl OutputFormatter for JunitFormatter { TestResult::TrBench(ref b) => { self.write_message(&*format!( - "", - desc.name.as_slice(), - b.ns_iter_summ.sum + class_name, test_name, b.ns_iter_summ.sum ))?; } TestResult::TrOk | TestResult::TrAllowedFail => { self.write_message(&*format!( - "", - desc.name.as_slice(), + class_name, + test_name, duration.as_secs() ))?; } @@ -132,3 +136,39 @@ impl OutputFormatter for JunitFormatter { Ok(state.failed == 0) } } + +fn parse_class_name(desc: &TestDesc) -> (String, String) { + match desc.test_type { + TestType::UnitTest => parse_class_name_unit(desc), + TestType::DocTest => parse_class_name_doc(desc), + TestType::IntegrationTest => parse_class_name_integration(desc), + TestType::Unknown => (String::from("unknown"), String::from(desc.name.as_slice())), + } +} + +fn parse_class_name_unit(desc: &TestDesc) -> (String, String) { + // Module path => classname + // Function name => name + let module_segments: Vec<&str> = desc.name.as_slice().split("::").collect(); + let (class_name, test_name) = match module_segments[..] { + [test] => (String::from("crate"), String::from(test)), + [ref path @ .., test] => (path.join("::"), String::from(test)), + [..] => unreachable!(), + }; + (class_name, test_name) +} + +fn parse_class_name_doc(desc: &TestDesc) -> (String, String) { + // File path => classname + // Line # => test name + let segments: Vec<&str> = desc.name.as_slice().split(" - ").collect(); + let (class_name, test_name) = match segments[..] { + [file, line] => (String::from(file.trim()), String::from(line.trim())), + [..] => unreachable!(), + }; + (class_name, test_name) +} + +fn parse_class_name_integration(desc: &TestDesc) -> (String, String) { + (String::from("integration"), String::from(desc.name.as_slice())) +}