Skip to content

Commit c590931

Browse files
committed
test: add negative cue_vet_test with should_fail attr
1 parent 2b496ff commit c590931

4 files changed

Lines changed: 29 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/bazel-bin
2-
/bazel-out
3-
/bazel-rules_cue
4-
/bazel-testlogs
1+
/bazel-*
52

63
*~

cue/cue.bzl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,18 @@ def _cue_vet_test_impl(ctx):
947947

948948
concrete_flag = "'-c'" if ctx.attr.concrete else ""
949949

950+
# Choose invocation style based on should_fail (known at analysis time)
951+
if ctx.attr.should_fail:
952+
vet_invocation = """\
953+
if "${{CUE}}" vet "${{vet_args[@]+"${{vet_args[@]}}}}" {instance_arg} "${{data_files[@]}}"; then
954+
echo >&2 "cue vet succeeded but failure was expected"
955+
exit 1
956+
else
957+
exit 0
958+
fi"""
959+
else:
960+
vet_invocation = 'exec "${{CUE}}" vet "${{vet_args[@]+"${{vet_args[@]}}}}" {instance_arg} "${{data_files[@]}}"'
961+
950962
runfiles_boilerplate = """\
951963
# Copy-pasted from the Bazel Bash runfiles library v2.
952964
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
@@ -985,7 +997,7 @@ vet_args=()
985997
{concrete_line}
986998
{schema_expr_lines}
987999
988-
exec "${{CUE}}" vet "${{vet_args[@]+"${{vet_args[@]}}}}" {instance_arg} "${{data_files[@]}}"
1000+
{vet_invocation}
9891001
""".format(
9901002
runfiles_boilerplate = runfiles_boilerplate,
9911003
cue_tool_rlocation = _runfile_path(ctx, cue_tool),
@@ -994,6 +1006,7 @@ exec "${{CUE}}" vet "${{vet_args[@]+"${{vet_args[@]}}}}" {instance_arg} "${{data
9941006
concrete_line = 'vet_args+=({})'.format(concrete_flag) if concrete_flag else "# concrete flag not set",
9951007
schema_expr_lines = schema_expr_lines if schema_expr_lines else "# no schema expressions",
9961008
instance_arg = instance_arg,
1009+
vet_invocation = vet_invocation.format(instance_arg = instance_arg),
9971010
)
9981011

9991012
script = ctx.actions.declare_file(ctx.label.name + "_vet_test.sh")
@@ -1043,6 +1056,10 @@ Each expression is passed as a "-d" flag to "cue vet".""",
10431056
doc = "Require all values to be concrete (passes -c to cue vet).",
10441057
default = True,
10451058
),
1059+
"should_fail": attr.bool(
1060+
doc = "When True, the test passes if cue vet exits non-zero (i.e. validation fails as expected).",
1061+
default = False,
1062+
),
10461063
"_bash_runfiles": attr.label(
10471064
default = Label("@bazel_tools//tools/bash/runfiles"),
10481065
),

test/testdata/vet/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,11 @@ cue_vet_test(
1313
data = ["valid.yaml"],
1414
schema_expression = ["#Config"],
1515
)
16+
17+
cue_vet_test(
18+
name = "invalid_config_test",
19+
instance = ":vet_test_instance",
20+
data = ["invalid.yaml"],
21+
schema_expression = ["#Config"],
22+
should_fail = True,
23+
)

test/testdata/vet/invalid.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name: myservice
2+
port: "not_a_number"

0 commit comments

Comments
 (0)