[英]Bazel- Can I pass system property to test JVM (skylark rule)?
我正在使用scala_specs2_junit_test
測試規則,並且希望通過測試以獲取系統屬性(通常可以使用-DmyProp=myValue
。
我發現這個線程聽起來很相關,但它涉及到java
(可以訪問命令行參數的本地規則)並run
(vs test
)。
這可能嗎?
您可以使用--test_arg ,但是測試規則本身必須解釋這些標志。
scala_specs2_junit_test
宏包裝scala_junit_test
規則。 作為測試規則,后者必須編寫ctx.outputs.executable
(請參閱rule()#test ),這是Bazel在bazel test //my:test
並傳遞--test_arg
標志到的內容。
我在Scala規則中沒有找到可以解析--jvm_flags
標志的邏輯,因此似乎需要對其進行更新才能處理該標志。
<工作區> /my_test.bzl:
def _my_test_impl(ctx):
ctx.file_action(
ctx.outputs.executable,
"\n".join([
"#!/bin/bash",
"echo \"DEBUG: Hello from $(basename $0)\"",
"echo \"DEBUG: Argv=($@)\"",
# Flag parsing (e.g. --jvm_flags) would have to happen here
]),
True)
my_test = rule(
implementation = _my_test_impl,
test = True,
)
<工作區> /已構建:
load("//:my_test.bzl", "my_test")
my_test(
name = "my_test",
)
輸出:
$ bazel test //:my_test --test_output=streamed -t- --test_arg=--foo=bar --test_arg=--jvm_flags=blah
(...)
INFO: (14:45:05.379) Found 1 test target...
DEBUG: Hello from my_test
DEBUG: Argv=(--foo=bar --jvm_flags=blah)
Target //:my_test up-to-date:
bazel-bin/my_test
INFO: (14:45:05.501) Elapsed time: 0.393s, Critical Path: 0.11s
INFO: (14:45:05.501) Build completed successfully, 3 total actions
//:my_test PASSED in 0.1s
編輯:在my_test.bzl中添加了注釋以突出顯示標志解析將發生的位置
您始終可以使用System.setProperty(name,value)以編程方式在Java中設置系統屬性。
在單元測試中執行此操作的缺點是,該屬性將在測試后保持設置狀態。 確保在測試后將該值設置為null。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.