diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index 2a75f3b23c72..916c77f9bd71 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -196,6 +196,8 @@ pub struct Config { pub gas_reports: Vec, /// list of contracts to ignore for gas reports pub gas_reports_ignore: Vec, + /// Whether to include gas reports for tests. + pub gas_reports_include_tests: bool, /// The Solc instance to use if any. /// /// This takes precedence over `auto_detect_solc`, if a version is set then this overrides @@ -2164,6 +2166,7 @@ impl Default for Config { evm_version: EvmVersion::Paris, gas_reports: vec!["*".to_string()], gas_reports_ignore: vec![], + gas_reports_include_tests: false, solc: None, vyper: Default::default(), auto_detect_solc: true, diff --git a/crates/forge/bin/cmd/test/mod.rs b/crates/forge/bin/cmd/test/mod.rs index fa20e26d8788..4c973217f4d7 100644 --- a/crates/forge/bin/cmd/test/mod.rs +++ b/crates/forge/bin/cmd/test/mod.rs @@ -583,6 +583,7 @@ impl TestArgs { GasReport::new( config.gas_reports.clone(), config.gas_reports_ignore.clone(), + config.gas_reports_include_tests, if self.json { GasReportKind::JSON } else { GasReportKind::Markdown }, ) }); diff --git a/crates/forge/src/gas_report.rs b/crates/forge/src/gas_report.rs index ac2d0db00262..ea8a49d373c2 100644 --- a/crates/forge/src/gas_report.rs +++ b/crates/forge/src/gas_report.rs @@ -36,6 +36,8 @@ pub struct GasReport { report_for: HashSet, /// Contracts to ignore when generating the report. ignore: HashSet, + /// Whether to include gas reports for tests. + include_tests: bool, /// All contracts that were analyzed grouped by their identifier /// ``test/Counter.t.sol:CounterTest pub contracts: BTreeMap, @@ -45,13 +47,14 @@ impl GasReport { pub fn new( report_for: impl IntoIterator, ignore: impl IntoIterator, + include_tests: bool, report_kind: GasReportKind, ) -> Self { let report_for = report_for.into_iter().collect::>(); let ignore = ignore.into_iter().collect::>(); let report_any = report_for.is_empty() || report_for.contains("*"); let report_type = report_kind; - Self { report_any, report_type, report_for, ignore, ..Default::default() } + Self { report_any, report_type, report_for, ignore, include_tests, ..Default::default() } } /// Whether the given contract should be reported. @@ -122,7 +125,7 @@ impl GasReport { } else if let Some(DecodedCallData { signature, .. }) = decoded().await.call_data { let name = signature.split('(').next().unwrap(); // ignore any test/setup functions - if !name.test_function_kind().is_known() { + if self.include_tests || !name.test_function_kind().is_known() { trace!(contract_name, signature, "adding gas info"); let gas_info = contract_info .functions diff --git a/crates/forge/tests/cli/cmd.rs b/crates/forge/tests/cli/cmd.rs index f34d0e2dec5d..f9e8997bd99c 100644 --- a/crates/forge/tests/cli/cmd.rs +++ b/crates/forge/tests/cli/cmd.rs @@ -2717,3 +2717,101 @@ interface Counter { "#]], ); }); + +// checks that `clean` also works with the "out" value set in Config +forgetest_init!(gas_report_include_tests, |prj, cmd| { + prj.write_config(Config { + gas_reports_include_tests: true, + fuzz: FuzzConfig { runs: 1, ..Default::default() }, + ..Default::default() + }); + + cmd.args(["test", "--mt", "test_Increment", "--gas-report"]).assert_success().stdout_eq(str![ + [r#" +... +| src/Counter.sol:Counter contract | | | | | | +|----------------------------------|-----------------|-------|--------|-------|---------| +| Deployment Cost | Deployment Size | | | | | +| 106715 | 277 | | | | | +| Function Name | min | avg | median | max | # calls | +| increment | 43404 | 43404 | 43404 | 43404 | 1 | +| number | 283 | 283 | 283 | 283 | 1 | +| setNumber | 23582 | 23582 | 23582 | 23582 | 1 | + + +| test/Counter.t.sol:CounterTest contract | | | | | | +|-----------------------------------------|-----------------|--------|--------|--------|---------| +| Deployment Cost | Deployment Size | | | | | +| 965418 | 4661 | | | | | +| Function Name | min | avg | median | max | # calls | +| setUp | 168064 | 168064 | 168064 | 168064 | 1 | +| test_Increment | 52367 | 52367 | 52367 | 52367 | 1 | +... + +"#] + ]); + + cmd.forge_fuse() + .args(["test", "--mt", "test_Increment", "--gas-report", "--json"]) + .assert_success() + .stdout_eq( + str![[r#" +[ + { + "contract": "src/Counter.sol:Counter", + "deployment": { + "gas": 106715, + "size": 277 + }, + "functions": { + "increment()": { + "calls": 1, + "min": 43404, + "mean": 43404, + "median": 43404, + "max": 43404 + }, + "number()": { + "calls": 1, + "min": 283, + "mean": 283, + "median": 283, + "max": 283 + }, + "setNumber(uint256)": { + "calls": 1, + "min": 23582, + "mean": 23582, + "median": 23582, + "max": 23582 + } + } + }, + { + "contract": "test/Counter.t.sol:CounterTest", + "deployment": { + "gas": 965418, + "size": 4661 + }, + "functions": { + "setUp()": { + "calls": 1, + "min": 168064, + "mean": 168064, + "median": 168064, + "max": 168064 + }, + "test_Increment()": { + "calls": 1, + "min": 52367, + "mean": 52367, + "median": 52367, + "max": 52367 + } + } + } +] +"#]] + .is_json(), + ); +}); diff --git a/crates/forge/tests/cli/config.rs b/crates/forge/tests/cli/config.rs index d7860381cb19..f6ec7c88eddf 100644 --- a/crates/forge/tests/cli/config.rs +++ b/crates/forge/tests/cli/config.rs @@ -43,6 +43,7 @@ forgetest!(can_extract_config_values, |prj, cmd| { evm_version: EvmVersion::Byzantium, gas_reports: vec!["Contract".to_string()], gas_reports_ignore: vec![], + gas_reports_include_tests: false, solc: Some(SolcReq::Local(PathBuf::from("custom-solc"))), auto_detect_solc: false, auto_detect_remappings: true,