Skip to content

chore(all): mocks generation improved #3628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ jobs:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup-go-for-project
- shell: bash
run: scripts/mock.gen.sh
- shell: bash
run: .github/workflows/check-clean-branch.sh
run: |
grep -lr -E '^// Code generated by MockGen\. DO NOT EDIT\.$' . | xargs -r rm
go generate -run "go.uber.org/mock/mockgen" ./...
.github/workflows/check-clean-branch.sh
go_mod_tidy:
name: Up-to-date go.mod and go.sum
runs-on: ubuntu-latest
Expand Down
38 changes: 34 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,41 @@ To start developing on AvalancheGo, you'll need a few things installed.
./scripts/protobuf_codegen.sh
```

- 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.
#### Autogenerated mocks

```sh
./scripts/mock.gen.sh
```
💁 The general direction is to **reduce** usage of mocks, so use the following with moderation.

Mocks are auto-generated using [mockgen](https://pkg.go.dev/go.uber.org/mock/mockgen) and `//go:generate` commands in the code.

- To **re-generate all mocks**, use the command below from the root of the project:

```sh
go generate -run "go.uber.org/mock/mockgen" ./...
```

- To **add** an interface that needs a corresponding mock generated:
- if the file `mocks_generate_test.go` exists in the package where the interface is located, either:
- modify its `//go:generate go run go.uber.org/mock/mockgen` to generate a mock for your interface (preferred); or
- add another `//go:generate go run go.uber.org/mock/mockgen` to generate a mock for your interface according to specific mock generation settings
- 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):

```go
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package mypackage

//go:generate go run go.uber.org/mock/mockgen -package=${GOPACKAGE} -destination=mocks_test.go . YourInterface
```

Notes:
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.
1. Prefer using reflect mode to generate mocks than source mode, unless you need a mock for an unexported interface, which should be rare.
- To **remove** an interface from having a corresponding mock generated:
1. Edit the `mocks_generate_test.go` file in the directory where the interface is defined
1. If the `//go:generate` mockgen command line:
- generates a mock file for multiple interfaces, remove your interface from the line
- generates a mock file only for the interface, remove the entire line. If the file is empty, remove `mocks_generate_test.go` as well.

### Testing

Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@ For more information, refer to the [GRPC Golang Quick Start Guide](https://grpc.

### Running mock codegen

To regenerate the [gomock](https://github.com/uber-go/mock) code, run `scripts/mock.gen.sh` from the root of the repo.

This should only be necessary when modifying exported interfaces or after modifying `scripts/mock.mockgen.txt`.
See [the Contributing document autogenerated mocks section](CONTRIBUTING.md#Autogenerated-mocks).

## Versioning

Expand Down
148 changes: 0 additions & 148 deletions api/server/servermock/server.go

This file was deleted.

29 changes: 15 additions & 14 deletions chains/atomic/atomicmock/shared_memory.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions chains/atomic/mocks_generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package atomic

//go:generate go run go.uber.org/mock/mockgen -package=${GOPACKAGE}mock -destination=${GOPACKAGE}mock/shared_memory.go -mock_names=SharedMemory=SharedMemory . SharedMemory
Loading
Loading