Bazel:如何在测试前运行py_binary以生成输入文件

Bazel: How to run py_binary to produce input files before testing

本文关键字:binary 文件 输入 py 运行 测试 Bazel      更新时间:2023-10-16

我有一个这样的py_binary规则:

py_binary(
name = "testInputs",
srcs = ["testInputs.py"],
)

还有这样的cc_test

cc_test(
name = "test",
src = ["test.cc"],
data = [":testInputs"],
)

test.cc 旁边需要一个由testInputs.py生成的输入文件(比如input.txt(。 我希望testInputs运行并向test提供输入文件。

正如这里提到的,我试图依赖data部分中的testInputs。但测试在附近找不到输入文件。tree bazel-out | grep -F input.txt的结果表明,即使testInput规则也根本没有运行 - 因为input.txt文件根本不存在。

cc_test上的data = [":testInputs"]将使py_binary本身可供cc_test使用,而不是py_binary运行时可能产生的任何东西。

你会想要这样的东西:

cc_test(
name = "test",
src = ["test.cc"],
data = [":test_input.txt"],
)
genrule(
name = "gen_test_inputs",
tools = [":test_input_generator"],
outs = ["test_input.txt"],
cmd = "$(location :test_input_generator) $@"
)
py_binary(
name = "test_input_generator",
srcs = ["test_input_generator.py"],
)