diff --git a/.gitignore b/.gitignore index a8738ea41d..62bbca261e 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,6 @@ gclient_config.py_entries /tools/wpt/certs/serial* /ext/websocket/autobahn/reports + +# JUnit files produced by deno test --junit +junit.xml diff --git a/Cargo.lock b/Cargo.lock index e631742d59..578ceac1c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -114,6 +114,15 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "0.3.2" @@ -188,7 +197,7 @@ dependencies = [ "num-traits", "rusticata-macros", "thiserror", - "time", + "time 0.3.20", ] [[package]] @@ -514,7 +523,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec837a71355b28f6556dbd569b37b3f363091c0bd4b2e735674521b4c5fd9bc5" dependencies = [ "android-tzdata", + "iana-time-zone", + "js-sys", "num-traits", + "time 0.1.45", + "wasm-bindgen", + "winapi", ] [[package]] @@ -833,7 +847,7 @@ dependencies = [ "http", "hyper 0.14.26", "import_map", - "indexmap", + "indexmap 1.9.2", "jsonc-parser", "junction", "lazy-regex", @@ -849,6 +863,7 @@ dependencies = [ "percent-encoding", "pin-project", "pretty_assertions", + "quick-junit", "rand", "regex", "ring", @@ -982,7 +997,7 @@ dependencies = [ "bytes", "deno_ops", "futures", - "indexmap", + "indexmap 1.9.2", "libc", "log", "once_cell", @@ -1130,7 +1145,7 @@ dependencies = [ "deno_ast", "deno_semver", "futures", - "indexmap", + "indexmap 1.9.2", "monch", "once_cell", "parking_lot 0.12.1", @@ -1294,7 +1309,7 @@ dependencies = [ "hex", "hkdf", "idna 0.3.0", - "indexmap", + "indexmap 1.9.2", "lazy-regex", "libz-sys", "md-5", @@ -1669,7 +1684,7 @@ checksum = "e6563addfa2b6c6fa96acdda0341090beba2c5c4ff6ef91f3a232a6d4dd34156" dependencies = [ "anyhow", "bumpalo", - "indexmap", + "indexmap 1.9.2", "rustc-hash", "serde", "unicode-width", @@ -1902,6 +1917,12 @@ dependencies = [ "termcolor", ] +[[package]] +name = "equivalent" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88bffebc5d80432c9b140ee17875ff173a8ab62faad5b257da912bd2f6c1c0a1" + [[package]] name = "errno" version = "0.2.8" @@ -2349,7 +2370,7 @@ dependencies = [ "futures-sink", "futures-util", "http", - "indexmap", + "indexmap 1.9.2", "slab", "tokio", "tokio-util", @@ -2371,6 +2392,12 @@ dependencies = [ "ahash 0.8.3", ] +[[package]] +name = "hashbrown" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a" + [[package]] name = "hashlink" version = "0.8.2" @@ -2554,6 +2581,29 @@ dependencies = [ "tokio-rustls", ] +[[package]] +name = "iana-time-zone" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fad5b825842d2b38bd206f3e81d6957625fd7f0a361e345c30e01a0ae2dd613" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "idna" version = "0.2.3" @@ -2588,7 +2638,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "632089ec08bd62e807311104122fb26d5c911ab172e2b9864be154a575979e29" dependencies = [ "cfg-if", - "indexmap", + "indexmap 1.9.2", "log", "serde", "serde_json", @@ -2606,6 +2656,16 @@ dependencies = [ "serde", ] +[[package]] +name = "indexmap" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5477fe2230a79769d8dc68e0eabf5437907c0457a5614a9e8dddb67f65eb65d" +dependencies = [ + "equivalent", + "hashbrown 0.14.0", +] + [[package]] name = "inotify" version = "0.9.6" @@ -3143,6 +3203,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" +[[package]] +name = "nextest-workspace-hack" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d906846a98739ed9d73d66e62c2641eef8321f1734b7a1156ab045a0248fb2b3" + [[package]] name = "nibble_vec" version = "0.1.0" @@ -3513,7 +3579,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4dd7d28ee937e54fe3080c91faa1c3a46c06de6252988a7f4592ba2310ef22a4" dependencies = [ "fixedbitset", - "indexmap", + "indexmap 1.9.2", ] [[package]] @@ -3791,6 +3857,29 @@ version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" +[[package]] +name = "quick-junit" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bf780b59d590c25f8c59b44c124166a2a93587868b619fb8f5b47fb15e9ed6d" +dependencies = [ + "chrono", + "indexmap 2.0.0", + "nextest-workspace-hack", + "quick-xml", + "thiserror", + "uuid", +] + +[[package]] +name = "quick-xml" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81b9228215d82c7b61490fec1de287136b5de6f5700f6e58ea9ad61a7964ca51" +dependencies = [ + "memchr", +] + [[package]] name = "quote" version = "0.6.13" @@ -4361,7 +4450,7 @@ version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1c533a59c9d8a93a09c6ab31f0fd5e5f4dd1b8fc9434804029839884765d04ea" dependencies = [ - "indexmap", + "indexmap 1.9.2", "itoa", "ryu", "serde", @@ -4700,7 +4789,7 @@ dependencies = [ "ahash 0.7.6", "anyhow", "crc", - "indexmap", + "indexmap 1.9.2", "is-macro", "once_cell", "parking_lot 0.12.1", @@ -4756,7 +4845,7 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89c8fc2c12bb1634c7c32fc3c9b6b963ad8f034cc62c4ecddcf215dc4f6f959d" dependencies = [ - "indexmap", + "indexmap 1.9.2", "serde", "serde_json", "swc_config_macro", @@ -4878,7 +4967,7 @@ checksum = "6232e641bef05c462bc7da34a3771f9b3f1f3352349ae0cd72b8eee8b0f5d5e0" dependencies = [ "better_scoped_tls", "bitflags 2.1.0", - "indexmap", + "indexmap 1.9.2", "once_cell", "phf", "rustc-hash", @@ -4928,7 +5017,7 @@ checksum = "8d27c12926427f235d149e60f9a9e67a2181fe1eb418c12b53b8e0778c5052a2" dependencies = [ "ahash 0.7.6", "dashmap", - "indexmap", + "indexmap 1.9.2", "once_cell", "petgraph", "rustc-hash", @@ -4974,7 +5063,7 @@ dependencies = [ "ahash 0.7.6", "base64 0.13.1", "dashmap", - "indexmap", + "indexmap 1.9.2", "once_cell", "serde", "sha-1", @@ -5012,7 +5101,7 @@ version = "0.117.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad791bbfdafcebd878584021e050964c8ab68aba7eeac9d0ee4afba4c284a629" dependencies = [ - "indexmap", + "indexmap 1.9.2", "num_cpus", "once_cell", "rustc-hash", @@ -5056,7 +5145,7 @@ version = "0.19.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6291149aec4ba55076fd54a12ceb84cac1f703b2f571c3b2f19aa66ab9ec3009" dependencies = [ - "indexmap", + "indexmap 1.9.2", "petgraph", "rustc-hash", "swc_common", @@ -5278,6 +5367,17 @@ dependencies = [ "syn 2.0.22", ] +[[package]] +name = "time" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi", +] + [[package]] name = "time" version = "0.3.20" @@ -5430,7 +5530,7 @@ version = "0.19.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc18466501acd8ac6a3f615dd29a3438f8ca6bb3b19537138b3106e575621274" dependencies = [ - "indexmap", + "indexmap 1.9.2", "toml_datetime", "winnow", ] @@ -5551,7 +5651,7 @@ dependencies = [ "radix_trie", "rand", "thiserror", - "time", + "time 0.3.20", "tokio", "tracing", "trust-dns-proto", @@ -5618,7 +5718,7 @@ dependencies = [ "futures-util", "serde", "thiserror", - "time", + "time 0.3.20", "tokio", "toml", "tracing", @@ -5897,6 +5997,12 @@ version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -6059,6 +6165,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" +dependencies = [ + "windows-targets 0.48.0", +] + [[package]] name = "windows-sys" version = "0.42.0" @@ -6258,7 +6373,7 @@ dependencies = [ "oid-registry", "rusticata-macros", "thiserror", - "time", + "time 0.3.20", ] [[package]] diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 4c000ea55d..88ec8050a0 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -92,6 +92,7 @@ once_cell.workspace = true os_pipe.workspace = true percent-encoding.workspace = true pin-project.workspace = true +quick-junit = "^0.3.3" rand = { workspace = true, features = ["small_rng"] } regex.workspace = true ring.workspace = true diff --git a/cli/args/flags.rs b/cli/args/flags.rs index d06a17a06e..3f4498dac5 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -221,6 +221,7 @@ pub struct TestFlags { pub concurrent_jobs: Option, pub trace_ops: bool, pub watch: Option, + pub junit_path: Option, } #[derive(Clone, Debug, Eq, PartialEq)] @@ -1848,6 +1849,16 @@ Directory arguments are expanded to all contained files matching the glob ) .arg(no_clear_screen_arg()) .arg(script_arg().last(true)) + .arg( + Arg::new("junit") + .long("junit") + .value_name("PATH") + .value_hint(ValueHint::FilePath) + .help("Write a JUnit XML test report to PATH. Use '-' to write to stdout which is the default when PATH is not provided.") + .num_args(0..=1) + .require_equals(true) + .default_missing_value("-") + ) ) } @@ -3034,6 +3045,8 @@ fn test_parse(flags: &mut Flags, matches: &mut ArgMatches) { Vec::new() }; + let junit_path = matches.remove_one::("junit"); + flags.subcommand = DenoSubcommand::Test(TestFlags { no_run, doc, @@ -3046,6 +3059,7 @@ fn test_parse(flags: &mut Flags, matches: &mut ArgMatches) { concurrent_jobs, trace_ops, watch: watch_arg_parse(matches), + junit_path, }); } @@ -5910,6 +5924,7 @@ mod tests { trace_ops: true, coverage_dir: Some("cov".to_string()), watch: Default::default(), + junit_path: None, }), unstable: true, no_prompt: true, @@ -5988,6 +6003,7 @@ mod tests { trace_ops: false, coverage_dir: None, watch: Default::default(), + junit_path: None, }), type_check_mode: TypeCheckMode::Local, no_prompt: true, @@ -6020,6 +6036,7 @@ mod tests { trace_ops: false, coverage_dir: None, watch: Default::default(), + junit_path: None, }), type_check_mode: TypeCheckMode::Local, no_prompt: true, @@ -6056,6 +6073,7 @@ mod tests { trace_ops: false, coverage_dir: None, watch: Default::default(), + junit_path: None, }), no_prompt: true, type_check_mode: TypeCheckMode::Local, @@ -6086,6 +6104,7 @@ mod tests { trace_ops: false, coverage_dir: None, watch: Default::default(), + junit_path: None, }), no_prompt: true, type_check_mode: TypeCheckMode::Local, @@ -6117,6 +6136,7 @@ mod tests { watch: Some(WatchFlags { no_clear_screen: false, }), + junit_path: None, }), no_prompt: true, type_check_mode: TypeCheckMode::Local, @@ -6147,6 +6167,7 @@ mod tests { watch: Some(WatchFlags { no_clear_screen: false, }), + junit_path: None, }), no_prompt: true, type_check_mode: TypeCheckMode::Local, @@ -6179,6 +6200,67 @@ mod tests { watch: Some(WatchFlags { no_clear_screen: true, }), + junit_path: None, + }), + type_check_mode: TypeCheckMode::Local, + no_prompt: true, + ..Flags::default() + } + ); + } + + #[test] + fn test_junit_default() { + let r = flags_from_vec(svec!["deno", "test", "--junit"]); + assert_eq!( + r.unwrap(), + Flags { + subcommand: DenoSubcommand::Test(TestFlags { + no_run: false, + doc: false, + fail_fast: None, + filter: None, + allow_none: false, + shuffle: None, + files: FileFlags { + include: vec![], + ignore: vec![], + }, + concurrent_jobs: None, + trace_ops: false, + coverage_dir: None, + watch: Default::default(), + junit_path: Some("-".to_string()), + }), + type_check_mode: TypeCheckMode::Local, + no_prompt: true, + ..Flags::default() + } + ); + } + + #[test] + fn test_junit_with_path() { + let r = flags_from_vec(svec!["deno", "test", "--junit=junit.xml"]); + assert_eq!( + r.unwrap(), + Flags { + subcommand: DenoSubcommand::Test(TestFlags { + no_run: false, + doc: false, + fail_fast: None, + filter: None, + allow_none: false, + shuffle: None, + files: FileFlags { + include: vec![], + ignore: vec![], + }, + concurrent_jobs: None, + trace_ops: false, + coverage_dir: None, + watch: Default::default(), + junit_path: Some("junit.xml".to_string()), }), type_check_mode: TypeCheckMode::Local, no_prompt: true, diff --git a/cli/args/mod.rs b/cli/args/mod.rs index 9a6050347f..a979aa10c6 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -227,6 +227,7 @@ pub struct TestOptions { pub shuffle: Option, pub concurrent_jobs: NonZeroUsize, pub trace_ops: bool, + pub junit_path: Option, } impl TestOptions { @@ -251,6 +252,7 @@ impl TestOptions { no_run: test_flags.no_run, shuffle: test_flags.shuffle, trace_ops: test_flags.trace_ops, + junit_path: test_flags.junit_path, }) } } diff --git a/cli/tools/test.rs b/cli/tools/test.rs index 919361eaaa..902d76585a 100644 --- a/cli/tools/test.rs +++ b/cli/tools/test.rs @@ -25,6 +25,9 @@ use crate::worker::CliMainWorkerFactory; use deno_ast::swc::common::comments::CommentKind; use deno_ast::MediaType; use deno_ast::SourceRangedForSpanned; +use deno_core::anyhow; +use deno_core::anyhow::bail; +use deno_core::anyhow::Context as _; use deno_core::error::generic_error; use deno_core::error::AnyError; use deno_core::error::JsError; @@ -346,6 +349,7 @@ struct TestSpecifiersOptions { fail_fast: Option, log_level: Option, specifier: TestSpecifierOptions, + junit_path: Option, } #[derive(Debug, Clone)] @@ -411,13 +415,158 @@ trait TestReporter { tests: &IndexMap, test_steps: &IndexMap, ); + fn flush_report( + &mut self, + elapsed: &Duration, + tests: &IndexMap, + test_steps: &IndexMap, + ) -> anyhow::Result<()>; } fn get_test_reporter(options: &TestSpecifiersOptions) -> Box { - Box::new(PrettyTestReporter::new( + let pretty = Box::new(PrettyTestReporter::new( options.concurrent_jobs.get() > 1, options.log_level != Some(Level::Error), - )) + )); + if let Some(junit_path) = &options.junit_path { + let junit = Box::new(JunitTestReporter::new(junit_path.clone())); + // If junit is writing to stdout, only enable the junit reporter + if junit_path == "-" { + junit + } else { + Box::new(CompoundTestReporter::new(vec![pretty, junit])) + } + } else { + pretty + } +} + +struct CompoundTestReporter { + test_reporters: Vec>, +} + +impl CompoundTestReporter { + fn new(test_reporters: Vec>) -> Self { + Self { test_reporters } + } +} + +impl TestReporter for CompoundTestReporter { + fn report_register(&mut self, description: &TestDescription) { + for reporter in &mut self.test_reporters { + reporter.report_register(description); + } + } + + fn report_plan(&mut self, plan: &TestPlan) { + for reporter in &mut self.test_reporters { + reporter.report_plan(plan); + } + } + + fn report_wait(&mut self, description: &TestDescription) { + for reporter in &mut self.test_reporters { + reporter.report_wait(description); + } + } + + fn report_output(&mut self, output: &[u8]) { + for reporter in &mut self.test_reporters { + reporter.report_output(output); + } + } + + fn report_result( + &mut self, + description: &TestDescription, + result: &TestResult, + elapsed: u64, + ) { + for reporter in &mut self.test_reporters { + reporter.report_result(description, result, elapsed); + } + } + + fn report_uncaught_error(&mut self, origin: &str, error: Box) { + for reporter in &mut self.test_reporters { + reporter.report_uncaught_error(origin, error.clone()); + } + } + + fn report_step_register(&mut self, description: &TestStepDescription) { + for reporter in &mut self.test_reporters { + reporter.report_step_register(description) + } + } + + fn report_step_wait(&mut self, description: &TestStepDescription) { + for reporter in &mut self.test_reporters { + reporter.report_step_wait(description) + } + } + + fn report_step_result( + &mut self, + desc: &TestStepDescription, + result: &TestStepResult, + elapsed: u64, + tests: &IndexMap, + test_steps: &IndexMap, + ) { + for reporter in &mut self.test_reporters { + reporter.report_step_result(desc, result, elapsed, tests, test_steps); + } + } + + fn report_summary( + &mut self, + elapsed: &Duration, + tests: &IndexMap, + test_steps: &IndexMap, + ) { + for reporter in &mut self.test_reporters { + reporter.report_summary(elapsed, tests, test_steps); + } + } + + fn report_sigint( + &mut self, + tests_pending: &HashSet, + tests: &IndexMap, + test_steps: &IndexMap, + ) { + for reporter in &mut self.test_reporters { + reporter.report_sigint(tests_pending, tests, test_steps); + } + } + + fn flush_report( + &mut self, + elapsed: &Duration, + tests: &IndexMap, + test_steps: &IndexMap, + ) -> anyhow::Result<()> { + let mut errors = vec![]; + for reporter in &mut self.test_reporters { + if let Err(err) = reporter.flush_report(elapsed, tests, test_steps) { + errors.push(err) + } + } + + if errors.is_empty() { + Ok(()) + } else { + bail!( + "error in one or more wrapped reporters:\n{}", + errors + .iter() + .enumerate() + .fold(String::new(), |acc, (i, err)| { + format!("{}Error #{}: {:?}\n", acc, i + 1, err) + }) + ) + } + } } struct PrettyTestReporter { @@ -965,6 +1114,206 @@ impl TestReporter for PrettyTestReporter { println!(); self.in_new_line = true; } + + fn flush_report( + &mut self, + _elapsed: &Duration, + _tests: &IndexMap, + _test_steps: &IndexMap, + ) -> anyhow::Result<()> { + Ok(()) + } +} + +struct JunitTestReporter { + path: String, + // Stores TestCases (i.e. Tests) by the Test ID + cases: IndexMap, +} + +impl JunitTestReporter { + fn new(path: String) -> Self { + Self { + path, + cases: IndexMap::new(), + } + } + + fn convert_status(status: &TestResult) -> quick_junit::TestCaseStatus { + match status { + TestResult::Ok => quick_junit::TestCaseStatus::success(), + TestResult::Ignored => quick_junit::TestCaseStatus::skipped(), + TestResult::Failed(failure) => quick_junit::TestCaseStatus::NonSuccess { + kind: quick_junit::NonSuccessKind::Failure, + message: Some(failure.to_string()), + ty: None, + description: None, + reruns: vec![], + }, + TestResult::Cancelled => quick_junit::TestCaseStatus::NonSuccess { + kind: quick_junit::NonSuccessKind::Error, + message: Some("Cancelled".to_string()), + ty: None, + description: None, + reruns: vec![], + }, + } + } +} + +impl TestReporter for JunitTestReporter { + fn report_register(&mut self, description: &TestDescription) { + self.cases.insert( + description.id, + quick_junit::TestCase::new( + description.name.clone(), + quick_junit::TestCaseStatus::skipped(), + ), + ); + } + + fn report_plan(&mut self, _plan: &TestPlan) {} + + fn report_wait(&mut self, _description: &TestDescription) {} + + fn report_output(&mut self, _output: &[u8]) { + /* + TODO(skycoop): Right now I can't include stdout/stderr in the report because + we have a global pair of output streams that don't differentiate between the + output of different tests. This is a nice to have feature, so we can come + back to it later + */ + } + + fn report_result( + &mut self, + description: &TestDescription, + result: &TestResult, + elapsed: u64, + ) { + if let Some(case) = self.cases.get_mut(&description.id) { + case.status = Self::convert_status(result); + case.set_time(Duration::from_millis(elapsed)); + } + } + + fn report_uncaught_error(&mut self, _origin: &str, _error: Box) {} + + 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, + _tests: &IndexMap, + test_steps: &IndexMap, + ) { + let status = match result { + TestStepResult::Ok => "passed", + TestStepResult::Ignored => "skipped", + TestStepResult::Failed(_) => "failure", + }; + + let root_id: usize; + let mut name = String::new(); + { + let mut ancestors = vec![]; + let mut current_desc = description; + loop { + if let Some(d) = test_steps.get(¤t_desc.parent_id) { + ancestors.push(&d.name); + current_desc = d; + } else { + root_id = current_desc.parent_id; + break; + } + } + ancestors.reverse(); + for n in ancestors { + name.push_str(n); + name.push_str(" ... "); + } + name.push_str(&description.name); + } + + if let Some(case) = self.cases.get_mut(&root_id) { + case.add_property(quick_junit::Property::new( + format!("step[{}]", status), + name, + )); + } + } + + fn report_summary( + &mut self, + _elapsed: &Duration, + _tests: &IndexMap, + _test_steps: &IndexMap, + ) { + } + + fn report_sigint( + &mut self, + tests_pending: &HashSet, + tests: &IndexMap, + _test_steps: &IndexMap, + ) { + for id in tests_pending { + if let Some(description) = tests.get(id) { + self.report_result(description, &TestResult::Cancelled, 0) + } + } + } + + fn flush_report( + &mut self, + elapsed: &Duration, + tests: &IndexMap, + _test_steps: &IndexMap, + ) -> anyhow::Result<()> { + let mut suites: IndexMap = IndexMap::new(); + for (id, case) in &self.cases { + if let Some(test) = tests.get(id) { + suites + .entry(test.location.file_name.clone()) + .and_modify(|s| { + s.add_test_case(case.clone()); + }) + .or_insert_with(|| { + quick_junit::TestSuite::new(test.location.file_name.clone()) + .add_test_case(case.clone()) + .to_owned() + }); + } + } + + let mut report = quick_junit::Report::new("deno test"); + report.set_time(*elapsed).add_test_suites( + suites + .values() + .cloned() + .collect::>(), + ); + + if self.path == "-" { + report + .serialize(std::io::stdout()) + .with_context(|| "Failed to write JUnit report to stdout")?; + } else { + let file = + std::fs::File::create(self.path.clone()).with_context(|| { + format!("Failed to open JUnit report file {}", self.path) + })?; + report.serialize(file).with_context(|| { + format!("Failed to write JUnit report to {}", self.path) + })?; + } + + Ok(()) + } } fn abbreviate_test_error(js_error: &JsError) -> JsError { @@ -1547,6 +1896,7 @@ async fn test_specifiers( } TestEvent::Sigint => { + let elapsed = Instant::now().duration_since(earlier); reporter.report_sigint( &tests_started .difference(&tests_with_result) @@ -1555,6 +1905,11 @@ async fn test_specifiers( &tests, &test_steps, ); + if let Err(err) = + reporter.flush_report(&elapsed, &tests, &test_steps) + { + eprint!("Test reporter failed to flush: {}", err) + } std::process::exit(130); } } @@ -1565,6 +1920,12 @@ async fn test_specifiers( let elapsed = Instant::now().duration_since(earlier); reporter.report_summary(&elapsed, &tests, &test_steps); + if let Err(err) = reporter.flush_report(&elapsed, &tests, &test_steps) { + return Err(generic_error(format!( + "Test reporter failed to flush: {}", + err + ))); + } if used_only { return Err(generic_error( @@ -1756,6 +2117,7 @@ pub async fn run_tests( concurrent_jobs: test_options.concurrent_jobs, fail_fast: test_options.fail_fast, log_level, + junit_path: test_options.junit_path, specifier: TestSpecifierOptions { filter: TestFilter::from_flag(&test_options.filter), shuffle: test_options.shuffle, @@ -1886,6 +2248,7 @@ pub async fn run_tests_with_watch( concurrent_jobs: test_options.concurrent_jobs, fail_fast: test_options.fail_fast, log_level, + junit_path: test_options.junit_path, specifier: TestSpecifierOptions { filter: TestFilter::from_flag(&test_options.filter), shuffle: test_options.shuffle,