Skip to content

Commit

Permalink
fix(fuzz): do not exceed configured runs (#7971)
Browse files Browse the repository at this point in the history
* fix(fuzz): do not exceed configured runs

* Add test
  • Loading branch information
grandizzy authored May 22, 2024
1 parent a539f3a commit 44d98ea
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
3 changes: 3 additions & 0 deletions crates/forge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ impl TestOptions {
failure_persistence: file_failure_persistence,
cases,
max_global_rejects: self.fuzz.max_test_rejects,
// Disable proptest shrink: for fuzz tests we provide single counterexample,
// for invariant tests we shrink outside proptest.
max_shrink_iters: 0,
..Default::default()
};

Expand Down
51 changes: 50 additions & 1 deletion crates/forge/tests/cli/test_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Contains various tests for `forge test`.
use foundry_config::Config;
use alloy_primitives::U256;
use foundry_config::{Config, FuzzConfig};
use foundry_test_utils::{
rpc,
util::{OutputExt, OTHER_SOLC_VERSION, SOLC_VERSION},
Expand Down Expand Up @@ -544,3 +545,51 @@ contract Dummy {
cmd.args(["test", "--match-path", "src/dummy.sol"]);
cmd.assert_success()
});

forgetest_init!(should_not_shrink_fuzz_failure, |prj, cmd| {
prj.wipe_contracts();

// deterministic test so we always have 54 runs until test fails with overflow
let config = Config {
fuzz: { FuzzConfig { runs: 256, seed: Some(U256::from(100)), ..Default::default() } },
..Default::default()
};
prj.write_config(config);

prj.add_test(
"CounterFuzz.t.sol",
r#"pragma solidity 0.8.24;
import {Test} from "forge-std/Test.sol";
contract Counter {
uint256 public number = 0;
function addOne(uint256 x) external pure returns (uint256) {
return x + 100_000_000;
}
}
contract CounterTest is Test {
Counter public counter;
function setUp() public {
counter = new Counter();
}
function testAddOne(uint256 x) public view {
assertEq(counter.addOne(x), x + 100_000_000);
}
}
"#,
)
.unwrap();

cmd.args(["test"]);
let (stderr, _) = cmd.unchecked_output_lossy();
let runs = stderr.find("runs:").and_then(|start_runs| {
let runs_split = &stderr[start_runs + 6..];
runs_split.find(',').map(|end_runs| &runs_split[..end_runs])
});
// make sure there are only 54 runs (with proptest shrinking same test results in 292 runs)
assert_eq!(runs.unwrap().parse::<usize>().unwrap(), 54);
});

0 comments on commit 44d98ea

Please sign in to comment.