Skip to content

Commit

Permalink
fix(forge): disable artifacts for coverage (#9692)
Browse files Browse the repository at this point in the history
* feat(`forge`): diff artifacts dir for coverage

* nit

* nit

* flip `no_artifacts` to true

* nit
  • Loading branch information
yash-atreya authored Jan 21, 2025
1 parent 5993795 commit 423644e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
10 changes: 9 additions & 1 deletion crates/forge/bin/cmd/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ impl CoverageArgs {
/// Builds the project.
fn build(&self, config: &Config) -> Result<(Project, ProjectCompileOutput)> {
// Set up the project
let mut project = config.create_project(false, false)?;
let mut project = config.create_project(false, true)?;

if self.ir_minimum {
// print warning message
sh_warn!("{}", concat!(
Expand Down Expand Up @@ -138,6 +139,13 @@ impl CoverageArgs {
project.settings.solc.optimizer.details = None;
project.settings.solc.via_ir = None;
}
let mut warning =
"optimizer settings have been disabled for accurate coverage reports".to_string();
if !self.ir_minimum {
warning += ", if you encounter \"stack too deep\" errors, consider using `--ir-minimum` which enables viaIR with minimum optimization resolving most of the errors";
}

sh_warn!("{warning}")?;

let output = ProjectCompiler::default()
.compile(&project)?
Expand Down
62 changes: 61 additions & 1 deletion crates/forge/tests/cli/coverage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use foundry_common::fs;
use foundry_common::fs::{self, files_with_ext};
use foundry_test_utils::{
snapbox::{Data, IntoData},
TestCommand, TestProject,
Expand Down Expand Up @@ -1691,3 +1691,63 @@ contract AContract {
fn assert_lcov(cmd: &mut TestCommand, data: impl IntoData) {
cmd.args(["--report=lcov", "--report-file"]).assert_file(data.into_data());
}

forgetest!(no_artifacts_written, |prj, cmd| {
prj.insert_ds_test();
prj.add_source(
"AContract.sol",
r#"
contract AContract {
int public i;
function init() public {
i = 0;
}
function foo() public {
i = 1;
}
}
"#,
)
.unwrap();

prj.add_source(
"AContractTest.sol",
r#"
import "./test.sol";
import {AContract} from "./AContract.sol";
contract AContractTest is DSTest {
AContract a;
function setUp() public {
a = new AContract();
a.init();
}
function testFoo() public {
a.foo();
}
}
"#,
)
.unwrap();

cmd.forge_fuse().arg("coverage").assert_success().stdout_eq(str![[r#"
...
╭-------------------+---------------+---------------+---------------+---------------╮
| File | % Lines | % Statements | % Branches | % Funcs |
+===================================================================================+
| src/AContract.sol | 100.00% (4/4) | 100.00% (2/2) | 100.00% (0/0) | 100.00% (2/2) |
|-------------------+---------------+---------------+---------------+---------------|
| Total | 100.00% (4/4) | 100.00% (2/2) | 100.00% (0/0) | 100.00% (2/2) |
╰-------------------+---------------+---------------+---------------+---------------╯
...
"#]]);

// no artifacts are to be written
let files = files_with_ext(prj.artifacts(), "json").collect::<Vec<_>>();

assert!(files.is_empty());
});

0 comments on commit 423644e

Please sign in to comment.