Skip to content

Golang: Do you commit your generated mocks to repo?

| golang |

I’ve discovered that there is no consensus about “Committing generate mocks to the repo”. That’s why I’ve looked around and found some discussions, pros&cons (see References).

Also there is interesting statement in the official Golang article About the go command:

As mentioned above, the go command is not a general-purpose build tool. In particular, it does not have any facility for generating Go source files during a build, although it does provide go generate, which can automate the creation of Go files before the build. For more advanced build setups, you may need to write a makefile (or a configuration file for the build tool of your choice) to run whatever tool creates the Go files and then check those generated source files into your repository. This is more work for you, the package author, but it is significantly less work for your users, who can use “go get” without needing to obtain and build any additional tools.

So actually they are suggesting always committing generated code.

I made a conclusion for myself: you should commit generated mocks to the repo!

References

The popularity of Golang mock libraries:

I have decided to check some public repos and whether they are committing generated mocks.

Based on libraries popularity I will concentrate on golang/mock and vektra/mockery only (see report below).

Hashicorp

Repo Comments or Samples
waypoint ⭐ 4.7k vektra/mockery is_auth_method__method.go
terraform ⭐ 36.2k golang/mock mock_proto/mock.go
consul-terraform-sync ⭐ 110 vektra/mockery terraformExec.go
consul ⭐ 26.1k manually implemented mock_api_test.go
vektra/mockery mock_Login.go

Uber

Repo Comments or Samples
cadence ⭐ 6.7k golang/mock authority_mock.go, cadence/search?q=MockGen
aresdb ⭐ 2.9k vektra/mockery PeerDataNode_BenchmarkFileTransferClient.go

Dropbox

Repo Comments or Samples
kglb ⭐ 131 manually implemented mock_modules.go
dropbox/godropbox ⭐ 4.1k manually implemented mock_client_test.go

Gitlab

Repo Comments or Samples
gitlab-runner ⭐ 2.1k vektra/mockery mock_requester.go
fargate ⭐ 40 vektra/mockery mock_ec2Client.go

Other

Repo Comments or Samples
jaegertracing/jaeger ⭐ 17.1k vektra/mockery DependenciesReaderPluginServer.go
ignite/cli ⭐ 1k vektra/mockery gasometer.go
projectcontour/contour ⭐ 3.3k vektra/mockery manager.go
tendermint/tendermint ⭐ 5.4k vektra/mockery block_store.go
weaveworks/eksctl ⭐ 4.3k vektra/mockery ConfigProvider.go
google/skia-buildbot ⭐ 119 vektra/mockery DataFrameBuilder.go