Common Bazel gomock rule

622 Views Asked by At

Is there a way to declare a gomock rule in one library and reference it in another? The instructions for gomock work for interfaces declared in the same package. For example:

backend/service/db/BUILD.bazel:

# Contains StargateQueryExecutor interface
go_library(
    name = "db",
    srcs = ["db.go"],
    importpath = "backend/service/db",
    visibility = ["//visibility:public"],
    deps = [
        "@com_github_huandu_go_sqlbuilder//:go-sqlbuilder",
        "@com_github_stargate_stargate_grpc_go_client//stargate/pkg/proto",
    ],
)

gomock(
    name = "mock_db",
    out = "mock_db_test.go",
    interfaces = ["StargateQueryExecutor"],
    library = ":db",
    package = "db",
)

go_test(
    name = "db_test",
    srcs = [
        "db_test.go",
        "mock_db_test.go",
    ],
    embed = [":db"],
    deps = [
        "@com_github_golang_mock//gomock",
        "@com_github_google_go_cmp//cmp:go_default_library",
        "@com_github_stargate_stargate_grpc_go_client//stargate/pkg/proto",
        "@org_golang_google_protobuf//testing/protocmp:go_default_library",
    ],
)

What I'd like to do is depend on :mock_db in this and other build files. I would want it to look something like:

backend/common/db/BUILD.bazel:

# Contains StargateQueryExecutor interface
go_library(
    name = "db",
    srcs = ["interfaces.go"],
    importpath = "backend/common/db",
    visibility = ["//visibility:public"],
    deps = [
        "@com_github_stargate_stargate_grpc_go_client//stargate/pkg/proto",
    ],
)
gomock(
    name = "mock_db",
    out = "mock_db_test.go",
    interfaces = ["StargateQueryExecutor"],
    library = ":db",
    package = "db",
)

backend/service/db/BUILD.bazel:

go_library(
    name = "db",
    srcs = ["db.go"],
    importpath = "backend/service/db",
    visibility = ["//visibility:public"],
    deps = [
        "//backend/common/db",
        "@com_github_huandu_go_sqlbuilder//:go-sqlbuilder",
        "@com_github_stargate_stargate_grpc_go_client//stargate/pkg/proto",
    ],
)
go_test(
    name = "db_test",
    srcs = [
        "db_test.go",
        "mock_db_test.go",
    ],
    embed = [":db"],
    deps = [
        "//backend/common/db:mock_db", # <--- THIS
        "@com_github_golang_mock//gomock",
        "@com_github_google_go_cmp//cmp:go_default_library",
        "@com_github_stargate_stargate_grpc_go_client//stargate/pkg/proto",
        "@org_golang_google_protobuf//testing/protocmp:go_default_library",
    ],
)

When I try this, it can't find the mock_db_test.go source file.

0

There are 0 best solutions below