-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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: add option to ignore directories from coverage report #7301
Changes from 2 commits
3e7a3c1
92baf15
2e94a0c
0b609db
bf3244e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,16 @@ use std::{ | |
fmt::Display, | ||
ops::{AddAssign, Deref, DerefMut}, | ||
}; | ||
use std::path::Path; | ||
|
||
use eyre::{Context, Result}; | ||
use foundry_common::CoverageFilter; | ||
|
||
pub mod analysis; | ||
pub mod anchors; | ||
|
||
mod inspector; | ||
|
||
pub use inspector::CoverageCollector; | ||
|
||
/// A coverage report. | ||
|
@@ -75,7 +78,7 @@ impl CoverageReport { | |
} | ||
|
||
/// Get coverage summaries by source file path | ||
pub fn summary_by_file(&self) -> impl Iterator<Item = (String, CoverageSummary)> { | ||
pub fn summary_by_file(&self) -> impl Iterator<Item=(String, CoverageSummary)> { | ||
let mut summaries: BTreeMap<String, CoverageSummary> = BTreeMap::new(); | ||
|
||
for (version, items) in self.items.iter() { | ||
|
@@ -98,7 +101,7 @@ impl CoverageReport { | |
} | ||
|
||
/// Get coverage items by source file path | ||
pub fn items_by_source(&self) -> impl Iterator<Item = (String, Vec<CoverageItem>)> { | ||
pub fn items_by_source(&self) -> impl Iterator<Item=(String, Vec<CoverageItem>)> { | ||
let mut items_by_source: BTreeMap<String, Vec<CoverageItem>> = BTreeMap::new(); | ||
|
||
for (version, items) in self.items.iter() { | ||
|
@@ -151,6 +154,30 @@ impl CoverageReport { | |
} | ||
Ok(()) | ||
} | ||
|
||
pub fn filter_out_ignored_sources(&mut self, filter: &impl CoverageFilter) { | ||
let mut new_items = HashMap::new(); | ||
for (version, items) in self.items.iter() { | ||
let new_items_for_version = items | ||
.iter() | ||
.filter(|item| filter.matches_file_path( | ||
Path::new(&self.get_source_path(version, item.loc.source_id)) | ||
)) | ||
.cloned() | ||
.collect(); | ||
new_items.insert(version.clone(), new_items_for_version); | ||
} | ||
self.items = new_items; | ||
} | ||
|
||
fn get_source_path(&self, version: &Version, source_id: usize) -> String { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. docs here too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added |
||
self.source_paths | ||
.get(&(version.clone(), source_id)) | ||
.cloned() | ||
.unwrap_or_else(|| { | ||
format!("Unknown (ID: {}, solc: {version})", source_id) | ||
}) | ||
} | ||
} | ||
|
||
/// A collection of [HitMap]s | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ use foundry_common::glob::GlobMatcher; | |
use foundry_compilers::{FileFilter, ProjectPathsConfig}; | ||
use foundry_config::Config; | ||
use std::{fmt, path::Path}; | ||
use foundry_common::CoverageFilter; | ||
|
||
/// The filter to use during testing. | ||
/// | ||
|
@@ -40,6 +41,10 @@ pub struct FilterArgs { | |
value_name = "GLOB" | ||
)] | ||
pub path_pattern_inverse: Option<GlobMatcher>, | ||
|
||
/// Only show coverage for files that do not match the specified glob pattern. | ||
#[arg(long = "no-coverage-path", visible_alias = "ncp", value_name = "GLOB")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's name it ignore instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. renamed |
||
pub path_pattern_ignore_coverage: Option<GlobMatcher>, | ||
} | ||
|
||
impl FilterArgs { | ||
|
@@ -73,6 +78,9 @@ impl FilterArgs { | |
if self.path_pattern_inverse.is_none() { | ||
self.path_pattern_inverse = config.path_pattern_inverse.clone().map(Into::into); | ||
} | ||
if self.path_pattern_ignore_coverage.is_none() { | ||
self.path_pattern_ignore_coverage = config.path_pattern_ignore_coverage.clone().map(Into::into); | ||
} | ||
ProjectPathsAwareFilter { args_filter: self, paths: config.project_paths() } | ||
} | ||
} | ||
|
@@ -86,6 +94,7 @@ impl fmt::Debug for FilterArgs { | |
.field("no-match-contract", &self.contract_pattern_inverse.as_ref().map(|r| r.as_str())) | ||
.field("match-path", &self.path_pattern.as_ref().map(|g| g.as_str())) | ||
.field("no-match-path", &self.path_pattern_inverse.as_ref().map(|g| g.as_str())) | ||
.field("no-coverage-path", &self.path_pattern_ignore_coverage.as_ref().map(|g| g.as_str())) | ||
.finish_non_exhaustive() | ||
} | ||
} | ||
|
@@ -97,10 +106,10 @@ impl FileFilter for FilterArgs { | |
/// [`FoundryPathExt::is_sol_test()`]. | ||
fn is_match(&self, file: &Path) -> bool { | ||
if let Some(glob) = &self.path_pattern { | ||
return glob.is_match(file) | ||
return glob.is_match(file); | ||
} | ||
if let Some(glob) = &self.path_pattern_inverse { | ||
return !glob.is_match(file) | ||
return !glob.is_match(file); | ||
} | ||
file.is_sol_test() | ||
} | ||
|
@@ -141,6 +150,16 @@ impl TestFilter for FilterArgs { | |
} | ||
} | ||
|
||
impl CoverageFilter for FilterArgs { | ||
/// Returns true if the file path does not match the ignore coverage pattern. | ||
fn matches_file_path(&self, path: &Path) -> bool { | ||
if let Some(glob) = &self.path_pattern_ignore_coverage { | ||
return !glob.is_match(path); | ||
} | ||
true | ||
} | ||
} | ||
|
||
impl fmt::Display for FilterArgs { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
if let Some(p) = &self.test_pattern { | ||
|
@@ -161,6 +180,9 @@ impl fmt::Display for FilterArgs { | |
if let Some(p) = &self.path_pattern_inverse { | ||
writeln!(f, "\tno-match-path: `{}`", p.as_str())?; | ||
} | ||
if let Some(p) = &self.path_pattern_ignore_coverage { | ||
writeln!(f, "\tno-coverage-path: `{}`", p.as_str())?; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: docs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added