Skip to content

Commit 8fd2d7b

Browse files
authored
chore(all): mocks generation improved (#3628)
1 parent 7efd463 commit 8fd2d7b

File tree

104 files changed

+1057
-1198
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+1057
-1198
lines changed

.github/workflows/ci.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,10 @@ jobs:
155155
- uses: actions/checkout@v4
156156
- uses: ./.github/actions/setup-go-for-project
157157
- shell: bash
158-
run: scripts/mock.gen.sh
159-
- shell: bash
160-
run: .github/workflows/check-clean-branch.sh
158+
run: |
159+
grep -lr -E '^// Code generated by MockGen\. DO NOT EDIT\.$' . | xargs -r rm
160+
go generate -run "go.uber.org/mock/mockgen" ./...
161+
.github/workflows/check-clean-branch.sh
161162
go_mod_tidy:
162163
name: Up-to-date go.mod and go.sum
163164
runs-on: ubuntu-latest

CONTRIBUTING.md

+34-4
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,41 @@ To start developing on AvalancheGo, you'll need a few things installed.
4545
./scripts/protobuf_codegen.sh
4646
```
4747

48-
- To add or remove an interface that needs a corresponding mock generated, add it to the mock file [here](./scripts/mocks.mockgen.txt). You can regenerate the mocks by running the following script.
48+
#### Autogenerated mocks
4949

50-
```sh
51-
./scripts/mock.gen.sh
52-
```
50+
💁 The general direction is to **reduce** usage of mocks, so use the following with moderation.
51+
52+
Mocks are auto-generated using [mockgen](https://pkg.go.dev/go.uber.org/mock/mockgen) and `//go:generate` commands in the code.
53+
54+
- To **re-generate all mocks**, use the command below from the root of the project:
55+
56+
```sh
57+
go generate -run "go.uber.org/mock/mockgen" ./...
58+
```
59+
60+
- To **add** an interface that needs a corresponding mock generated:
61+
- if the file `mocks_generate_test.go` exists in the package where the interface is located, either:
62+
- modify its `//go:generate go run go.uber.org/mock/mockgen` to generate a mock for your interface (preferred); or
63+
- add another `//go:generate go run go.uber.org/mock/mockgen` to generate a mock for your interface according to specific mock generation settings
64+
- 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):
65+
66+
```go
67+
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
68+
// See the file LICENSE for licensing terms.
69+
70+
package mypackage
71+
72+
//go:generate go run go.uber.org/mock/mockgen -package=${GOPACKAGE} -destination=mocks_test.go . YourInterface
73+
```
74+
75+
Notes:
76+
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.
77+
1. Prefer using reflect mode to generate mocks than source mode, unless you need a mock for an unexported interface, which should be rare.
78+
- To **remove** an interface from having a corresponding mock generated:
79+
1. Edit the `mocks_generate_test.go` file in the directory where the interface is defined
80+
1. If the `//go:generate` mockgen command line:
81+
- generates a mock file for multiple interfaces, remove your interface from the line
82+
- generates a mock file only for the interface, remove the entire line. If the file is empty, remove `mocks_generate_test.go` as well.
5383

5484
### Testing
5585

README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,7 @@ For more information, refer to the [GRPC Golang Quick Start Guide](https://grpc.
187187

188188
### Running mock codegen
189189

190-
To regenerate the [gomock](https://github.com/uber-go/mock) code, run `scripts/mock.gen.sh` from the root of the repo.
191-
192-
This should only be necessary when modifying exported interfaces or after modifying `scripts/mock.mockgen.txt`.
190+
See [the Contributing document autogenerated mocks section](CONTRIBUTING.md#Autogenerated-mocks).
193191

194192
## Versioning
195193

api/server/servermock/server.go

-148
This file was deleted.

chains/atomic/atomicmock/shared_memory.go

+15-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chains/atomic/mocks_generate_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package atomic
5+
6+
//go:generate go run go.uber.org/mock/mockgen -package=${GOPACKAGE}mock -destination=${GOPACKAGE}mock/shared_memory.go -mock_names=SharedMemory=SharedMemory . SharedMemory

0 commit comments

Comments
 (0)