diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2024-03-13 22:15:39 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-13 22:15:39 -0400 |
commit | 6f5a86ce5166440980a9100db5051452e9734fa6 (patch) | |
tree | 980f46c3c0f14a0a918fd8f3c6dc28cd45984f85 /tests/specs/mod.rs | |
parent | da58722851fcdf965084a0736d83aac678d67032 (diff) |
chore: improve spec tests output (#22908)
Diffstat (limited to 'tests/specs/mod.rs')
-rw-r--r-- | tests/specs/mod.rs | 61 |
1 files changed, 46 insertions, 15 deletions
diff --git a/tests/specs/mod.rs b/tests/specs/mod.rs index 831b94430..6a61ae2a6 100644 --- a/tests/specs/mod.rs +++ b/tests/specs/mod.rs @@ -1,8 +1,13 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +use std::cell::RefCell; use std::collections::HashSet; +use std::panic::AssertUnwindSafe; +use std::rc::Rc; +use std::sync::Arc; use deno_core::anyhow::Context; +use deno_core::parking_lot::Mutex; use deno_core::serde_json; use deno_terminal::colors; use serde::Deserialize; @@ -21,22 +26,36 @@ pub fn main() { for category in &categories { eprintln!(); eprintln!(" {} {}", colors::green_bold("Running"), category.name); + eprintln!(); for test in &category.tests { - eprintln!(); - eprintln!("==== Starting {} ====", test.name); - let result = std::panic::catch_unwind(|| run_test(test)); + eprint!("test {} ... ", test.name); + let diagnostic_logger = Rc::new(RefCell::new(Vec::<u8>::new())); + let panic_message = Arc::new(Mutex::new(Vec::<u8>::new())); + std::panic::set_hook({ + let panic_message = panic_message.clone(); + Box::new(move |info| { + panic_message + .lock() + .extend(format!("{}", info).into_bytes()); + }) + }); + let result = std::panic::catch_unwind(AssertUnwindSafe(|| { + run_test(test, diagnostic_logger.clone()) + })); let success = result.is_ok(); if !success { - failures.push(&test.name); + let mut output = diagnostic_logger.borrow().clone(); + output.push(b'\n'); + output.extend(panic_message.lock().iter()); + failures.push((test, output)); } eprintln!( - "==== {} {} ====", + "{}", if success { - "Finished".to_string() + colors::green("ok") } else { - colors::red("^^FAILED^^").to_string() + colors::red("fail") }, - test.name ); } } @@ -44,13 +63,18 @@ pub fn main() { eprintln!(); if !failures.is_empty() { eprintln!("spec failures:"); - for failure in &failures { - eprintln!(" {}", failure); - } eprintln!(); + for (failure, output) in &failures { + eprintln!("---- {} ----", failure.name); + eprintln!("{}", String::from_utf8_lossy(output)); + eprintln!("Test file: {}", failure.manifest_file()); + eprintln!(); + } panic!("{} failed of {}", failures.len(), total_tests); + } else { + eprintln!("{} tests passed", total_tests); } - eprintln!("{} tests passed", total_tests); + eprintln!(); } fn parse_cli_arg_filter() -> Option<String> { @@ -60,9 +84,10 @@ fn parse_cli_arg_filter() -> Option<String> { maybe_filter.cloned() } -fn run_test(test: &Test) { +fn run_test(test: &Test, diagnostic_logger: Rc<RefCell<Vec<u8>>>) { let metadata = &test.metadata; let mut builder = TestContextBuilder::new(); + builder = builder.logging_capture(diagnostic_logger); let cwd = &test.cwd; if test.metadata.temp_dir { @@ -77,7 +102,7 @@ fn run_test(test: &Test) { builder = builder.add_npm_env_vars(); } "jsr" => { - builder = builder.add_jsr_env_vars(); + builder = builder.add_jsr_env_vars().add_npm_env_vars(); } _ => panic!("Unknown test base: {}", base), } @@ -179,9 +204,15 @@ struct Test { } impl Test { + pub fn manifest_file(&self) -> PathRef { + self.cwd.join("__test__.json") + } +} + +impl Test { pub fn resolve_test_and_assertion_files(&self) -> HashSet<PathRef> { let mut result = HashSet::with_capacity(self.metadata.steps.len() + 1); - result.insert(self.cwd.join("__test__.json")); + result.insert(self.manifest_file()); result.extend( self .metadata |