| 1 |
"""Bazel rules for running golden tests.""" |
| 2 |
|
| 3 |
def _gen_test_script(ctx): |
| 4 |
return """\ |
| 5 |
#!/bin/bash |
| 6 |
|
| 7 |
set -uo pipefail |
| 8 |
|
| 9 |
# We don't run 'init' or 'deinit' from tests.source, as these |
| 10 |
# just create & remove a TMP dir and Bazel deals with that for us; |
| 11 |
# instead we just need to ensure that the path for the Bazel |
| 12 |
# managed temp dir is available in TMP_DIR. |
| 13 |
|
| 14 |
TMP_DIR="$TEST_TMPDIR" |
| 15 |
VERBOSE=true |
| 16 |
DISABLE_ERROR_TESTS= |
| 17 |
SUMMARY_ONLY= |
| 18 |
|
| 19 |
source ./test_suite/tests.source |
| 20 |
|
| 21 |
GOLDEN_OUTPUT=$(cat '{golden_path}') |
| 22 |
|
| 23 |
test_eval '{jsonnet_path}' '{input_path}' '{expected_exit_code}' "$GOLDEN_OUTPUT" '{golden_kind}' |
| 24 |
|
| 25 |
if [ $FAILED -eq 0 ] ; then |
| 26 |
echo "$0: All $EXECUTED test scripts pass." |
| 27 |
exit 0 |
| 28 |
else |
| 29 |
echo "$0: FAILED: $FAILED / $EXECUTED" |
| 30 |
exit 1 |
| 31 |
fi |
| 32 |
""".format( |
| 33 |
jsonnet_path = ctx.executable.jsonnet.short_path, |
| 34 |
input_path = ctx.file.src.short_path, |
| 35 |
expected_exit_code = 1 if ctx.attr.expect_error else 0, |
| 36 |
golden_path = ctx.file.golden.short_path, |
| 37 |
golden_kind = "PLAIN", |
| 38 |
) |
| 39 |
|
| 40 |
def _jsonnet_json_golden_test_impl(ctx): |
| 41 |
test_script = ctx.actions.declare_file(ctx.label.name) |
| 42 |
ctx.actions.write( |
| 43 |
output = test_script, |
| 44 |
is_executable = True, |
| 45 |
content = _gen_test_script(ctx), |
| 46 |
) |
| 47 |
return DefaultInfo( |
| 48 |
executable = test_script, |
| 49 |
runfiles = ctx.runfiles( |
| 50 |
transitive_files = ctx.attr._test_sh_lib.files, |
| 51 |
files = [ |
| 52 |
ctx.executable.jsonnet, |
| 53 |
ctx.file.src, |
| 54 |
ctx.file.golden, |
| 55 |
] + ctx.files.data, |
| 56 |
), |
| 57 |
) |
| 58 |
|
| 59 |
jsonnet_json_golden_test = rule( |
| 60 |
implementation = _jsonnet_json_golden_test_impl, |
| 61 |
test = True, |
| 62 |
attrs = { |
| 63 |
"src": attr.label( |
| 64 |
mandatory = True, |
| 65 |
allow_single_file = True, |
| 66 |
), |
| 67 |
"data": attr.label_list(allow_files = True), |
| 68 |
"golden": attr.label( |
| 69 |
mandatory = True, |
| 70 |
allow_single_file = True, |
| 71 |
), |
| 72 |
"jsonnet": attr.label( |
| 73 |
default = "//cmd:jsonnet", |
| 74 |
executable = True, |
| 75 |
cfg = "exec", |
| 76 |
), |
| 77 |
"expect_error": attr.bool( |
| 78 |
doc = "If True, the golden file is the expected stderr output from jsonnet", |
| 79 |
), |
| 80 |
"canonicalize_golden": attr.bool( |
| 81 |
doc = "If True, the golden file will be reformatted prior to comparing against the jsonnet output", |
| 82 |
), |
| 83 |
"_test_sh_lib": attr.label( |
| 84 |
default = "//test_suite:tests_sh_lib", |
| 85 |
), |
| 86 |
}, |
| 87 |
) |