Skip to content

Commit 1fdaab7

Browse files
authored
chore(all): simplify mocks generation (#1413)
- Localized mockgen commands in the package where they are needed - Generate mocks from your IDE directly - Platform independent way of generating mocks - Check automatically mocks are kept up to date with their matching interfaces - Check automatically mocks all have a matching source controlled generation command - Remove license headers for generated mocks - Generating mocks section added in contributing document - Use same mockgen version as specified in go.mod
1 parent 9d9cf92 commit 1fdaab7

16 files changed

+78
-74
lines changed

.github/CONTRIBUTING.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,40 @@ guidelines:
2727
- Commit messages should be prefixed with the package(s) they modify.
2828
- E.g. "eth, rpc: make trace configs optional"
2929

30+
### Mocks
31+
32+
Mocks are auto-generated using [mockgen](https://pkg.go.dev/go.uber.org/mock/mockgen) and `//go:generate` commands in the code.
33+
34+
- To **re-generate all mocks**, use the command below from the root of the project:
35+
36+
```sh
37+
go generate -run "go.uber.org/mock/mockgen" ./...
38+
```
39+
40+
- To **add** an interface that needs a corresponding mock generated:
41+
- if the file `mocks_generate_test.go` exists in the package where the interface is located, either:
42+
- modify its `//go:generate go run go.uber.org/mock/mockgen` to generate a mock for your interface (preferred); or
43+
- add another `//go:generate go run go.uber.org/mock/mockgen` to generate a mock for your interface according to specific mock generation settings
44+
- if the file `mocks_generate_test.go` does not exist in the package where the interface is located, create it with content (adapt as needed):
45+
46+
```go
47+
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
48+
// See the file LICENSE for licensing terms.
49+
50+
package mypackage
51+
52+
//go:generate go run go.uber.org/mock/mockgen -package=${GOPACKAGE} -destination=mocks_test.go . YourInterface
53+
```
54+
55+
Notes:
56+
1. Ideally generate all mocks to `mocks_test.go` for the package you need to use the mocks for and do not export mocks to other packages. This reduces package dependencies, reduces production code pollution and forces to have locally defined narrow interfaces.
57+
1. Prefer using reflect mode to generate mocks than source mode, unless you need a mock for an unexported interface, which should be rare.
58+
- To **remove** an interface from having a corresponding mock generated:
59+
1. Edit the `mocks_generate_test.go` file in the directory where the interface is defined
60+
1. If the `//go:generate` mockgen command line:
61+
- generates a mock file for multiple interfaces, remove your interface from the line
62+
- generates a mock file only for the interface, remove the entire line. If the file is empty, remove `mocks_generate_test.go` as well.
63+
3064
## Documentation guidelines
3165

3266
- Code should be well commented, so it is easier to read and maintain.
@@ -35,7 +69,7 @@ guidelines:
3569
[commentary](https://go.dev/doc/effective_go#commentary) guidelines.
3670
- Changes with user facing impact (e.g., addition or modification of flags and
3771
options) should be accompanied by a link to a pull request to the [avalanche-docs](https://github.com/ava-labs/avalanche-docs)
38-
repository. [example](https://github.com/ava-labs/avalanche-docs/pull/1119/files).
72+
repository. [example](https://github.com/ava-labs/avalanche-docs/pull/1119/files).
3973
- Changes that modify a package significantly or add new features should
4074
either update the existing or include a new `README.md` file in that package.
4175

.github/workflows/check-clean-branch.sh

-9
This file was deleted.

.github/workflows/tests.yml

+7-12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ jobs:
5454
run: echo "TIMEOUT=1200s" >> "$GITHUB_ENV"
5555
- run: go mod download
5656
shell: bash
57+
- name: Mocks are up to date
58+
shell: bash
59+
run: |
60+
grep -lr -E '^// Code generated by MockGen\. DO NOT EDIT\.$' . | xargs -r rm
61+
go generate -run "go.uber.org/mock/mockgen" ./...
62+
git add --intent-to-add --all
63+
git diff --exit-code
5764
- run: ./scripts/build.sh
5865
shell: bash
5966
- run: ./scripts/build_test.sh
@@ -179,18 +186,6 @@ jobs:
179186
if: always()
180187
with:
181188
name: load-tmpnet-data
182-
mock_gen:
183-
name: MockGen Check
184-
runs-on: ubuntu-latest
185-
steps:
186-
- uses: actions/checkout@v4
187-
- uses: actions/setup-go@v4
188-
with:
189-
go-version: ${{ env.min_go_version }}
190-
- shell: bash
191-
run: scripts/mock.gen.sh
192-
- shell: bash
193-
run: .github/workflows/check-clean-branch.sh
194189
test_build_image:
195190
name: Image build
196191
runs-on: ubuntu-latest

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ require (
4444
go.uber.org/zap v1.26.0
4545
golang.org/x/crypto v0.31.0
4646
golang.org/x/exp v0.0.0-20231127185646-65229373498e
47+
golang.org/x/mod v0.17.0
4748
golang.org/x/sync v0.10.0
4849
golang.org/x/sys v0.28.0
4950
golang.org/x/text v0.21.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,8 @@ golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
719719
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
720720
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
721721
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
722+
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
723+
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
722724
golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
723725
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
724726
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

plugin/evm/validators/state/interfaces/mock_listener.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package interfaces
2+
3+
//go:generate go run go.uber.org/mock/mockgen -package=$GOPACKAGE -destination=mock_listener.go . StateCallbackListener

precompile/contract/mocks.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package contract
2+
3+
//go:generate go run go.uber.org/mock/mockgen -package=$GOPACKAGE -destination=mocks.go . BlockContext,AccessibleState,StateDB

precompile/precompileconfig/mocks.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package precompileconfig
2+
3+
//go:generate go run go.uber.org/mock/mockgen -package=$GOPACKAGE -destination=mocks.go . Predicater,Config,ChainConfig,Accepter

scripts/mock.gen.sh

-41
This file was deleted.

scripts/mocks.mockgen.txt

-3
This file was deleted.

tools.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// (c) 2025, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package subnetevm
5+
6+
import (
7+
_ "golang.org/x/mod/modfile" // golang.org/x/mod to satisfy requirement for go.uber.org/mock/[email protected]
8+
)

warp/aggregator/mock_signature_getter.go

+10-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package aggregator
2+
3+
//go:generate go run go.uber.org/mock/mockgen -package=$GOPACKAGE -source=signature_getter.go -destination=mock_signature_getter.go -exclude_interfaces=NetworkClient

0 commit comments

Comments
 (0)