1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00
denoland-deno/cli/tools/test/reporters/tap.rs

266 lines
6.9 KiB
Rust
Raw Normal View History

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use deno_core::serde_json::json;
use deno_core::serde_json::{self};
use serde::Serialize;
use super::common;
use super::fmt::to_relative_path_or_remote_url;
use super::*;
const VERSION_HEADER: &str = "TAP version 14";
/// A test reporter for the Test Anything Protocol as defined at
/// https://testanything.org/tap-version-14-specification.html
pub struct TapTestReporter {
cwd: Url,
is_concurrent: bool,
header: bool,
planned: usize,
n: usize,
step_n: usize,
step_results: HashMap<usize, Vec<(TestStepDescription, TestStepResult)>>,
failure_format_options: TestFailureFormatOptions,
}
#[allow(clippy::print_stdout)]
impl TapTestReporter {
pub fn new(
cwd: Url,
is_concurrent: bool,
failure_format_options: TestFailureFormatOptions,
) -> TapTestReporter {
TapTestReporter {
fix(cli): output more detailed information for steps when using JUnit reporter (#22797) This patch gets JUnit reporter to output more detailed information for test steps (subtests). ## Issue with previous implementation In the previous implementation, the test hierarchy was represented using several XML tags like the following: - `<testsuites>` corresponds to the entire test (one execution of `deno test` has exactly one `<testsuites>` tag) - `<testsuite>` corresponds to one file, such as `main_test.ts` - `<testcase>` corresponds to one `Deno.test(...)` - `<property>` corresponds to one `t.step(...)` This structure describes the test layers but one problem is that `<property>` tag is used for any use cases so some tools that can ingest a JUnit XML file might not be able to interpret `<property>` as subtests. ## How other tools address it Some of the testing frameworks in the ecosystem address this issue by fitting subtests into the `<testcase>` layer. For instance, take a look at the following Go test file: ```go package main_test import "testing" func TestMain(t *testing.T) { t.Run("child 1", func(t *testing.T) { // OK }) t.Run("child 2", func(t *testing.T) { // Error t.Fatal("error") }) } ``` Running [gotestsum], we can get the output like this: ```xml <?xml version="1.0" encoding="UTF-8"?> <testsuites tests="3" failures="2" errors="0" time="1.013694"> <testsuite tests="3" failures="2" time="0.510000" name="example/gosumtest" timestamp="2024-03-11T12:26:39+09:00"> <properties> <property name="go.version" value="go1.22.1 darwin/arm64"></property> </properties> <testcase classname="example/gosumtest" name="TestMain/child_2" time="0.000000"> <failure message="Failed" type="">=== RUN TestMain/child_2&#xA; main_test.go:12: error&#xA;--- FAIL: TestMain/child_2 (0.00s)&#xA;</failure> </testcase> <testcase classname="example/gosumtest" name="TestMain" time="0.000000"> <failure message="Failed" type="">=== RUN TestMain&#xA;--- FAIL: TestMain (0.00s)&#xA;</failure> </testcase> <testcase classname="example/gosumtest" name="TestMain/child_1" time="0.000000"></testcase> </testsuite> </testsuites> ``` This output shows that nested test cases are squashed into the `<testcase>` layer by treating them as the same layer as their parent, `TestMain`. We can still distinguish nested ones by their `name` attributes that look like `TestMain/<subtest_name>`. As described in #22795, [vitest] solves the issue in the same way as [gotestsum]. One downside of this would be that one test failure that happens in a nested test case will end up being counted multiple times, because not only the subtest but also its wrapping container(s) are considered to be failures. In fact, in the [gotestsum] output above, `TestMain/child_2` failed (which is totally expected) while its parent, `TestMain`, was also counted as failure. As https://github.com/denoland/deno/pull/20273#discussion_r1307558757 pointed out, there is a test runner that offers flexibility to prevent this, but I personally don't think the "duplicate failure count" issue is a big deal. ## How to fix the issue in this patch This patch fixes the issue with the same approach as [gotestsum] and [vitest]. More specifically, nested test cases are put into the `<testcase>` level and their names are now represented as squashed test names concatenated by `>` (e.g. `parent 2 > child 1 > grandchild 1`). This change also allows us to put a detailed error message as `<failure>` tag within the `<testcase>` tag, which should be handled nicely by third-party tools supporting JUnit XML. ## Extra fix Also, file paths embedded into XML outputs are changed from absolute path to relative path, which is helpful when running the test suites in several different environments like CI. Resolves #22795 [gotestsum]: https://github.com/gotestyourself/gotestsum [vitest]: https://vitest.dev/ --------- Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
2024-03-25 11:08:46 -04:00
cwd,
is_concurrent,
header: false,
planned: 0,
n: 0,
step_n: 0,
step_results: HashMap::new(),
failure_format_options,
}
}
fn escape_description(description: &str) -> String {
description
.replace('\\', "\\\\")
.replace('\n', "\\n")
.replace('\r', "\\r")
.replace('#', "\\#")
}
fn print_diagnostic(
&self,
indent: usize,
failure: &TestFailure,
location: DiagnosticLocation,
) {
// Unspecified behaviour:
// The diagnostic schema is not specified by the TAP spec,
// but there is an example, so we use it.
// YAML is a superset of JSON, so we can avoid a YAML dependency here.
// This makes the output less readable though.
let diagnostic = serde_json::to_string(&json!({
"message": failure.format(&self.failure_format_options),
"severity": "fail".to_string(),
"at": location,
}))
.expect("failed to serialize TAP diagnostic");
println!("{:indent$} ---", "", indent = indent);
println!("{:indent$} {}", "", diagnostic, indent = indent);
println!("{:indent$} ...", "", indent = indent);
}
fn print_line(
indent: usize,
status: &str,
step: usize,
description: &str,
directive: &str,
) {
println!(
"{:indent$}{} {} - {}{}",
"",
status,
step,
Self::escape_description(description),
directive,
indent = indent
);
}
fn print_step_result(
&mut self,
desc: &TestStepDescription,
result: &TestStepResult,
) {
if self.step_n == 0 {
println!("# Subtest: {}", desc.root_name)
}
let (status, directive) = match result {
TestStepResult::Ok => ("ok", ""),
TestStepResult::Ignored => ("ok", " # SKIP"),
TestStepResult::Failed(_failure) => ("not ok", ""),
};
self.step_n += 1;
Self::print_line(4, status, self.step_n, &desc.name, directive);
if let TestStepResult::Failed(failure) = result {
self.print_diagnostic(
4,
failure,
DiagnosticLocation {
file: to_relative_path_or_remote_url(&self.cwd, &desc.origin),
line: desc.location.line_number,
},
);
}
}
}
#[allow(clippy::print_stdout)]
impl TestReporter for TapTestReporter {
fn report_register(&mut self, _description: &TestDescription) {}
fn report_plan(&mut self, plan: &TestPlan) {
if !self.header {
println!("{}", VERSION_HEADER);
self.header = true;
}
self.planned += plan.total;
if !self.is_concurrent {
// Unspecified behavior: Consumers tend to interpret a comment as a test suite name.
// During concurrent execution these would not correspond to the actual test file, so skip them.
println!(
"# {}",
to_relative_path_or_remote_url(&self.cwd, &plan.origin)
)
}
}
fn report_wait(&mut self, _description: &TestDescription) {
// flush for faster feedback when line buffered
std::io::stdout().flush().unwrap();
}
fn report_slow(&mut self, _description: &TestDescription, _elapsed: u64) {}
fn report_output(&mut self, _output: &[u8]) {}
fn report_result(
&mut self,
description: &TestDescription,
result: &TestResult,
_elapsed: u64,
) {
if self.is_concurrent {
let results = self.step_results.remove(&description.id);
for (desc, result) in results.iter().flat_map(|v| v.iter()) {
self.print_step_result(desc, result);
}
}
if self.step_n != 0 {
println!(" 1..{}", self.step_n);
self.step_n = 0;
}
let (status, directive) = match result {
TestResult::Ok => ("ok", ""),
TestResult::Ignored => ("ok", " # SKIP"),
TestResult::Failed(_failure) => ("not ok", ""),
TestResult::Cancelled => ("not ok", ""),
};
self.n += 1;
Self::print_line(0, status, self.n, &description.name, directive);
if let TestResult::Failed(failure) = result {
self.print_diagnostic(
0,
failure,
DiagnosticLocation {
file: to_relative_path_or_remote_url(&self.cwd, &description.origin),
line: description.location.line_number,
},
);
}
}
fn report_uncaught_error(&mut self, _origin: &str, _errorr: Box<JsError>) {}
fn report_step_register(&mut self, _description: &TestStepDescription) {}
fn report_step_wait(&mut self, _description: &TestStepDescription) {
// flush for faster feedback when line buffered
std::io::stdout().flush().unwrap();
}
fn report_step_result(
&mut self,
desc: &TestStepDescription,
result: &TestStepResult,
_elapsed: u64,
_tests: &IndexMap<usize, TestDescription>,
_test_steps: &IndexMap<usize, TestStepDescription>,
) {
if self.is_concurrent {
// All subtests must be reported immediately before the parent test.
// So during concurrent execution we need to defer printing the results.
// TODO(SyrupThinker) This only outputs one level of subtests, it could support multiple.
self
.step_results
.entry(desc.root_id)
.or_default()
.push((desc.clone(), result.clone()));
return;
}
self.print_step_result(desc, result);
}
fn report_summary(
&mut self,
_elapsed: &Duration,
_tests: &IndexMap<usize, TestDescription>,
_test_steps: &IndexMap<usize, TestStepDescription>,
) {
println!("1..{}", self.planned);
}
fn report_sigint(
&mut self,
tests_pending: &HashSet<usize>,
tests: &IndexMap<usize, TestDescription>,
test_steps: &IndexMap<usize, TestStepDescription>,
) {
println!("Bail out! SIGINT received.");
common::report_sigint(
&mut std::io::stdout(),
&self.cwd,
tests_pending,
tests,
test_steps,
);
}
fn report_completed(&mut self) {}
fn flush_report(
&mut self,
_elapsed: &Duration,
_tests: &IndexMap<usize, TestDescription>,
_test_steps: &IndexMap<usize, TestStepDescription>,
) -> anyhow::Result<()> {
Ok(())
}
}
#[derive(Serialize)]
struct DiagnosticLocation {
file: String,
line: u32,
}