diff options
Diffstat (limited to 'cli')
-rw-r--r-- | cli/Cargo.toml | 1 | ||||
-rw-r--r-- | cli/args/flags.rs | 82 | ||||
-rw-r--r-- | cli/args/mod.rs | 2 | ||||
-rw-r--r-- | cli/tools/test.rs | 367 |
4 files changed, 450 insertions, 2 deletions
diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 4c000ea55..88ec8050a 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 d06a17a06..3f4498dac 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -221,6 +221,7 @@ pub struct TestFlags { pub concurrent_jobs: Option<NonZeroUsize>, pub trace_ops: bool, pub watch: Option<WatchFlags>, + pub junit_path: Option<String>, } #[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::<String>("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 9a6050347..a979aa10c 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -227,6 +227,7 @@ pub struct TestOptions { pub shuffle: Option<u64>, pub concurrent_jobs: NonZeroUsize, pub trace_ops: bool, + pub junit_path: Option<String>, } 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 919361eaa..902d76585 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<NonZeroUsize>, log_level: Option<log::Level>, specifier: TestSpecifierOptions, + junit_path: Option<String>, } #[derive(Debug, Clone)] @@ -411,13 +415,158 @@ trait TestReporter { tests: &IndexMap<usize, TestDescription>, test_steps: &IndexMap<usize, TestStepDescription>, ); + fn flush_report( + &mut self, + elapsed: &Duration, + tests: &IndexMap<usize, TestDescription>, + test_steps: &IndexMap<usize, TestStepDescription>, + ) -> anyhow::Result<()>; } fn get_test_reporter(options: &TestSpecifiersOptions) -> Box<dyn TestReporter> { - 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<Box<dyn TestReporter>>, +} + +impl CompoundTestReporter { + fn new(test_reporters: Vec<Box<dyn TestReporter>>) -> 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<JsError>) { + 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<usize, TestDescription>, + test_steps: &IndexMap<usize, TestStepDescription>, + ) { + 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<usize, TestDescription>, + test_steps: &IndexMap<usize, TestStepDescription>, + ) { + for reporter in &mut self.test_reporters { + reporter.report_summary(elapsed, tests, test_steps); + } + } + + fn report_sigint( + &mut self, + tests_pending: &HashSet<usize>, + tests: &IndexMap<usize, TestDescription>, + test_steps: &IndexMap<usize, TestStepDescription>, + ) { + for reporter in &mut self.test_reporters { + reporter.report_sigint(tests_pending, tests, test_steps); + } + } + + fn flush_report( + &mut self, + elapsed: &Duration, + tests: &IndexMap<usize, TestDescription>, + test_steps: &IndexMap<usize, TestStepDescription>, + ) -> 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<usize, TestDescription>, + _test_steps: &IndexMap<usize, TestStepDescription>, + ) -> anyhow::Result<()> { + Ok(()) + } +} + +struct JunitTestReporter { + path: String, + // Stores TestCases (i.e. Tests) by the Test ID + cases: IndexMap<usize, quick_junit::TestCase>, +} + +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<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, + _tests: &IndexMap<usize, TestDescription>, + test_steps: &IndexMap<usize, TestStepDescription>, + ) { + 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<usize, TestDescription>, + _test_steps: &IndexMap<usize, TestStepDescription>, + ) { + } + + fn report_sigint( + &mut self, + tests_pending: &HashSet<usize>, + tests: &IndexMap<usize, TestDescription>, + _test_steps: &IndexMap<usize, TestStepDescription>, + ) { + 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<usize, TestDescription>, + _test_steps: &IndexMap<usize, TestStepDescription>, + ) -> anyhow::Result<()> { + let mut suites: IndexMap<String, quick_junit::TestSuite> = 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::<Vec<quick_junit::TestSuite>>(), + ); + + 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, |