Skip to content

Commit 91c0782

Browse files
authored
fix(fuzz): apply inline max-test-rejects config (#8793)
1 parent d90e997 commit 91c0782

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

crates/forge/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ impl TestOptions {
115115
.unwrap();
116116
self.fuzzer_with_cases(
117117
fuzz_config.runs,
118+
fuzz_config.max_test_rejects,
118119
Some(Box::new(FileFailurePersistence::Direct(failure_persist_path.leak()))),
119120
)
120121
}
@@ -128,7 +129,7 @@ impl TestOptions {
128129
/// - `test_fn` is the name of the test function declared inside the test contract.
129130
pub fn invariant_runner(&self, contract_id: &str, test_fn: &str) -> TestRunner {
130131
let invariant = self.invariant_config(contract_id, test_fn);
131-
self.fuzzer_with_cases(invariant.runs, None)
132+
self.fuzzer_with_cases(invariant.runs, invariant.max_assume_rejects, None)
132133
}
133134

134135
/// Returns a "fuzz" configuration setup. Parameters are used to select tight scoped fuzz
@@ -156,12 +157,13 @@ impl TestOptions {
156157
pub fn fuzzer_with_cases(
157158
&self,
158159
cases: u32,
160+
max_global_rejects: u32,
159161
file_failure_persistence: Option<Box<dyn FailurePersistence>>,
160162
) -> TestRunner {
161163
let config = proptest::test_runner::Config {
162164
failure_persistence: file_failure_persistence,
163165
cases,
164-
max_global_rejects: self.fuzz.max_test_rejects,
166+
max_global_rejects,
165167
// Disable proptest shrink: for fuzz tests we provide single counterexample,
166168
// for invariant tests we shrink outside proptest.
167169
max_shrink_iters: 0,

crates/forge/tests/it/fuzz.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use forge::{
77
fuzz::CounterExample,
88
result::{SuiteResult, TestStatus},
99
};
10-
use foundry_test_utils::Filter;
10+
use foundry_test_utils::{forgetest_init, str, Filter};
1111
use std::collections::BTreeMap;
1212

1313
#[tokio::test(flavor = "multi_thread")]
@@ -176,3 +176,29 @@ async fn test_scrape_bytecode() {
176176
}
177177
}
178178
}
179+
180+
// tests that inline max-test-rejects config is properly applied
181+
forgetest_init!(test_inline_max_test_rejects, |prj, cmd| {
182+
prj.wipe_contracts();
183+
184+
prj.add_test(
185+
"Contract.t.sol",
186+
r#"
187+
import {Test} from "forge-std/Test.sol";
188+
189+
contract InlineMaxRejectsTest is Test {
190+
/// forge-config: default.fuzz.max-test-rejects = 1
191+
function test_fuzz_bound(uint256 a) public {
192+
vm.assume(a == 0);
193+
}
194+
}
195+
"#,
196+
)
197+
.unwrap();
198+
199+
cmd.args(["test"]).assert_failure().stdout_eq(str![[r#"
200+
...
201+
[FAIL. Reason: The `vm.assume` cheatcode rejected too many inputs (1 allowed)] test_fuzz_bound(uint256) (runs: 0, [AVG_GAS])
202+
...
203+
"#]]);
204+
});

0 commit comments

Comments
 (0)