Skip to content

Commit 0dacd85

Browse files
authored
Move scarb metadata call to binary (#160)
**Stack**: - #168 - #167 - #166 - #165 - #164 - #163 - #160 ⬅ - #159 - #158 ⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do not merge manually using the UI - doing so may have unexpected results.*
1 parent 7ca3886 commit 0dacd85

File tree

9 files changed

+30
-43
lines changed

9 files changed

+30
-43
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cairo-coverage-core/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ itertools.workspace = true
1414
ignore.workspace = true
1515
serde.workspace = true
1616
serde_json.workspace = true
17-
scarb-metadata.workspace = true
1817
regex.workspace = true
1918
indoc.workspace = true
2019
rayon.workspace = true

crates/cairo-coverage-core/benches/benchmarks/mod.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use cairo_coverage_core::args::RunOptions;
21
use cairo_coverage_test_utils::read_files_from_dir;
32
use camino::Utf8PathBuf;
43
use criterion::Criterion;
@@ -11,17 +10,11 @@ fn trace_files_for_benches(dir_name: &str) -> Vec<Utf8PathBuf> {
1110
read_files_from_dir(format!("benches/project-traces/{dir_name}/trace"))
1211
}
1312

14-
/// Create [`RunOptions`] with set `project_path` and empty `include`.
15-
/// If we leave the `project_path` as `None`, the `scarb_metadata` will fail.
16-
fn run_options(dir_name: &str) -> RunOptions {
17-
let project_path = Utf8PathBuf::from(format!("benches/project-traces/{dir_name}"))
13+
/// Return `project_path` and for the benchmark project.
14+
fn project_path(dir_name: &str) -> Utf8PathBuf {
15+
Utf8PathBuf::from(format!("benches/project-traces/{dir_name}"))
1816
.canonicalize_utf8()
1917
.unwrap()
20-
.into();
21-
RunOptions {
22-
include: Vec::default(),
23-
project_path,
24-
}
2518
}
2619

2720
/// Config of [`Criterion`] that should be used for all benchmarks.

crates/cairo-coverage-core/benches/benchmarks/starknet_staking.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::benchmarks::{config, run_options, trace_files_for_benches};
1+
use crate::benchmarks::{config, project_path, trace_files_for_benches};
2+
use cairo_coverage_core::args::RunOptions;
23
use criterion::{Criterion, criterion_group};
34
use std::hint::black_box;
45

@@ -8,11 +9,13 @@ const PROJECT_NAME: &str = "starknet-staking";
89
/// The trace files should be generated using `download_bench_project.sh` script.
910
fn starknet_staking_benchmark(c: &mut Criterion) {
1011
let trace_files = trace_files_for_benches(PROJECT_NAME);
11-
let run_options = run_options(PROJECT_NAME);
12+
let project_path = project_path(PROJECT_NAME);
13+
let run_options = RunOptions::default();
1214
c.bench_function("starknet-staking", |b| {
1315
b.iter(|| {
1416
cairo_coverage_core::run(
1517
black_box(trace_files.clone()),
18+
black_box(project_path.clone()),
1619
black_box(run_options.clone()),
1720
)
1821
});

crates/cairo-coverage-core/src/args.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
use camino::Utf8PathBuf;
2-
31
/// Options accepted by `cairo_coverage_core` `run` function.
42
#[derive(Default, Clone)]
53
pub struct RunOptions {
64
/// Include additional components in the coverage report.
75
pub include: Vec<IncludedComponent>,
8-
9-
/// Path to the project directory. If not provided, the project directory is inferred from the trace.
10-
pub project_path: Option<Utf8PathBuf>,
116
}
127

138
/// Additional components that can be included in the coverage report.

crates/cairo-coverage-core/src/lib.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,17 @@ use crate::output::lcov;
1515
use anyhow::{Context, Result};
1616
use camino::Utf8PathBuf;
1717
use rayon::iter::{IntoParallelIterator, ParallelIterator};
18-
use scarb_metadata::{Metadata, MetadataCommand};
1918

20-
/// Run the core logic of `cairo-coverage` with the provided trace files and [`RunOptions`].
19+
/// Run the core logic of `cairo-coverage` with the provided trace files, project path and [`RunOptions`].
2120
/// This function generates a coverage report in the LCOV format.
2221
/// # Errors
2322
/// Fails if it can't produce the coverage report with the error message explaining the reason.
2423
#[expect(clippy::needless_pass_by_value)] // In case if we ever needed to take ownership of the arguments.
2524
pub fn run(
2625
trace_files: Vec<Utf8PathBuf>,
27-
RunOptions {
28-
include,
29-
project_path,
30-
}: RunOptions,
26+
project_path: Utf8PathBuf,
27+
RunOptions { include }: RunOptions,
3128
) -> Result<String> {
32-
let project_path = if let Some(project_path) = project_path {
33-
project_path
34-
} else {
35-
scarb_metadata()?.workspace.root
36-
};
37-
3829
let ignore_matcher = ignore_matcher::build(&project_path)?;
3930

4031
let coverage_data = execution_data::load(&trace_files)?
@@ -59,12 +50,3 @@ pub fn run(
5950

6051
Ok(lcov::fmt_string(&coverage_data))
6152
}
62-
63-
/// Run `scarb metadata` command and return the metadata.
64-
fn scarb_metadata() -> Result<Metadata> {
65-
MetadataCommand::new()
66-
.inherit_stderr()
67-
.inherit_stdout()
68-
.exec()
69-
.context("could not gather project metadata from Scarb due to previous error")
70-
}

crates/cairo-coverage/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ cairo-coverage-core = { path = "../cairo-coverage-core" }
88
console.workspace = true
99
camino.workspace = true
1010
anyhow.workspace = true
11+
scarb-metadata.workspace = true
1112
clap.workspace = true
1213
walkdir.workspace = true
1314

crates/cairo-coverage/src/args/run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct RunArgs {
1717
#[arg(long, short, num_args = 1..)]
1818
pub include: Vec<IncludedComponent>,
1919

20-
/// Path to the project directory. If not provided, the project directory is inferred from the trace.
20+
/// Path to the project directory. If not provided, the project directory is inferred using `scarb metadata`.
2121
#[arg(value_parser = parse_project_path, long)]
2222
pub project_path: Option<Utf8PathBuf>,
2323
}

crates/cairo-coverage/src/commands/run.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::args::run::{IncludedComponent, RunArgs};
22
use anyhow::{Context, Result};
33
use cairo_coverage_core::args::{IncludedComponent as CoreIncludedComponent, RunOptions};
4+
use scarb_metadata::{Metadata, MetadataCommand};
45
use std::fs::OpenOptions;
56
use std::io::Write;
67

@@ -14,12 +15,17 @@ pub fn run(
1415
trace_files,
1516
}: RunArgs,
1617
) -> Result<()> {
18+
let project_path = if let Some(project_path) = project_path {
19+
project_path
20+
} else {
21+
scarb_metadata()?.workspace.root
22+
};
23+
1724
let options = RunOptions {
1825
include: include.into_iter().map(Into::into).collect(),
19-
project_path,
2026
};
2127

22-
let lcov = cairo_coverage_core::run(trace_files, options)?;
28+
let lcov = cairo_coverage_core::run(trace_files, project_path, options)?;
2329

2430
OpenOptions::new()
2531
.append(true)
@@ -32,6 +38,14 @@ pub fn run(
3238
Ok(())
3339
}
3440

41+
/// Run `scarb metadata` command and return the metadata.
42+
fn scarb_metadata() -> Result<Metadata> {
43+
MetadataCommand::new()
44+
.inherit_stderr()
45+
.exec()
46+
.context("could not gather project metadata from Scarb due to previous error")
47+
}
48+
3549
impl From<IncludedComponent> for CoreIncludedComponent {
3650
fn from(component: IncludedComponent) -> Self {
3751
match component {

0 commit comments

Comments
 (0)