Skip to content

Commit

Permalink
fix(forge): forge script --json always returns zero exit code #2508 (
Browse files Browse the repository at this point in the history
…#7071)

* add basic tests

* catch error even if --json is passed

* nit
  • Loading branch information
zerosnacks authored Feb 10, 2024
1 parent d37328a commit 174752f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/forge/bin/cmd/script/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,13 @@ impl ScriptArgs {
let j = serde_json::to_string(&output)?;
shell::println(j)?;

if !result.success {
return Err(eyre::eyre!(
"script failed: {}",
decode::decode_revert(&result.returned[..], None, None)
));
}

Ok(())
}

Expand Down
41 changes: 41 additions & 0 deletions crates/forge/tests/cli/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,47 @@ contract Demo {
);
});

static FAILING_SCRIPT: &str = r#"
import "forge-std/Script.sol";
contract FailingScript is Script {
function run() external {
revert("failed");
}
}
"#;

// Tests that execution throws upon encountering a revert in the script.
forgetest_async!(assert_exit_code_error_on_failure_script, |prj, cmd| {
foundry_test_utils::util::initialize(prj.root());
let script = prj.add_source("FailingScript", FAILING_SCRIPT).unwrap();

// set up command
cmd.arg("script").arg(script);

// run command and assert error exit code
cmd.assert_err();

let output = cmd.stderr_lossy();
assert!(output.contains("script failed: revert: failed"));
});

// Tests that execution throws upon encountering a revert in the script with --json option.
// <https://github.com/foundry-rs/foundry/issues/2508>
forgetest_async!(assert_exit_code_error_on_failure_script_with_json, |prj, cmd| {
foundry_test_utils::util::initialize(prj.root());
let script = prj.add_source("FailingScript", FAILING_SCRIPT).unwrap();

// set up command
cmd.arg("script").arg(script).arg("--json");

// run command and assert error exit code
cmd.assert_err();

let output = cmd.stderr_lossy();
assert!(output.contains("script failed: revert: failed"));
});

// Tests that the manually specified gas limit is used when using the --unlocked option
forgetest_async!(can_execute_script_command_with_manual_gas_limit_unlocked, |prj, cmd| {
foundry_test_utils::util::initialize(prj.root());
Expand Down

0 comments on commit 174752f

Please sign in to comment.