仅在特定配置下执行的 Bazel 测试规则
Bazel test rule that only executes under certain configuration
我有一个自定义测试规则来验证生成的二进制文件的大小。我希望此规则仅在特定配置(优化,--compilation_mode=opt
)下执行,否则不会在 运行(或无操作通过)下执行。
具体来说,
bazel test //my:example_size_test
不应该 运行 测试(最好是,虽然 运行ning 和总是通过是可以接受的)
bazel test -c opt //my:example_size_test
应该 运行 测试,通过测试结果
有办法实现吗?
我试过使用宏有条件地为规则设置别名:
size_test
是实例化的宏
$name_enabled_test
,_size_test
类型的实际测试目标
$name_disabled_test
,一个 noop_test
规则(本质上是 exit 0
的自定义规则)
$name
,根据 select
的配置,在 $name_enabled_test
和 $name_disabled_test
之间选择的 alias
但是,假设的 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
:
size_test
是实例化的宏
$name_enabled_test
,_size_test
类型的实际测试目标
$name_disabled_test
,一个 noop_test
规则(本质上是 exit 0
的自定义规则)
$name
,具有 tests
属性的 test_suite
select
介于 $name_enabled_test
和 $name_disabled_test
之间,具体取决于配置
但是,这不起作用,因为 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
的位置并测试其大小.
我有一个自定义测试规则来验证生成的二进制文件的大小。我希望此规则仅在特定配置(优化,--compilation_mode=opt
)下执行,否则不会在 运行(或无操作通过)下执行。
具体来说,
bazel test //my:example_size_test
不应该 运行 测试(最好是,虽然 运行ning 和总是通过是可以接受的)bazel test -c opt //my:example_size_test
应该 运行 测试,通过测试结果
有办法实现吗?
我试过使用宏有条件地为规则设置别名:
size_test
是实例化的宏$name_enabled_test
,_size_test
类型的实际测试目标
$name_disabled_test
,一个noop_test
规则(本质上是exit 0
的自定义规则)$name
,根据select
的配置,在
$name_enabled_test
和$name_disabled_test
之间选择的alias
但是,假设的 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 itstests
attribute.
我试过使用 test_suite
而不是 alias
:
size_test
是实例化的宏$name_enabled_test
,_size_test
类型的实际测试目标
$name_disabled_test
,一个noop_test
规则(本质上是exit 0
的自定义规则)$name
,具有tests
属性的test_suite
select
介于$name_enabled_test
和$name_disabled_test
之间,具体取决于配置
但是,这不起作用,因为 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
的位置并测试其大小.