2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-08-02 12:38:10 -04:00
|
|
|
|
|
|
|
use super::common;
|
|
|
|
use super::fmt::to_relative_path_or_remote_url;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub struct DotTestReporter {
|
|
|
|
n: usize,
|
|
|
|
width: usize,
|
|
|
|
cwd: Url,
|
|
|
|
summary: TestSummary,
|
|
|
|
}
|
|
|
|
|
2024-05-08 22:45:06 -04:00
|
|
|
#[allow(clippy::print_stdout)]
|
2023-08-02 12:38:10 -04:00
|
|
|
impl DotTestReporter {
|
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
 main_test.go:12: error
--- FAIL: TestMain/child_2 (0.00s)
</failure>
</testcase>
<testcase classname="example/gosumtest" name="TestMain" time="0.000000">
<failure message="Failed" type="">=== RUN TestMain
--- FAIL: TestMain (0.00s)
</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
|
|
|
pub fn new(cwd: Url) -> DotTestReporter {
|
2023-08-02 12:38:10 -04:00
|
|
|
let console_width = if let Some(size) = crate::util::console::console_size()
|
|
|
|
{
|
|
|
|
size.cols as usize
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
let console_width = (console_width as f32 * 0.8) as usize;
|
|
|
|
DotTestReporter {
|
|
|
|
n: 0,
|
|
|
|
width: console_width,
|
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
 main_test.go:12: error
--- FAIL: TestMain/child_2 (0.00s)
</failure>
</testcase>
<testcase classname="example/gosumtest" name="TestMain" time="0.000000">
<failure message="Failed" type="">=== RUN TestMain
--- FAIL: TestMain (0.00s)
</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,
|
2023-08-02 12:38:10 -04:00
|
|
|
summary: TestSummary::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_status(&mut self, status: String) {
|
|
|
|
// Non-TTY console prints every result on a separate line.
|
|
|
|
if self.width == 0 {
|
|
|
|
println!("{}", status);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.n != 0 && self.n % self.width == 0 {
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
self.n += 1;
|
|
|
|
|
|
|
|
print!("{}", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_test_step_result(&mut self, result: &TestStepResult) {
|
|
|
|
let status = match result {
|
|
|
|
TestStepResult::Ok => fmt_ok(),
|
|
|
|
TestStepResult::Ignored => fmt_ignored(),
|
|
|
|
TestStepResult::Failed(_failure) => fmt_failed(),
|
|
|
|
};
|
|
|
|
self.print_status(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_test_result(&mut self, result: &TestResult) {
|
|
|
|
let status = match result {
|
|
|
|
TestResult::Ok => fmt_ok(),
|
|
|
|
TestResult::Ignored => fmt_ignored(),
|
|
|
|
TestResult::Failed(_failure) => fmt_failed(),
|
|
|
|
TestResult::Cancelled => fmt_cancelled(),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.print_status(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fmt_ok() -> String {
|
|
|
|
colors::gray(".").to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fmt_ignored() -> String {
|
|
|
|
colors::cyan(",").to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fmt_failed() -> String {
|
|
|
|
colors::red_bold("!").to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fmt_cancelled() -> String {
|
|
|
|
colors::gray("!").to_string()
|
|
|
|
}
|
|
|
|
|
2024-05-08 22:45:06 -04:00
|
|
|
#[allow(clippy::print_stdout)]
|
2023-08-02 12:38:10 -04:00
|
|
|
impl TestReporter for DotTestReporter {
|
|
|
|
fn report_register(&mut self, _description: &TestDescription) {}
|
|
|
|
|
|
|
|
fn report_plan(&mut self, plan: &TestPlan) {
|
|
|
|
self.summary.total += plan.total;
|
|
|
|
self.summary.filtered_out += plan.filtered_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_wait(&mut self, _description: &TestDescription) {
|
|
|
|
// flush for faster feedback when line buffered
|
|
|
|
std::io::stdout().flush().unwrap();
|
|
|
|
}
|
|
|
|
|
2024-05-22 10:08:27 -04:00
|
|
|
fn report_slow(&mut self, _description: &TestDescription, _elapsed: u64) {}
|
2023-08-02 12:38:10 -04:00
|
|
|
fn report_output(&mut self, _output: &[u8]) {}
|
|
|
|
|
|
|
|
fn report_result(
|
|
|
|
&mut self,
|
|
|
|
description: &TestDescription,
|
|
|
|
result: &TestResult,
|
|
|
|
_elapsed: u64,
|
|
|
|
) {
|
|
|
|
match &result {
|
|
|
|
TestResult::Ok => {
|
|
|
|
self.summary.passed += 1;
|
|
|
|
}
|
|
|
|
TestResult::Ignored => {
|
|
|
|
self.summary.ignored += 1;
|
|
|
|
}
|
|
|
|
TestResult::Failed(failure) => {
|
|
|
|
self.summary.failed += 1;
|
|
|
|
self
|
|
|
|
.summary
|
|
|
|
.failures
|
2024-02-05 12:27:17 -05:00
|
|
|
.push((description.into(), failure.clone()));
|
2023-08-02 12:38:10 -04:00
|
|
|
}
|
|
|
|
TestResult::Cancelled => {
|
|
|
|
self.summary.failed += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.print_test_result(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_uncaught_error(&mut self, origin: &str, error: Box<JsError>) {
|
|
|
|
self.summary.failed += 1;
|
|
|
|
self
|
|
|
|
.summary
|
|
|
|
.uncaught_errors
|
|
|
|
.push((origin.to_string(), error));
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"Uncaught error from {} {}",
|
|
|
|
to_relative_path_or_remote_url(&self.cwd, origin),
|
|
|
|
colors::red("FAILED")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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>,
|
|
|
|
) {
|
|
|
|
match &result {
|
|
|
|
TestStepResult::Ok => {
|
|
|
|
self.summary.passed_steps += 1;
|
|
|
|
}
|
|
|
|
TestStepResult::Ignored => {
|
|
|
|
self.summary.ignored_steps += 1;
|
|
|
|
}
|
|
|
|
TestStepResult::Failed(failure) => {
|
|
|
|
self.summary.failed_steps += 1;
|
|
|
|
self.summary.failures.push((
|
2024-02-05 12:27:17 -05:00
|
|
|
TestFailureDescription {
|
2023-08-02 12:38:10 -04:00
|
|
|
id: desc.id,
|
|
|
|
name: common::format_test_step_ancestry(desc, tests, test_steps),
|
|
|
|
origin: desc.origin.clone(),
|
|
|
|
location: desc.location.clone(),
|
|
|
|
},
|
|
|
|
failure.clone(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.print_test_step_result(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_summary(
|
|
|
|
&mut self,
|
|
|
|
elapsed: &Duration,
|
|
|
|
_tests: &IndexMap<usize, TestDescription>,
|
|
|
|
_test_steps: &IndexMap<usize, TestStepDescription>,
|
|
|
|
) {
|
2023-10-05 06:25:15 -04:00
|
|
|
common::report_summary(
|
|
|
|
&mut std::io::stdout(),
|
|
|
|
&self.cwd,
|
|
|
|
&self.summary,
|
|
|
|
elapsed,
|
|
|
|
);
|
2023-10-05 16:45:11 -04:00
|
|
|
println!();
|
2023-08-02 12:38:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn report_sigint(
|
|
|
|
&mut self,
|
|
|
|
tests_pending: &HashSet<usize>,
|
|
|
|
tests: &IndexMap<usize, TestDescription>,
|
|
|
|
test_steps: &IndexMap<usize, TestStepDescription>,
|
|
|
|
) {
|
2023-10-05 06:25:15 -04:00
|
|
|
common::report_sigint(
|
|
|
|
&mut std::io::stdout(),
|
|
|
|
&self.cwd,
|
|
|
|
tests_pending,
|
|
|
|
tests,
|
|
|
|
test_steps,
|
|
|
|
);
|
2023-08-02 12:38:10 -04:00
|
|
|
}
|
|
|
|
|
2024-02-28 17:12:21 -05:00
|
|
|
fn report_completed(&mut self) {}
|
|
|
|
|
2023-08-02 12:38:10 -04:00
|
|
|
fn flush_report(
|
|
|
|
&mut self,
|
|
|
|
_elapsed: &Duration,
|
|
|
|
_tests: &IndexMap<usize, TestDescription>,
|
|
|
|
_test_steps: &IndexMap<usize, TestStepDescription>,
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|