Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion crates/cairo-coverage-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 3 additions & 10 deletions crates/cairo-coverage-core/benches/benchmarks/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,17 +10,11 @@ fn trace_files_for_benches(dir_name: &str) -> Vec<Utf8PathBuf> {
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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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()),
)
});
Expand Down
5 changes: 0 additions & 5 deletions crates/cairo-coverage-core/src/args.rs
Original file line number Diff line number Diff line change
@@ -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<IncludedComponent>,

/// Path to the project directory. If not provided, the project directory is inferred from the trace.
pub project_path: Option<Utf8PathBuf>,
}

/// Additional components that can be included in the coverage report.
Expand Down
24 changes: 3 additions & 21 deletions crates/cairo-coverage-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Utf8PathBuf>,
RunOptions {
include,
project_path,
}: RunOptions,
project_path: Utf8PathBuf,
RunOptions { include }: RunOptions,
) -> Result<String> {
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)?
Expand All @@ -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<Metadata> {
MetadataCommand::new()
.inherit_stderr()
.inherit_stdout()
.exec()
.context("could not gather project metadata from Scarb due to previous error")
}
1 change: 1 addition & 0 deletions crates/cairo-coverage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-coverage/src/args/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct RunArgs {
#[arg(long, short, num_args = 1..)]
pub include: Vec<IncludedComponent>,

/// 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<Utf8PathBuf>,
}
Expand Down
18 changes: 16 additions & 2 deletions crates/cairo-coverage/src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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)
Expand All @@ -32,6 +38,14 @@ pub fn run(
Ok(())
}

/// Run `scarb metadata` command and return the metadata.
fn scarb_metadata() -> Result<Metadata> {
MetadataCommand::new()
.inherit_stderr()
.exec()
.context("could not gather project metadata from Scarb due to previous error")
}

impl From<IncludedComponent> for CoreIncludedComponent {
fn from(component: IncludedComponent) -> Self {
match component {
Expand Down
Loading