Skip to content
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

feat(--gas-report): add option to include tests #9232

Merged
merged 1 commit into from
Oct 30, 2024
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
3 changes: 3 additions & 0 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ pub struct Config {
pub gas_reports: Vec<String>,
/// list of contracts to ignore for gas reports
pub gas_reports_ignore: Vec<String>,
/// 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
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions crates/forge/bin/cmd/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
)
});
Expand Down
7 changes: 5 additions & 2 deletions crates/forge/src/gas_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub struct GasReport {
report_for: HashSet<String>,
/// Contracts to ignore when generating the report.
ignore: HashSet<String>,
/// 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<String, ContractInfo>,
Expand All @@ -45,13 +47,14 @@ impl GasReport {
pub fn new(
report_for: impl IntoIterator<Item = String>,
ignore: impl IntoIterator<Item = String>,
include_tests: bool,
report_kind: GasReportKind,
) -> Self {
let report_for = report_for.into_iter().collect::<HashSet<_>>();
let ignore = ignore.into_iter().collect::<HashSet<_>>();
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.
Expand Down Expand Up @@ -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
Expand Down
98 changes: 98 additions & 0 deletions crates/forge/tests/cli/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
});
1 change: 1 addition & 0 deletions crates/forge/tests/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading