diff --git a/crates/evm/evm/src/executors/fuzz/mod.rs b/crates/evm/evm/src/executors/fuzz/mod.rs index dfe969ea2c193..a89ef3c267ebd 100644 --- a/crates/evm/evm/src/executors/fuzz/mod.rs +++ b/crates/evm/evm/src/executors/fuzz/mod.rs @@ -48,6 +48,8 @@ struct FuzzTestData { coverage: Option, // Stores logs for all fuzz cases logs: Vec, + // Stores logs from the last successful run (for display at verbosity >= 2) + last_run_logs: Vec, // Deprecated cheatcodes mapped to their replacements. deprecated_cheatcodes: HashMap<&'static str, Option<&'static str>>, // Runs performed in fuzz test. @@ -198,6 +200,9 @@ impl FuzzedExecutor { test_data.breakpoints.replace(case.breakpoints); } + // Always store logs from the last run for display at verbosity >= 2 + test_data.last_run_logs = case.logs.clone(); + if self.config.show_logs { test_data.logs.extend(case.logs); } @@ -255,6 +260,18 @@ impl FuzzedExecutor { (call.traces.clone(), call.cheatcodes.map(|c| c.breakpoints)) }; + // Include logs from the last run if show_logs is false, so they can be displayed + // at verbosity >= 2. If show_logs is true, test_data.logs already contains all logs. + // For failed tests, use logs from the counterexample. + let result_logs = if test_data.failure.is_some() { + // For failed tests, logs are already included in test_data.logs from the counterexample + test_data.logs + } else if self.config.show_logs { + test_data.logs + } else { + test_data.last_run_logs + }; + let mut result = FuzzTestResult { first_case: test_data.first_case.unwrap_or_default(), gas_by_case: test_data.gas_by_case, @@ -262,7 +279,7 @@ impl FuzzedExecutor { skipped: false, reason: None, counterexample: None, - logs: test_data.logs, + logs: result_logs, labels: call.labels, traces: last_run_traces, breakpoints: last_run_breakpoints, diff --git a/crates/forge/tests/cli/test_cmd/mod.rs b/crates/forge/tests/cli/test_cmd/mod.rs index 821af8a7e7a9c..3389967a67cb0 100644 --- a/crates/forge/tests/cli/test_cmd/mod.rs +++ b/crates/forge/tests/cli/test_cmd/mod.rs @@ -1122,8 +1122,8 @@ Ran 1 test suite [ELAPSED]: 1 tests passed, 0 failed, 0 skipped (1 total tests) "#]]); }); -// tests that `forge test` with config `show_logs: false` for fuzz tests will not display -// `console.log` info +// tests that `forge test` with config `show_logs: false` for fuzz tests will +// still display `console.log` from the last run at verbosity >= 2 (issue #11039) forgetest_init!(should_not_show_logs_when_fuzz_test, |prj, cmd| { // run fuzz test 3 times prj.update_config(|config| { @@ -1145,6 +1145,7 @@ forgetest_init!(should_not_show_logs_when_fuzz_test, |prj, cmd| { } "#, ); + // At verbosity >= 2, logs from the last run should be shown even when show_logs is false cmd.args(["test", "-vv"]).assert_success().stdout_eq(str![[r#" [COMPILING_FILES] with [SOLC_VERSION] [SOLC_VERSION] [ELAPSED] @@ -1152,6 +1153,9 @@ Compiler run successful! Ran 1 test for test/ContractFuzz.t.sol:ContractFuzz [PASS] testFuzzConsoleLog(uint256) (runs: 3, [AVG_GAS]) +Logs: + inside fuzz test, x is: [..] + Suite result: ok. 1 passed; 0 failed; 0 skipped; [ELAPSED] Ran 1 test suite [ELAPSED]: 1 tests passed, 0 failed, 0 skipped (1 total tests) @@ -1159,8 +1163,8 @@ Ran 1 test suite [ELAPSED]: 1 tests passed, 0 failed, 0 skipped (1 total tests) "#]]); }); -// tests that `forge test` with inline config `show_logs = false` for fuzz tests will not -// display `console.log` info +// tests that `forge test` with inline config `show_logs = false` for fuzz tests will +// still display `console.log` from the last run at verbosity >= 2 (issue #11039) forgetest_init!(should_not_show_logs_when_fuzz_test_inline_config, |prj, cmd| { // run fuzz test 3 times prj.update_config(|config| { @@ -1182,6 +1186,7 @@ contract ContractFuzz is Test { } "#, ); + // At verbosity >= 2, logs from the last run should be shown even when show_logs is false cmd.args(["test", "-vv"]).assert_success().stdout_eq(str![[r#" [COMPILING_FILES] with [SOLC_VERSION] [SOLC_VERSION] [ELAPSED] @@ -1189,6 +1194,9 @@ Compiler run successful! Ran 1 test for test/ContractFuzz.t.sol:ContractFuzz [PASS] testFuzzConsoleLog(uint256) (runs: 3, [AVG_GAS]) +Logs: + inside fuzz test, x is: [..] + Suite result: ok. 1 passed; 0 failed; 0 skipped; [ELAPSED] Ran 1 test suite [ELAPSED]: 1 tests passed, 0 failed, 0 skipped (1 total tests)