Skip to content

Commit

Permalink
Add .cairo-coverage-ignore (#109)
Browse files Browse the repository at this point in the history
**Stack**:
- #111
- #110
- #109 ⬅
- #108
  • Loading branch information
ksew1 authored Dec 5, 2024
1 parent 2d83ac2 commit 6f45f5c
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ cairo-lang-sierra-to-casm = "2.8.2"
cairo-lang-starknet-classes = "2.8.2"
derived-deref = "2.1.0"
itertools = "0.13.0"
ignore = "0.4.23"
serde = "1.0.215"
serde_json = "1.0.133"
snapbox = "0.6.20"
Expand Down
1 change: 1 addition & 0 deletions crates/cairo-coverage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ cairo-lang-starknet-classes.workspace = true
clap.workspace = true
derived-deref.workspace = true
itertools.workspace = true
ignore.workspace = true
serde.workspace = true
serde_json.workspace = true
regex.workspace = true
Expand Down
49 changes: 49 additions & 0 deletions crates/cairo-coverage/src/input/filter/ignore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use crate::input::filter::statement_category_filter::VIRTUAL_FILE_REGEX;
use camino::{Utf8Path, Utf8PathBuf};
use ignore::gitignore::Gitignore;
use ignore::Match;

const CAIRO_COVERAGE_IGNORE: &str = ".cairo-coverage-ignore";

pub struct CairoCoverageIgnoreMatcher(Gitignore);

impl CairoCoverageIgnoreMatcher {
/// Create a new instance of the [`CairoCoverageIgnoreMatcher`] that will be based on the [`CAIRO_COVERAGE_IGNORE`] file.
pub fn new(path: &Utf8Path) -> anyhow::Result<Self> {
let ignore_matcher = find_ignore_file(path)
.map(Gitignore::new)
.map(|(ignore, error)| {
if let Some(error) = error {
Err(anyhow::Error::from(error))
} else {
Ok(ignore)
}
})
.transpose()?
.unwrap_or_else(Gitignore::empty);

Ok(Self(ignore_matcher))
}

/// Check if the given path is ignored by the [`CAIRO_COVERAGE_IGNORE`] file.
pub fn is_ignored(&self, path: &str) -> bool {
let path: Utf8PathBuf = VIRTUAL_FILE_REGEX.replace_all(path, "").to_string().into();
let result = self.0.matched(&path, path.is_dir());
matches!(result, Match::Ignore(_))
}
}

/// Search for a [`CAIRO_COVERAGE_IGNORE`] file from the given directory and its parents until the root.
fn find_ignore_file(start_dir: &Utf8Path) -> Option<Utf8PathBuf> {
let mut current_dir = Some(start_dir);

while let Some(dir) = current_dir {
let candidate = dir.join(CAIRO_COVERAGE_IGNORE);
if candidate.is_file() {
return Some(candidate);
}
current_dir = dir.parent();
}

None
}
1 change: 1 addition & 0 deletions crates/cairo-coverage/src/input/filter/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod ignore;
pub mod statement_category_filter;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::cli::IncludedComponent;
use crate::data_loader::LoadedData;
use crate::input::filter::ignore::CairoCoverageIgnoreMatcher;
use crate::input::sierra_to_cairo_map::{SimpleLibfuncName, StatementOrigin};
use cairo_annotations::annotations::coverage::SourceFileFullPath;
use cairo_annotations::annotations::profiler::FunctionName;
Expand Down Expand Up @@ -48,6 +49,7 @@ enum StatementCategory {
NonUserFunction,
Macro,
NotReliableLibfunc,
Ignored,
}

impl From<IncludedComponent> for StatementCategory {
Expand All @@ -63,6 +65,7 @@ pub struct StatementCategoryFilter {
user_project_path: String,
allowed_statement_categories: HashSet<StatementCategory>,
test_functions: HashSet<String>,
ignore_matcher: CairoCoverageIgnoreMatcher,
}

impl StatementCategoryFilter {
Expand All @@ -87,12 +90,15 @@ impl StatementCategoryFilter {
.chain(once(StatementCategory::UserFunction))
.collect();

let ignore_matcher = CairoCoverageIgnoreMatcher::new(user_project_path)
.expect("Failed to create ignore matcher");
let user_project_path = user_project_path.to_string();

Self {
user_project_path,
allowed_statement_categories,
test_functions,
ignore_matcher,
}
}

Expand Down Expand Up @@ -128,6 +134,10 @@ impl StatementCategoryFilter {
labels.insert(StatementCategory::NotReliableLibfunc);
}

if self.ignore_matcher.is_ignored(source_file_full_path) {
labels.insert(StatementCategory::Ignored);
}

labels
}
}
Expand Down

0 comments on commit 6f45f5c

Please sign in to comment.