Skip to content

Commit 6a0445c

Browse files
authored
feat(bazel): mock_names flag support in archive mode (#262)
Hi! In #259 was introduced archive mode for bazel rule, but no support for mock_names. This PR added `mock_names` attr to `_gomock_archive` rule.
1 parent aa11bfc commit 6a0445c

File tree

7 files changed

+47
-8
lines changed

7 files changed

+47
-8
lines changed

bazel/rules/gomock.bzl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,15 @@ _gomock_source = rule(
177177
def gomock(name, out, library = None, source_importpath = "", source = None, interfaces = [], package = "", self_package = "", aux_files = {}, mockgen_tool = _MOCKGEN_TOOL, mockgen_args = [], imports = {}, copyright_file = None, mock_names = {}, **kwargs):
178178
"""Calls [mockgen](https://github.com/golang/mock) to generates a Go file containing mocks from the given library.
179179
180-
If `source` is given, the mocks are generated in source mode; otherwise in reflective mode.
180+
If `source` is given, the mocks are generated in source mode; otherwise in archive mode.
181181
182182
Args:
183183
name: the target name.
184184
out: the output Go file name.
185-
library: the Go library to look into for the interfaces (reflective mode) or source (source mode). If running in source mode, you can specify source_importpath instead of this parameter.
185+
library: the Go library to look into for the interfaces (archive mode) or source (source mode). If running in source mode, you can specify source_importpath instead of this parameter.
186186
source_importpath: the importpath for the source file. Alternative to passing library, which can lead to circular dependencies between mock and library targets. Only valid for source mode.
187187
source: a Go file in the given `library`. If this is given, `gomock` will call mockgen in source mode to mock all interfaces in the file.
188-
interfaces: a list of interfaces in the given `library` to be mocked in reflective mode.
188+
interfaces: a list of interfaces in the given `library` to be mocked in archive mode.
189189
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.
190190
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.
191191
aux_files: a map from source files to their package path. This only needed when `source` is provided. See [mockgen's -aux_files](https://github.com/golang/mock#flags) for more information.
@@ -223,11 +223,17 @@ def gomock(name, out, library = None, source_importpath = "", source = None, int
223223
self_package = self_package,
224224
mockgen_tool = mockgen_tool,
225225
copyright_file = copyright_file,
226+
mock_names = mock_names,
226227
**kwargs
227228
)
228229

229230
def _gomock_archive_impl(ctx):
230231
args = ctx.actions.args()
232+
233+
if len(ctx.attr.mock_names.items()):
234+
mock_names = ",".join(["{0}={1}".format(name, pkg) for name, pkg in ctx.attr.mock_names.items()])
235+
args.add("-mock_names", mock_names)
236+
231237
args.add("-package", ctx.attr.library[GoInfo].importpath)
232238
args.add("-archive", ctx.attr.library[GoArchive].data.export_file.path)
233239
args.add("-destination", ctx.outputs.out)
@@ -282,6 +288,10 @@ _gomock_archive = rule(
282288
"self_package": attr.string(
283289
doc = "The full package import path for the generated code.",
284290
),
291+
"mock_names": attr.string_dict(
292+
doc = "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.",
293+
default = {},
294+
),
285295
"mockgen_tool": attr.label(
286296
default = Label("@com_github_golang_mock//mockgen"),
287297
executable = True,

bazel/tests/README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ gomock
33

44
Tests that ensure the gomock rules can be called correctly under different input permutations.
55

6-
reflective
6+
archive
77
------------------------
8-
Checks that gomock can be run in "reflective" mode when passed a `GoLibrary` and `interfaces`.
8+
Checks that gomock can be run in "archive" mode when passed a `GoLibrary` and `interfaces`.
99

1010
source
1111
------------------------

bazel/tests/reflective/BUILD.bazel renamed to bazel/tests/archive/BUILD.bazel

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
22
load("//rules:gomock.bzl", "gomock")
33

4-
54
go_library(
65
name = "client",
76
srcs = [
@@ -11,7 +10,7 @@ go_library(
1110
visibility = ["//visibility:public"],
1211
)
1312

14-
# Build the mocks using reflective mode (i.e. without passing source)
13+
# Build the mocks using archive mode (i.e. without passing source)
1514
gomock(
1615
name = "mocks",
1716
out = "client_mock.go",
@@ -21,10 +20,24 @@ gomock(
2120
visibility = ["//visibility:public"],
2221
)
2322

23+
# Build the mocks using mock_names renaming.
24+
gomock(
25+
name = "renamed_mocks",
26+
out = "client_mock_renamed.go",
27+
interfaces = ["Client"],
28+
library = ":client",
29+
mock_names = {
30+
"Client": "MockRenamedClient",
31+
},
32+
package = "client",
33+
visibility = ["//visibility:public"],
34+
)
35+
2436
go_test(
2537
name = "client_test",
2638
srcs = [
2739
"client_mock.go",
40+
"client_mock_renamed.go",
2841
"client_test.go",
2942
],
3043
embed = [":client"],
File renamed without changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
package client
22

33
var _ Client = (*MockClient)(nil)
4+
5+
var _ Client = (*MockRenamedClient)(nil)

bazel/tests/source/BUILD.bazel

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
22
load("//rules:gomock.bzl", "gomock")
33

4-
54
go_library(
65
name = "client",
76
srcs = [
@@ -21,6 +20,18 @@ gomock(
2120
visibility = ["//visibility:public"],
2221
)
2322

23+
gomock(
24+
name = "renamed_mocks",
25+
out = "renamed_client_mock.go",
26+
library = ":client",
27+
mock_names = {
28+
"Client": "MockRenamedClient",
29+
},
30+
package = "client",
31+
source = "client.go",
32+
visibility = ["//visibility:public"],
33+
)
34+
2435
gomock(
2536
name = "wrapper_mocks",
2637
out = "wrapper_mock.go",
@@ -39,6 +50,7 @@ go_test(
3950
srcs = [
4051
"client_mock.go",
4152
"client_test.go",
53+
"renamed_client_mock.go",
4254
"wrapper_mock.go",
4355
],
4456
embed = [":client"],

bazel/tests/source/client_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
package client
22

33
var _ Client = (*MockClient)(nil)
4+
5+
var _ Client = (*MockRenamedClient)(nil)

0 commit comments

Comments
 (0)