Skip to content

Commit

Permalink
fix(coverage): handle ignore patterns (denoland#23974)
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k committed May 28, 2024
1 parent c4211e2 commit d99c6c1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
26 changes: 19 additions & 7 deletions cli/tools/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,7 @@ fn collect_coverages(
)?
}
}),
exclude: PathOrPatternSet::from_exclude_relative_path_or_patterns(
initial_cwd,
&files.ignore,
)
.context("Invalid ignore pattern.")?,
exclude: PathOrPatternSet::new(vec![]),
};
let file_paths = FileCollector::new(|e| {
e.path.extension().map(|ext| ext == "json").unwrap_or(false)
Expand All @@ -411,12 +407,28 @@ fn collect_coverages(
.set_vendor_folder(cli_options.vendor_dir_path().map(ToOwned::to_owned))
.collect_file_patterns(file_patterns)?;

let coverage_patterns = FilePatterns {
base: initial_cwd.to_path_buf(),
include: None,
exclude: PathOrPatternSet::from_exclude_relative_path_or_patterns(
initial_cwd,
&files.ignore,
)
.context("Invalid ignore pattern.")?,
};

for file_path in file_paths {
let new_coverage = fs::read_to_string(file_path.as_path())
.map_err(AnyError::from)
.and_then(|json| serde_json::from_str(&json).map_err(AnyError::from))
.and_then(|json| {
serde_json::from_str::<cdp::ScriptCoverage>(&json)
.map_err(AnyError::from)
})
.with_context(|| format!("Failed reading '{}'", file_path.display()))?;
coverages.push(new_coverage);
let url = Url::parse(&new_coverage.url)?;
if coverage_patterns.matches_specifier(&url) {
coverages.push(new_coverage);
}
}

coverages.sort_by_key(|k| k.url.clone());
Expand Down
45 changes: 36 additions & 9 deletions tests/integration/coverage_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,14 +547,15 @@ fn test_summary_reporter() {
output.assert_exit_code(0);
output.skip_output_check();

let output = context
.new_command()
.args_vec(vec!["coverage".to_string(), format!("{}/", tempdir)])
.run();

output.assert_exit_code(0);
output.assert_matches_text(
"----------------------------------
{
let output = context
.new_command()
.args_vec(vec!["coverage".to_string(), format!("{}/", tempdir)])
.run();

output.assert_exit_code(0);
output.assert_matches_text(
"----------------------------------
File | Branch % | Line % |
----------------------------------
bar.ts | 0.0 | 57.1 |
Expand All @@ -565,7 +566,33 @@ File | Branch % | Line % |
All files | 40.0 | 61.0 |
----------------------------------
",
);
);
}

// test --ignore flag works
{
let output = context
.new_command()
.args_vec(vec![
"coverage".to_string(),
format!("{}/", tempdir),
"--ignore=**/bar.ts,**/quux.ts".to_string(),
])
.run();

output.assert_exit_code(0);
output.assert_matches_text(
"---------------------------------
File | Branch % | Line % |
---------------------------------
baz/qux.ts | 100.0 | 100.0 |
foo.ts | 50.0 | 76.9 |
---------------------------------
All files | 66.7 | 85.0 |
---------------------------------
",
);
}
}

#[test]
Expand Down

0 comments on commit d99c6c1

Please sign in to comment.