Skip to content

Commit 268f442

Browse files
authored
test(forge): add optimism deployCode coverage (#16453)
1 parent a0f0780 commit 268f442

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

  • crates/forge/tests/cli/test_cmd

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,63 @@ forgetest!(flaky_testdata, |_prj, cmd| {
174174
cmd.args(["test", &mc]).assert_success();
175175
});
176176

177+
// Ensures `vm.deployCode` works with the optimism network family active, covering the OP EVM's
178+
// nested frame execution path which is only reachable through this cheatcode.
179+
forgetest_init!(deploy_code_cheatcode_on_optimism_network, |prj, cmd| {
180+
prj.update_config(|config| {
181+
config.networks = foundry_evm_networks::NetworkConfigs::with_optimism();
182+
});
183+
184+
prj.add_source(
185+
"OpCounter.sol",
186+
r#"
187+
contract OpCounter {
188+
uint256 public number;
189+
190+
constructor(uint256 initial) {
191+
number = initial;
192+
}
193+
194+
function increment() external {
195+
number++;
196+
}
197+
}
198+
"#,
199+
);
200+
201+
prj.add_test(
202+
"OpDeployCode.t.sol",
203+
r#"
204+
import {Test} from "forge-std/Test.sol";
205+
import {OpCounter} from "../src/OpCounter.sol";
206+
207+
contract OpDeployCodeTest is Test {
208+
function testDeployCodeOnOptimism() public {
209+
address deployed = vm.deployCode("src/OpCounter.sol:OpCounter", abi.encode(41));
210+
assertGt(deployed.code.length, 0);
211+
212+
OpCounter counter = OpCounter(deployed);
213+
assertEq(counter.number(), 41);
214+
counter.increment();
215+
assertEq(counter.number(), 42);
216+
}
217+
}
218+
"#,
219+
);
220+
221+
cmd.args(["test", "--match-contract", "OpDeployCodeTest"]).assert_success().stdout_eq(str![[
222+
r#"
223+
...
224+
Ran 1 test for test/OpDeployCode.t.sol:OpDeployCodeTest
225+
[PASS] testDeployCodeOnOptimism() ([GAS])
226+
Suite result: ok. 1 passed; 0 failed; 0 skipped; [ELAPSED]
227+
228+
Ran 1 test suite [ELAPSED]: 1 tests passed, 0 failed, 0 skipped (1 total tests)
229+
230+
"#
231+
]]);
232+
});
233+
177234
// tests that test filters are handled correctly
178235
forgetest!(can_set_filter_values, |prj, cmd| {
179236
let patt = regex::Regex::new("test*").unwrap();

0 commit comments

Comments
 (0)