Skip to content
Open
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
7 changes: 7 additions & 0 deletions bazel/rules/gomock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = ",")

Expand Down Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions bazel/tests/archive/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down
2 changes: 2 additions & 0 deletions bazel/tests/archive/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ package client
var _ Client = (*MockClient)(nil)

var _ Client = (*MockRenamedClient)(nil)

var _ Client = (*MockClientNoCmdComment)(nil)
23 changes: 23 additions & 0 deletions bazel/tests/archive/mockgen_args_content_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}