Skip to content

Commit b66c3c5

Browse files
committed
Truncate coverage report
commit-id:6426d392
1 parent 2958f2f commit b66c3c5

File tree

14 files changed

+112
-80
lines changed

14 files changed

+112
-80
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
#### Changed
1111
- macros are now by default included in the coverage report. If you want to exclude them, use the `--include` without the
1212
`macros` option (can also have empty value)
13+
- by default, the hit count of the lines will be truncated to 1. This can be changed with the `--no-truncation` flag
1314

1415
## [0.4.0] - 2025-01-03
1516

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
pub struct RunOptions {
44
/// Include additional components in the coverage report.
55
pub include: Vec<IncludedComponent>,
6+
7+
/// If set, the hit count of the lines will not be truncated to 1.
8+
pub no_truncation: bool,
69
}
710

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

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,16 @@ fn register_line_execution(
4747
*function_coverage.entry(line).or_default() += executed_statement_count;
4848
}
4949
}
50+
51+
/// Truncates the execution count of each statement to 1.
52+
/// Currently, execution counts are not stable between `scarb` versions,
53+
/// so truncating to 1 is a way of achieving stability.
54+
pub fn truncate_to_one(project_coverage: &mut ProjectCoverage) {
55+
for file_coverage in project_coverage.values_mut() {
56+
for function_coverage in file_coverage.values_mut() {
57+
for execution_count in function_coverage.values_mut() {
58+
*execution_count = (*execution_count).min(1);
59+
}
60+
}
61+
}
62+
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ use rayon::iter::{IntoParallelIterator, ParallelIterator};
2424
pub fn run(
2525
trace_files: Vec<Utf8PathBuf>,
2626
project_path: Utf8PathBuf,
27-
RunOptions { include }: RunOptions,
27+
RunOptions {
28+
include,
29+
no_truncation,
30+
}: RunOptions,
2831
) -> Result<String> {
2932
let ignore_matcher = ignore_matcher::build(&project_path)?;
3033

31-
let coverage_data = execution_data::load(&trace_files)?
34+
let mut project_coverage = execution_data::load(&trace_files)?
3235
.into_par_iter()
3336
.map(|execution_data| {
3437
let filter = statement_category_filter::build(
@@ -48,5 +51,9 @@ pub fn run(
4851
.reduce(merge)
4952
.context("at least one trace file must be provided")?;
5053

51-
Ok(lcov::fmt_string(&coverage_data))
54+
if !no_truncation {
55+
coverage::project::truncate_to_one(&mut project_coverage);
56+
}
57+
58+
Ok(lcov::fmt_string(&project_coverage))
5259
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ pub struct RunArgs {
1717
#[arg(long, short, num_args = 0.., default_value = "macros")]
1818
pub include: Vec<IncludedComponent>,
1919

20+
/// If set, the hit count of the lines will not be truncated to 1.
21+
#[arg(long)]
22+
pub no_truncation: bool,
23+
2024
/// Path to the project directory. If not provided, the project directory is inferred using `scarb metadata`.
2125
#[arg(value_parser = parse_project_path, long)]
2226
pub project_path: Option<Utf8PathBuf>,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub fn run(
1313
project_path,
1414
output_path,
1515
trace_files,
16+
no_truncation,
1617
}: RunArgs,
1718
) -> Result<()> {
1819
let project_path = if let Some(project_path) = project_path {
@@ -23,6 +24,7 @@ pub fn run(
2324

2425
let options = RunOptions {
2526
include: include.into_iter().map(Into::into).collect(),
27+
no_truncation,
2628
};
2729

2830
let lcov = cairo_coverage_core::run(trace_files, project_path, options)?;

crates/cairo-coverage/tests/expected_output/complex_calculator.lcov

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ SF:{dir}/src/lib.cairo
33
FN:2,complex_calculator::add
44
FNDA:1,complex_calculator::add
55
FN:18,complex_calculator::divide
6-
FNDA:2,complex_calculator::divide
6+
FNDA:1,complex_calculator::divide
77
FN:28,complex_calculator::factorial
8-
FNDA:25,complex_calculator::factorial
8+
FNDA:1,complex_calculator::factorial
99
FN:45,complex_calculator::fibonacci
10-
FNDA:2,complex_calculator::fibonacci
10+
FNDA:1,complex_calculator::fibonacci
1111
FN:54,complex_calculator::is_prime
12-
FNDA:216,complex_calculator::is_prime
12+
FNDA:1,complex_calculator::is_prime
1313
FN:10,complex_calculator::multiply
1414
FNDA:1,complex_calculator::multiply
1515
FN:38,complex_calculator::power
16-
FNDA:22,complex_calculator::power
16+
FNDA:1,complex_calculator::power
1717
FN:6,complex_calculator::subtract
1818
FNDA:1,complex_calculator::subtract
1919
FN:14,complex_calculator::unsafe_divide
@@ -24,22 +24,22 @@ DA:2,1
2424
DA:6,1
2525
DA:10,1
2626
DA:14,0
27-
DA:18,2
27+
DA:18,1
2828
DA:21,0
29-
DA:28,25
30-
DA:29,10
31-
DA:30,10
32-
DA:38,22
33-
DA:39,6
34-
DA:40,6
29+
DA:28,1
30+
DA:29,1
31+
DA:30,1
32+
DA:38,1
33+
DA:39,1
34+
DA:40,1
3535
DA:45,1
36-
DA:46,2
36+
DA:46,1
3737
DA:47,0
3838
DA:49,0
39-
DA:54,4
40-
DA:59,216
41-
DA:60,158
42-
DA:63,80
39+
DA:54,1
40+
DA:59,1
41+
DA:60,1
42+
DA:63,1
4343
LF:20
4444
LH:16
4545
end_of_record

crates/cairo-coverage/tests/expected_output/readme_example.lcov

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
TN:
22
SF:{dir}/src/lib.cairo
33
FN:8,readme_example::add
4-
FNDA:2,readme_example::add
4+
FNDA:1,readme_example::add
55
FN:16,readme_example::calculator
6-
FNDA:4,readme_example::calculator
6+
FNDA:1,readme_example::calculator
77
FN:12,readme_example::multiply
88
FNDA:0,readme_example::multiply
99
FNF:3
1010
FNH:2
11-
DA:8,2
11+
DA:8,1
1212
DA:12,0
13-
DA:16,4
14-
DA:17,2
13+
DA:16,1
14+
DA:17,1
1515
DA:18,0
1616
LF:5
1717
LH:3
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
TN:
22
SF:{dir}/src/lib.cairo
33
FN:8,scarb_template::fib
4-
FNDA:69,scarb_template::fib
4+
FNDA:1,scarb_template::fib
55
FNF:1
66
FNH:1
7-
DA:8,69
8-
DA:9,32
9-
DA:11,32
7+
DA:8,1
8+
DA:9,1
9+
DA:11,1
1010
LF:3
1111
LH:3
1212
end_of_record
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
TN:
22
SF:{dir}/src/lib.cairo
33
FN:7,simple::increase_by_one
4-
FNDA:2,simple::increase_by_one
4+
FNDA:1,simple::increase_by_one
55
FN:2,simple::increase_by_two
6-
FNDA:3,simple::increase_by_two
6+
FNDA:1,simple::increase_by_two
77
FNF:2
88
FNH:2
99
DA:2,1
10-
DA:3,3
11-
DA:7,2
12-
DA:8,2
10+
DA:3,1
11+
DA:7,1
12+
DA:8,1
1313
LF:4
1414
LH:4
1515
end_of_record

0 commit comments

Comments
 (0)