Skip to content

Commit

Permalink
bzlmod: Add support for gomock
Browse files Browse the repository at this point in the history
This introduces a dependency on the new gazelle module, which requires
using Bazel at HEAD for c068b0d2347905c5e9be793b741f693c7edb6e53.

Aliases are introduced for the required gomock targets so that users
don't have to declare their own dependency on gomock with bzlmod.
  • Loading branch information
fmeum committed Jul 11, 2022
1 parent df02d01 commit edfc8fd
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 8 deletions.
5 changes: 4 additions & 1 deletion .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ tasks:
ubuntu2004_bcr_tests:
name: BCR test module
platform: ubuntu2004
bazel: 6.0.0-pre.20220608.2
bazel: last_green
shell_commands:
# Patch io_bazel_rules_go to rules_go.
- curl https://raw.githubusercontent.com/bazelbuild/bazel-central-registry/main/modules/rules_go/0.33.0/patches/bcr.patch | patch -r -u -p1 -d ../..
working_directory: tests/bcr
build_flags:
- "--experimental_enable_bzlmod"
Expand Down
5 changes: 5 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ use_repo(
go_sdk = use_extension("//go:extensions.bzl", "go_sdk")
go_sdk.download(name = "go_default_sdk", version = "1.18.3")
use_repo(go_sdk, "go_default_sdk")

bazel_dep(name = "gazelle", version = "0.26.0")
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.module(importpath = "github.com/golang/mock", version = "v1.6.0", sum = "h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=")
use_repo(go_deps, "com_github_golang_mock")
2 changes: 1 addition & 1 deletion docs/go/extras/extras.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ If `source` is given, the mocks are generated in source mode; otherwise in refle
| <a id="gomock-package"></a>package | the name of the package the generated mocks should be in. If not specified, uses mockgen's default. See [mockgen's -package](https://github.com/golang/mock#flags) for more information. | <code>""</code> |
| <a id="gomock-self_package"></a>self_package | the full package import path for the generated code. The purpose of this flag is to prevent import cycles in the generated code by trying to include its own package. See [mockgen's -self_package](https://github.com/golang/mock#flags) for more information. | <code>""</code> |
| <a id="gomock-aux_files"></a>aux_files | a map from source files to their package path. This only needed when <code>source</code> is provided. See [mockgen's -aux_files](https://github.com/golang/mock#flags) for more information. | <code>{}</code> |
| <a id="gomock-mockgen_tool"></a>mockgen_tool | the mockgen tool to run. | <code>"@com_github_golang_mock//mockgen"</code> |
| <a id="gomock-mockgen_tool"></a>mockgen_tool | the mockgen tool to run. | <code>Label("//extras/gomock:mockgen")</code> |
| <a id="gomock-imports"></a>imports | dictionary of name-path pairs of explicit imports to use. See [mockgen's -imports](https://github.com/golang/mock#flags) for more information. | <code>{}</code> |
| <a id="gomock-copyright_file"></a>copyright_file | optional file containing copyright to prepend to the generated contents. See [mockgen's -copyright_file](https://github.com/golang/mock#flags) for more information. | <code>None</code> |
| <a id="gomock-mock_names"></a>mock_names | dictionary of interface name to mock name pairs to change the output names of the mock objects. Mock names default to 'Mock' prepended to the name of the interface. See [mockgen's -mock_names](https://github.com/golang/mock#flags) for more information. | <code>{}</code> |
Expand Down
10 changes: 5 additions & 5 deletions extras/gomock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ load("//go/private/rules:wrappers.bzl", go_binary = "go_binary_macro")
load("//go/private:providers.bzl", "GoLibrary")
load("@bazel_skylib//lib:paths.bzl", "paths")

_MOCKGEN_TOOL = "@com_github_golang_mock//mockgen"
_MOCKGEN_MODEL_LIB = "@com_github_golang_mock//mockgen/model"
_MOCKGEN_TOOL = Label("//extras/gomock:mockgen")
_MOCKGEN_MODEL_LIB = Label("//extras/gomock:mockgen_model")

def _gomock_source_impl(ctx):
go_ctx = go_context(ctx)
Expand Down Expand Up @@ -137,7 +137,7 @@ _gomock_source = rule(
),
"mockgen_tool": attr.label(
doc = "The mockgen tool to run",
default = Label(_MOCKGEN_TOOL),
default = _MOCKGEN_TOOL,
allow_single_file = True,
executable = True,
cfg = "exec",
Expand Down Expand Up @@ -271,7 +271,7 @@ _gomock_prog_gen = rule(
),
"mockgen_tool": attr.label(
doc = "The mockgen tool to run",
default = Label(_MOCKGEN_TOOL),
default = _MOCKGEN_TOOL,
allow_single_file = True,
executable = True,
cfg = "exec",
Expand Down Expand Up @@ -353,7 +353,7 @@ _gomock_prog_exec = rule(
),
"mockgen_tool": attr.label(
doc = "The mockgen tool to run",
default = Label(_MOCKGEN_TOOL),
default = _MOCKGEN_TOOL,
allow_single_file = True,
executable = True,
cfg = "exec",
Expand Down
17 changes: 17 additions & 0 deletions extras/gomock/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
alias(
name = "gomock",
actual = "@com_github_golang_mock//gomock",
visibility = ["//visibility:public"],
)

alias(
name = "mockgen",
actual = "@com_github_golang_mock//mockgen",
visibility = ["//visibility:public"],
)

alias(
name = "mockgen_model",
actual = "@com_github_golang_mock//mockgen/model",
visibility = ["//visibility:public"],
)
2 changes: 1 addition & 1 deletion tests/bcr/.bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.0.0-pre.20220526.1
last_green
28 changes: 28 additions & 0 deletions tests/bcr/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@my_rules_go//extras:gomock.bzl", "gomock")
load("@my_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")

go_library(
Expand All @@ -17,3 +18,30 @@ go_test(
srcs = ["test.go"],
embed = [":lib"],
)

go_library(
name = "mockable",
srcs = [
"mockable.go",
],
importpath = "example.com/mockable",
)

gomock(
name = "mocks",
out = "mockable_mock.go",
library = ":mockable",
package = "mockable",
source = "mockable.go",
visibility = ["//visibility:public"],
)

go_test(
name = "mockable_test",
srcs = [
"mockable_mock.go",
"mockable_test.go",
],
embed = [":mockable"],
deps = ["@my_rules_go//extras/gomock"],
)
7 changes: 7 additions & 0 deletions tests/bcr/mockable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package mockable

import "net/url"

type Client interface {
Connect(addr string) url.URL
}
3 changes: 3 additions & 0 deletions tests/bcr/mockable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package mockable

var _ Client = (*MockClient)(nil)

0 comments on commit edfc8fd

Please sign in to comment.