Skip to content

Commit fda2b7e

Browse files
avorylliDaniPopes
andauthored
fix: emit console.log on fuzz test last run at verbosity >= 2 (#12478)
* Update mod.rs * Update mod.rs * Update mod.rs * Update mod.rs --------- Co-authored-by: DaniPopes <[email protected]>
1 parent c280686 commit fda2b7e

File tree

2 files changed

+26
-6
lines changed
  • crates
    • evm/evm/src/executors/fuzz
    • forge/tests/cli/test_cmd

2 files changed

+26
-6
lines changed

crates/evm/evm/src/executors/fuzz/mod.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ struct FuzzTestData {
4646
breakpoints: Option<Breakpoints>,
4747
// Stores coverage information for all fuzz cases.
4848
coverage: Option<HitMaps>,
49-
// Stores logs for all fuzz cases
49+
// Stores logs for all fuzz cases (when show_logs is true) or just the last run (when show_logs
50+
// is false)
5051
logs: Vec<Log>,
5152
// Deprecated cheatcodes mapped to their replacements.
5253
deprecated_cheatcodes: HashMap<&'static str, Option<&'static str>>,
@@ -200,8 +201,13 @@ impl FuzzedExecutor {
200201
test_data.breakpoints.replace(case.breakpoints);
201202
}
202203

204+
// Always store logs from the last run in test_data.logs for display at
205+
// verbosity >= 2. When show_logs is true,
206+
// accumulate all logs. When false, only keep the last run's logs.
203207
if self.config.show_logs {
204208
test_data.logs.extend(case.logs);
209+
} else {
210+
test_data.logs = case.logs;
205211
}
206212

207213
HitMaps::merge_opt(&mut test_data.coverage, case.coverage);
@@ -259,14 +265,20 @@ impl FuzzedExecutor {
259265
(call.traces.clone(), call.cheatcodes.map(|c| c.breakpoints))
260266
};
261267

268+
// test_data.logs already contains the appropriate logs:
269+
// - For failed tests: logs from the counterexample
270+
// - For successful tests with show_logs=true: all logs from all runs
271+
// - For successful tests with show_logs=false: logs from the last run only
272+
let result_logs = test_data.logs;
273+
262274
let mut result = FuzzTestResult {
263275
first_case: test_data.first_case.unwrap_or_default(),
264276
gas_by_case: test_data.gas_by_case,
265277
success: test_data.failure.is_none(),
266278
skipped: false,
267279
reason: None,
268280
counterexample: None,
269-
logs: test_data.logs,
281+
logs: result_logs,
270282
labels: call.labels,
271283
traces: last_run_traces,
272284
breakpoints: last_run_breakpoints,

crates/forge/tests/cli/test_cmd/mod.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,8 +1126,8 @@ Ran 1 test suite [ELAPSED]: 1 tests passed, 0 failed, 0 skipped (1 total tests)
11261126
"#]]);
11271127
});
11281128

1129-
// tests that `forge test` with config `show_logs: false` for fuzz tests will not display
1130-
// `console.log` info
1129+
// tests that `forge test` with config `show_logs: false` for fuzz tests will
1130+
// still display `console.log` from the last run at verbosity >= 2 (issue #11039)
11311131
forgetest_init!(should_not_show_logs_when_fuzz_test, |prj, cmd| {
11321132
// run fuzz test 3 times
11331133
prj.update_config(|config| {
@@ -1149,22 +1149,26 @@ forgetest_init!(should_not_show_logs_when_fuzz_test, |prj, cmd| {
11491149
}
11501150
"#,
11511151
);
1152+
// At verbosity >= 2, logs from the last run should be shown even when show_logs is false
11521153
cmd.args(["test", "-vv"]).assert_success().stdout_eq(str![[r#"
11531154
[COMPILING_FILES] with [SOLC_VERSION]
11541155
[SOLC_VERSION] [ELAPSED]
11551156
Compiler run successful!
11561157
11571158
Ran 1 test for test/ContractFuzz.t.sol:ContractFuzz
11581159
[PASS] testFuzzConsoleLog(uint256) (runs: 3, [AVG_GAS])
1160+
Logs:
1161+
inside fuzz test, x is: [..]
1162+
11591163
Suite result: ok. 1 passed; 0 failed; 0 skipped; [ELAPSED]
11601164
11611165
Ran 1 test suite [ELAPSED]: 1 tests passed, 0 failed, 0 skipped (1 total tests)
11621166
11631167
"#]]);
11641168
});
11651169

1166-
// tests that `forge test` with inline config `show_logs = false` for fuzz tests will not
1167-
// display `console.log` info
1170+
// tests that `forge test` with inline config `show_logs = false` for fuzz tests will
1171+
// still display `console.log` from the last run at verbosity >= 2 (issue #11039)
11681172
forgetest_init!(should_not_show_logs_when_fuzz_test_inline_config, |prj, cmd| {
11691173
// run fuzz test 3 times
11701174
prj.update_config(|config| {
@@ -1186,13 +1190,17 @@ contract ContractFuzz is Test {
11861190
}
11871191
"#,
11881192
);
1193+
// At verbosity >= 2, logs from the last run should be shown even when show_logs is false
11891194
cmd.args(["test", "-vv"]).assert_success().stdout_eq(str![[r#"
11901195
[COMPILING_FILES] with [SOLC_VERSION]
11911196
[SOLC_VERSION] [ELAPSED]
11921197
Compiler run successful!
11931198
11941199
Ran 1 test for test/ContractFuzz.t.sol:ContractFuzz
11951200
[PASS] testFuzzConsoleLog(uint256) (runs: 3, [AVG_GAS])
1201+
Logs:
1202+
inside fuzz test, x is: [..]
1203+
11961204
Suite result: ok. 1 passed; 0 failed; 0 skipped; [ELAPSED]
11971205
11981206
Ran 1 test suite [ELAPSED]: 1 tests passed, 0 failed, 0 skipped (1 total tests)

0 commit comments

Comments
 (0)