@@ -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
966967cache_path = "cache"
967968snapshots = "snapshots"
968969gas_snapshot_check = false
970+ gas_snapshot_emit = true
969971broadcast = "broadcast"
970972allow_paths = []
971973include_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