xref: /aosp_15_r20/build/bazel/rules/apex/apex_mk_test.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1# Copyright (C) 2022 The Android Open Source Project
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
15load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
16load("//build/bazel/rules:prebuilt_file.bzl", "prebuilt_file")
17load("//build/bazel/rules:sh_binary.bzl", "sh_binary")
18load("//build/bazel/rules/cc:cc_binary.bzl", "cc_binary")
19load("//build/bazel/rules/cc:cc_library_shared.bzl", "cc_library_shared")
20load(":apex_info.bzl", "ApexMkInfo")
21load(":apex_test_helpers.bzl", "test_apex")
22
23def _apex_files_info_test(ctx):
24    env = analysistest.begin(ctx)
25    target = analysistest.target_under_test(env)
26
27    # no attr.string_keyed_string_dict_list.. so we'll have to make do :-)
28    expected_files_info = [json.decode(i) for i in ctx.attr.expected_files_info]
29    actual_files_info = target[ApexMkInfo].files_info
30
31    asserts.equals(env, len(expected_files_info), len(actual_files_info))
32
33    for idx, expected in enumerate(expected_files_info):
34        actual = actual_files_info[idx]
35
36        asserts.equals(env, len(expected), len(actual))
37        for k, v in expected.items():
38            if k in ["built_file", "unstripped_built_file"]:
39                # don't test the part that contains the configuration hash, which is sensitive to changes.
40                expected_path_without_config = v.split("bazel-out/")[-1]
41                asserts.true(env, actual[k].endswith(expected_path_without_config))
42            else:
43                asserts.equals(env, v, actual[k])
44    return analysistest.end(env)
45
46apex_files_info_test = analysistest.make(
47    _apex_files_info_test,
48    attrs = {
49        "expected_files_info": attr.string_list(
50            doc = "expected files info",
51        ),
52    },
53)
54
55def _test_apex_files_info_basic():
56    name = "apex_files_info_basic"
57    test_name = name + "_test"
58
59    test_apex(name = name)
60
61    apex_files_info_test(
62        name = test_name,
63        target_under_test = name,
64        expected_files_info = [
65            # deliberately empty.
66        ],
67    )
68
69    return test_name
70
71def _test_apex_files_info_complex():
72    name = "apex_files_info_complex"
73    test_name = name + "_test"
74
75    prebuilt_file(
76        name = name + "_file",
77        src = name + "_file.txt",
78        dir = "etc",
79        tags = ["manual"],
80    )
81
82    sh_binary(
83        name = name + "_bin_sh",
84        srcs = [name + "_bin.sh"],
85        tags = ["manual"],
86    )
87
88    cc_binary(
89        name = name + "_bin_cc",
90        srcs = [name + "_bin.cc"],
91        tags = ["manual"],
92    )
93
94    cc_library_shared(
95        name = name + "_lib_cc",
96        srcs = [name + "_lib.cc"],
97        tags = ["manual"],
98    )
99
100    cc_library_shared(
101        name = name + "_lib2_cc",
102        srcs = [name + "_lib2.cc"],
103        tags = ["manual"],
104    )
105
106    test_apex(
107        name = name,
108        binaries = [name + "_bin_sh", name + "_bin_cc"],
109        prebuilts = [name + "_file"],
110        native_shared_libs_32 = [name + "_lib_cc"],
111        native_shared_libs_64 = [name + "_lib2_cc"],
112    )
113
114    apex_files_info_test(
115        name = test_name,
116        target_under_test = name,
117        target_compatible_with = ["//build/bazel_common_rules/platforms/os:android", "//build/bazel_common_rules/platforms/arch:arm64"],
118        expected_files_info = [json.encode(i) for i in [
119            {
120                "built_file": "bazel-out/bin/build/bazel/rules/apex/bin/apex_files_info_complex_bin_cc/apex_files_info_complex_bin_cc",
121                "class": "nativeExecutable",
122                "install_dir": "bin",
123                "basename": "apex_files_info_complex_bin_cc",
124                "package": "build/bazel/rules/apex",
125                "make_module_name": "apex_files_info_complex_bin_cc",
126                "arch": "arm64",
127                "unstripped_built_file": "bazel-out/build/bazel/rules/apex/apex_files_info_complex_bin_cc_unstripped",
128            },
129            {
130                "built_file": "bazel-out/bin/build/bazel/rules/apex/apex_files_info_complex_bin_sh",
131                "class": "shBinary",
132                "install_dir": "bin",
133                "basename": "apex_files_info_complex_bin_sh",
134                "package": "build/bazel/rules/apex",
135                "make_module_name": "apex_files_info_complex_bin_sh",
136                "arch": "arm64",
137            },
138            {
139                "built_file": "build/bazel/rules/apex/apex_files_info_complex_file.txt",
140                "class": "etc",
141                "install_dir": "etc",
142                "basename": "apex_files_info_complex_file",
143                "package": "build/bazel/rules/apex",
144                "make_module_name": "apex_files_info_complex_file",
145                "arch": "arm64",
146            },
147            {
148                "built_file": "bazel-out/bin/build/bazel/rules/apex/apex_files_info_complex_lib2_cc/apex_files_info_complex_lib2_cc.so",
149                "class": "nativeSharedLib",
150                "install_dir": "lib64",
151                "basename": "apex_files_info_complex_lib2_cc.so",
152                "package": "build/bazel/rules/apex",
153                "make_module_name": "apex_files_info_complex_lib2_cc",
154                "arch": "arm64",
155                "unstripped_built_file": "bazel-out/bin/build/bazel/rules/apex/libapex_files_info_complex_lib2_cc_unstripped.so",
156            },
157            {
158                "built_file": "bazel-out/bin/build/bazel/rules/apex/apex_files_info_complex_lib_cc/apex_files_info_complex_lib_cc.so",
159                "class": "nativeSharedLib",
160                "install_dir": "lib",
161                "basename": "apex_files_info_complex_lib_cc.so",
162                "package": "build/bazel/rules/apex",
163                "make_module_name": "apex_files_info_complex_lib_cc",
164                "arch": "arm",
165                "unstripped_built_file": "bazel-out/bin/build/bazel/rules/apex/libapex_files_info_complex_lib_cc_unstripped.so",
166            },
167            {
168                "built_file": "bazel-out/bin/external/libcxx/libc++/libc++.so",
169                "class": "nativeSharedLib",
170                "install_dir": "lib",
171                "basename": "libc++.so",
172                "package": "external/libcxx",
173                "make_module_name": "libc++",
174                "arch": "arm",
175                "unstripped_built_file": "bazel-out/bin/external/libcxx/liblibc++_unstripped.so",
176            },
177            {
178                "built_file": "bazel-out/bin/external/libcxx/libc++/libc++.so",
179                "class": "nativeSharedLib",
180                "install_dir": "lib64",
181                "basename": "libc++.so",
182                "package": "external/libcxx",
183                "make_module_name": "libc++",
184                "arch": "arm64",
185                "unstripped_built_file": "bazel-out/bin/external/libcxx/liblibc++_unstripped.so",
186            },
187        ]],
188    )
189
190    return test_name
191
192def apex_mk_test_suite(name):
193    native.test_suite(
194        name = name,
195        tests = [
196            _test_apex_files_info_basic(),
197            _test_apex_files_info_complex(),
198        ],
199    )
200