Skip to content

Commit

Permalink
fix bad state reset (foundry-rs#226)
Browse files Browse the repository at this point in the history
* fix bad state reset

* fmt

* clippy
  • Loading branch information
brockelmore authored Dec 15, 2021
1 parent 9a0065a commit 483a781
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
7 changes: 5 additions & 2 deletions forge/src/multi_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,12 @@ where
) -> Result<BTreeMap<String, BTreeMap<String, TestResult>>> {
// TODO: Convert to iterator, ideally parallel one?
let contracts = std::mem::take(&mut self.contracts);

let init_state: S = self.evm.state().clone();
let results = contracts
.iter()
.map(|(name, (abi, address, logs))| {
let result = self.run_tests(name, abi, *address, logs, &pattern)?;
let result = self.run_tests(name, abi, *address, logs, &pattern, &init_state)?;
Ok((name.clone(), result))
})
.filter_map(|x: Result<_>| x.ok())
Expand All @@ -162,10 +164,11 @@ where
address: Address,
init_logs: &[String],
pattern: &Regex,
init_state: &S,
) -> Result<BTreeMap<String, TestResult>> {
let mut runner =
ContractRunner::new(&mut self.evm, contract, address, self.sender, init_logs);
runner.run_tests(pattern, self.fuzzer.as_mut())
runner.run_tests(pattern, self.fuzzer.as_mut(), init_state)
}
}

Expand Down
19 changes: 14 additions & 5 deletions forge/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl<'a, S: Clone, E: Evm<S>> ContractRunner<'a, S, E> {
&mut self,
regex: &Regex,
fuzzer: Option<&mut TestRunner>,
init_state: &S,
) -> Result<BTreeMap<String, TestResult>> {
tracing::info!("starting tests");
let start = Instant::now();
Expand All @@ -108,8 +109,6 @@ impl<'a, S: Clone, E: Evm<S>> ContractRunner<'a, S, E> {
.filter(|func| regex.is_match(&func.name))
.collect::<Vec<_>>();

let init_state = self.evm.state().clone();

// run all unit tests
let unit_tests = test_fns
.iter()
Expand Down Expand Up @@ -284,14 +283,20 @@ mod tests {
.deploy(Address::zero(), compiled.bytecode().unwrap().clone(), 0.into())
.unwrap();

let init_state = evm.state().clone();

let mut runner =
ContractRunner::new(&mut evm, compiled.abi.as_ref().unwrap(), addr, None, &[]);

let mut cfg = FuzzConfig::default();
cfg.failure_persistence = None;
let mut fuzzer = TestRunner::new(cfg);
let results = runner
.run_tests(&Regex::from_str("testGreeting").unwrap(), Some(&mut fuzzer))
.run_tests(
&Regex::from_str("testGreeting").unwrap(),
Some(&mut fuzzer),
&init_state,
)
.unwrap();
assert!(results["testGreeting()"].success);
assert!(results["testGreeting(string)"].success);
Expand All @@ -311,14 +316,16 @@ mod tests {
.deploy(Address::zero(), compiled.bytecode().unwrap().clone(), 0.into())
.unwrap();

let init_state = evm.state().clone();

let mut runner =
ContractRunner::new(&mut evm, compiled.abi.as_ref().unwrap(), addr, None, &[]);

let mut cfg = FuzzConfig::default();
cfg.failure_persistence = None;
let mut fuzzer = TestRunner::new(cfg);
let results = runner
.run_tests(&Regex::from_str("testFuzz.*").unwrap(), Some(&mut fuzzer))
.run_tests(&Regex::from_str("testFuzz.*").unwrap(), Some(&mut fuzzer), &init_state)
.unwrap();
for (_, res) in results {
assert!(!res.success);
Expand Down Expand Up @@ -422,10 +429,12 @@ mod tests {
let (addr, _, _, _) =
evm.deploy(Address::zero(), compiled.bytecode().unwrap().clone(), 0.into()).unwrap();

let init_state = evm.state().clone();

let mut runner =
ContractRunner::new(&mut evm, compiled.abi.as_ref().unwrap(), addr, None, &[]);

let res = runner.run_tests(&".*".parse().unwrap(), None).unwrap();
let res = runner.run_tests(&".*".parse().unwrap(), None, &init_state).unwrap();
assert!(!res.is_empty());
assert!(res.iter().all(|(_, result)| result.success));
}
Expand Down

0 comments on commit 483a781

Please sign in to comment.