diff options
author | Evan <96965321+0xIchigo@users.noreply.github.com> | 2024-05-13 17:18:38 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-13 23:18:38 +0200 |
commit | 329a8ae0c098619c8f976f2035e5599094cb43f4 (patch) | |
tree | e4e022c48a1dc57a2fb59243e25c855b15b1d5cd | |
parent | 88529f0b47667435689c6ef4f81af80cead7d557 (diff) |
fix(cli): panic with `deno coverage` (#23353)
This PR directly addresses the issue raised in #23282 where Deno panics
if `deno coverage` is called with `--include` regex that returns no
matches.
I've opted not to change the return value of `collect_summary` for
simplicity and return an empty `HashMap` instead
-rw-r--r-- | cli/tools/coverage/reporter.rs | 7 | ||||
-rw-r--r-- | tests/integration/coverage_tests.rs | 54 |
2 files changed, 60 insertions, 1 deletions
diff --git a/cli/tools/coverage/reporter.rs b/cli/tools/coverage/reporter.rs index f6f8144a4..6547f2036 100644 --- a/cli/tools/coverage/reporter.rs +++ b/cli/tools/coverage/reporter.rs @@ -52,7 +52,12 @@ pub trait CoverageReporter { file_reports: &'a Vec<(CoverageReport, String)>, ) -> CoverageSummary { let urls = file_reports.iter().map(|rep| &rep.0.url).collect(); - let root = util::find_root(urls).unwrap().to_file_path().unwrap(); + let root = match util::find_root(urls) + .and_then(|root_path| root_path.to_file_path().ok()) + { + Some(path) => path, + None => return HashMap::new(), + }; // summary by file or directory // tuple of (line hit, line miss, branch hit, branch miss, parent) let mut summary = HashMap::new(); diff --git a/tests/integration/coverage_tests.rs b/tests/integration/coverage_tests.rs index a8a7ca023..6e9a1454c 100644 --- a/tests/integration/coverage_tests.rs +++ b/tests/integration/coverage_tests.rs @@ -555,3 +555,57 @@ File | Branch % | Line % | ", ); } + +#[test] +fn test_collect_summary_with_no_matches() { + let context: TestContext = TestContext::default(); + let temp_dir: &TempDir = context.temp_dir(); + let temp_dir_path: PathRef = PathRef::new(temp_dir.path().join("cov")); + + let empty_test_dir: PathRef = temp_dir_path.join("empty_dir"); + empty_test_dir.create_dir_all(); + + let output: util::TestCommandOutput = context + .new_command() + .args_vec(vec![ + "test".to_string(), + "--quiet".to_string(), + "--allow-read".to_string(), + format!("--coverage={}", temp_dir_path.as_path().display()), + empty_test_dir.as_path().to_str().unwrap().to_string(), + ]) + .run(); + + output.assert_exit_code(1); + + let actual: &str = output.combined_output(); + let expected_message: &str = "error: No test modules found"; + assert_contains!(actual, expected_message); + + // Check the contents of the coverage directory, ignoring 'empty_dir' + let mut unexpected_contents: Vec<std::path::PathBuf> = Vec::new(); + for entry in std::fs::read_dir(temp_dir_path.as_path()) + .unwrap() + .flatten() + { + if entry.file_name() != "empty_dir" { + // Ignore the 'empty_dir' + unexpected_contents.push(entry.path()); + } + } + + // Report unexpected contents + if !unexpected_contents.is_empty() { + eprintln!("Unexpected files or directories in the coverage directory:"); + for path in &unexpected_contents { + eprintln!("{:?}", path); + } + } + + // Assert that the coverage directory is otherwise empty + assert!( + unexpected_contents.is_empty(), + "Expected the coverage directory to be empty except for 'empty_dir', but found: {:?}", + unexpected_contents + ); +} |