|
1 | 1 | use super::{install, test::filter::ProjectPathsAwareFilter, watch::WatchArgs}; |
2 | 2 | use alloy_primitives::U256; |
3 | 3 | use chrono::Utc; |
4 | | -use clap::{Parser, ValueHint}; |
| 4 | +use clap::{builder::FalseyValueParser, ArgAction, Parser, ValueHint}; |
5 | 5 | use eyre::{Context, OptionExt, Result}; |
6 | 6 | use forge::{ |
7 | 7 | decode::decode_console_logs, |
@@ -122,6 +122,14 @@ pub struct TestArgs { |
122 | 122 | #[arg(long, env = "FORGE_ALLOW_FAILURE")] |
123 | 123 | allow_failure: bool, |
124 | 124 |
|
| 125 | + /// Enable/disable writing snapshot results |
| 126 | + #[arg(long, env = "FORGE_SNAPSHOT_EMIT", action = ArgAction::Set, default_value="true", default_missing_value = "true", require_equals=true, num_args = 0..=1)] |
| 127 | + snapshot_emit: bool, |
| 128 | + |
| 129 | + /// Check snapshot results |
| 130 | + #[arg(long, env = "FORGE_SNAPSHOT_CHECK", value_parser=FalseyValueParser::new())] |
| 131 | + snapshot_check: bool, |
| 132 | + |
125 | 133 | /// Output test results as JUnit XML report. |
126 | 134 | #[arg(long, conflicts_with_all = ["quiet", "json", "gas_report", "summary", "list", "show_progress"], help_heading = "Display options")] |
127 | 135 | pub junit: bool, |
@@ -662,7 +670,7 @@ impl TestArgs { |
662 | 670 | if !gas_snapshots.is_empty() { |
663 | 671 | // Check for differences in gas snapshots if `FORGE_SNAPSHOT_CHECK` is set. |
664 | 672 | // Exiting early with code 1 if differences are found. |
665 | | - if std::env::var("FORGE_SNAPSHOT_CHECK").is_ok() { |
| 673 | + if self.snapshot_check { |
666 | 674 | let differences_found = gas_snapshots.clone().into_iter().fold( |
667 | 675 | false, |
668 | 676 | |mut found, (group, snapshots)| { |
@@ -717,17 +725,19 @@ impl TestArgs { |
717 | 725 | } |
718 | 726 | } |
719 | 727 |
|
720 | | - // Create `snapshots` directory if it doesn't exist. |
721 | | - fs::create_dir_all(&config.snapshots)?; |
722 | | - |
723 | | - // Write gas snapshots to disk per group. |
724 | | - gas_snapshots.clone().into_iter().for_each(|(group, snapshots)| { |
725 | | - fs::write_pretty_json_file( |
726 | | - &config.snapshots.join(format!("{group}.json")), |
727 | | - &snapshots, |
728 | | - ) |
729 | | - .expect("Failed to write gas snapshots to disk"); |
730 | | - }); |
| 728 | + if self.snapshot_emit { |
| 729 | + // Create `snapshots` directory if it doesn't exist. |
| 730 | + fs::create_dir_all(&config.snapshots)?; |
| 731 | + |
| 732 | + // Write gas snapshots to disk per group. |
| 733 | + gas_snapshots.clone().into_iter().for_each(|(group, snapshots)| { |
| 734 | + fs::write_pretty_json_file( |
| 735 | + &config.snapshots.join(format!("{group}.json")), |
| 736 | + &snapshots, |
| 737 | + ) |
| 738 | + .expect("Failed to write gas snapshots to disk"); |
| 739 | + }); |
| 740 | + } |
731 | 741 | } |
732 | 742 |
|
733 | 743 | // Print suite summary. |
@@ -973,6 +983,40 @@ mod tests { |
973 | 983 | test("--chain-id=42", Chain::from_id(42)); |
974 | 984 | } |
975 | 985 |
|
| 986 | + fn env_bool(env_name: &str, test_fn: impl Fn(Option<bool>)) { |
| 987 | + for env_val in [None, Some(false), Some(true)] { |
| 988 | + match env_val { |
| 989 | + None => std::env::remove_var(env_name), |
| 990 | + Some(value) => std::env::set_var(env_name, value.to_string()), |
| 991 | + } |
| 992 | + test_fn(env_val); |
| 993 | + } |
| 994 | + } |
| 995 | + |
| 996 | + #[test] |
| 997 | + fn snapshot_emit_env() { |
| 998 | + env_bool("FORGE_SNAPSHOT_EMIT", |env_val| { |
| 999 | + let args: TestArgs = TestArgs::parse_from(["foundry-cli"]); |
| 1000 | + assert!(args.snapshot_emit == env_val.unwrap_or(true)); |
| 1001 | + let args: TestArgs = TestArgs::parse_from(["foundry-cli", "--snapshot-emit"]); |
| 1002 | + assert!(args.snapshot_emit); |
| 1003 | + let args: TestArgs = TestArgs::parse_from(["foundry-cli", "--snapshot-emit=true"]); |
| 1004 | + assert!(args.snapshot_emit); |
| 1005 | + let args: TestArgs = TestArgs::parse_from(["foundry-cli", "--snapshot-emit=false"]); |
| 1006 | + assert!(!args.snapshot_emit); |
| 1007 | + }); |
| 1008 | + } |
| 1009 | + |
| 1010 | + #[test] |
| 1011 | + fn snapshot_check_env() { |
| 1012 | + env_bool("FORGE_SNAPSHOT_CHECK", |env_val| { |
| 1013 | + let args: TestArgs = TestArgs::parse_from(["foundry-cli"]); |
| 1014 | + assert!(args.snapshot_check == env_val.unwrap_or(false)); |
| 1015 | + let args: TestArgs = TestArgs::parse_from(["foundry-cli", "--snapshot-check"]); |
| 1016 | + assert!(args.snapshot_check); |
| 1017 | + }); |
| 1018 | + } |
| 1019 | + |
976 | 1020 | forgetest_async!(gas_report_fuzz_invariant, |prj, _cmd| { |
977 | 1021 | // speed up test by running with depth of 15 |
978 | 1022 | let config = Config { |
|
0 commit comments