Skip to content

Commit fdd78d9

Browse files
fix(forge): catch faulty test constructors (foundry-rs#9909)
* fix(`forge`): catch test contract deployment failures * nit * test * fix * nit * Revert "nit" This reverts commit 5712a93. * Revert "fix" This reverts commit 9f6bee1. * fix test * cleaner * nit Co-authored-by: zerosnacks <[email protected]> --------- Co-authored-by: zerosnacks <[email protected]>
1 parent 087c676 commit fdd78d9

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

crates/forge/src/result.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,8 @@ pub struct TestSetup {
765765
pub reason: Option<String>,
766766
/// Whether setup and entire test suite is skipped.
767767
pub skipped: bool,
768+
/// Whether the test failed to deploy.
769+
pub deployment_failure: bool,
768770
}
769771

770772
impl TestSetup {

crates/forge/src/runner.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ impl<'a> ContractRunner<'a> {
158158
U256::ZERO,
159159
Some(&self.mcr.revert_decoder),
160160
);
161+
162+
result.deployment_failure = deploy_result.is_err();
163+
161164
if let Ok(dr) = &deploy_result {
162165
debug_assert_eq!(dr.address, address);
163166
}
@@ -340,9 +343,14 @@ impl<'a> ContractRunner<'a> {
340343

341344
if setup.reason.is_some() {
342345
// The setup failed, so we return a single test result for `setUp`
346+
let fail_msg = if !setup.deployment_failure {
347+
"setUp()".to_string()
348+
} else {
349+
"constructor()".to_string()
350+
};
343351
return SuiteResult::new(
344352
start.elapsed(),
345-
[("setUp()".to_string(), TestResult::setup_result(setup))].into(),
353+
[(fail_msg, TestResult::setup_result(setup))].into(),
346354
warnings,
347355
)
348356
}

crates/forge/tests/cli/test_cmd.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3137,3 +3137,34 @@ Encountered a total of 1 failing tests, 1 tests succeeded
31373137
31383138
"#]]);
31393139
});
3140+
3141+
forgetest_init!(catch_test_deployment_failure, |prj, cmd| {
3142+
prj.add_test(
3143+
"TestDeploymentFailure.t.sol",
3144+
r#"
3145+
import "forge-std/Test.sol";
3146+
contract TestDeploymentFailure is Test {
3147+
3148+
constructor() {
3149+
require(false);
3150+
}
3151+
3152+
function setUp() public {
3153+
require(true);
3154+
}
3155+
3156+
function test_something() public {
3157+
require(1 == 1);
3158+
}
3159+
}
3160+
"#,
3161+
)
3162+
.unwrap();
3163+
3164+
cmd.args(["t", "--mt", "test_something"]).assert_failure().stdout_eq(str![[r#"
3165+
...
3166+
Failing tests:
3167+
Encountered 1 failing test in test/TestDeploymentFailure.t.sol:TestDeploymentFailure
3168+
[FAIL: EvmError: Revert] constructor() ([GAS])
3169+
..."#]]);
3170+
});

0 commit comments

Comments
 (0)