From fd25c20310eec6a72567161eeadb1c28b7bab3a6 Mon Sep 17 00:00:00 2001 From: Madeveda <55543890+Madeveda@users.noreply.github.com> Date: Tue, 2 Jul 2024 00:03:10 +0200 Subject: [PATCH] add gasmetering to tests (#1) --- .forge-snapshots/Increment counter number.snap | 1 + .forge-snapshots/Set counter number.snap | 1 + .github/workflows/test.yaml | 11 +++++++---- foundry.toml | 18 ++++++++++++++++-- lib/forge-std | 2 +- test/Counter.t.sol | 7 +++++-- 6 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 .forge-snapshots/Increment counter number.snap create mode 100644 .forge-snapshots/Set counter number.snap diff --git a/.forge-snapshots/Increment counter number.snap b/.forge-snapshots/Increment counter number.snap new file mode 100644 index 0000000..1d3d6fb --- /dev/null +++ b/.forge-snapshots/Increment counter number.snap @@ -0,0 +1 @@ +26276 \ No newline at end of file diff --git a/.forge-snapshots/Set counter number.snap b/.forge-snapshots/Set counter number.snap new file mode 100644 index 0000000..c013c6c --- /dev/null +++ b/.forge-snapshots/Set counter number.snap @@ -0,0 +1 @@ +26363 \ No newline at end of file diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 42c3cda..12fc339 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -3,12 +3,14 @@ name: test on: pull_request: branches: [main, master, staging, dev, feat/**, fix/**] + push: + branches: [main, master, staging, dev] env: - FOUNDRY_PROFILE: ci + FOUNDRY_PROFILE: ${{ github.event_name == 'push' && 'ci' || 'pr' }} jobs: - check: + test: strategy: fail-fast: true @@ -31,6 +33,7 @@ jobs: id: build - name: Run Forge tests - run: | - forge test -vvv + run: forge test --isolate -vvv id: test + env: + FORGE_SNAPSHOT_CHECK: true diff --git a/foundry.toml b/foundry.toml index baea3d3..88deca3 100644 --- a/foundry.toml +++ b/foundry.toml @@ -8,6 +8,10 @@ via_ir = true solc = "0.8.23" verbosity = 2 ffi = true +fs_permissions = [ + { access = "read-write", path = ".forge-snapshots"}, + { access = "read", path = "script/" } +] remappings = [ "forge-std=lib/forge-std/src", @@ -15,9 +19,19 @@ remappings = [ "@openzeppelin/contracts-upgradeable=lib/openzeppelin-contracts-upgradeable/contracts" ] -[profile.intense.fuzz] +[profile.default.fuzz] +runs = 1000 + +[profile.pr.fuzz] runs = 10000 -max_test_rejects = 999999 + +[profile.ci.fuzz] +runs = 100000 + +[profile.debug] +via_ir = false +optimizer_runs = 200 +fuzz.runs = 100 [fmt] line_length = 160 diff --git a/lib/forge-std b/lib/forge-std index 2f11269..5475f85 160000 --- a/lib/forge-std +++ b/lib/forge-std @@ -1 +1 @@ -Subproject commit 2f112697506eab12d433a65fdc31a639548fe365 +Subproject commit 5475f852e3f530d7e25dfb4596aa1f9baa8ffdfc diff --git a/test/Counter.t.sol b/test/Counter.t.sol index 12eb00d..2341ab0 100644 --- a/test/Counter.t.sol +++ b/test/Counter.t.sol @@ -3,6 +3,7 @@ pragma solidity 0.8.23; import "forge-std/Test.sol"; import "test/util/TestHelpers.sol"; +import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol"; import {Counter} from "src/Counter.sol"; @@ -15,18 +16,20 @@ abstract contract Deployed is Test, TestHelpers { } } -contract CounterTest_Deployed is Deployed { - function test_IsInitialized() public { +contract CounterTest_Deployed is Deployed, GasSnapshot { + function test_IsInitialized() public view { assertEq(counter.number(), 10); } function test_IncrementsNumber() public { counter.increment(); + snapLastCall("Increment counter number"); assertEq(counter.number(), 11); } function testFuzz_SetsNumber(uint256 x) public { counter.setNumber(x); + snapLastCall("Set counter number"); assertEq(counter.number(), x); } }