diff options
| author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2023-10-05 11:25:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-05 11:25:15 +0100 |
| commit | 551a08145098e95022efb778308d677db60a67cc (patch) | |
| tree | 1ea66cdf7066a9e7e3229a1b97ac9ad4f50bbd06 /cli/tools/test/reporters | |
| parent | fd4fc2d81830c8350ccdc7be689db31183ada235 (diff) | |
refactor(test): support custom writer in PrettyTestReporter (#20783)
Diffstat (limited to 'cli/tools/test/reporters')
| -rw-r--r-- | cli/tools/test/reporters/common.rs | 50 | ||||
| -rw-r--r-- | cli/tools/test/reporters/dot.rs | 15 | ||||
| -rw-r--r-- | cli/tools/test/reporters/pretty.rs | 97 | ||||
| -rw-r--r-- | cli/tools/test/reporters/tap.rs | 8 |
4 files changed, 122 insertions, 48 deletions
diff --git a/cli/tools/test/reporters/common.rs b/cli/tools/test/reporters/common.rs index ce1aad602..889110057 100644 --- a/cli/tools/test/reporters/common.rs +++ b/cli/tools/test/reporters/common.rs @@ -66,6 +66,7 @@ pub fn format_test_step_for_summary( } pub(super) fn report_sigint( + writer: &mut dyn std::io::Write, cwd: &Url, tests_pending: &HashSet<usize>, tests: &IndexMap<usize, TestDescription>, @@ -84,17 +85,20 @@ pub(super) fn report_sigint( .insert(format_test_step_for_summary(cwd, desc, tests, test_steps)); } } - println!( + writeln!( + writer, "\n{} The following tests were pending:\n", colors::intense_blue("SIGINT") - ); + ) + .unwrap(); for entry in formatted_pending { - println!("{}", entry); + writeln!(writer, "{}", entry).unwrap(); } - println!(); + writeln!(writer).unwrap(); } pub(super) fn report_summary( + writer: &mut dyn std::io::Write, cwd: &Url, summary: &TestSummary, elapsed: &Duration, @@ -120,14 +124,20 @@ pub(super) fn report_summary( } // note: the trailing whitespace is intentional to get a red background - println!("\n{}\n", colors::white_bold_on_red(" ERRORS ")); + writeln!(writer, "\n{}\n", colors::white_bold_on_red(" ERRORS ")).unwrap(); for (origin, (failures, uncaught_error)) in failures_by_origin { for (description, failure) in failures { if !failure.hide_in_summary() { let failure_title = format_test_for_summary(cwd, description); - println!("{}", &failure_title); - println!("{}: {}", colors::red_bold("error"), failure.to_string()); - println!(); + writeln!(writer, "{}", &failure_title).unwrap(); + writeln!( + writer, + "{}: {}", + colors::red_bold("error"), + failure.to_string() + ) + .unwrap(); + writeln!(writer).unwrap(); failure_titles.push(failure_title); } } @@ -136,22 +146,24 @@ pub(super) fn report_summary( "{} (uncaught error)", to_relative_path_or_remote_url(cwd, &origin) ); - println!("{}", &failure_title); - println!( + writeln!(writer, "{}", &failure_title).unwrap(); + writeln!( + writer, "{}: {}", colors::red_bold("error"), format_test_error(js_error) - ); - println!("This error was not caught from a test and caused the test runner to fail on the referenced module."); - println!("It most likely originated from a dangling promise, event/timeout handler or top-level code."); - println!(); + ) + .unwrap(); + writeln!(writer, "This error was not caught from a test and caused the test runner to fail on the referenced module.").unwrap(); + writeln!(writer, "It most likely originated from a dangling promise, event/timeout handler or top-level code.").unwrap(); + writeln!(writer).unwrap(); failure_titles.push(failure_title); } } // note: the trailing whitespace is intentional to get a red background - println!("{}\n", colors::white_bold_on_red(" FAILURES ")); + writeln!(writer, "{}\n", colors::white_bold_on_red(" FAILURES ")).unwrap(); for failure_title in failure_titles { - println!("{failure_title}"); + writeln!(writer, "{failure_title}").unwrap(); } } @@ -201,10 +213,12 @@ pub(super) fn report_summary( write!(summary_result, " | {} filtered out", summary.filtered_out).unwrap() }; - println!( + writeln!( + writer, "\n{} | {} {}\n", status, summary_result, colors::gray(format!("({})", display::human_elapsed(elapsed.as_millis()))), - ); + ) + .unwrap(); } diff --git a/cli/tools/test/reporters/dot.rs b/cli/tools/test/reporters/dot.rs index 4aa3fd89e..cb005b297 100644 --- a/cli/tools/test/reporters/dot.rs +++ b/cli/tools/test/reporters/dot.rs @@ -184,7 +184,12 @@ impl TestReporter for DotTestReporter { _tests: &IndexMap<usize, TestDescription>, _test_steps: &IndexMap<usize, TestStepDescription>, ) { - common::report_summary(&self.cwd, &self.summary, elapsed); + common::report_summary( + &mut std::io::stdout(), + &self.cwd, + &self.summary, + elapsed, + ); } fn report_sigint( @@ -193,7 +198,13 @@ impl TestReporter for DotTestReporter { tests: &IndexMap<usize, TestDescription>, test_steps: &IndexMap<usize, TestStepDescription>, ) { - common::report_sigint(&self.cwd, tests_pending, tests, test_steps); + common::report_sigint( + &mut std::io::stdout(), + &self.cwd, + tests_pending, + tests, + test_steps, + ); } fn flush_report( diff --git a/cli/tools/test/reporters/pretty.rs b/cli/tools/test/reporters/pretty.rs index 8a2b77bb0..c3b61c66c 100644 --- a/cli/tools/test/reporters/pretty.rs +++ b/cli/tools/test/reporters/pretty.rs @@ -9,6 +9,7 @@ pub struct PrettyTestReporter { echo_output: bool, in_new_line: bool, filter: bool, + repl: bool, scope_test_id: Option<usize>, cwd: Url, did_have_user_output: bool, @@ -16,6 +17,7 @@ pub struct PrettyTestReporter { child_results_buffer: HashMap<usize, IndexMap<usize, (TestStepDescription, TestStepResult, u64)>>, summary: TestSummary, + writer: Box<dyn std::io::Write>, } impl PrettyTestReporter { @@ -23,35 +25,40 @@ impl PrettyTestReporter { parallel: bool, echo_output: bool, filter: bool, + repl: bool, ) -> PrettyTestReporter { PrettyTestReporter { parallel, echo_output, in_new_line: true, filter, + repl, scope_test_id: None, cwd: Url::from_directory_path(std::env::current_dir().unwrap()).unwrap(), did_have_user_output: false, started_tests: false, child_results_buffer: Default::default(), summary: TestSummary::new(), + writer: Box::new(std::io::stdout()), } } fn force_report_wait(&mut self, description: &TestDescription) { if !self.in_new_line { - println!(); + writeln!(&mut self.writer).unwrap(); } if self.parallel { - print!( + write!( + &mut self.writer, "{}", colors::gray(format!( "{} => ", to_relative_path_or_remote_url(&self.cwd, &description.origin) )) - ); + ) + .unwrap(); } - print!("{} ...", description.name); + write!(&mut self.writer, "{} ...", description.name).unwrap(); self.in_new_line = false; // flush for faster feedback when line buffered std::io::stdout().flush().unwrap(); @@ -61,9 +68,15 @@ impl PrettyTestReporter { fn force_report_step_wait(&mut self, description: &TestStepDescription) { self.write_output_end(); if !self.in_new_line { - println!(); + writeln!(&mut self.writer).unwrap(); } - print!("{}{} ...", " ".repeat(description.level), description.name); + write!( + &mut self.writer, + "{}{} ...", + " ".repeat(description.level), + description.name + ) + .unwrap(); self.in_new_line = false; // flush for faster feedback when line buffered std::io::stdout().flush().unwrap(); @@ -99,19 +112,21 @@ impl PrettyTestReporter { TestStepResult::Ignored => colors::yellow("ignored").to_string(), TestStepResult::Failed(failure) => failure.format_label(), }; - print!(" {}", status); + write!(&mut self.writer, " {}", status).unwrap(); if let TestStepResult::Failed(failure) = result { if let Some(inline_summary) = failure.format_inline_summary() { - print!(" ({})", inline_summary) + write!(&mut self.writer, " ({})", inline_summary).unwrap() } } if !matches!(result, TestStepResult::Failed(TestFailure::Incomplete)) { - print!( + write!( + &mut self.writer, " {}", colors::gray(format!("({})", display::human_elapsed(elapsed.into()))) - ); + ) + .unwrap(); } - println!(); + writeln!(&mut self.writer).unwrap(); self.in_new_line = true; if self.parallel { self.scope_test_id = None; @@ -127,7 +142,12 @@ impl PrettyTestReporter { fn write_output_end(&mut self) { if self.did_have_user_output { - println!("{}", colors::gray("----- output end -----")); + writeln!( + &mut self.writer, + "{}", + colors::gray("----- output end -----") + ) + .unwrap(); self.in_new_line = true; self.did_have_user_output = false; } @@ -139,11 +159,15 @@ impl TestReporter for PrettyTestReporter { fn report_plan(&mut self, plan: &TestPlan) { self.summary.total += plan.total; self.summary.filtered_out += plan.filtered_out; + if self.repl { + return; + } if self.parallel || (self.filter && plan.total == 0) { return; } let inflection = if plan.total == 1 { "test" } else { "tests" }; - println!( + writeln!( + &mut self.writer, "{}", colors::gray(format!( "running {} {} from {}", @@ -151,7 +175,8 @@ impl TestReporter for PrettyTestReporter { inflection, to_relative_path_or_remote_url(&self.cwd, &plan.origin) )) - ); + ) + .unwrap(); self.in_new_line = true; } @@ -170,9 +195,14 @@ impl TestReporter for PrettyTestReporter { if !self.did_have_user_output && self.started_tests { self.did_have_user_output = true; if !self.in_new_line { - println!(); + writeln!(&mut self.writer).unwrap(); } - println!("{}", colors::gray("------- output -------")); + writeln!( + &mut self.writer, + "{}", + colors::gray("------- output -------") + ) + .unwrap(); self.in_new_line = true; } @@ -221,16 +251,18 @@ impl TestReporter for PrettyTestReporter { TestResult::Failed(failure) => failure.format_label(), TestResult::Cancelled => colors::gray("cancelled").to_string(), }; - print!(" {}", status); + write!(&mut self.writer, " {}", status).unwrap(); if let TestResult::Failed(failure) = result { if let Some(inline_summary) = failure.format_inline_summary() { - print!(" ({})", inline_summary) + write!(&mut self.writer, " ({})", inline_summary).unwrap(); } } - println!( + writeln!( + &mut self.writer, " {}", colors::gray(format!("({})", display::human_elapsed(elapsed.into()))) - ); + ) + .unwrap(); self.in_new_line = true; self.scope_test_id = None; } @@ -243,13 +275,15 @@ impl TestReporter for PrettyTestReporter { .push((origin.to_string(), error)); if !self.in_new_line { - println!(); + writeln!(&mut self.writer).unwrap(); } - println!( + writeln!( + &mut self.writer, "Uncaught error from {} {}", to_relative_path_or_remote_url(&self.cwd, origin), colors::red("FAILED") - ); + ) + .unwrap(); self.in_new_line = true; self.did_have_user_output = false; } @@ -295,14 +329,16 @@ impl TestReporter for PrettyTestReporter { if self.parallel { self.write_output_end(); - print!( + write!( + &mut self.writer, "{} {} ...", colors::gray(format!( "{} =>", to_relative_path_or_remote_url(&self.cwd, &desc.origin) )), common::format_test_step_ancestry(desc, tests, test_steps) - ); + ) + .unwrap(); self.in_new_line = false; self.scope_test_id = Some(desc.id); self.force_report_step_result(desc, result, elapsed); @@ -331,7 +367,7 @@ impl TestReporter for PrettyTestReporter { _tests: &IndexMap<usize, TestDescription>, _test_steps: &IndexMap<usize, TestStepDescription>, ) { - common::report_summary(&self.cwd, &self.summary, elapsed); + common::report_summary(&mut self.writer, &self.cwd, &self.summary, elapsed); self.in_new_line = true; } @@ -341,7 +377,13 @@ impl TestReporter for PrettyTestReporter { tests: &IndexMap<usize, TestDescription>, test_steps: &IndexMap<usize, TestStepDescription>, ) { - common::report_sigint(&self.cwd, tests_pending, tests, test_steps); + common::report_sigint( + &mut self.writer, + &self.cwd, + tests_pending, + tests, + test_steps, + ); self.in_new_line = true; } @@ -351,6 +393,7 @@ impl TestReporter for PrettyTestReporter { _tests: &IndexMap<usize, TestDescription>, _test_steps: &IndexMap<usize, TestStepDescription>, ) -> anyhow::Result<()> { + self.writer.flush().unwrap(); Ok(()) } } diff --git a/cli/tools/test/reporters/tap.rs b/cli/tools/test/reporters/tap.rs index c40c1eed7..921874377 100644 --- a/cli/tools/test/reporters/tap.rs +++ b/cli/tools/test/reporters/tap.rs @@ -227,7 +227,13 @@ impl TestReporter for TapTestReporter { test_steps: &IndexMap<usize, TestStepDescription>, ) { println!("Bail out! SIGINT received."); - common::report_sigint(&self.cwd, tests_pending, tests, test_steps); + common::report_sigint( + &mut std::io::stdout(), + &self.cwd, + tests_pending, + tests, + test_steps, + ); } fn flush_report( |
