-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: forge test snapshot flags #9710
base: master
Are you sure you want to change the base?
Conversation
0cd7bd4
to
ab193b2
Compare
#[arg(long, env = "FORGE_SNAPSHOT_EMIT", action = ArgAction::Set, default_value="true", default_missing_value = "true", require_equals=true, num_args = 0..=1)] | ||
snapshot_emit: bool, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a little strange but it's the only way to get decent behavior out of clap
@@ -662,7 +670,7 @@ impl TestArgs { | |||
if !gas_snapshots.is_empty() { | |||
// Check for differences in gas snapshots if `FORGE_SNAPSHOT_CHECK` is set. | |||
// Exiting early with code 1 if differences are found. | |||
if std::env::var("FORGE_SNAPSHOT_CHECK").is_ok() { | |||
if self.snapshot_check { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
direct env var access is now moved to another clap flag.
the behavior now supports 'false' values as well, instead of just conditioning on presence.
cc @zerosnacks assigned to #9697 |
Generally supportive on the approach, defaulting to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you! left couple of comments
@@ -973,6 +983,40 @@ mod tests { | |||
test("--chain-id=42", Chain::from_id(42)); | |||
} | |||
|
|||
fn env_bool(env_name: &str, test_fn: impl Fn(Option<bool>)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is needed, you could use figment::Jail
for such as in
foundry/crates/config/src/lib.rs
Lines 2996 to 3004 in 90a5fdf
fn test_toml_file_non_existing_config_var_failure() { | |
figment::Jail::expect_with(|jail| { | |
jail.set_env("FOUNDRY_CONFIG", "this config does not exist"); | |
let _config = Config::load(); | |
Ok(()) | |
}); | |
} |
#[arg(long, env = "FORGE_SNAPSHOT_EMIT", action = ArgAction::Set, default_value="true", default_missing_value = "true", require_equals=true, num_args = 0..=1)] | ||
snapshot_emit: bool, | ||
|
||
/// Check snapshot results |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Check snapshot results | |
/// Disable checking for differences in recorded gas snapshots. |
@@ -122,6 +122,14 @@ pub struct TestArgs { | |||
#[arg(long, env = "FORGE_ALLOW_FAILURE")] | |||
allow_failure: bool, | |||
|
|||
/// Enable/disable writing snapshot results |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Enable/disable writing snapshot results | |
/// Disable recording of gas snapshot results. |
@@ -122,6 +122,14 @@ pub struct TestArgs { | |||
#[arg(long, env = "FORGE_ALLOW_FAILURE")] | |||
allow_failure: bool, | |||
|
|||
/// Enable/disable writing snapshot results | |||
#[arg(long, env = "FORGE_SNAPSHOT_EMIT", action = ArgAction::Set, default_value="true", default_missing_value = "true", require_equals=true, num_args = 0..=1)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe have FORGE_NO_SNAPSHOT_RECORD
/ no_snapshot_record
like
/// Disable recording of snapshot results.
#[arg(long, env = "FORGE_SNAPSHOT_NO_RECORD", help_heading = "Gas snapshot options")]
no_snapshot_record: bool,
so one should only pass --no-snapshot-record
arg. Also suggest adding a help heading to have them grouped, e.g.
Gas snapshot options:
--snapshot-no-record
Enable/disable writing snapshot results
[env: FORGE_SNAPSHOT_NO_RECORD=]
--snapshot-no-check
Omit checking snapshot results
[env: FORGE_SNAPSHOT_CHECK=]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in my use case, i'd like to mainly control via env vars. using a 'negative' env variable seems ergonomically poor.
additionally, clap seems to have trouble with 'negation' flags that interact with each other, especially if env variables are used, and especially when default values are present.
i can make another attempt at this, but clap seems to very strongly encourage the =[true/false]
format.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, maybe put them in config then? You could then make different profiles to run gas snapshot without need of args
} | ||
} | ||
|
||
#[test] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO would be better to have test to pass these flags and check if the gas snapshot dir is (or is not) written to disk, something like
forgetest_init!(config_load_no_panic, |prj, cmd| {
prj.add_test(
"GasSnapshotRecordTest.t.sol",
r#"
import "forge-std/Test.sol";
contract GasSnapshotRecordTest is Test {
function test_start_stop_gas_recording() public {
vm.startSnapshotGas("testRecord");
uint256 a = 256;
a++;
vm.stopSnapshotGas();
}
}
"#,
)
.unwrap();
cmd.args(["clean"]).assert_success();
cmd.forge_fuse().args(["test", "--mc", "GasSnapshotRecordTest", "--snapshot-no-record"]).assert_success();
assert!(!prj.root().join("snapshots/").exists());
cmd.forge_fuse().args(["test", "--mc", "GasSnapshotRecordTest"]).assert_success();
assert!(prj.root().join("snapshots/").exists());
});
snapshot_emit: bool, | ||
|
||
/// Check snapshot results | ||
#[arg(long, env = "FORGE_SNAPSHOT_CHECK", value_parser=FalseyValueParser::new())] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think #[arg(long, env = "FORGE_SNAPSHOT_NO_CHECK", help_heading = "Gas snapshot options")]
is enough, that is one could pass --snapshot-no-check
arg to omit differences check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FORGE_SNAPSHOT_CHECK
is already established as an env var, and i was avoiding the introduction of another FORGE_SNAPSHOT_NO_CHECK
var.
if both variables exist, it raises a lot of questions! i would be worried about creating a breaking change.
I'd suggest having args like |
the environment variable i made the
but now i see that also raises many questions, and it is not entirely consistent with the behavior of my added flag. i will revert changes to |
added the |
thanks for working on this, wound up with other tasks on my plate and wasn't able to make time |
the emit behavior still remains to be implemented? will leave this open for now, might get to it with free time this week |
@turbocrime No worries! Feel free to ping me if you run into (time) issues / have questions |
Motivation
fixes #9697
Solution
i've added command-line flags and an environment variable
FORGE_SNAPSHOT_EMIT
which allow a user to control snapshot generation.i also slightly refactored the use of environment variable
FORGE_SNAPSHOT_CHECK
.existing default behaviors are maintained.