【问题标题】:How to have bazel genrule depend on test passing?如何让 bazel genrule 取决于测试通过?
【发布时间】:2023-04-02 01:15:01
【问题描述】:

我有以下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 依赖测试?

【问题讨论】:

    标签: bazel


    【解决方案1】:

    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.
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多