Skip to content

Commit 994bf1c

Browse files
committed
docs(contributing): add mocks section
1 parent 16a12c6 commit 994bf1c

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

.github/CONTRIBUTING.md

+33
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,36 @@ Please make sure your contributions adhere to our coding guidelines:
3333
Before you submit a feature request, please check and make sure that it isn't
3434
possible through some other means.
3535

36+
## Mocks
37+
38+
Mocks are auto-generated using [mockgen](https://pkg.go.dev/go.uber.org/mock/mockgen) and `//go:generate` commands in the code.
39+
40+
* To **re-generate all mocks**, use the command below from the root of the project:
41+
42+
```sh
43+
go generate -run "go.uber.org/mock/mockgen" ./...
44+
```
45+
46+
* To **add** an interface that needs a corresponding mock generated:
47+
* if the file `mocks_generate_test.go` exists in the package where the interface is located, either:
48+
* modify its `//go:generate go run go.uber.org/mock/[email protected]` to generate a mock for your interface (preferred); or
49+
* add another `//go:generate go run go.uber.org/mock/[email protected]` to generate a mock for your interface according to specific mock generation settings
50+
* 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):
51+
52+
```go
53+
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
54+
// See the file LICENSE for licensing terms.
55+
56+
package mypackage
57+
58+
//go:generate go run go.uber.org/mock/[email protected] -package=${GOPACKAGE} -destination=mocks_test.go . YourInterface
59+
```
60+
61+
Notes:
62+
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.
63+
1. Prefer using reflect mode to generate mocks than source mode, unless you need a mock for an unexported interface, which should be rare.
64+
* To **remove** an interface from having a corresponding mock generated:
65+
1. Edit the `mocks_generate_test.go` file in the directory where the interface is defined
66+
1. If the `//go:generate` mockgen command line:
67+
* generates a mock file for multiple interfaces, remove your interface from the line
68+
* generates a mock file only for the interface, remove the entire line. If the file is empty, remove `mocks_generate_test.go` as well.

0 commit comments

Comments
 (0)