diff --git a/Cargo.lock b/Cargo.lock index 06e704c..7cd6c49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -226,6 +226,7 @@ dependencies = [ "camino", "clap", "console", + "scarb-metadata", "snapbox", "walkdir", "which", @@ -249,7 +250,6 @@ dependencies = [ "itertools 0.14.0", "rayon", "regex", - "scarb-metadata", "serde", "serde_json", ] diff --git a/crates/cairo-coverage-core/Cargo.toml b/crates/cairo-coverage-core/Cargo.toml index 1cbbafb..c591d6d 100644 --- a/crates/cairo-coverage-core/Cargo.toml +++ b/crates/cairo-coverage-core/Cargo.toml @@ -14,7 +14,6 @@ itertools.workspace = true ignore.workspace = true serde.workspace = true serde_json.workspace = true -scarb-metadata.workspace = true regex.workspace = true indoc.workspace = true rayon.workspace = true diff --git a/crates/cairo-coverage-core/benches/benchmarks/mod.rs b/crates/cairo-coverage-core/benches/benchmarks/mod.rs index 65bb37b..f21282f 100644 --- a/crates/cairo-coverage-core/benches/benchmarks/mod.rs +++ b/crates/cairo-coverage-core/benches/benchmarks/mod.rs @@ -1,4 +1,3 @@ -use cairo_coverage_core::args::RunOptions; use cairo_coverage_test_utils::read_files_from_dir; use camino::Utf8PathBuf; use criterion::Criterion; @@ -11,17 +10,11 @@ fn trace_files_for_benches(dir_name: &str) -> Vec { read_files_from_dir(format!("benches/project-traces/{dir_name}/trace")) } -/// Create [`RunOptions`] with set `project_path` and empty `include`. -/// If we leave the `project_path` as `None`, the `scarb_metadata` will fail. -fn run_options(dir_name: &str) -> RunOptions { - let project_path = Utf8PathBuf::from(format!("benches/project-traces/{dir_name}")) +/// Return `project_path` and for the benchmark project. +fn project_path(dir_name: &str) -> Utf8PathBuf { + Utf8PathBuf::from(format!("benches/project-traces/{dir_name}")) .canonicalize_utf8() .unwrap() - .into(); - RunOptions { - include: Vec::default(), - project_path, - } } /// Config of [`Criterion`] that should be used for all benchmarks. diff --git a/crates/cairo-coverage-core/benches/benchmarks/starknet_staking.rs b/crates/cairo-coverage-core/benches/benchmarks/starknet_staking.rs index af0bb37..8103c71 100644 --- a/crates/cairo-coverage-core/benches/benchmarks/starknet_staking.rs +++ b/crates/cairo-coverage-core/benches/benchmarks/starknet_staking.rs @@ -1,4 +1,5 @@ -use crate::benchmarks::{config, run_options, trace_files_for_benches}; +use crate::benchmarks::{config, project_path, trace_files_for_benches}; +use cairo_coverage_core::args::RunOptions; use criterion::{Criterion, criterion_group}; use std::hint::black_box; @@ -8,11 +9,13 @@ const PROJECT_NAME: &str = "starknet-staking"; /// The trace files should be generated using `download_bench_project.sh` script. fn starknet_staking_benchmark(c: &mut Criterion) { let trace_files = trace_files_for_benches(PROJECT_NAME); - let run_options = run_options(PROJECT_NAME); + let project_path = project_path(PROJECT_NAME); + let run_options = RunOptions::default(); c.bench_function("starknet-staking", |b| { b.iter(|| { cairo_coverage_core::run( black_box(trace_files.clone()), + black_box(project_path.clone()), black_box(run_options.clone()), ) }); diff --git a/crates/cairo-coverage-core/src/args.rs b/crates/cairo-coverage-core/src/args.rs index bb8f6d9..4cd6fe6 100644 --- a/crates/cairo-coverage-core/src/args.rs +++ b/crates/cairo-coverage-core/src/args.rs @@ -1,13 +1,8 @@ -use camino::Utf8PathBuf; - /// Options accepted by `cairo_coverage_core` `run` function. #[derive(Default, Clone)] pub struct RunOptions { /// Include additional components in the coverage report. pub include: Vec, - - /// Path to the project directory. If not provided, the project directory is inferred from the trace. - pub project_path: Option, } /// Additional components that can be included in the coverage report. diff --git a/crates/cairo-coverage-core/src/lib.rs b/crates/cairo-coverage-core/src/lib.rs index 99f3684..f30bebb 100644 --- a/crates/cairo-coverage-core/src/lib.rs +++ b/crates/cairo-coverage-core/src/lib.rs @@ -15,26 +15,17 @@ use crate::output::lcov; use anyhow::{Context, Result}; use camino::Utf8PathBuf; use rayon::iter::{IntoParallelIterator, ParallelIterator}; -use scarb_metadata::{Metadata, MetadataCommand}; -/// Run the core logic of `cairo-coverage` with the provided trace files and [`RunOptions`]. +/// Run the core logic of `cairo-coverage` with the provided trace files, project path and [`RunOptions`]. /// This function generates a coverage report in the LCOV format. /// # Errors /// Fails if it can't produce the coverage report with the error message explaining the reason. #[expect(clippy::needless_pass_by_value)] // In case if we ever needed to take ownership of the arguments. pub fn run( trace_files: Vec, - RunOptions { - include, - project_path, - }: RunOptions, + project_path: Utf8PathBuf, + RunOptions { include }: RunOptions, ) -> Result { - let project_path = if let Some(project_path) = project_path { - project_path - } else { - scarb_metadata()?.workspace.root - }; - let ignore_matcher = ignore_matcher::build(&project_path)?; let coverage_data = execution_data::load(&trace_files)? @@ -59,12 +50,3 @@ pub fn run( Ok(lcov::fmt_string(&coverage_data)) } - -/// Run `scarb metadata` command and return the metadata. -fn scarb_metadata() -> Result { - MetadataCommand::new() - .inherit_stderr() - .inherit_stdout() - .exec() - .context("could not gather project metadata from Scarb due to previous error") -} diff --git a/crates/cairo-coverage/Cargo.toml b/crates/cairo-coverage/Cargo.toml index ad8057b..b4192e2 100644 --- a/crates/cairo-coverage/Cargo.toml +++ b/crates/cairo-coverage/Cargo.toml @@ -8,6 +8,7 @@ cairo-coverage-core = { path = "../cairo-coverage-core" } console.workspace = true camino.workspace = true anyhow.workspace = true +scarb-metadata.workspace = true clap.workspace = true walkdir.workspace = true diff --git a/crates/cairo-coverage/src/args/run.rs b/crates/cairo-coverage/src/args/run.rs index fd01732..08b6381 100644 --- a/crates/cairo-coverage/src/args/run.rs +++ b/crates/cairo-coverage/src/args/run.rs @@ -17,7 +17,7 @@ pub struct RunArgs { #[arg(long, short, num_args = 1..)] pub include: Vec, - /// Path to the project directory. If not provided, the project directory is inferred from the trace. + /// Path to the project directory. If not provided, the project directory is inferred using `scarb metadata`. #[arg(value_parser = parse_project_path, long)] pub project_path: Option, } diff --git a/crates/cairo-coverage/src/commands/run.rs b/crates/cairo-coverage/src/commands/run.rs index 34f0640..1dd6342 100644 --- a/crates/cairo-coverage/src/commands/run.rs +++ b/crates/cairo-coverage/src/commands/run.rs @@ -1,6 +1,7 @@ use crate::args::run::{IncludedComponent, RunArgs}; use anyhow::{Context, Result}; use cairo_coverage_core::args::{IncludedComponent as CoreIncludedComponent, RunOptions}; +use scarb_metadata::{Metadata, MetadataCommand}; use std::fs::OpenOptions; use std::io::Write; @@ -14,12 +15,17 @@ pub fn run( trace_files, }: RunArgs, ) -> Result<()> { + let project_path = if let Some(project_path) = project_path { + project_path + } else { + scarb_metadata()?.workspace.root + }; + let options = RunOptions { include: include.into_iter().map(Into::into).collect(), - project_path, }; - let lcov = cairo_coverage_core::run(trace_files, options)?; + let lcov = cairo_coverage_core::run(trace_files, project_path, options)?; OpenOptions::new() .append(true) @@ -32,6 +38,14 @@ pub fn run( Ok(()) } +/// Run `scarb metadata` command and return the metadata. +fn scarb_metadata() -> Result { + MetadataCommand::new() + .inherit_stderr() + .exec() + .context("could not gather project metadata from Scarb due to previous error") +} + impl From for CoreIncludedComponent { fn from(component: IncludedComponent) -> Self { match component {