Skip to content

Commit

Permalink
Create a failure test rule
Browse files Browse the repository at this point in the history
When testing for analysis-phase-failures in rules this rule provides
otherwise necessary boilerplate that simply allows to test for correct
Error messages
  • Loading branch information
FaBrand committed May 27, 2021
1 parent c6f6b54 commit dbfe808
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions rules/failure_test.bzl
@@ -0,0 +1,36 @@
# Copyright 2019 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""A rule that allows to test for failures of rules
Allows to test for simple failures of rules and asserting the error message of the error.
"""

load("//lib:unittest.bzl", "analysistest", "asserts")

def _failure_test_impl(ctx):
env = analysistest.begin(ctx)
asserts.expect_failure(env, expected_failure_msg = ctx.attr.error_message)
return analysistest.end(env)

failure_test = analysistest.make(
_failure_test_impl,
expect_failure = True,
attrs = {
"error_message": attr.string(
mandatory = True,
doc = "The expected error message that is asserted",
),
},
)

0 comments on commit dbfe808

Please sign in to comment.