Skip to content

Commit dc9ff38

Browse files
authored
feat(forge): allow invariant contract address as targetContract (#10274)
1 parent c0914e4 commit dc9ff38

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

crates/evm/evm/src/executors/invariant/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,11 @@ impl<'a> InvariantExecutor<'a> {
721721
.setup_contracts
722722
.iter()
723723
.filter(|&(addr, (identifier, _))| {
724+
// Include to address if explicitly set as target.
725+
if *addr == to && selected.contains(&to) {
726+
return true;
727+
}
728+
724729
*addr != to &&
725730
*addr != CHEATCODE_ADDRESS &&
726731
*addr != HARDHAT_CONSOLE_ADDRESS &&

crates/forge/tests/it/invariant.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,3 +1267,69 @@ Warning: Failure from "[..]/invariant/failures/OwnableTest/invariant_never_owner
12671267
...
12681268
"#]]);
12691269
});
1270+
1271+
// <https://github.com/foundry-rs/foundry/issues/10253>
1272+
forgetest_init!(invariant_test_target, |prj, cmd| {
1273+
prj.update_config(|config| {
1274+
config.invariant.runs = 5;
1275+
config.invariant.depth = 5;
1276+
});
1277+
prj.add_test(
1278+
"InvariantTest.t.sol",
1279+
r#"
1280+
import {Test} from "forge-std/Test.sol";
1281+
1282+
contract InvariantTest is Test {
1283+
uint256 count;
1284+
1285+
function setCount(uint256 _count) public {
1286+
count = _count;
1287+
}
1288+
1289+
function setUp() public {
1290+
}
1291+
1292+
function invariant_check_count() public {
1293+
}
1294+
}
1295+
"#,
1296+
)
1297+
.unwrap();
1298+
1299+
cmd.args(["test", "--mt", "invariant_check_count"]).assert_failure().stdout_eq(str![[r#"
1300+
...
1301+
[FAIL: failed to set up invariant testing environment: No contracts to fuzz.] invariant_check_count() (runs: 0, calls: 0, reverts: 0)
1302+
...
1303+
"#]]);
1304+
1305+
prj.add_test(
1306+
"InvariantTest.t.sol",
1307+
r#"
1308+
import {Test} from "forge-std/Test.sol";
1309+
1310+
contract InvariantTest is Test {
1311+
uint256 count;
1312+
1313+
function setCount(uint256 _count) public {
1314+
count = _count;
1315+
}
1316+
1317+
function setUp() public {
1318+
targetContract(address(this));
1319+
}
1320+
1321+
function invariant_check_count() public {
1322+
}
1323+
}
1324+
"#,
1325+
)
1326+
.unwrap();
1327+
1328+
cmd.forge_fuse().args(["test", "--mt", "invariant_check_count"]).assert_success().stdout_eq(
1329+
str![[r#"
1330+
...
1331+
[PASS] invariant_check_count() (runs: 5, calls: 25, reverts: 0)
1332+
...
1333+
"#]],
1334+
);
1335+
});

0 commit comments

Comments
 (0)