仅在特定配置下执行的 Bazel 测试规则

Bazel test rule that only executes under certain configuration

我有一个自定义测试规则来验证生成的二进制文件的大小。我希望此规则仅在特定配置(优化,--compilation_mode=opt)下执行,否则不会在 运行(或无操作通过)下执行。

具体来说,

有办法实现吗?

我试过使用宏有条件地为规则设置别名:

但是,假设的 bazel test //my:example_size_test 构建但不 运行 测试。这是 documented:

Tests are not run if their alias is mentioned on the command line. To define an alias that runs the referenced test, use a test_suite rule with a single target in its tests attribute.

我试过使用 test_suite 而不是 alias:

但是,这不起作用,因为 tests attribute is non-configurable

是否有一种惯用的(甚至是迂回的)方法来实现仅适用于某些配置的测试?

听起来像是 select() 的工作。

-c opt定义一个config_setting,并在测试的data属性中使用select来依赖二进制文件。还将一些标志传递给测试以指示它是否应该验证二进制文件的大小。

我会给你 sh_test 的例子,因为我不想对 size_test 做任何假设:

some_test(
    name = "example_size_test",
    srcs = [...],  # you need to implement this
    deps = ["@bazel_tools//tools/bash/runfiles"],
    data = select({
        ":config_opt": ["//my:binary"],
        "//conditions:default": [],
    }),
    args = select({
        ":config_opt": [],
        "//conditions:default": ["do_not_run"],
    }),
)

如果 == "do_not_run",则测试应退出 0,否则应使用运行文件库(参见 Bash, C++, Java, Python)检索 //my:binary 的位置并测试其大小.