-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mod ignore; | ||
pub mod statement_category_filter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters