diff options
author | Casper Beyer <caspervonb@pm.me> | 2021-09-04 21:16:35 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-04 15:16:35 +0200 |
commit | ce79cb579784e8417596fed03f3d2a5bbbad487d (patch) | |
tree | 846d81dbbbf9408268d1c54aa865074717653537 /cli/tools/test.rs | |
parent | 44ca3ce6aeb99cb968776f5f13420e76acbc8117 (diff) |
refactor(testing): redirect console output via reporter (#11911)
This feeds console output to the reporter and handles silencing there
instead of in the JavaScript code.
Diffstat (limited to 'cli/tools/test.rs')
-rw-r--r-- | cli/tools/test.rs | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/cli/tools/test.rs b/cli/tools/test.rs index 90293173b..819f9980c 100644 --- a/cli/tools/test.rs +++ b/cli/tools/test.rs @@ -70,6 +70,13 @@ pub struct TestDescription { #[derive(Debug, Clone, PartialEq, Deserialize)] #[serde(rename_all = "camelCase")] +pub enum TestOutput { + // TODO(caspervonb): add stdout and stderr redirection. + Console(String), +} + +#[derive(Debug, Clone, PartialEq, Deserialize)] +#[serde(rename_all = "camelCase")] pub enum TestResult { Ok, Ignored, @@ -90,6 +97,7 @@ pub struct TestPlan { pub enum TestEvent { Plan(TestPlan), Wait(TestDescription), + Output(TestOutput), Result(TestDescription, TestResult, u64), } @@ -129,6 +137,7 @@ impl TestSummary { trait TestReporter { fn report_plan(&mut self, plan: &TestPlan); fn report_wait(&mut self, description: &TestDescription); + fn report_output(&mut self, output: &TestOutput); fn report_result( &mut self, description: &TestDescription, @@ -140,11 +149,15 @@ trait TestReporter { struct PrettyTestReporter { concurrent: bool, + echo_output: bool, } impl PrettyTestReporter { - fn new(concurrent: bool) -> PrettyTestReporter { - PrettyTestReporter { concurrent } + fn new(concurrent: bool, echo_output: bool) -> PrettyTestReporter { + PrettyTestReporter { + concurrent, + echo_output, + } } } @@ -160,6 +173,14 @@ impl TestReporter for PrettyTestReporter { } } + fn report_output(&mut self, output: &TestOutput) { + if self.echo_output { + match output { + TestOutput::Console(line) => println!("{}", line), + } + } + } + fn report_result( &mut self, description: &TestDescription, @@ -217,8 +238,11 @@ impl TestReporter for PrettyTestReporter { } } -fn create_reporter(concurrent: bool) -> Box<dyn TestReporter + Send> { - Box::new(PrettyTestReporter::new(concurrent)) +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 @@ -248,7 +272,6 @@ async fn test_specifier( test_source.push_str(&format!( "await Deno[Deno.internal].runTests({});\n", json!({ - "disableLog": program_state.flags.log_level == Some(Level::Error), "filter": filter, "shuffle": shuffle, }), @@ -558,6 +581,7 @@ async fn test_specifiers( shuffle: Option<u64>, concurrent_jobs: NonZeroUsize, ) -> Result<(), AnyError> { + let log_level = program_state.flags.log_level; let specifiers_with_mode = if let Some(seed) = shuffle { let mut rng = SmallRng::seed_from_u64(seed); let mut specifiers_with_mode = specifiers_with_mode.clone(); @@ -602,7 +626,9 @@ 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); + let mut reporter = + create_reporter(concurrent_jobs.get() > 1, log_level != Some(Level::Error)); + let handler = { tokio::task::spawn_blocking(move || { let earlier = Instant::now(); @@ -626,6 +652,10 @@ async fn test_specifiers( reporter.report_wait(&description); } + TestEvent::Output(output) => { + reporter.report_output(&output); + } + TestEvent::Result(description, result, elapsed) => { match &result { TestResult::Ok => { |