Search code examples
bazel

How to have bazel genrule depend on test passing?


I have the following genrule which generates a new buf image file:

genrule(
    name = "build_buf_image",
    srcs = [":proto_srcs"],
    outs = ["go/src/grail.com/examples/microservice-grpc/server/apimodels/grpc/buf-image.json"],
    cmd = "$(location //third_party/buf:generate-image) $(OUTS) $(SRCS)",
    tools = [
        "//third_party/buf:generate-image",
        "@buf",
        "@jq",
    ],
)

But before that's executed, I would like the following test to be run:

buf_proto_breaking_test(
    name = "proto_breaking_check",
    against_input = "buf-image.json",
    protos = [
        ":proto_lib",
    ],
)

How can this be done? Is there a way for the genrule to depend on the test?


Solution

  • It is not possible for a non-testonly rule to depend on a testonly rule. From https://docs.bazel.build/versions/main/be/common-definitions.html:

    If True, only testonly targets (such as tests) can depend on this target.
    
    Equivalently, a rule that is not testonly is not allowed to depend on any rule that is testonly.
    
    Tests (*_test rules) and test suites (test_suite rules) are testonly by default.
    
    This attribute is intended to mean that the target should not be contained in binaries that are released to production.
    
    Because testonly is enforced at build time, not run time, and propagates virally through the dependency tree, it should be applied judiciously. For example, stubs and fakes that are useful for unit tests may also be useful for integration tests involving the same binaries that will be released to production, and therefore should probably not be marked testonly. Conversely, rules that are dangerous to even link in, perhaps because they unconditionally override normal behavior, should definitely be marked testonly.
    

    It should be possible to create a custom rule that will call the functionality of the test, though.