xref: /aosp_15_r20/external/bazelbuild-rules_cc/tests/rule_based_toolchain/analysis_test_suite.bzl (revision eed53cd41c5909d05eedc7ad9720bb158fd93452)
1# Copyright 2024 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Test suites for the rule based toolchain."""
15
16load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
17load(":subjects.bzl", "FACTORIES")
18
19visibility("//tests/rule_based_toolchain/...")
20
21_DEFAULT_TARGET = "//tests/rule_based_toolchain/actions:c_compile"
22
23# Tests of internal starlark functions will often not require any targets,
24# but analysis_test requires at least one, so we pick an arbitrary one.
25def analysis_test_suite(name, tests, targets = [_DEFAULT_TARGET]):
26    """A test suite for the internals of the toolchain.
27
28    Args:
29      name: (str) The name of the test suite.
30      tests: (dict[str, fn]) A mapping from test name to implementations.
31      targets: (List[Label|str]) List of targets accessible to the test.
32    """
33    targets = [native.package_relative_label(target) for target in targets]
34
35    test_case_names = []
36    for test_name, impl in tests.items():
37        if not test_name.endswith("_test"):
38            fail("Expected test keys to end with '_test', got test case %r" % test_name)
39        test_case_names.append(":" + test_name)
40        analysis_test(
41            name = test_name,
42            impl = impl,
43            provider_subject_factories = FACTORIES,
44            targets = {label.name: label for label in targets},
45        )
46
47    native.test_suite(
48        name = name,
49        tests = test_case_names,
50    )
51