diff --git a/bazel/rules/gomock.bzl b/bazel/rules/gomock.bzl index d3abb6a..ee245e2 100644 --- a/bazel/rules/gomock.bzl +++ b/bazel/rules/gomock.bzl @@ -221,6 +221,7 @@ 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, copyright_file = copyright_file, mock_names = mock_names, **kwargs @@ -238,6 +239,8 @@ def _gomock_archive_impl(ctx): args.add("-destination", ctx.outputs.out) args.add("-package", ctx.attr.package) args.add("-self_package", ctx.attr.self_package) + if ctx.attr.mockgen_args: + args.add_all(ctx.attr.mockgen_args) args.add(ctx.attr.library[GoInfo].importpath) args.add_joined(ctx.attr.interfaces, join_with = ",") @@ -296,6 +299,10 @@ _gomock_archive = rule( executable = True, cfg = "exec", ), + "mockgen_args": attr.string_list( + doc = "Additional arguments to pass to the mockgen tool.", + mandatory = False, + ), "use_underlying_names": attr.bool( doc = "Use alias underlying type names in generated mocks instead of the alias names directly", default = False, diff --git a/bazel/tests/archive/BUILD.bazel b/bazel/tests/archive/BUILD.bazel index db70a0c..894411f 100644 --- a/bazel/tests/archive/BUILD.bazel +++ b/bazel/tests/archive/BUILD.bazel @@ -33,10 +33,31 @@ gomock( visibility = ["//visibility:public"], ) +# Build the mocks using mockgen_args to pass additional flags. +gomock( + name = "mocks_no_cmd_comment", + out = "client_mock_no_cmd_comment.go", + interfaces = ["Client"], + library = ":client", + mock_names = { + "Client": "MockClientNoCmdComment", + }, + mockgen_args = ["-write_command_comment=false"], + package = "client", + visibility = ["//visibility:public"], +) + +go_test( + name = "mockgen_args_test", + srcs = ["mockgen_args_content_test.go"], + data = [":mocks_no_cmd_comment"], +) + go_test( name = "client_test", srcs = [ "client_mock.go", + "client_mock_no_cmd_comment.go", "client_mock_renamed.go", "client_test.go", ], diff --git a/bazel/tests/archive/client_test.go b/bazel/tests/archive/client_test.go index 7a23500..c8577ae 100644 --- a/bazel/tests/archive/client_test.go +++ b/bazel/tests/archive/client_test.go @@ -3,3 +3,5 @@ package client var _ Client = (*MockClient)(nil) var _ Client = (*MockRenamedClient)(nil) + +var _ Client = (*MockClientNoCmdComment)(nil) diff --git a/bazel/tests/archive/mockgen_args_content_test.go b/bazel/tests/archive/mockgen_args_content_test.go new file mode 100644 index 0000000..d5d0edb --- /dev/null +++ b/bazel/tests/archive/mockgen_args_content_test.go @@ -0,0 +1,23 @@ +package client_test + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +func TestWriteCommandCommentSuppressed(t *testing.T) { + path := filepath.Join( + os.Getenv("TEST_SRCDIR"), + os.Getenv("TEST_WORKSPACE"), + "tests/archive/client_mock_no_cmd_comment.go", + ) + content, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read generated file: %v", err) + } + if strings.Contains(string(content), "Generated by this command") { + t.Error("-write_command_comment=false had no effect: 'Generated by this command' block still present") + } +}