【问题标题】:How to pass test args in Skylark Bazel?如何在 Skylark Ba​​zel 中通过测试参数?
【发布时间】:2021-06-16 09:59:55
【问题描述】:

我正在编写一些 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(),
    },
)

【问题讨论】:

    标签: bazel starlark skylark


    【解决方案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
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-27
      • 2020-05-29
      • 1970-01-01
      • 2017-07-04
      相关资源
      最近更新 更多