Skip to content
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

bzlmod: Add support for gomock #3232

Merged
merged 1 commit into from
Jul 13, 2022
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
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. The patch may be slightly out of date, so we ignore the exit status.
- "curl https://raw.githubusercontent.com/bazelbuild/bazel-central-registry/main/modules/rules_go/0.33.0/patches/bcr.patch | patch -r -u -p1 -d ../.. || exit 0"
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> |
fmeum marked this conversation as resolved.
Show resolved Hide resolved
| <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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of introducing these aliases?

Copy link
Member Author

@fmeum fmeum Jul 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With bzlmod, modules can only see repositories they explicitly depend on in their MODULE.bazel. Without these aliases, rules_go users would need to depend on Gazelle themselves and import the @com_github_golang_gomock repo via a use_repo directive just to use the rules_go provided gomock macro.

That seemed very tedious, so I introduced these aliases instead.

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)