Skip to content

fix(forge): show lcov hits for do while statements #10423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 1, 2025
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
14 changes: 8 additions & 6 deletions crates/forge/src/coverage.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Coverage reports.

use alloy_primitives::map::HashMap;
use alloy_primitives::map::{HashMap, HashSet};
use comfy_table::{modifiers::UTF8_ROUND_CORNERS, Attribute, Cell, Color, Row, Table};
use evm_disassembler::disassemble_bytes;
use foundry_common::fs;
Expand Down Expand Up @@ -118,6 +118,8 @@ impl CoverageReporter for LcovReporter {
writeln!(out, "TN:")?;
writeln!(out, "SF:{}", path.display())?;

let mut recorded_lines = HashSet::new();

for item in items {
let line = item.loc.lines.start;
// `lines` is half-open, so we need to subtract 1 to get the last included line.
Expand All @@ -140,8 +142,11 @@ impl CoverageReporter for LcovReporter {
writeln!(out, "FNDA:{hits},{name}")?;
}
}
CoverageItemKind::Line => {
writeln!(out, "DA:{line},{hits}")?;
// Add lines / statement hits only once.
CoverageItemKind::Line | CoverageItemKind::Statement => {
if recorded_lines.insert(line) {
writeln!(out, "DA:{line},{hits}")?;
}
}
CoverageItemKind::Branch { branch_id, path_id, .. } => {
writeln!(
Expand All @@ -150,9 +155,6 @@ impl CoverageReporter for LcovReporter {
if hits == 0 { "-".to_string() } else { hits.to_string() }
)?;
}
// Statements are not in the LCOV format.
// We don't add them in order to avoid doubling line hits.
CoverageItemKind::Statement => {}
}
}

Expand Down
66 changes: 66 additions & 0 deletions crates/forge/tests/cli/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1845,6 +1845,72 @@ contract ArrayConditionTest is DSTest {
"#]]);
});

// <https://github.com/foundry-rs/foundry/issues/10422>
// Test that line hits are properly recorded in lcov report.
forgetest!(do_while_lcov, |prj, cmd| {
prj.insert_ds_test();
prj.add_source(
"Counter.sol",
r#"
contract Counter {
uint256 public number = 21;

function increment() public {
uint256 i = 0;
do {
number++;
if (number > 20) {
number -= 2;
}
} while (++i < 10);
}
}
"#,
)
.unwrap();

prj.add_source(
"Counter.t.sol",
r#"
import "./test.sol";
import "./Counter.sol";

contract CounterTest is DSTest {
function test_do_while() public {
Counter counter = new Counter();
counter.increment();
}
}
"#,
)
.unwrap();

assert_lcov(
cmd.arg("coverage"),
str![[r#"
TN:
SF:src/Counter.sol
DA:7,1
FN:7,Counter.increment
FNDA:1,Counter.increment
DA:8,1
DA:14,10
DA:10,10
DA:11,10
BRDA:11,0,0,6
DA:12,6
FNF:1
FNH:1
LF:3
LH:3
BRF:1
BRH:1
end_of_record

"#]],
);
});

#[track_caller]
fn assert_lcov(cmd: &mut TestCommand, data: impl IntoData) {
cmd.args(["--report=lcov", "--report-file"]).assert_file(data.into_data());
Expand Down