xref: /aosp_15_r20/external/bazelbuild-rules_android/test/rules/android_local_test/test.bzl (revision 9e965d6fece27a77de5377433c2f7e6999b8cc0b)
1# Copyright 2018 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"""Bazel rules that test the Android Local Test rule.
16
17rule_test: Inspect and assert on rule providers.
18"""
19
20load("//rules:providers.bzl", "AndroidFilteredJdepsInfo")
21load("//test/utils:asserts.bzl", "asserts")
22
23VALIDATION = "_validation"
24
25def _rule_test_impl(ctx):
26    # Assert on expected providers
27    if not JavaInfo in ctx.attr.target_under_test:
28        fail("Missing JavaInfo provider")
29    if not AndroidFilteredJdepsInfo in ctx.attr.target_under_test:
30        fail("Missing AndroidFilteredJdepsInfo provider")
31
32    # Collecting validation outputs from deps
33    transitive_validation_outputs = []
34    for dep in ctx.attr.deps:
35        if hasattr(dep[OutputGroupInfo], VALIDATION):
36            transitive_validation_outputs.append(dep[OutputGroupInfo]._validation)
37
38    output_group_info = dict(ctx.attr.expected_output_group_info)
39    if VALIDATION in output_group_info:
40        output_group_info[VALIDATION] = (
41            output_group_info[VALIDATION] +
42            [f.basename for f in depset(transitive = transitive_validation_outputs).to_list()]
43        )
44    asserts.provider.output_group_info(
45        output_group_info,
46        ctx.attr.target_under_test[OutputGroupInfo],
47    )
48
49    # Create test script to assert on provider contents
50    args = dict(
51        jdeps_print_tool = ctx.executable._jdeps_print_tool.short_path,
52        jdeps = ctx.attr.target_under_test[JavaInfo].outputs.jdeps.short_path,
53        filtered_jdeps = ctx.attr.target_under_test[AndroidFilteredJdepsInfo].jdeps.short_path,
54        res_jar = ctx.attr.target_under_test.label.name + "_resources.jar",
55        expect_resources = str(ctx.attr.expect_resources),
56    )
57    test_script = ctx.actions.declare_file("%s_script.sh" % ctx.label.name)
58    ctx.actions.write(
59        test_script,
60        """
61jdeps=`{jdeps_print_tool} --in {jdeps} | sed 's#_migrated/##g'`
62filtered_jdeps=`{jdeps_print_tool} --in {filtered_jdeps} | sed 's#_migrated/##g'`
63
64entries=`echo "$jdeps" | wc -l`
65matches=`echo "$jdeps" | grep '{res_jar}' | wc -l`
66filtered_entries=`echo "$filtered_jdeps" | wc -l`
67filtered_matches=`echo "$filtered_jdeps" | grep '{res_jar}' | wc -l`
68
69expected_matches=1
70expected_filtering_differences=1
71if [ {expect_resources} == "False" ]; then
72  expected_matches=0
73  expected_filtering_differences=0
74fi
75
76if [ $matches -ne $expected_matches ]; then
77  echo "Expected one resource.jar in jdeps"
78  exit 1
79elif [ $filtered_matches -ne 0 ]; then
80  echo "Expected no resource.jar in filtered jdeps"
81  exit 1
82elif [ $(($entries-$filtered_entries)) -ne $expected_filtering_differences ]; then
83  echo "Expected to remove one item when filtering"
84  exit 1
85fi
86""".format(**args),
87        is_executable = True,
88    )
89    return [
90        DefaultInfo(
91            runfiles = ctx.runfiles(
92                files = [
93                    test_script,
94                    ctx.executable._jdeps_print_tool,
95                    ctx.attr.target_under_test[JavaInfo].outputs.jdeps,
96                    ctx.attr.target_under_test[AndroidFilteredJdepsInfo].jdeps,
97                ],
98            ),
99            executable = test_script,
100        ),
101    ]
102
103rule_test = rule(
104    attrs = dict(
105        asserts.provider.attrs.items(),
106        expect_resources = attr.bool(default = True),
107        target_under_test = attr.label(),
108        deps = attr.label_list(),
109        _jdeps_print_tool = attr.label(
110            cfg = "exec",
111            default = "//src/tools/jdeps:print_jdeps",
112            executable = True,
113        ),
114    ),
115    implementation = _rule_test_impl,
116    test = True,
117)
118