Skip to content

Commit

Permalink
gomock: allow passing extra flags to mockgen_tool (#4066)
Browse files Browse the repository at this point in the history
**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**

#4067 is another way to do it, but I think accepting an argument list is
cleaner
  • Loading branch information
TvdW committed Aug 28, 2024
1 parent e8b55e1 commit f7e35cd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
4 changes: 2 additions & 2 deletions docs/go/extras/extras.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ This rule has moved. See [gazelle rule] in the Gazelle repository.

<pre>
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>,
<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-typed">typed</a>, <a href="#gomock-kwargs">kwargs</a>)
<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>)
</pre>

Calls [mockgen](https://github.com/golang/mock) to generates a Go file containing mocks from the given library.
Expand All @@ -57,10 +57,10 @@ If `source` is given, the mocks are generated in source mode; otherwise in refle
| <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>Label("//extras/gomock:mockgen")</code> |
| <a id="gomock-mockgen_args"></a>mockgen_args | additional arguments to pass to the mockgen tool. | <code>[]</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> |
| <a id="gomock-typed"></a>typed | generate type-safe 'Return', 'Do', 'DoAndReturn' functions. See [mockgen's -typed](https://github.com/uber-go/mock#flags) for more information. | <code>False</code> |
| <a id="gomock-kwargs"></a>kwargs | <p align="center"> - </p> | none |


31 changes: 16 additions & 15 deletions extras/gomock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,6 @@ _gomock_source = rule(
allow_single_file = True,
mandatory = False,
),
"typed": attr.bool(
doc = "Generate type-safe 'Return', 'Do', 'DoAndReturn' functions.",
mandatory = False,
),
"mockgen_tool": attr.label(
doc = "The mockgen tool to run",
default = _MOCKGEN_TOOL,
Expand All @@ -164,14 +160,18 @@ _gomock_source = rule(
cfg = "exec",
mandatory = False,
),
"mockgen_args": attr.string_list(
doc = "Additional arguments to pass to the mockgen tool",
mandatory = False,
),
"_go_context_data": attr.label(
default = "//:go_context_data",
),
},
toolchains = [GO_TOOLCHAIN],
)

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 = {}, typed = False, **kwargs):
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):
"""Calls [mockgen](https://github.com/golang/mock) to generates a Go file containing mocks from the given library.
If `source` is given, the mocks are generated in source mode; otherwise in reflective mode.
Expand All @@ -187,11 +187,11 @@ def gomock(name, out, library = None, source_importpath = "", source = None, int
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.
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.
mockgen_tool: the mockgen tool to run.
mockgen_args: additional arguments to pass to the mockgen tool.
imports: dictionary of name-path pairs of explicit imports to use. See [mockgen's -imports](https://github.com/golang/mock#flags) for more information.
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.
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.
typed: generate type-safe 'Return', 'Do', 'DoAndReturn' functions. See [mockgen's -typed](https://github.com/uber-go/mock#flags) for more information.
kwargs: [common attributes](https://bazel.build/reference/be/common-definitions#common-attributes) to all Bazel rules.
kwargs: common attributes](https://bazel.build/reference/be/common-definitions#common-attributes) to all Bazel rules.
"""
if source:
_gomock_source(
Expand All @@ -204,10 +204,10 @@ def gomock(name, out, library = None, source_importpath = "", source = None, int
self_package = self_package,
aux_files = aux_files,
mockgen_tool = mockgen_tool,
mockgen_args = mockgen_args,
imports = imports,
copyright_file = copyright_file,
mock_names = mock_names,
typed = typed,
**kwargs
)
else:
Expand All @@ -219,10 +219,10 @@ def gomock(name, out, library = None, source_importpath = "", source = None, int
package = package,
self_package = self_package,
mockgen_tool = mockgen_tool,
mockgen_args = mockgen_args,
imports = imports,
copyright_file = copyright_file,
mock_names = mock_names,
typed = typed,
**kwargs
)

Expand Down Expand Up @@ -367,10 +367,6 @@ _gomock_prog_exec = rule(
allow_single_file = True,
mandatory = False,
),
"typed": attr.bool(
doc = "Generate type-safe 'Return', 'Do', 'DoAndReturn' functions.",
mandatory = False,
),
"prog_bin": attr.label(
doc = "The program binary generated by mockgen's -prog_only and compiled by bazel.",
allow_single_file = True,
Expand All @@ -386,6 +382,11 @@ _gomock_prog_exec = rule(
cfg = "exec",
mandatory = False,
),
"mockgen_args": attr.string_list(
doc = "Additional arguments to pass to the mockgen tool",
mandatory = False,
default = [],
),
"_go_context_data": attr.label(
default = "//:go_context_data",
),
Expand All @@ -409,7 +410,7 @@ def _handle_shared_args(ctx, args):
if len(ctx.attr.mock_names) > 0:
mock_names = ",".join(["{0}={1}".format(name, pkg) for name, pkg in ctx.attr.mock_names.items()])
args += ["-mock_names", mock_names]
if ctx.attr.typed:
args += ["-typed"]
if ctx.attr.mockgen_args:
args += ctx.attr.mockgen_args

return args, needed_files

0 comments on commit f7e35cd

Please sign in to comment.