0

我正在编写一些 bazel 测试,我需要能够提供某些文件的完整路径。

bazel test //webservice:checkstyle-test --test_arg="path_to_some_file"

我的问题是如何解析 bazel 测试中的输入参数?有什么类似的ctx.arg吗?

建造

load("//:checkstyle.bzl", "checkstyle_test")

checkstyle_test(
   name = ""
   src = []
   config = ""
)

checkstyle.bzl

def _checkstyle_test_impl(ctx):
    // How can I get my input parameter here? 

checkstyle_test = rule(
    implementation = _checkstyle_test_impl,
    test = True,
    attrs = {
        "_classpath": attr.label_list(default=[
            Label("@com_puppycrawl_tools_checkstyle//jar")
        ]),
        "config": attr.label(allow_single_file=True, default = "//config:checkstyle"),
        "suppressions": attr.label(allow_single_file=True, default = "//config:suppressions"),
        "license": attr.label(allow_single_file=True, default = "//config:license"),
        "properties": attr.label(allow_single_file=True),
        "opts": attr.string_list(),
        "string_opts": attr.string_dict(),
        "srcs": attr.label_list(allow_files = True),
        "deps": attr.label_list(),
    },
)
4

1 回答 1

1

的值--test_arg在 bazel 运行期间作为程序参数传递给测试可执行文件bazel test,请参阅https://docs.bazel.build/versions/main/command-line-reference.html#flag--test_arg

例如:

def _impl(ctx):
  ctx.actions.write(
      output = ctx.outputs.executable,
      content = "echo test args: ; echo $@",
  )

my_test = rule(
  implementation = _impl,
  test = True,
)
load(":defs.bzl", "my_test")
my_test(name = "foo")
$ bazel test foo --test_arg=--abc=123 --test_output=streamed
WARNING: Streamed test output requested. All tests will be run locally, without sharding, one at a time
INFO: Analyzed target //:foo (24 packages loaded, 277 targets configured).
INFO: Found 1 test target...
test args:
--abc=123
Target //:foo up-to-date:
  bazel-bin/foo
INFO: Elapsed time: 0.559s, Critical Path: 0.13s
INFO: 5 processes: 3 internal, 2 linux-sandbox.
INFO: Build completed successfully, 5 total actions
//:foo                                                                   PASSED in 0.0s

Executed 1 out of 1 test: 1 test passes.
INFO: Build completed successfully, 5 total actions

我认为目前没有一种方法可以--test_arg在 Starlark 规则实现中获取值(例如,它没有添加到下面ctx.fragments)。

于 2021-06-16T16:45:08.330 回答