Bazel में Starlark कोड की जांच करने के कई तरीके हैं. इस पेज पर, इस्तेमाल के उदाहरण के हिसाब से, मौजूदा सबसे सही तरीकों और फ़्रेमवर्क को इकट्ठा किया गया है.
जांच के नियम
Skylib ने टेस्ट unittest.bzl
नाम का एक टेस्ट फ़्रेमवर्क लागू किया है. इसकी मदद से, नियमों और कार्रवाइयों के तरीके के बारे में विश्लेषण किया जा सकता है, जैसे कि सेवा देने वाली कंपनियां. इस तरह की जांच को "विश्लेषण टेस्ट" कहा जाता है. फ़िलहाल, नियमों की सबसे अच्छी तरह से जांच करने का यह सबसे अच्छा विकल्प है.
कुछ चेतावनियां:
टेस्ट के दावे, बिल्ड में होते हैं, न कि किसी अलग रनर प्रोसेस के लिए. टेस्ट के ज़रिए बनाए गए टारगेट को नाम इस तरह देना चाहिए कि वे दूसरे टेस्ट या बिल्ड के टारगेट से मेल न खाएं. टेस्ट के दौरान एक गड़बड़ी, टेस्ट गड़बड़ी के बजाय बिल्ड ब्रेकेज के तौर पर दिखती है.
टेस्ट के तहत और टेस्ट दावे वाले नियम सेट अप करने के लिए, बॉयलरप्लेट की काफ़ी मात्रा की ज़रूरत होती है. यह बॉयलरप्लेट पहली बार में मुश्किल लग सकता है. इससे ध्यान रखने में मदद मिलती है कि मैक्रो का आकलन किया जाता है और लोड होने के दौरान टारगेट जनरेट किया जाता है, जबकि नियम लागू करने के फ़ंक्शन बाद के समय तक नहीं चलते हैं.
विश्लेषण की जांच काफ़ी छोटी और हल्की होती है. विश्लेषण टेस्टिंग फ़्रेमवर्क की कुछ सुविधाएं, ज़्यादा से ज़्यादा ट्रांज़िटिव डिपेंडेंसी वाले टारगेट की पुष्टि कर सकती हैं (फ़िलहाल, 500 हैं). इसकी वजह यह है कि परफ़ॉर्मेंस को बेहतर बनाने के लिए, ज़्यादा सुविधाओं के साथ इन सुविधाओं का इस्तेमाल किया जा सकता है.
बुनियादी नियम, टेस्टिंग के ऐसे नियम को तय करना है जो नियम की जांच के हिसाब से हो. इससे, टेस्ट करने वाले नियम को सेवा देने वाली कंपनियों को ऐक्सेस देने की सुविधा मिलती है.
जांच के नियम लागू करने के दावे सही हैं. अगर कोई
गड़बड़ी होती है, तो इन्हें तुरंत fail()
पर कॉल करके बढ़ाया जाता है (जो
विश्लेषण के समय बनने वाली गड़बड़ी को ट्रिगर करेगा), लेकिन गड़बड़ियों को
जांच की गई प्रोसेस के दौरान फ़ेल होने वाली जनरेट की गई स्क्रिप्ट में सेव करके कर दिया जाता है.
नीचे खिलौनों का एक छोटा सा उदाहरण देखें. इसके बाद, एक उदाहरण दिया गया है, जो कार्रवाइयों की जांच करता है.
कम से कम उदाहरण
//mypkg/myrules.bzl
:
MyInfo = provider(fields = {
"val": "string value",
"out": "output File",
})
def _myrule_impl(ctx):
"""Rule that just generates a file and returns a provider."""
out = ctx.actions.declare_file(ctx.label.name + ".out")
ctx.actions.write(out, "abc")
return [MyInfo(val="some value", out=out)]
myrule = rule(
implementation = _myrule_impl,
)
//mypkg/myrules_test.bzl
:
load("@bazel_skylib//lib:unittest.bzl", "asserts", "analysistest")
load(":myrules.bzl", "myrule", "MyInfo")
# ==== Check the provider contents ====
def _provider_contents_test_impl(ctx):
env = analysistest.begin(ctx)
target_under_test = analysistest.target_under_test(env)
# If preferred, could pass these values as "expected" and "actual" keyword
# arguments.
asserts.equals(env, "some value", target_under_test[MyInfo].val)
# If you forget to return end(), you will get an error about an analysis
# test needing to return an instance of AnalysisTestResultInfo.
return analysistest.end(env)
# Create the testing rule to wrap the test logic. This must be bound to a global
# variable, not called in a macro's body, since macros get evaluated at loading
# time but the rule gets evaluated later, at analysis time. Since this is a test
# rule, its name must end with "_test".
provider_contents_test = analysistest.make(_provider_contents_test_impl)
# Macro to setup the test.
def _test_provider_contents():
# Rule under test. Be sure to tag 'manual', as this target should not be
# built using `:all` except as a dependency of the test.
myrule(name = "provider_contents_subject", tags = ["manual"])
# Testing rule.
provider_contents_test(name = "provider_contents_test",
target_under_test = ":provider_contents_subject")
# Note the target_under_test attribute is how the test rule depends on
# the real rule target.
# Entry point from the BUILD file; macro for running each test case's macro and
# declaring a test suite that wraps them together.
def myrules_test_suite(name):
# Call all test functions and wrap their targets in a suite.
_test_provider_contents()
# ...
native.test_suite(
name = name,
tests = [
":provider_contents_test",
# ...
],
)
//mypkg/BUILD
:
load(":myrules.bzl", "myrule")
load(":myrules_test.bzl", "myrules_test_suite")
# Production use of the rule.
myrule(
name = "mytarget",
)
# Call a macro that defines targets that perform the tests at analysis time,
# and that can be executed with "bazel test" to return the result.
myrules_test_suite(name = "myrules_test")
टेस्ट को bazel test //mypkg:myrules_test
के साथ चलाया जा सकता है.
शुरुआती load()
स्टेटमेंट के अलावा, फ़ाइल के दो मुख्य हिस्से होते हैं:
जांच में से हर एक में ये शामिल होते हैं: 1) जांच के नियम के लिए विश्लेषण लागू करने का फ़ंक्शन, 2)
analysistest.make()
की मदद से जांच करने के नियम का एलान करना, और 3) नियम की जांच के दौरान (और इसकी डिपेंडेंसी) एलान करने के लिए लोडिंग के समय का फ़ंक्शन (मैक्रो). अगर दावों में, टेस्ट केस (1) और 2) के बीच कोई बदलाव नहीं होता है, तो वे एक से ज़्यादा टेस्ट केस में शेयर किए जा सकते हैं.टेस्ट सुइट फ़ंक्शन, जो हर टेस्ट के लिए लोड होने में लगने वाले समय वाले फ़ंक्शन को कॉल करता है और सभी टेस्ट को एक साथ बंडल करने के लिए
test_suite
टारगेट बताता है.
एक जैसा नाम रखने के लिए, सुझाए गए नाम का पालन करें: foo
को जांच के नाम वाले उस हिस्से के लिए इस्तेमाल करने की अनुमति दें जो जांच में सही जानकारी देता है
(ऊपर दिए गए उदाहरण में provider_contents
). उदाहरण के लिए, JUnit टेस्ट का तरीका testFoo
नाम दिया जाएगा.
इसके बाद:
जो मैक्रो टेस्ट और टारगेट को टेस्ट जनरेट करता है उसका नाम
_test_foo
(_test_provider_contents
) होना चाहिएइसके टेस्ट नियम का नाम
foo_test
(provider_contents_test
) होना चाहिएइस नियम प्रकार के लक्ष्य का लेबल
foo_test
(provider_contents_test
) होना चाहिएजांच के नियम लागू करने के फ़ंक्शन का नाम
_foo_test_impl
(_provider_contents_test_impl
) होना चाहिएटेस्ट में शामिल नियमों के टारगेट और उनकी डिपेंडेंसी के आगे
foo_
(provider_contents_
) लगा होना चाहिए
ध्यान दें कि सभी टारगेट के लेबल, एक ही BUILD पैकेज में अन्य लेबल के साथ टकराव कर सकते हैं. इसलिए, टेस्ट के लिए किसी खास नाम का इस्तेमाल करना उपयोगी होता है.
टेस्ट नहीं हो सका
यह पुष्टि करने में मदद मिल सकती है कि कोई नियम, कुछ इनपुट या किसी खास स्थिति में काम नहीं करता. विश्लेषण के विश्लेषण फ़्रेमवर्क का इस्तेमाल करके ऐसा किया जा सकता है:
analysistest.make
के साथ बनाए गए टेस्ट के नियम में expect_failure
की जानकारी होनी चाहिए:
failure_testing_test = analysistest.make(
_failure_testing_test_impl,
expect_failure = True,
)
जांच के नियम लागू होने से, होने वाली गलतियों के बारे में पता चलना चाहिए. खास तौर पर, गड़बड़ी का मैसेज होना चाहिए:
def _failure_testing_test_impl(ctx):
env = analysistest.begin(ctx)
asserts.expect_failure(env, "This rule should never work")
return analysistest.end(env)
साथ ही, पक्का करें कि टारगेट की जांच में 'मैन्युअल' को टैग किया गया हो.
इसके बिना, आपके पैकेज में :all
का इस्तेमाल करके सभी टारगेट बनाने की वजह से जान-बूझकर टारगेट नहीं बनाया जा सकेगा. साथ ही, यह बिल्ड में असफलता भी दिखाएगा. 'मैन्युअल' में, 'टेस्ट के तहत आपका टारगेट' सिर्फ़ तब बनेगा, जब साफ़ तौर पर या किसी ऐसे टारगेट की निर्भरता के तौर पर
जो मैन्युअल नहीं है (जैसे कि आपका टेस्ट नियम):
def _test_failure():
myrule(name = "this_should_fail", tags = ["manual"])
failure_testing_test(name = "failure_testing_test",
target_under_test = ":this_should_fail")
# Then call _test_failure() in the macro which generates the test suite and add
# ":failure_testing_test" to the suite's test targets.
रजिस्टर की गई कार्रवाइयों की पुष्टि करना
उदाहरण के लिए, ctx.actions.run()
का इस्तेमाल करके, हो सकता है कि आप ऐसे टेस्ट लिखना चाहें जिनसे आपके नियम के बारे में पता चलता हो. यह आपके विश्लेषण
टेस्ट नियम लागू करने के फ़ंक्शन में किया जा सकता है. एक उदाहरण:
def _inspect_actions_test_impl(ctx):
env = analysistest.begin(ctx)
target_under_test = analysistest.target_under_test(env)
actions = analysistest.target_actions(env)
asserts.equals(env, 1, len(actions))
action_output = actions[0].outputs.to_list()[0]
asserts.equals(
env, target_under_test.label.name + ".out", action_output.basename)
return analysistest.end(env)
ध्यान दें कि analysistest.target_actions(env)
, ऐसे Action
ऑब्जेक्ट की सूची दिखाता है जो जांच के तहत, टारगेट से रजिस्टर की गई कार्रवाइयों को दिखाते हैं.
अलग-अलग फ़्लैग के तहत नियम के व्यवहार की पुष्टि करना
हो सकता है कि आप इस बात की पुष्टि करना चाहें कि आपका असल नियम कुछ खास तरह के बिल्ड फ़्लैग के साथ काम करता है. उदाहरण के लिए, आपका नियम तब अलग तरह से काम कर सकता है, जब कोई उपयोगकर्ता:
bazel build //mypkg:real_target -c opt
बनाम
bazel build //mypkg:real_target -c dbg
पहली बार में, ऐसा किया जा सकता है. इसके लिए, सही बिल्ड फ़्लैग का इस्तेमाल करके, टेस्ट में टारगेट का टेस्ट करें.
bazel test //mypkg:myrules_test -c opt
हालांकि, इसके बाद आपके टेस्ट सुइट में एक साथ यह जांच नहीं की जा सकती कि
-c opt
के तहत नियम का व्यवहार क्या है. वहीं, दूसरा टेस्ट
-c dbg
के तहत, नियम के व्यवहार की पुष्टि करता है. दोनों टेस्ट एक ही बिल्ड में नहीं चल सकेंगे!
टेस्ट के नियम को तय करते समय, इसमें अपने हिसाब से बिल्ड फ़्लैग फ़्लैग डाल सकते हैं:
myrule_c_opt_test = analysistest.make(
_myrule_c_opt_test_impl,
config_settings = {
"//command_line_option:compilation_mode": "opt",
},
)
आम तौर पर, मौजूदा बिल्ड फ़्लैग को ध्यान में रखते हुए, टेस्ट में शामिल टारगेट का विश्लेषण किया जाता है.
config_settings
तय करने से, बताई गई कमांड लाइन के विकल्पों की वैल्यू बदल जाती है. (तय नहीं किए गए सभी विकल्प, असल वैल्यू से
अपने मान को बनाए रखेंगे).
बताए गए config_settings
शब्दकोश में, कमांड लाइन फ़्लैग के आगे एक खास प्लेसहोल्डर वैल्यू //command_line_option:
लगाई जानी चाहिए, जैसा कि ऊपर दिखाया गया है.
आर्टफ़ैक्ट की पुष्टि की जा रही है
आपकी जनरेट की गई फ़ाइलें सही हैं या नहीं, इसकी जांच करने के मुख्य तरीके ये हैं:
आपके पास शेल, Python या किसी दूसरी भाषा में टेस्ट स्क्रिप्ट लिखने का विकल्प होता है. साथ ही, सही
*_test
नियम टाइप के लिए, एक टारगेट बनाया जा सकता है.आपको जिस तरह की जांच करनी है उसके लिए खास नियम का इस्तेमाल करें.
टेस्ट टारगेट का इस्तेमाल करना
आर्टफ़ैक्ट की पुष्टि करने का सबसे आसान तरीका है कि आप स्क्रिप्ट लिखें और
अपनी BUILD फ़ाइल में *_test
टारगेट जोड़ें. आपको जिन खास आर्टफ़ैक्ट की जांच करनी है वे इस टारगेट के डेटा
पर निर्भरता होनी चाहिए. अगर पुष्टि करने वाला लॉजिक,
एक से ज़्यादा टेस्ट के लिए फिर से इस्तेमाल किया जा सकता है, तो यह एक ऐसी स्क्रिप्ट होनी चाहिए जिसमें कमांड लाइन आर्ग्युमेंट हों. ये आर्ग्युमेंट, टेस्ट टारगेट के args
एट्रिब्यूट से कंट्रोल होते हैं. यहां एक उदाहरण दिया गया है, जिससे यह पुष्टि होती है कि myrule
का आउटपुट "abc"
है.
//mypkg/myrule_validator.sh
:
if [ "$(cat $1)" = "abc" ]; then
echo "Passed"
exit 0
else
echo "Failed"
exit 1
fi
//mypkg/BUILD
:
...
myrule(
name = "mytarget",
)
...
# Needed for each target whose artifacts are to be checked.
sh_test(
name = "validate_mytarget",
srcs = [":myrule_validator.sh"],
args = ["$(location :mytarget.out)"],
data = [":mytarget.out"],
)
कस्टम नियम का उपयोग करना
शेल स्क्रिप्ट को टेंप्लेट के तौर पर लिखना ज़्यादा मुश्किल विकल्प है. इससे नए नियम के लागू होने की वजह से इंस्टैंशिएट होता है. इसमें ज़्यादा डायरेक्टिव और Starlark लॉजिक शामिल हैं. हालांकि, इससे क्लीनर BUILD फ़ाइलें भी बनती हैं. इसके अलावा, किसी भी आर्ग्युमेंट को पहले से प्रोसेस करने के लिए, स्क्रिप्ट के बजाय Starlark का इस्तेमाल किया जा सकता है. साथ ही, स्क्रिप्ट में अपने-आप दस्तावेज़ बनाने की सुविधा पहले से मौजूद है, क्योंकि इसमें अंकों वाले प्लेसहोल्डर के बजाय, सिंबल (प्लेसहोल्डर) का इस्तेमाल किया जाता है.
//mypkg/myrule_validator.sh.template
:
if [ "$(cat %TARGET%)" = "abc" ]; then
echo "Passed"
exit 0
else
echo "Failed"
exit 1
fi
//mypkg/myrule_validation.bzl
:
def _myrule_validation_test_impl(ctx):
"""Rule for instantiating myrule_validator.sh.template for a given target."""
exe = ctx.outputs.executable
target = ctx.file.target
ctx.actions.expand_template(output = exe,
template = ctx.file._script,
is_executable = True,
substitutions = {
"%TARGET%": target.short_path,
})
# This is needed to make sure the output file of myrule is visible to the
# resulting instantiated script.
return [DefaultInfo(runfiles=ctx.runfiles(files=[target]))]
myrule_validation_test = rule(
implementation = _myrule_validation_test_impl,
attrs = {"target": attr.label(allow_single_file=True),
# You need an implicit dependency in order to access the template.
# A target could potentially override this attribute to modify
# the test logic.
"_script": attr.label(allow_single_file=True,
default=Label("//mypkg:myrule_validator"))},
test = True,
)
//mypkg/BUILD
:
...
myrule(
name = "mytarget",
)
...
# Needed just once, to expose the template. Could have also used export_files(),
# and made the _script attribute set allow_files=True.
filegroup(
name = "myrule_validator",
srcs = [":myrule_validator.sh.template"],
)
# Needed for each target whose artifacts are to be checked. Notice that you no
# longer have to specify the output file name in a data attribute, or its
# $(location) expansion in an args attribute, or the label for the script
# (unless you want to override it).
myrule_validation_test(
name = "validate_mytarget",
target = ":mytarget",
)
इसके अलावा, टेंप्लेट को बड़ा करने की कार्रवाई का इस्तेमाल करने के बजाय, आप टेंप्लेट को .bzl फ़ाइल में एक स्ट्रिंग के तौर पर इनलाइन कर सकते थे. साथ ही, विश्लेषण के दौरान str.format
मैथड या %
-फ़ॉर्मैटिंग का इस्तेमाल करके इसे बड़ा कर सकते थे.
Starlark की सुविधाओं की जांच करना
Skylib के
unittest.bzl
फ़्रेमवर्क का इस्तेमाल, यूटिलिटी फ़ंक्शन की जांच करने के लिए किया जा सकता है. इसका मतलब है कि न तो मैक्रो और न ही नियम लागू करने के फ़ंक्शन. unittest.bzl
की analysistest
लाइब्रेरी का इस्तेमाल करने के बजाय, unittest
का इस्तेमाल किया जा सकता है. ऐसे टेस्ट सुइट के लिए, सुविधाओं का इस्तेमाल unittest.suite()
बॉयलरप्लेट को कम करने के लिए किया जा सकता है.
//mypkg/myhelpers.bzl
:
def myhelper():
return "abc"
//mypkg/myhelpers_test.bzl
:
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
load(":myhelpers.bzl", "myhelper")
def _myhelper_test_impl(ctx):
env = unittest.begin(ctx)
asserts.equals(env, "abc", myhelper())
return unittest.end(env)
myhelper_test = unittest.make(_myhelper_test_impl)
# No need for a test_myhelper() setup function.
def myhelpers_test_suite(name):
# unittest.suite() takes care of instantiating the testing rules and creating
# a test_suite.
unittest.suite(
name,
myhelper_test,
# ...
)
//mypkg/BUILD
:
load(":myhelpers_test.bzl", "myhelpers_test_suite")
myhelpers_test_suite(name = "myhelpers_tests")
ज़्यादा उदाहरणों के लिए, Skylib की अपनी जांच देखें.