2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-07-26 20:16:12 -04:00
|
|
|
|
2023-08-02 12:38:10 -04:00
|
|
|
use super::common;
|
|
|
|
use super::fmt::to_relative_path_or_remote_url;
|
2023-07-26 20:16:12 -04:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub struct PrettyTestReporter {
|
|
|
|
parallel: bool,
|
|
|
|
echo_output: bool,
|
|
|
|
in_new_line: bool,
|
2024-02-28 17:12:21 -05:00
|
|
|
phase: &'static str,
|
2023-09-06 08:54:21 -04:00
|
|
|
filter: bool,
|
2023-10-05 06:25:15 -04:00
|
|
|
repl: bool,
|
2023-07-26 20:16:12 -04:00
|
|
|
scope_test_id: Option<usize>,
|
|
|
|
cwd: Url,
|
|
|
|
did_have_user_output: bool,
|
|
|
|
started_tests: bool,
|
2024-02-28 17:12:21 -05:00
|
|
|
ended_tests: bool,
|
2023-07-26 20:16:12 -04:00
|
|
|
child_results_buffer:
|
|
|
|
HashMap<usize, IndexMap<usize, (TestStepDescription, TestStepResult, u64)>>,
|
|
|
|
summary: TestSummary,
|
2023-10-05 06:25:15 -04:00
|
|
|
writer: Box<dyn std::io::Write>,
|
2023-07-26 20:16:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PrettyTestReporter {
|
2023-09-06 08:54:21 -04:00
|
|
|
pub fn new(
|
|
|
|
parallel: bool,
|
|
|
|
echo_output: bool,
|
|
|
|
filter: bool,
|
2023-10-05 06:25:15 -04:00
|
|
|
repl: bool,
|
2023-09-06 08:54:21 -04:00
|
|
|
) -> PrettyTestReporter {
|
2023-07-26 20:16:12 -04:00
|
|
|
PrettyTestReporter {
|
|
|
|
parallel,
|
|
|
|
echo_output,
|
|
|
|
in_new_line: true,
|
2024-02-28 17:12:21 -05:00
|
|
|
phase: "",
|
2023-09-06 08:54:21 -04:00
|
|
|
filter,
|
2023-10-05 06:25:15 -04:00
|
|
|
repl,
|
2023-07-26 20:16:12 -04:00
|
|
|
scope_test_id: None,
|
|
|
|
cwd: Url::from_directory_path(std::env::current_dir().unwrap()).unwrap(),
|
|
|
|
did_have_user_output: false,
|
|
|
|
started_tests: false,
|
2024-02-28 17:12:21 -05:00
|
|
|
ended_tests: false,
|
2023-07-26 20:16:12 -04:00
|
|
|
child_results_buffer: Default::default(),
|
|
|
|
summary: TestSummary::new(),
|
2023-10-05 06:25:15 -04:00
|
|
|
writer: Box::new(std::io::stdout()),
|
2023-07-26 20:16:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-05 16:45:11 -04:00
|
|
|
pub fn with_writer(self, writer: Box<dyn std::io::Write>) -> Self {
|
|
|
|
Self { writer, ..self }
|
|
|
|
}
|
|
|
|
|
2023-07-26 20:16:12 -04:00
|
|
|
fn force_report_wait(&mut self, description: &TestDescription) {
|
|
|
|
if !self.in_new_line {
|
2023-10-05 06:25:15 -04:00
|
|
|
writeln!(&mut self.writer).unwrap();
|
2023-07-26 20:16:12 -04:00
|
|
|
}
|
|
|
|
if self.parallel {
|
2023-10-05 06:25:15 -04:00
|
|
|
write!(
|
|
|
|
&mut self.writer,
|
2023-07-26 20:16:12 -04:00
|
|
|
"{}",
|
|
|
|
colors::gray(format!(
|
|
|
|
"{} => ",
|
2023-08-02 12:38:10 -04:00
|
|
|
to_relative_path_or_remote_url(&self.cwd, &description.origin)
|
2023-07-26 20:16:12 -04:00
|
|
|
))
|
2023-10-05 06:25:15 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
2023-07-26 20:16:12 -04:00
|
|
|
}
|
2023-10-05 06:25:15 -04:00
|
|
|
write!(&mut self.writer, "{} ...", description.name).unwrap();
|
2023-07-26 20:16:12 -04:00
|
|
|
self.in_new_line = false;
|
|
|
|
// flush for faster feedback when line buffered
|
|
|
|
std::io::stdout().flush().unwrap();
|
|
|
|
self.scope_test_id = Some(description.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn force_report_step_wait(&mut self, description: &TestStepDescription) {
|
|
|
|
self.write_output_end();
|
|
|
|
if !self.in_new_line {
|
2023-10-05 06:25:15 -04:00
|
|
|
writeln!(&mut self.writer).unwrap();
|
2023-07-26 20:16:12 -04:00
|
|
|
}
|
2023-10-05 06:25:15 -04:00
|
|
|
write!(
|
|
|
|
&mut self.writer,
|
|
|
|
"{}{} ...",
|
|
|
|
" ".repeat(description.level),
|
|
|
|
description.name
|
|
|
|
)
|
|
|
|
.unwrap();
|
2023-07-26 20:16:12 -04:00
|
|
|
self.in_new_line = false;
|
|
|
|
// flush for faster feedback when line buffered
|
|
|
|
std::io::stdout().flush().unwrap();
|
|
|
|
self.scope_test_id = Some(description.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn force_report_step_result(
|
|
|
|
&mut self,
|
|
|
|
description: &TestStepDescription,
|
|
|
|
result: &TestStepResult,
|
|
|
|
elapsed: u64,
|
|
|
|
) {
|
|
|
|
self.write_output_end();
|
|
|
|
if self.in_new_line || self.scope_test_id != Some(description.id) {
|
|
|
|
self.force_report_step_wait(description);
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.parallel {
|
|
|
|
let child_results = self
|
|
|
|
.child_results_buffer
|
|
|
|
.remove(&description.id)
|
|
|
|
.unwrap_or_default();
|
|
|
|
for (desc, result, elapsed) in child_results.values() {
|
|
|
|
self.force_report_step_result(desc, result, *elapsed);
|
|
|
|
}
|
|
|
|
if !child_results.is_empty() {
|
|
|
|
self.force_report_step_wait(description);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let status = match &result {
|
|
|
|
TestStepResult::Ok => colors::green("ok").to_string(),
|
|
|
|
TestStepResult::Ignored => colors::yellow("ignored").to_string(),
|
|
|
|
TestStepResult::Failed(failure) => failure.format_label(),
|
|
|
|
};
|
2023-10-05 06:25:15 -04:00
|
|
|
write!(&mut self.writer, " {}", status).unwrap();
|
2023-07-26 20:16:12 -04:00
|
|
|
if let TestStepResult::Failed(failure) = result {
|
|
|
|
if let Some(inline_summary) = failure.format_inline_summary() {
|
2023-10-05 06:25:15 -04:00
|
|
|
write!(&mut self.writer, " ({})", inline_summary).unwrap()
|
2023-07-26 20:16:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !matches!(result, TestStepResult::Failed(TestFailure::Incomplete)) {
|
2023-10-05 06:25:15 -04:00
|
|
|
write!(
|
|
|
|
&mut self.writer,
|
2023-07-26 20:16:12 -04:00
|
|
|
" {}",
|
|
|
|
colors::gray(format!("({})", display::human_elapsed(elapsed.into())))
|
2023-10-05 06:25:15 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
2023-07-26 20:16:12 -04:00
|
|
|
}
|
2023-10-05 06:25:15 -04:00
|
|
|
writeln!(&mut self.writer).unwrap();
|
2023-07-26 20:16:12 -04:00
|
|
|
self.in_new_line = true;
|
|
|
|
if self.parallel {
|
|
|
|
self.scope_test_id = None;
|
|
|
|
} else {
|
|
|
|
self.scope_test_id = Some(description.parent_id);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
.child_results_buffer
|
|
|
|
.entry(description.parent_id)
|
|
|
|
.or_default()
|
2024-02-08 20:40:26 -05:00
|
|
|
.shift_remove(&description.id);
|
2023-07-26 20:16:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_output_end(&mut self) {
|
|
|
|
if self.did_have_user_output {
|
2023-10-05 06:25:15 -04:00
|
|
|
writeln!(
|
|
|
|
&mut self.writer,
|
|
|
|
"{}",
|
2024-02-28 17:12:21 -05:00
|
|
|
colors::gray(format!("----- {}output end -----", self.phase))
|
2023-10-05 06:25:15 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
2023-07-26 20:16:12 -04:00
|
|
|
self.in_new_line = true;
|
|
|
|
self.did_have_user_output = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestReporter for PrettyTestReporter {
|
|
|
|
fn report_register(&mut self, _description: &TestDescription) {}
|
|
|
|
fn report_plan(&mut self, plan: &TestPlan) {
|
2024-02-28 17:12:21 -05:00
|
|
|
self.write_output_end();
|
2023-07-26 20:16:12 -04:00
|
|
|
self.summary.total += plan.total;
|
|
|
|
self.summary.filtered_out += plan.filtered_out;
|
2023-10-05 06:25:15 -04:00
|
|
|
if self.repl {
|
|
|
|
return;
|
|
|
|
}
|
2023-09-06 08:54:21 -04:00
|
|
|
if self.parallel || (self.filter && plan.total == 0) {
|
2023-07-26 20:16:12 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let inflection = if plan.total == 1 { "test" } else { "tests" };
|
2023-10-05 06:25:15 -04:00
|
|
|
writeln!(
|
|
|
|
&mut self.writer,
|
2023-07-26 20:16:12 -04:00
|
|
|
"{}",
|
|
|
|
colors::gray(format!(
|
|
|
|
"running {} {} from {}",
|
|
|
|
plan.total,
|
|
|
|
inflection,
|
2023-08-02 12:38:10 -04:00
|
|
|
to_relative_path_or_remote_url(&self.cwd, &plan.origin)
|
2023-07-26 20:16:12 -04:00
|
|
|
))
|
2023-10-05 06:25:15 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
2023-07-26 20:16:12 -04:00
|
|
|
self.in_new_line = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_wait(&mut self, description: &TestDescription) {
|
|
|
|
if !self.parallel {
|
|
|
|
self.force_report_wait(description);
|
|
|
|
}
|
|
|
|
self.started_tests = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_output(&mut self, output: &[u8]) {
|
|
|
|
if !self.echo_output {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-28 17:12:21 -05:00
|
|
|
if !self.did_have_user_output {
|
2023-07-26 20:16:12 -04:00
|
|
|
self.did_have_user_output = true;
|
|
|
|
if !self.in_new_line {
|
2023-10-05 06:25:15 -04:00
|
|
|
writeln!(&mut self.writer).unwrap();
|
2023-07-26 20:16:12 -04:00
|
|
|
}
|
2024-02-28 17:12:21 -05:00
|
|
|
self.phase = if !self.started_tests {
|
|
|
|
"pre-test "
|
|
|
|
} else if self.ended_tests {
|
|
|
|
"post-test "
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
};
|
2023-10-05 06:25:15 -04:00
|
|
|
writeln!(
|
|
|
|
&mut self.writer,
|
|
|
|
"{}",
|
2024-02-28 17:12:21 -05:00
|
|
|
colors::gray(format!("------- {}output -------", self.phase))
|
2023-10-05 06:25:15 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
2023-07-26 20:16:12 -04:00
|
|
|
self.in_new_line = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// output everything to stdout in order to prevent
|
|
|
|
// stdout and stderr racing
|
|
|
|
std::io::stdout().write_all(output).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-26 20:16:12 -04:00
|
|
|
}
|
|
|
|
TestResult::Cancelled => {
|
|
|
|
self.summary.failed += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.parallel {
|
|
|
|
self.force_report_wait(description);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.write_output_end();
|
|
|
|
if self.in_new_line || self.scope_test_id != Some(description.id) {
|
|
|
|
self.force_report_wait(description);
|
|
|
|
}
|
|
|
|
|
|
|
|
let status = match result {
|
|
|
|
TestResult::Ok => colors::green("ok").to_string(),
|
|
|
|
TestResult::Ignored => colors::yellow("ignored").to_string(),
|
|
|
|
TestResult::Failed(failure) => failure.format_label(),
|
|
|
|
TestResult::Cancelled => colors::gray("cancelled").to_string(),
|
|
|
|
};
|
2023-10-05 06:25:15 -04:00
|
|
|
write!(&mut self.writer, " {}", status).unwrap();
|
2023-07-26 20:16:12 -04:00
|
|
|
if let TestResult::Failed(failure) = result {
|
|
|
|
if let Some(inline_summary) = failure.format_inline_summary() {
|
2023-10-05 06:25:15 -04:00
|
|
|
write!(&mut self.writer, " ({})", inline_summary).unwrap();
|
2023-07-26 20:16:12 -04:00
|
|
|
}
|
|
|
|
}
|
2023-10-05 06:25:15 -04:00
|
|
|
writeln!(
|
|
|
|
&mut self.writer,
|
2023-07-26 20:16:12 -04:00
|
|
|
" {}",
|
|
|
|
colors::gray(format!("({})", display::human_elapsed(elapsed.into())))
|
2023-10-05 06:25:15 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
2023-07-26 20:16:12 -04:00
|
|
|
self.in_new_line = true;
|
|
|
|
self.scope_test_id = None;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_uncaught_error(&mut self, origin: &str, error: Box<JsError>) {
|
|
|
|
self.summary.failed += 1;
|
|
|
|
self
|
|
|
|
.summary
|
|
|
|
.uncaught_errors
|
|
|
|
.push((origin.to_string(), error));
|
|
|
|
|
|
|
|
if !self.in_new_line {
|
2023-10-05 06:25:15 -04:00
|
|
|
writeln!(&mut self.writer).unwrap();
|
2023-07-26 20:16:12 -04:00
|
|
|
}
|
2023-10-05 06:25:15 -04:00
|
|
|
writeln!(
|
|
|
|
&mut self.writer,
|
2023-07-26 20:16:12 -04:00
|
|
|
"Uncaught error from {} {}",
|
2023-08-02 12:38:10 -04:00
|
|
|
to_relative_path_or_remote_url(&self.cwd, origin),
|
2023-07-26 20:16:12 -04:00
|
|
|
colors::red("FAILED")
|
2023-10-05 06:25:15 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
2023-07-26 20:16:12 -04:00
|
|
|
self.in_new_line = true;
|
|
|
|
self.did_have_user_output = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_step_register(&mut self, _description: &TestStepDescription) {}
|
|
|
|
|
|
|
|
fn report_step_wait(&mut self, description: &TestStepDescription) {
|
|
|
|
if !self.parallel && self.scope_test_id == Some(description.parent_id) {
|
|
|
|
self.force_report_step_wait(description);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-26 20:16:12 -04:00
|
|
|
id: desc.id,
|
2023-08-02 12:38:10 -04:00
|
|
|
name: common::format_test_step_ancestry(desc, tests, test_steps),
|
2023-07-26 20:16:12 -04:00
|
|
|
origin: desc.origin.clone(),
|
|
|
|
location: desc.location.clone(),
|
|
|
|
},
|
|
|
|
failure.clone(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.parallel {
|
|
|
|
self.write_output_end();
|
2023-10-05 06:25:15 -04:00
|
|
|
write!(
|
|
|
|
&mut self.writer,
|
2023-07-26 20:16:12 -04:00
|
|
|
"{} {} ...",
|
|
|
|
colors::gray(format!(
|
|
|
|
"{} =>",
|
2023-08-02 12:38:10 -04:00
|
|
|
to_relative_path_or_remote_url(&self.cwd, &desc.origin)
|
2023-07-26 20:16:12 -04:00
|
|
|
)),
|
2023-08-02 12:38:10 -04:00
|
|
|
common::format_test_step_ancestry(desc, tests, test_steps)
|
2023-10-05 06:25:15 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
2023-07-26 20:16:12 -04:00
|
|
|
self.in_new_line = false;
|
|
|
|
self.scope_test_id = Some(desc.id);
|
|
|
|
self.force_report_step_result(desc, result, elapsed);
|
|
|
|
} else {
|
|
|
|
let sibling_results =
|
|
|
|
self.child_results_buffer.entry(desc.parent_id).or_default();
|
|
|
|
if self.scope_test_id == Some(desc.id)
|
|
|
|
|| self.scope_test_id == Some(desc.parent_id)
|
|
|
|
{
|
|
|
|
let sibling_results = std::mem::take(sibling_results);
|
|
|
|
self.force_report_step_result(desc, result, elapsed);
|
|
|
|
// Flush buffered sibling results.
|
|
|
|
for (desc, result, elapsed) in sibling_results.values() {
|
|
|
|
self.force_report_step_result(desc, result, *elapsed);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sibling_results
|
|
|
|
.insert(desc.id, (desc.clone(), result.clone(), elapsed));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_summary(
|
|
|
|
&mut self,
|
|
|
|
elapsed: &Duration,
|
|
|
|
_tests: &IndexMap<usize, TestDescription>,
|
|
|
|
_test_steps: &IndexMap<usize, TestStepDescription>,
|
|
|
|
) {
|
2024-02-28 17:12:21 -05:00
|
|
|
self.write_output_end();
|
2023-10-05 06:25:15 -04:00
|
|
|
common::report_summary(&mut self.writer, &self.cwd, &self.summary, elapsed);
|
2023-10-05 16:45:11 -04:00
|
|
|
if !self.repl {
|
|
|
|
writeln!(&mut self.writer).unwrap();
|
|
|
|
}
|
2023-07-26 20:16:12 -04:00
|
|
|
self.in_new_line = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 self.writer,
|
|
|
|
&self.cwd,
|
|
|
|
tests_pending,
|
|
|
|
tests,
|
|
|
|
test_steps,
|
|
|
|
);
|
2023-07-26 20:16:12 -04:00
|
|
|
self.in_new_line = true;
|
|
|
|
}
|
|
|
|
|
2024-02-28 17:12:21 -05:00
|
|
|
fn report_completed(&mut self) {
|
|
|
|
self.write_output_end();
|
|
|
|
self.ended_tests = true;
|
|
|
|
}
|
|
|
|
|
2023-07-26 20:16:12 -04:00
|
|
|
fn flush_report(
|
|
|
|
&mut self,
|
|
|
|
_elapsed: &Duration,
|
|
|
|
_tests: &IndexMap<usize, TestDescription>,
|
|
|
|
_test_steps: &IndexMap<usize, TestStepDescription>,
|
|
|
|
) -> anyhow::Result<()> {
|
2023-10-05 06:25:15 -04:00
|
|
|
self.writer.flush().unwrap();
|
2023-07-26 20:16:12 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|