如何让 bazel genrule 取决于测试通过?

How to have bazel genrule depend on test passing?

我有以下 genrule 生成新的 buf 图像文件:

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",
    ],
)

但在执行之前,我希望下面的测试是 运行:

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

如何做到这一点? genrule有没有办法依赖测试?

testonly 规则不可能依赖于testonly 规则。来自 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.

不过,应该可以创建调用测试功能的自定义规则。