Bazel rules for using mockery
MIT License
This is a relatively quick-and-dirty approach to try and bazelify Golang mocks generation using the third-party Mockery tool.
Contributions welcome! Feel free to open up PRs or raise issues.
The gomockery.bzl
file provides two rules that allow for Bazel-generated mock
files for your Golang interfaces. In order for these rules to work correctly you will need to add
the following dependencies to your own WORKSPACE
file if they are not already present.
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "io_bazel_rules_go",
urls = ["https://github.com/bazelbuild/rules_go/releases/download/0.16.5/rules_go-0.16.5.tar.gz"],
sha256 = "7be7dc01f1e0afdba6c8eb2b43d2fa01c743be1b9273ab1eaf6c233df078d705",
)
load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains")
go_rules_dependencies()
go_register_toolchains()
http_archive(
name = "bazel_gazelle",
urls = ["https://github.com/bazelbuild/bazel-gazelle/releases/download/0.16.0/bazel-gazelle-0.16.0.tar.gz"],
sha256 = "7949fc6cc17b5b191103e97481cf8889217263acf52e00b560683413af204fcb",
)
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
gazelle_dependencies()
go_repository(
name = "com_github_vektra_mockery",
importpath = "github.com/vektra/mockery",
tag = "e78b021dcbb558a8e7ac1fc5bc757ad7c277bb81",
)
go_repository(
name = "com_github_stretchr_testify",
importpath = "github.com/stretchr/testify",
tag = "363ebb24d041ccea8068222281c2e963e997b9dc",
)
The rules that gomockery.bzl
provides are:
go_mockery
- Which generates the mocks for the interfaces exposed by the specified package. Thename
(optional) Name of the rule on which other go_*
rules should depend if they wantgo_default_mocks
.src
(required) Label of the go_library
in the sources of which should be looked for theinterfaces
(required) Explicit list of the names of interfaces for which mocks should becase
(optional) Casing of the file names that will be generated and contain the mocksunderscore
, camel
, snake
). Default is underscore
.outpkg
(optional) Name of the package that will contain the mocks. Default is mocks
.mockery_tool
(optional) Alternative label that builds the mockery
binary that should be@com_github_vektra_mockery//cmd/mockery:mockery
.go_mockery_with_library
- Which generates the mocks for the interfaces exposed by the specifiedgo_library
as well that can be depended on by other go_*
go_mockery
tool this rule also takes:
name
(optional) Name of the rule that will correspond to the generated go_library
.go_default_library
.mocks_name
(optional) Name of the rule on which other go_*
rules should depend if theygo_default_mocks
.importpath
(required) The Go package path with which the generated mocks package can bego_library
testify_mock_lib
(optional) Alternative label that builds thegithub.com/stretchr/testify/mocks
package on which the Go mocks library will need to@com_github_stretchr_testify//mocks:go_default_library
.