Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): panic with deno coverage #23353

Merged
merged 19 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion cli/tools/coverage/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
54 changes: 54 additions & 0 deletions tests/integration/coverage_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}