Skip to content

Commit

Permalink
When testing, use jsonnet to canonicalize golden file before comparis…
Browse files Browse the repository at this point in the history
…on (#16)
  • Loading branch information
sparkprime authored and davidzchen committed Feb 8, 2017
1 parent 4ef65ef commit 00b1091
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions jsonnet/jsonnet.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def _jsonnet_to_json_impl(ctx):
["-J %s" % im for im in depinfo.imports] +
["-J ."] +
["--var '%s'='%s'"
% (var, jsonnet_vars[var]) for var in jsonnet_vars.keys()] +
% (var, jsonnet_vars[var]) for var in jsonnet_vars.keys()] +
["--code-var '%s'='%s'"
% (var, jsonnet_code_vars[var]) for var in jsonnet_code_vars.keys()])
% (var, jsonnet_code_vars[var]) for var in jsonnet_code_vars.keys()])

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

_EXIT_CODE_COMPARE_COMMAND = """
EXIT_CODE=$?
Expand All @@ -129,7 +129,7 @@ fi
"""

_DIFF_COMMAND = """
GOLDEN=$(cat %s)
GOLDEN=$(%s %s)
if [ "$OUTPUT" != "$GOLDEN" ]; then
echo "FAIL (output mismatch): %s"
echo "Diff:"
Expand All @@ -141,7 +141,7 @@ fi
"""

_REGEX_DIFF_COMMAND = """
GOLDEN_REGEX=$(cat %s)
GOLDEN_REGEX=$(%s %s)
if [[ ! "$OUTPUT" =~ $GOLDEN_REGEX ]]; then
echo "FAIL (regex mismatch): %s"
echo "Output: $OUTPUT"
Expand All @@ -159,11 +159,17 @@ def _jsonnet_to_json_test_impl(ctx):
if ctx.file.golden:
golden_files += [ctx.file.golden]
if ctx.attr.regex:
diff_command = _REGEX_DIFF_COMMAND % (ctx.file.golden.short_path,
ctx.label.name)
diff_command = _REGEX_DIFF_COMMAND % (
ctx.file.jsonnet.short_path,
ctx.file.golden.short_path,
ctx.label.name,
)
else:
diff_command = _DIFF_COMMAND % (ctx.file.golden.short_path,
ctx.label.name)
diff_command = _DIFF_COMMAND % (
ctx.file.jsonnet.short_path,
ctx.file.golden.short_path,
ctx.label.name,
)

jsonnet_vars = ctx.attr.vars
jsonnet_code_vars = ctx.attr.code_vars
Expand All @@ -173,9 +179,9 @@ def _jsonnet_to_json_test_impl(ctx):
["-J %s" % im for im in depinfo.imports] +
["-J ."] +
["--var %s=%s"
% (var, jsonnet_vars[var]) for var in jsonnet_vars.keys()] +
% (var, jsonnet_vars[var]) for var in jsonnet_vars.keys()] +
["--code-var %s=%s"
% (var, jsonnet_code_vars[var]) for var in jsonnet_code_vars.keys()] +
% (var, jsonnet_code_vars[var]) for var in jsonnet_code_vars.keys()] +
[
ctx.file.src.path,
"2>&1)",
Expand Down

0 comments on commit 00b1091

Please sign in to comment.