Skip to content

Commit 7666720

Browse files
TvdWtyler-french
authored andcommitted
gomock: allow passing extra flags to mockgen_tool (#4066)
**What type of PR is this?** > Feature **What does this PR do? Why is it needed?** Adds the ability to pass extra arguments to gomock **Which issues(s) does this PR fix?** Fixes #4065 Closes #4067 **Other notes for review** cleaner
1 parent c8fca5c commit 7666720

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

docs/go/extras/extras.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ This rule has moved. See [gazelle rule] in the Gazelle repository.
3434

3535
<pre>
3636
gomock(<a href="#gomock-name">name</a>, <a href="#gomock-out">out</a>, <a href="#gomock-library">library</a>, <a href="#gomock-source_importpath">source_importpath</a>, <a href="#gomock-source">source</a>, <a href="#gomock-interfaces">interfaces</a>, <a href="#gomock-package">package</a>, <a href="#gomock-self_package">self_package</a>, <a href="#gomock-aux_files">aux_files</a>,
37-
<a href="#gomock-mockgen_tool">mockgen_tool</a>, <a href="#gomock-imports">imports</a>, <a href="#gomock-copyright_file">copyright_file</a>, <a href="#gomock-mock_names">mock_names</a>, <a href="#gomock-kwargs">kwargs</a>)
37+
<a href="#gomock-mockgen_tool">mockgen_tool</a>, <a href="#gomock-mockgen_args">mockgen_args</a>, <a href="#gomock-imports">imports</a>, <a href="#gomock-copyright_file">copyright_file</a>, <a href="#gomock-mock_names">mock_names</a>, <a href="#gomock-kwargs">kwargs</a>)
3838
</pre>
3939

4040
Calls [mockgen](https://github.com/golang/mock) to generates a Go file containing mocks from the given library.
@@ -57,6 +57,7 @@ If `source` is given, the mocks are generated in source mode; otherwise in refle
5757
| <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> |
5858
| <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> |
5959
| <a id="gomock-mockgen_tool"></a>mockgen_tool | the mockgen tool to run. | <code>Label("//extras/gomock:mockgen")</code> |
60+
| <a id="gomock-mockgen_args"></a>mockgen_args | additional arguments to pass to the mockgen tool. | <code>[]</code> |
6061
| <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> |
6162
| <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> |
6263
| <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> |

extras/gomock.bzl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,18 @@ _gomock_source = rule(
160160
cfg = "exec",
161161
mandatory = False,
162162
),
163+
"mockgen_args": attr.string_list(
164+
doc = "Additional arguments to pass to the mockgen tool",
165+
mandatory = False,
166+
),
163167
"_go_context_data": attr.label(
164168
default = "//:go_context_data",
165169
),
166170
},
167171
toolchains = [GO_TOOLCHAIN],
168172
)
169173

170-
def gomock(name, out, library = None, source_importpath = "", source = None, interfaces = [], package = "", self_package = "", aux_files = {}, mockgen_tool = _MOCKGEN_TOOL, imports = {}, copyright_file = None, mock_names = {}, **kwargs):
174+
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):
171175
"""Calls [mockgen](https://github.com/golang/mock) to generates a Go file containing mocks from the given library.
172176
173177
If `source` is given, the mocks are generated in source mode; otherwise in reflective mode.
@@ -183,6 +187,7 @@ def gomock(name, out, library = None, source_importpath = "", source = None, int
183187
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.
184188
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.
185189
mockgen_tool: the mockgen tool to run.
190+
mockgen_args: additional arguments to pass to the mockgen tool.
186191
imports: dictionary of name-path pairs of explicit imports to use. See [mockgen's -imports](https://github.com/golang/mock#flags) for more information.
187192
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.
188193
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.
@@ -199,6 +204,7 @@ def gomock(name, out, library = None, source_importpath = "", source = None, int
199204
self_package = self_package,
200205
aux_files = aux_files,
201206
mockgen_tool = mockgen_tool,
207+
mockgen_args = mockgen_args,
202208
imports = imports,
203209
copyright_file = copyright_file,
204210
mock_names = mock_names,
@@ -213,6 +219,7 @@ def gomock(name, out, library = None, source_importpath = "", source = None, int
213219
package = package,
214220
self_package = self_package,
215221
mockgen_tool = mockgen_tool,
222+
mockgen_args = mockgen_args,
216223
imports = imports,
217224
copyright_file = copyright_file,
218225
mock_names = mock_names,
@@ -375,6 +382,11 @@ _gomock_prog_exec = rule(
375382
cfg = "exec",
376383
mandatory = False,
377384
),
385+
"mockgen_args": attr.string_list(
386+
doc = "Additional arguments to pass to the mockgen tool",
387+
mandatory = False,
388+
default = [],
389+
),
378390
"_go_context_data": attr.label(
379391
default = "//:go_context_data",
380392
),
@@ -398,5 +410,7 @@ def _handle_shared_args(ctx, args):
398410
if len(ctx.attr.mock_names) > 0:
399411
mock_names = ",".join(["{0}={1}".format(name, pkg) for name, pkg in ctx.attr.mock_names.items()])
400412
args += ["-mock_names", mock_names]
413+
if ctx.attr.mockgen_args:
414+
args += ctx.attr.mockgen_args
401415

402416
return args, needed_files

0 commit comments

Comments
 (0)