Skip to content

Commit 3aa1fab

Browse files
chore: define a local jsonnet_json_golden_test rule instead of using rules_jsonnet
Although rules_jsonnet is the appropriate Bazel module for external Jsonnet users to use for their Jsonnet build actions, using it internally within the jsonnet repo itself is actually a little difficult as it creates a mutually recursive dependency. Since the only thing it's being used for is the jsonnet_json_test rule, and we have scripts to runs such tests anyway, it's relatively easy to define our own jsonnet_json_golden_test rule and avoid the recursive dependency.
1 parent 6542f41 commit 3aa1fab

File tree

3 files changed

+101
-19
lines changed

3 files changed

+101
-19
lines changed

cpp/testdata/BUILD

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,27 @@
1-
load(
2-
"@io_bazel_rules_jsonnet//jsonnet:jsonnet.bzl",
3-
"jsonnet_library",
4-
"jsonnet_to_json_test",
5-
)
1+
load("//tools/build_defs:golden_test.bzl", "jsonnet_json_golden_test")
62

73
package(default_visibility = ["//visibility:public"])
84

9-
jsonnet_to_json_test(
5+
jsonnet_json_golden_test(
106
name = "example_test",
117
src = "example.jsonnet",
128
golden = "example_golden.json",
139
)
1410

15-
jsonnet_to_json_test(
11+
jsonnet_json_golden_test(
1612
name = "importing_test",
1713
src = "importing.jsonnet",
14+
data = ["example.jsonnet"],
1815
golden = "importing_golden.json",
19-
imports = ["."],
20-
deps = [":testlib"],
2116
)
2217

23-
jsonnet_to_json_test(
18+
jsonnet_json_golden_test(
2419
name = "invalid_test",
2520
src = "invalid.jsonnet",
26-
error = 1,
21+
expect_error = True,
2722
golden = "invalid.out",
2823
)
2924

30-
jsonnet_library(
31-
name = "testlib",
32-
srcs = [
33-
"example.jsonnet",
34-
"importing.jsonnet",
35-
],
36-
)
37-
3825
filegroup(
3926
name = "testdata",
4027
srcs = [

test_suite/BUILD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package(default_visibility = ["//visibility:private"])
2+
3+
# Export the tests.source so it can be used by Bazel tests elsewhere in the repo.
4+
sh_library(
5+
name = "tests_sh_lib",
6+
srcs = ["tests.source"],
7+
visibility = ["//tools/build_defs:__pkg__"],
8+
)

tools/build_defs/golden_test.bzl

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
)

0 commit comments

Comments
 (0)