diff options
-rw-r--r-- | rules/cc/clang_tidy.bzl | 2 | ||||
-rw-r--r-- | rules/cc/clang_tidy_test.bzl | 39 | ||||
-rw-r--r-- | rules/test_common/rules.bzl | 31 |
3 files changed, 70 insertions, 2 deletions
diff --git a/rules/cc/clang_tidy.bzl b/rules/cc/clang_tidy.bzl index 5e1c07c1..db852a97 100644 --- a/rules/cc/clang_tidy.bzl +++ b/rules/cc/clang_tidy.bzl @@ -80,7 +80,7 @@ def _check_bad_tidy_checks(tidy_checks): if " " in check: fail("Check `%s` invalid, cannot contain spaces" % check) if "," in check: - fail("Check `%s` invalid, cannot contain commas. Split each entry into it's own string instead" % check) + fail("Check `%s` invalid, cannot contain commas. Split each entry into its own string instead" % check) def _add_with_tidy_flags(ctx, tidy_flags): with_tidy_flags = ctx.attr._with_tidy_flags[BuildSettingInfo].value diff --git a/rules/cc/clang_tidy_test.bzl b/rules/cc/clang_tidy_test.bzl index 701839b7..9ab735a3 100644 --- a/rules/cc/clang_tidy_test.bzl +++ b/rules/cc/clang_tidy_test.bzl @@ -17,6 +17,7 @@ limitations under the License. load("@bazel_skylib//lib:new_sets.bzl", "sets") load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") load(":clang_tidy.bzl", "generate_clang_tidy_actions") +load("//build/bazel/rules/test_common:rules.bzl", "expect_failure_test") _PACKAGE_HEADER_FILTER = "^build/bazel/rules/cc/" _DEFAULT_CHECKS = [ @@ -338,11 +339,47 @@ def _test_disabled_checks_are_removed(): test_name, ] +def _create_bad_tidy_checks_test(name, tidy_checks, failure_message): + name = "bad_tidy_checks_fail" + name + test_name = name + "_test" + + _clang_tidy( + name = name, + srcs = ["a.cpp"], + tidy_checks = tidy_checks, + tags = ["manual"], + ) + + expect_failure_test( + name = test_name, + target_under_test = name, + failure_message = failure_message, + ) + + return [ + test_name, + ] + +def _test_bad_tidy_checks_fail(): + return ( + _create_bad_tidy_checks_test( + name = "with_spaces", + tidy_checks = ["check with spaces"], + failure_message = "Check `check with spaces` invalid, cannot contain spaces", + ) + + _create_bad_tidy_checks_test( + name = "with_commas", + tidy_checks = ["check,with,commas"], + failure_message = "Check `check,with,commas` invalid, cannot contain commas. Split each entry into its own string instead", + ) + ) + def clang_tidy_test_suite(name): native.test_suite( name = name, tests = _test_clang_tidy() + _test_custom_header_dir() + - _test_disabled_checks_are_removed(), + _test_disabled_checks_are_removed() + + _test_bad_tidy_checks_fail(), ) diff --git a/rules/test_common/rules.bzl b/rules/test_common/rules.bzl new file mode 100644 index 00000000..5b9b8228 --- /dev/null +++ b/rules/test_common/rules.bzl @@ -0,0 +1,31 @@ +""" +Copyright (C) 2022 The Android Open Source Project + +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. +""" + +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") + +def _rule_failure_impl(ctx): + env = analysistest.begin(ctx) + asserts.expect_failure(env, ctx.attr.failure_message) + return analysistest.end(env) + +expect_failure_test = analysistest.make( + impl = _rule_failure_impl, + expect_failure = True, + attrs = { + "failure_message": attr.string(), + }, + doc = "This test checks that a rule fails with the expected failure_message", +) |