Skip to content

Commit 27e62a6

Browse files
authored
Truncate coverage report (#164)
**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 2958f2f commit 27e62a6

File tree

14 files changed

+112
-80
lines changed

14 files changed

+112
-80
lines changed

Diff for: CHANGELOG.md

+1
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

Diff for: crates/cairo-coverage-core/src/args.rs

+3
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.

Diff for: crates/cairo-coverage-core/src/coverage/project.rs

+13
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+
}

Diff for: crates/cairo-coverage-core/src/lib.rs

+10-3
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
}

Diff for: crates/cairo-coverage/src/args/run.rs

+4
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>,

Diff for: crates/cairo-coverage/src/commands/run.rs

+2
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)?;

Diff for: crates/cairo-coverage/tests/expected_output/complex_calculator.lcov

+17-17
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

Diff for: crates/cairo-coverage/tests/expected_output/readme_example.lcov

+5-5
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
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
+5-5
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
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
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
1616
TN:
1717
SF:{dir}/tests/test_call.cairo
1818
FN:2,simple_tests::test_call::my_test
19-
FNDA:9,simple_tests::test_call::my_test
19+
FNDA:1,simple_tests::test_call::my_test
2020
FNF:1
2121
FNH:1
22-
DA:2,9
22+
DA:2,1
2323
LF:1
2424
LH:1
2525
end_of_record
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
TN:
22
SF:{dir}/src/lib.cairo
33
FN:22,snforge_template::HelloStarknet::HelloStarknetImpl::get_balance
4-
FNDA:9,snforge_template::HelloStarknet::HelloStarknetImpl::get_balance
4+
FNDA:1,snforge_template::HelloStarknet::HelloStarknetImpl::get_balance
55
FN:17,snforge_template::HelloStarknet::HelloStarknetImpl::increase_balance
6-
FNDA:8,snforge_template::HelloStarknet::HelloStarknetImpl::increase_balance
6+
FNDA:1,snforge_template::HelloStarknet::HelloStarknetImpl::increase_balance
77
FN:21,snforge_template::HelloStarknet::__wrapper__HelloStarknetImpl__get_balance
8-
FNDA:27,snforge_template::HelloStarknet::__wrapper__HelloStarknetImpl__get_balance
8+
FNDA:1,snforge_template::HelloStarknet::__wrapper__HelloStarknetImpl__get_balance
99
FN:16,snforge_template::HelloStarknet::__wrapper__HelloStarknetImpl__increase_balance
10-
FNDA:19,snforge_template::HelloStarknet::__wrapper__HelloStarknetImpl__increase_balance
10+
FNDA:1,snforge_template::HelloStarknet::__wrapper__HelloStarknetImpl__increase_balance
1111
FN:1,snforge_template::IHelloStarknetDispatcherImpl::get_balance
12-
FNDA:16,snforge_template::IHelloStarknetDispatcherImpl::get_balance
12+
FNDA:1,snforge_template::IHelloStarknetDispatcherImpl::get_balance
1313
FN:1,snforge_template::IHelloStarknetDispatcherImpl::increase_balance
14-
FNDA:6,snforge_template::IHelloStarknetDispatcherImpl::increase_balance
14+
FNDA:1,snforge_template::IHelloStarknetDispatcherImpl::increase_balance
1515
FN:1,snforge_template::IHelloStarknetSafeDispatcherImpl::get_balance
16-
FNDA:5,snforge_template::IHelloStarknetSafeDispatcherImpl::get_balance
16+
FNDA:1,snforge_template::IHelloStarknetSafeDispatcherImpl::get_balance
1717
FN:1,snforge_template::IHelloStarknetSafeDispatcherImpl::increase_balance
18-
FNDA:3,snforge_template::IHelloStarknetSafeDispatcherImpl::increase_balance
18+
FNDA:1,snforge_template::IHelloStarknetSafeDispatcherImpl::increase_balance
1919
FNF:8
2020
FNH:8
21-
DA:1,30
22-
DA:16,19
23-
DA:17,7
24-
DA:18,8
25-
DA:21,27
26-
DA:22,9
21+
DA:1,4
22+
DA:16,1
23+
DA:17,1
24+
DA:18,1
25+
DA:21,1
26+
DA:22,1
2727
LF:6
2828
LH:6
2929
end_of_record
3030
TN:
3131
SF:{dir}/tests/test_contract.cairo
3232
FN:11,snforge_template_integrationtest::test_contract::deploy_contract
33-
FNDA:10,snforge_template_integrationtest::test_contract::deploy_contract
33+
FNDA:1,snforge_template_integrationtest::test_contract::deploy_contract
3434
FNF:1
3535
FNH:1
36-
DA:11,10
37-
DA:12,10
36+
DA:11,1
37+
DA:12,1
3838
LF:2
3939
LH:2
4040
end_of_record
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
TN:
22
SF:{dir}/src/lib.cairo
33
FN:22,snforge_template::HelloStarknet::HelloStarknetImpl::get_balance
4-
FNDA:9,snforge_template::HelloStarknet::HelloStarknetImpl::get_balance
4+
FNDA:1,snforge_template::HelloStarknet::HelloStarknetImpl::get_balance
55
FN:17,snforge_template::HelloStarknet::HelloStarknetImpl::increase_balance
6-
FNDA:8,snforge_template::HelloStarknet::HelloStarknetImpl::increase_balance
6+
FNDA:1,snforge_template::HelloStarknet::HelloStarknetImpl::increase_balance
77
FNF:2
88
FNH:2
9-
DA:17,7
10-
DA:18,8
11-
DA:22,9
9+
DA:17,1
10+
DA:18,1
11+
DA:22,1
1212
LF:3
1313
LH:3
1414
end_of_record
1515
TN:
1616
SF:{dir}/tests/test_contract.cairo
1717
FN:11,snforge_template_integrationtest::test_contract::deploy_contract
18-
FNDA:10,snforge_template_integrationtest::test_contract::deploy_contract
18+
FNDA:1,snforge_template_integrationtest::test_contract::deploy_contract
1919
FNF:1
2020
FNH:1
21-
DA:11,10
22-
DA:12,10
21+
DA:11,1
22+
DA:12,1
2323
LF:2
2424
LH:2
2525
end_of_record

0 commit comments

Comments
 (0)