Skip to content

Commit 00b1091

Browse files
sparkprimedavidzchen
authored andcommitted
When testing, use jsonnet to canonicalize golden file before comparison (#16)
1 parent 4ef65ef commit 00b1091

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

jsonnet/jsonnet.bzl

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ def _jsonnet_to_json_impl(ctx):
7575
["-J %s" % im for im in depinfo.imports] +
7676
["-J ."] +
7777
["--var '%s'='%s'"
78-
% (var, jsonnet_vars[var]) for var in jsonnet_vars.keys()] +
78+
% (var, jsonnet_vars[var]) for var in jsonnet_vars.keys()] +
7979
["--code-var '%s'='%s'"
80-
% (var, jsonnet_code_vars[var]) for var in jsonnet_code_vars.keys()])
80+
% (var, jsonnet_code_vars[var]) for var in jsonnet_code_vars.keys()])
8181

8282
outputs = []
8383
# If multiple_outputs is set to true, then jsonnet will be invoked with the
@@ -114,7 +114,7 @@ def _jsonnet_to_json_impl(ctx):
114114
mnemonic = "Jsonnet",
115115
command = " ".join(command),
116116
use_default_shell_env = True,
117-
progress_message = "Compiling Jsonnet to JSON for " + ctx.label.name);
117+
progress_message = "Compiling Jsonnet to JSON for " + ctx.label.name)
118118

119119
_EXIT_CODE_COMPARE_COMMAND = """
120120
EXIT_CODE=$?
@@ -129,7 +129,7 @@ fi
129129
"""
130130

131131
_DIFF_COMMAND = """
132-
GOLDEN=$(cat %s)
132+
GOLDEN=$(%s %s)
133133
if [ "$OUTPUT" != "$GOLDEN" ]; then
134134
echo "FAIL (output mismatch): %s"
135135
echo "Diff:"
@@ -141,7 +141,7 @@ fi
141141
"""
142142

143143
_REGEX_DIFF_COMMAND = """
144-
GOLDEN_REGEX=$(cat %s)
144+
GOLDEN_REGEX=$(%s %s)
145145
if [[ ! "$OUTPUT" =~ $GOLDEN_REGEX ]]; then
146146
echo "FAIL (regex mismatch): %s"
147147
echo "Output: $OUTPUT"
@@ -159,11 +159,17 @@ def _jsonnet_to_json_test_impl(ctx):
159159
if ctx.file.golden:
160160
golden_files += [ctx.file.golden]
161161
if ctx.attr.regex:
162-
diff_command = _REGEX_DIFF_COMMAND % (ctx.file.golden.short_path,
163-
ctx.label.name)
162+
diff_command = _REGEX_DIFF_COMMAND % (
163+
ctx.file.jsonnet.short_path,
164+
ctx.file.golden.short_path,
165+
ctx.label.name,
166+
)
164167
else:
165-
diff_command = _DIFF_COMMAND % (ctx.file.golden.short_path,
166-
ctx.label.name)
168+
diff_command = _DIFF_COMMAND % (
169+
ctx.file.jsonnet.short_path,
170+
ctx.file.golden.short_path,
171+
ctx.label.name,
172+
)
167173

168174
jsonnet_vars = ctx.attr.vars
169175
jsonnet_code_vars = ctx.attr.code_vars
@@ -173,9 +179,9 @@ def _jsonnet_to_json_test_impl(ctx):
173179
["-J %s" % im for im in depinfo.imports] +
174180
["-J ."] +
175181
["--var %s=%s"
176-
% (var, jsonnet_vars[var]) for var in jsonnet_vars.keys()] +
182+
% (var, jsonnet_vars[var]) for var in jsonnet_vars.keys()] +
177183
["--code-var %s=%s"
178-
% (var, jsonnet_code_vars[var]) for var in jsonnet_code_vars.keys()] +
184+
% (var, jsonnet_code_vars[var]) for var in jsonnet_code_vars.keys()] +
179185
[
180186
ctx.file.src.path,
181187
"2>&1)",

0 commit comments

Comments
 (0)