Skip to content

Commit 34bd4f6

Browse files
samschlegelmarkusdap
authored andcommitted
Make file output in tests configurable (#84)
* Add output_file_contents attribute * Add format param to _EXIT_CODE_COMPARE_COMMAND * Add smoke test
1 parent 6317ea9 commit 34bd4f6

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

examples/BUILD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,11 @@ jsonnet_to_json_test(
178178
extra_args = ["--string"],
179179
canonicalize_golden = False,
180180
)
181+
182+
jsonnet_to_json_test(
183+
name = "output_file_contents_smoke_test",
184+
src = "wordcount.jsonnet",
185+
golden = "wordcount_golden.json",
186+
deps = [":workflow"],
187+
output_file_contents = False,
188+
)

jsonnet/jsonnet.bzl

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ if [ $EXIT_CODE -ne $EXPECTED_EXIT_CODE ] ; then
259259
echo "FAIL (exit code): %s"
260260
echo "Expected: $EXPECTED_EXIT_CODE"
261261
echo "Actual: $EXIT_CODE"
262-
echo "Output: $OUTPUT"
262+
if [ %s = true]; then
263+
echo "Output: $OUTPUT"
264+
fi
263265
exit 1
264266
fi
265267
"""
@@ -270,8 +272,10 @@ if [ "$OUTPUT" != "$GOLDEN" ]; then
270272
echo "FAIL (output mismatch): %s"
271273
echo "Diff:"
272274
diff <(echo "$GOLDEN") <(echo "$OUTPUT")
273-
echo "Expected: $GOLDEN"
274-
echo "Actual: $OUTPUT"
275+
if [ %s = true]; then
276+
echo "Expected: $GOLDEN"
277+
echo "Actual: $OUTPUT"
278+
fi
275279
exit 1
276280
fi
277281
"""
@@ -280,7 +284,9 @@ _REGEX_DIFF_COMMAND = """
280284
GOLDEN_REGEX=$(%s %s)
281285
if [[ ! "$OUTPUT" =~ $GOLDEN_REGEX ]]; then
282286
echo "FAIL (regex mismatch): %s"
283-
echo "Output: $OUTPUT"
287+
if [ %s = true]; then
288+
echo "Output: $OUTPUT"
289+
fi
284290
exit 1
285291
fi
286292
"""
@@ -308,12 +314,14 @@ def _jsonnet_to_json_test_impl(ctx):
308314
dump_golden_cmd,
309315
ctx.file.golden.short_path,
310316
ctx.label.name,
317+
"true" if ctx.attr.output_file_contents else "false",
311318
)
312319
else:
313320
diff_command = _DIFF_COMMAND % (
314321
dump_golden_cmd,
315322
ctx.file.golden.short_path,
316323
ctx.label.name,
324+
"true" if ctx.attr.output_file_contents else "false",
317325
)
318326

319327
jsonnet_ext_str_envs = ctx.attr.ext_str_envs
@@ -353,7 +361,11 @@ def _jsonnet_to_json_test_impl(ctx):
353361
command = [
354362
"#!/bin/bash",
355363
jsonnet_command,
356-
_EXIT_CODE_COMPARE_COMMAND % (ctx.attr.error, ctx.label.name),
364+
_EXIT_CODE_COMPARE_COMMAND % (
365+
ctx.attr.error,
366+
ctx.label.name,
367+
"true" if ctx.attr.output_file_contents else "false",
368+
),
357369
]
358370
if diff_command:
359371
command += [diff_command]
@@ -645,6 +657,7 @@ _jsonnet_to_json_test_attrs = {
645657
"golden": attr.label(allow_single_file = True),
646658
"regex": attr.bool(),
647659
"canonicalize_golden": attr.bool(default = True),
660+
"output_file_contents": attr.bool(default = True),
648661
}
649662

650663
jsonnet_to_json_test = rule(

0 commit comments

Comments
 (0)