diff options
Diffstat (limited to 'cli/tools/test.rs')
-rw-r--r-- | cli/tools/test.rs | 146 |
1 files changed, 59 insertions, 87 deletions
diff --git a/cli/tools/test.rs b/cli/tools/test.rs index e42ea673b..e4b9ad398 100644 --- a/cli/tools/test.rs +++ b/cli/tools/test.rs @@ -278,38 +278,9 @@ impl TestSummary { } } -pub trait TestReporter { - fn report_register(&mut self, plan: &TestDescription); - fn report_plan(&mut self, plan: &TestPlan); - fn report_wait(&mut self, description: &TestDescription); - fn report_output(&mut self, output: &[u8]); - fn report_result( - &mut self, - description: &TestDescription, - result: &TestResult, - elapsed: u64, - ); - fn report_uncaught_error(&mut self, origin: &str, error: &JsError); - fn report_step_register(&mut self, description: &TestStepDescription); - fn report_step_wait(&mut self, description: &TestStepDescription); - fn report_step_result( - &mut self, - description: &TestStepDescription, - result: &TestStepResult, - elapsed: u64, - ); - fn report_summary(&mut self, summary: &TestSummary, elapsed: &Duration); -} - -enum DeferredStepOutput { - StepWait(TestStepDescription), - StepResult(TestStepDescription, TestStepResult, u64), -} - struct PrettyTestReporter { - concurrent: bool, + parallel: bool, echo_output: bool, - deferred_step_output: IndexMap<usize, Vec<DeferredStepOutput>>, in_new_line: bool, last_wait_id: Option<usize>, cwd: Url, @@ -318,12 +289,11 @@ struct PrettyTestReporter { } impl PrettyTestReporter { - fn new(concurrent: bool, echo_output: bool) -> PrettyTestReporter { + fn new(parallel: bool, echo_output: bool) -> PrettyTestReporter { PrettyTestReporter { - concurrent, + parallel, echo_output, in_new_line: true, - deferred_step_output: IndexMap::new(), last_wait_id: None, cwd: Url::from_directory_path(std::env::current_dir().unwrap()).unwrap(), did_have_user_output: false, @@ -335,6 +305,15 @@ impl PrettyTestReporter { if !self.in_new_line { println!(); } + if self.parallel { + print!( + "{}", + colors::gray(format!( + "{} => ", + self.to_relative_path_or_remote_url(&description.origin) + )) + ); + } print!("{} ...", description.name); self.in_new_line = false; // flush for faster feedback when line buffered @@ -408,12 +387,13 @@ impl PrettyTestReporter { self.did_have_user_output = false; } } -} -impl TestReporter for PrettyTestReporter { fn report_register(&mut self, _description: &TestDescription) {} fn report_plan(&mut self, plan: &TestPlan) { + if self.parallel { + return; + } let inflection = if plan.total == 1 { "test" } else { "tests" }; println!( "{}", @@ -428,7 +408,7 @@ impl TestReporter for PrettyTestReporter { } fn report_wait(&mut self, description: &TestDescription) { - if !self.concurrent { + if !self.parallel { self.force_report_wait(description); } self.started_tests = true; @@ -441,7 +421,9 @@ impl TestReporter for PrettyTestReporter { if !self.did_have_user_output && self.started_tests { self.did_have_user_output = true; - println!(); + if !self.in_new_line { + println!(); + } println!("{}", colors::gray("------- output -------")); self.in_new_line = true; } @@ -457,29 +439,8 @@ impl TestReporter for PrettyTestReporter { result: &TestResult, elapsed: u64, ) { - if self.concurrent { + if self.parallel { self.force_report_wait(description); - - if let Some(step_outputs) = - self.deferred_step_output.remove(&description.id) - { - for step_output in step_outputs { - match step_output { - DeferredStepOutput::StepWait(description) => { - self.force_report_step_wait(&description) - } - DeferredStepOutput::StepResult( - step_description, - step_result, - elapsed, - ) => self.force_report_step_result( - &step_description, - &step_result, - elapsed, - ), - } - } - } } self.write_output_end(); @@ -518,13 +479,7 @@ impl TestReporter for PrettyTestReporter { fn report_step_register(&mut self, _description: &TestStepDescription) {} fn report_step_wait(&mut self, description: &TestStepDescription) { - if self.concurrent { - self - .deferred_step_output - .entry(description.root_id) - .or_insert_with(Vec::new) - .push(DeferredStepOutput::StepWait(description.clone())); - } else { + if !self.parallel { self.force_report_step_wait(description); } } @@ -534,20 +489,40 @@ impl TestReporter for PrettyTestReporter { description: &TestStepDescription, result: &TestStepResult, elapsed: u64, + tests: &IndexMap<usize, TestDescription>, + test_steps: &IndexMap<usize, TestStepDescription>, ) { - if self.concurrent { - self - .deferred_step_output - .entry(description.root_id) - .or_insert_with(Vec::new) - .push(DeferredStepOutput::StepResult( - description.clone(), - result.clone(), - elapsed, - )); - } else { - self.force_report_step_result(description, result, elapsed); + if self.parallel { + self.write_output_end(); + let root; + let mut ancestor_names = vec![]; + let mut current_desc = description; + loop { + if let Some(step_desc) = test_steps.get(¤t_desc.parent_id) { + ancestor_names.push(&step_desc.name); + current_desc = step_desc; + } else { + root = tests.get(¤t_desc.parent_id).unwrap(); + break; + } + } + ancestor_names.reverse(); + print!( + "{}", + colors::gray(format!( + "{} =>", + self.to_relative_path_or_remote_url(&description.origin) + )) + ); + print!(" {} ...", root.name); + for name in ancestor_names { + print!(" {} ...", name); + } + print!(" {} ...", description.name); + self.in_new_line = false; + self.last_wait_id = Some(description.id); } + self.force_report_step_result(description, result, elapsed); } fn report_summary(&mut self, summary: &TestSummary, elapsed: &Duration) { @@ -735,13 +710,6 @@ pub fn format_test_error(js_error: &JsError) -> String { format_js_error(&js_error) } -fn create_reporter( - concurrent: bool, - echo_output: bool, -) -> Box<dyn TestReporter + Send> { - Box::new(PrettyTestReporter::new(concurrent, echo_output)) -} - /// Test a single specifier as documentation containing test programs, an executable test module or /// both. async fn test_specifier( @@ -1160,8 +1128,10 @@ async fn test_specifiers( .buffer_unordered(concurrent_jobs.get()) .collect::<Vec<Result<Result<(), AnyError>, tokio::task::JoinError>>>(); - let mut reporter = - create_reporter(concurrent_jobs.get() > 1, log_level != Some(Level::Error)); + let mut reporter = Box::new(PrettyTestReporter::new( + concurrent_jobs.get() > 1, + log_level != Some(Level::Error), + )); let handler = { tokio::task::spawn(async move { @@ -1261,6 +1231,8 @@ async fn test_specifiers( test_steps.get(&id).unwrap(), &result, duration, + &tests, + &test_steps, ); } } |