xref: /aosp_15_r20/external/bazel-skylib/rules/analysis_test.bzl (revision bcb5dc7965af6ee42bf2f21341a2ec00233a8c8a)
1# Copyright 2019 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
15"""A test verifying other targets can be successfully analyzed as part of a `bazel test`"""
16
17def _analysis_test_impl(ctx):
18    """Implementation function for analysis_test. """
19    _ignore = [ctx]  # @unused
20    return [AnalysisTestResultInfo(
21        success = True,
22        message = "All targets succeeded analysis",
23    )]
24
25analysis_test = rule(
26    _analysis_test_impl,
27    attrs = {"targets": attr.label_list(mandatory = True)},
28    test = True,
29    analysis_test = True,
30    doc = """Test rule checking that other targets can be successfully analyzed.
31
32    This rule essentially verifies that all targets under `targets` would
33    generate no errors when analyzed with `bazel build [targets] --nobuild`.
34    Action success/failure for the targets and their transitive dependencies
35    are not verified. An analysis test simply ensures that each target in the transitive
36    dependencies propagate providers appropriately and register actions for their outputs
37    appropriately.
38
39    NOTE: If the targets fail to analyze, instead of the analysis_test failing, the analysis_test
40    will fail to build. Ideally, it would instead result in a test failure. This is a current
41    infrastructure limitation that may be fixed in the future.
42
43    Typical usage:
44
45      load("@bazel_skylib//rules:analysis_test.bzl", "analysis_test")
46      analysis_test(
47          name = "my_analysis_test",
48          targets = [
49              "//some/package:rule",
50          ],
51      )
52
53    Args:
54      name: The name of the test rule.
55      targets: A list of targets to ensure build.""",
56)
57