Skip to content

Commit

Permalink
Enhance User Project Identification in Cairo Coverage
Browse files Browse the repository at this point in the history
commit-id:77eb0f76
  • Loading branch information
ksew1 committed Sep 13, 2024
1 parent 7d05962 commit bb3a29b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
#### Fixed

- Bug where hit count was not correctly calculated for functions declared at same line
- Bug where functions not from user project were included in coverage report if `SCARB_CACHE` was set

#### Changed

Expand Down
36 changes: 27 additions & 9 deletions crates/cairo-coverage/src/input/filter.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use crate::cli::IncludedComponent;
use crate::data_loader::LoadedData;
use crate::input::sierra_to_cairo_map::StatementOrigin;
use anyhow::{Context, Result};
use camino::Utf8PathBuf;
use regex::Regex;
use std::collections::HashSet;
use std::iter::once;
use std::sync::LazyLock;

pub static VIRTUAL_FILE_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\[.*?]").unwrap());
const SNFORGE_TEST_EXECUTABLE: &str = "snforge_internal_test_executable";
const SNFORGE_SIERRA_DIR: &str = ".snfoundry_versioned_programs";

#[derive(Eq, PartialEq, Hash)]
enum StatementCategory {
Expand All @@ -27,12 +30,17 @@ impl From<IncludedComponent> for StatementCategory {
}

pub struct Filter {
user_project_path: String,
allowed_statement_categories: HashSet<StatementCategory>,
test_functions: HashSet<String>,
}

impl Filter {
pub fn new(included_component: &[IncludedComponent], loaded_data: &LoadedData) -> Self {
pub fn new(
source_sierra_path: &str,
included_component: &[IncludedComponent],
loaded_data: &LoadedData,
) -> Result<Self> {
let test_functions = loaded_data
.debug_info
.executables
Expand All @@ -49,10 +57,12 @@ impl Filter {
.chain(once(StatementCategory::UserFunction))
.collect();

Self {
let user_project_path = find_user_project_path(source_sierra_path)?;
Ok(Self {
user_project_path,
allowed_statement_categories,
test_functions,
}
})
}

pub fn should_include(&self, statement_origin: &StatementOrigin) -> bool {
Expand All @@ -76,15 +86,23 @@ impl Filter {
labels.insert(StatementCategory::Macro);
}

// TODO(#55)
// TODO: We should probably filter by path to user project not by path to cache
// TODO: Can get this from source_sierra_path in call trace
if file_location.contains("com.swmansion.scarb") || file_location.contains(".cache/scarb") {
labels.insert(StatementCategory::NonUserFunction);
} else {
if file_location.contains(&self.user_project_path) {
labels.insert(StatementCategory::UserFunction);
} else {
labels.insert(StatementCategory::NonUserFunction);
}

labels
}
}

fn find_user_project_path(source_sierra_path: &str) -> Result<String> {
Utf8PathBuf::from(source_sierra_path)
.parent()
.filter(|parent| parent.ends_with(SNFORGE_SIERRA_DIR))
.and_then(|parent| parent.parent())
.map(ToString::to_string)
.context(format!(
"Source sierra path should be in the format: <project_root>/{SNFORGE_SIERRA_DIR}/<file>.sierra.json, got: {source_sierra_path}"
))
}
4 changes: 2 additions & 2 deletions crates/cairo-coverage/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ fn main() -> Result<()> {
.context(format!("Failed to open output file at path: {output_path}"))?;

let loaded_data = LoadedDataMap::load(&cli.trace_files)?;
for (_, loaded_data) in loaded_data.iter() {
let filter = Filter::new(&cli.include, loaded_data);
for (source_sierra_path, loaded_data) in loaded_data.iter() {
let filter = Filter::new(source_sierra_path, &cli.include, loaded_data)?;
let input_data = InputData::new(loaded_data, &filter)?;
let coverage_data = create_files_coverage_data_with_hits(&input_data);
let output_data = LcovFormat::from(coverage_data);
Expand Down

0 comments on commit bb3a29b

Please sign in to comment.