Skip to content

Commit 5f6bd20

Browse files
authored
feat: add way to disable emitting of gas snapshots to disk (#9710)
* feat: gas snapshot emit config * review change comment --------- Co-authored-by: turbocrime <[email protected]>
1 parent 1d5fa64 commit 5f6bd20

File tree

3 files changed

+134
-11
lines changed

3 files changed

+134
-11
lines changed

crates/config/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ pub struct Config {
202202
pub snapshots: PathBuf,
203203
/// whether to check for differences against previously stored gas snapshots
204204
pub gas_snapshot_check: bool,
205+
/// whether to emit gas snapshots to disk
206+
pub gas_snapshot_emit: bool,
205207
/// where the broadcast logs are stored
206208
pub broadcast: PathBuf,
207209
/// additional solc allow paths for `--allow-paths`
@@ -2321,6 +2323,7 @@ impl Default for Config {
23212323
broadcast: "broadcast".into(),
23222324
snapshots: "snapshots".into(),
23232325
gas_snapshot_check: false,
2326+
gas_snapshot_emit: true,
23242327
allow_paths: vec![],
23252328
include_paths: vec![],
23262329
force: false,

crates/forge/bin/cmd/test/mod.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ pub struct TestArgs {
122122
#[arg(long, env = "FORGE_SNAPSHOT_CHECK")]
123123
gas_snapshot_check: Option<bool>,
124124

125+
/// Enable/disable recording of gas snapshot results.
126+
#[arg(long, env = "FORGE_SNAPSHOT_EMIT")]
127+
gas_snapshot_emit: Option<bool>,
128+
125129
/// Exit with code 0 even if a test fails.
126130
#[arg(long, env = "FORGE_ALLOW_FAILURE")]
127131
allow_failure: bool,
@@ -732,17 +736,28 @@ impl TestArgs {
732736
}
733737
}
734738

735-
// Create `snapshots` directory if it doesn't exist.
736-
fs::create_dir_all(&config.snapshots)?;
737-
738-
// Write gas snapshots to disk per group.
739-
gas_snapshots.clone().into_iter().for_each(|(group, snapshots)| {
740-
fs::write_pretty_json_file(
741-
&config.snapshots.join(format!("{group}.json")),
742-
&snapshots,
743-
)
744-
.expect("Failed to write gas snapshots to disk");
745-
});
739+
// By default `gas_snapshot_emit` is set to `true` in the config.
740+
//
741+
// The user can either:
742+
// - Set `FORGE_SNAPSHOT_EMIT=false` in the environment.
743+
// - Pass `--gas-snapshot-emit=false` as a CLI argument.
744+
// - Set `gas_snapshot_emit = false` in the config.
745+
//
746+
// If the user passes `--gas-snapshot-emit=<bool>` then it will override the config
747+
// and the environment variable, enabling the check if `true` is passed.
748+
if self.gas_snapshot_emit.unwrap_or(config.gas_snapshot_emit) {
749+
// Create `snapshots` directory if it doesn't exist.
750+
fs::create_dir_all(&config.snapshots)?;
751+
752+
// Write gas snapshots to disk per group.
753+
gas_snapshots.clone().into_iter().for_each(|(group, snapshots)| {
754+
fs::write_pretty_json_file(
755+
&config.snapshots.join(format!("{group}.json")),
756+
&snapshots,
757+
)
758+
.expect("Failed to write gas snapshots to disk");
759+
});
760+
}
746761
}
747762

748763
// Print suite summary.

crates/forge/tests/cli/config.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ forgetest!(can_extract_config_values, |prj, cmd| {
4141
cache_path: "test-cache".into(),
4242
snapshots: "snapshots".into(),
4343
gas_snapshot_check: false,
44+
gas_snapshot_emit: true,
4445
broadcast: "broadcast".into(),
4546
force: true,
4647
evm_version: EvmVersion::Byzantium,
@@ -966,6 +967,7 @@ cache = true
966967
cache_path = "cache"
967968
snapshots = "snapshots"
968969
gas_snapshot_check = false
970+
gas_snapshot_emit = true
969971
broadcast = "broadcast"
970972
allow_paths = []
971973
include_paths = []
@@ -1122,6 +1124,7 @@ exclude = []
11221124
"cache_path": "cache",
11231125
"snapshots": "snapshots",
11241126
"gas_snapshot_check": false,
1127+
"gas_snapshot_emit": true,
11251128
"broadcast": "broadcast",
11261129
"allow_paths": [],
11271130
"include_paths": [],
@@ -1535,3 +1538,105 @@ Suite result: ok. 1 passed; 0 failed; 0 skipped; [ELAPSED]
15351538
...
15361539
"#]]);
15371540
});
1541+
1542+
forgetest_init!(test_gas_snapshot_emit_config, |prj, cmd| {
1543+
// Default settings: gas_snapshot_emit enabled.
1544+
cmd.forge_fuse().args(["config"]).assert_success().stdout_eq(str![[r#"
1545+
...
1546+
gas_snapshot_emit = true
1547+
...
1548+
"#]]);
1549+
1550+
prj.insert_ds_test();
1551+
1552+
prj.add_source(
1553+
"GasSnapshotEmitTest.sol",
1554+
r#"
1555+
import "./test.sol";
1556+
1557+
interface Vm {
1558+
function startSnapshotGas(string memory name) external;
1559+
function stopSnapshotGas() external returns (uint256);
1560+
}
1561+
1562+
contract GasSnapshotEmitTest is DSTest {
1563+
Vm constant vm = Vm(HEVM_ADDRESS);
1564+
1565+
function testSnapshotGasSection() public {
1566+
vm.startSnapshotGas("testSection");
1567+
int n = 1;
1568+
vm.stopSnapshotGas();
1569+
}
1570+
}
1571+
"#,
1572+
)
1573+
.unwrap();
1574+
1575+
// Assert that gas_snapshot_emit is enabled by default.
1576+
cmd.forge_fuse().args(["test"]).assert_success();
1577+
// Assert that snapshots were emitted to disk.
1578+
assert!(prj.root().join("snapshots/GasSnapshotEmitTest.json").exists());
1579+
1580+
// Remove the snapshot file.
1581+
fs::remove_file(prj.root().join("snapshots/GasSnapshotEmitTest.json")).unwrap();
1582+
1583+
// Test that `--gas-snapshot-emit=false` flag can be used to disable writing snapshots.
1584+
cmd.forge_fuse().args(["test", "--gas-snapshot-emit=false"]).assert_success();
1585+
// Assert that snapshots were not emitted to disk.
1586+
assert!(!prj.root().join("snapshots/GasSnapshotEmitTest.json").exists());
1587+
1588+
// Test that environment variable `FORGE_SNAPSHOT_EMIT` can be used to disable writing
1589+
// snapshots.
1590+
cmd.forge_fuse();
1591+
cmd.env("FORGE_SNAPSHOT_EMIT", "false");
1592+
cmd.args(["test"]).assert_success();
1593+
// Assert that snapshots were not emitted to disk.
1594+
assert!(!prj.root().join("snapshots/GasSnapshotEmitTest.json").exists());
1595+
1596+
// Test that `--gas-snapshot-emit=true` flag can be used to enable writing snapshots, even when
1597+
// `FORGE_SNAPSHOT_EMIT` is set to false.
1598+
cmd.forge_fuse();
1599+
cmd.env("FORGE_SNAPSHOT_EMIT", "false");
1600+
cmd.args(["test", "--gas-snapshot-emit=true"]).assert_success();
1601+
// Assert that snapshots were emitted to disk.
1602+
assert!(prj.root().join("snapshots/GasSnapshotEmitTest.json").exists());
1603+
1604+
// Remove the snapshot file.
1605+
fs::remove_file(prj.root().join("snapshots/GasSnapshotEmitTest.json")).unwrap();
1606+
1607+
// Disable gas_snapshot_emit in the config file.
1608+
prj.update_config(|config| config.gas_snapshot_emit = false);
1609+
cmd.forge_fuse().args(["config"]).assert_success();
1610+
1611+
// Test that snapshots are not emitted to disk, when disabled by config.
1612+
cmd.forge_fuse().args(["test"]).assert_success();
1613+
// Assert that snapshots were not emitted to disk.
1614+
assert!(!prj.root().join("snapshots/GasSnapshotEmitTest.json").exists());
1615+
1616+
// Test that `--gas-snapshot-emit=true` flag can be used to enable writing snapshots, when
1617+
// disabled by config.
1618+
cmd.forge_fuse();
1619+
cmd.args(["test", "--gas-snapshot-emit=true"]).assert_success();
1620+
// Assert that snapshots were emitted to disk.
1621+
assert!(prj.root().join("snapshots/GasSnapshotEmitTest.json").exists());
1622+
1623+
// Remove the snapshot file.
1624+
fs::remove_file(prj.root().join("snapshots/GasSnapshotEmitTest.json")).unwrap();
1625+
1626+
// Test that environment variable `FORGE_SNAPSHOT_EMIT` can be used to enable writing snapshots.
1627+
cmd.forge_fuse();
1628+
cmd.env("FORGE_SNAPSHOT_EMIT", "true");
1629+
cmd.args(["test"]).assert_success();
1630+
// Assert that snapshots were emitted to disk.
1631+
assert!(prj.root().join("snapshots/GasSnapshotEmitTest.json").exists());
1632+
1633+
// Remove the snapshot file.
1634+
fs::remove_file(prj.root().join("snapshots/GasSnapshotEmitTest.json")).unwrap();
1635+
1636+
// Test that `--gas-snapshot-emit=false` flag can be used to disable writing snapshots,
1637+
// even when `FORGE_SNAPSHOT_EMIT` is set to true.
1638+
cmd.forge_fuse().args(["test", "--gas-snapshot-emit=false"]).assert_success();
1639+
1640+
// Assert that snapshots were not emitted to disk.
1641+
assert!(!prj.root().join("snapshots/GasSnapshotEmitTest.json").exists());
1642+
});

0 commit comments

Comments
 (0)