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

feat(coverage): add option to ignore directories and files from coverage report #8321

Merged
merged 18 commits into from
Jul 1, 2024

Conversation

zerosnacks
Copy link
Member

@zerosnacks zerosnacks commented Jul 1, 2024

Motivation

Follow-up of #7301 as I'm not able to push directly to the fork

This PR includes the commits of https://github.com/dimazhornyk

Closes: #2988

Solution

Contrary to #7301 I decided that for flexibility it is preferable to generalize beyond glob and instead use a regex on the filepath (not just file name)

  • Fixes the formatting issues that slipped in
  • Fixes merge conflicts
  • Adds test verifying the applied filter (contractA is filtered out, contractB is preserved)

This allows for a syntax like:

forge coverage --no-match-coverage "(script|test|Foo|Bar)" where script|test are obviously paths and Foo|Bar files where a glob would be limited to a single folder and not individual files

There is currently no warning applied when all sources are filtered out, that could be a follow up nice to have.

Comment on lines +42 to +44
/// Only show coverage for files that do not match the specified regex pattern.
#[arg(long = "no-match-coverage", visible_alias = "nmco", value_name = "REGEX")]
pub coverage_pattern_inverse: Option<regex::Regex>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've opted for no-match-coverage to stay in line with the no-match-* pattern established (no-match-test, no-match-contract, no-match-path)

See: #7301 (comment)

@zerosnacks zerosnacks marked this pull request as ready for review July 1, 2024 12:15
@zerosnacks zerosnacks changed the title feat: add option to ignore directories from coverage report feat(coverage): add option to ignore directories and files from coverage report Jul 1, 2024
///
/// This function should only be called after all the sources were used, otherwise, the output
/// will be missing the ones that are dependent on them.
pub fn filter_out_ignored_sources(&mut self, filter: &impl CoverageFilter) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's just replace this with an Fn(&Path) -> bool

Comment on lines 159 to 175
let mut new_items = HashMap::new();
for (version, items) in self.items.iter() {
let new_items_for_version: Vec<_> = items
.iter()
.filter(|item| {
self.source_paths
.get(&(version.clone(), item.loc.source_id))
.map(|path| filter.matches_file_path(path))
.unwrap_or(false)
})
.cloned()
.collect();
if !new_items_for_version.is_empty() {
new_items.insert(version.clone(), new_items_for_version);
}
}
self.items = new_items;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this just a HashMap::retain?

@zerosnacks zerosnacks requested a review from mattsse July 1, 2024 14:31
@mattsse mattsse requested a review from klkvr July 1, 2024 18:30
Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

pending @klkvr

Copy link
Member

@klkvr klkvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mattsse mattsse merged commit 20b3da1 into master Jul 1, 2024
19 checks passed
@mattsse mattsse deleted the zerosnacks/ignore-directories-coverage branch July 1, 2024 18:55
@gosuto-inzasheru
Copy link

thanks for this 🥇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

forge coverage should allow excluding contracts
5 participants