xref: /aosp_15_r20/build/bazel/rules/aidl/aidl_library_test.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1"""Copyright (C) 2022 The Android Open Source Project
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7     http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14"""
15
16load("@bazel_skylib//lib:paths.bzl", "paths")
17load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
18load(":aidl_library.bzl", "AidlGenInfo", "aidl_library")
19
20PACKAGE_ROOT = "build/bazel/rules/aidl"
21
22def _test_include_dirs_are_transitive_impl(ctx):
23    env = analysistest.begin(ctx)
24    target_under_test = analysistest.target_under_test(env)
25
26    asserts.equals(
27        env,
28        expected = [
29            # direct include dir is the first in the list returned from
30            # transitive_include_dirs.to_list() because transitive_include_dir
31            # is created with preorder
32            # TODO(b/243825300): Move direct include_dir out of transitive_include_dir
33            # so that we don't have to rely on preorder traversal
34            paths.join(ctx.genfiles_dir.path, PACKAGE_ROOT, "_virtual_imports", "include_dirs_transitivity"),
35            paths.join(ctx.genfiles_dir.path, PACKAGE_ROOT, "_virtual_imports", "include_dirs_transitivity_dependency"),
36        ],
37        actual = target_under_test[AidlGenInfo].transitive_include_dirs.to_list(),
38    )
39
40    return analysistest.end(env)
41
42include_dirs_are_transitive_test = analysistest.make(_test_include_dirs_are_transitive_impl)
43
44def _test_include_dirs_transitivity():
45    test_base_name = "include_dirs_transitivity"
46    test_name = test_base_name + "_test"
47    aidl_dep = test_base_name + "_dependency"
48    aidl_library(
49        name = test_base_name,
50        strip_import_prefix = "testing",
51        deps = [":" + aidl_dep],
52        tags = ["manual"],
53    )
54    aidl_library(
55        name = aidl_dep,
56        strip_import_prefix = "testing2",
57        tags = ["manual"],
58    )
59    include_dirs_are_transitive_test(
60        name = test_name,
61        target_under_test = test_base_name,
62    )
63    return [
64        test_name,
65    ]
66
67def _test_empty_srcs_aggregates_deps_impl(ctx):
68    env = analysistest.begin(ctx)
69    target_under_test = analysistest.target_under_test(env)
70
71    asserts.equals(
72        env,
73        expected = [],
74        actual = target_under_test[AidlGenInfo].srcs.to_list(),
75    )
76
77    import_path = paths.join(
78        PACKAGE_ROOT,
79        "_virtual_imports",
80    )
81
82    asserts.equals(
83        env,
84        expected = [
85            paths.join(import_path, target_under_test.label.name + "_dependency2", "b.aidl"),
86            paths.join(import_path, target_under_test.label.name + "_dependency2", "header_b.aidl"),
87            paths.join(import_path, target_under_test.label.name + "_dependency3", "c.aidl"),
88            paths.join(import_path, target_under_test.label.name + "_dependency3", "header_c.aidl"),
89            paths.join(import_path, target_under_test.label.name + "_dependency1", "a.aidl"),
90            paths.join(import_path, target_under_test.label.name + "_dependency1", "header_a.aidl"),
91        ],
92        actual = [
93            file.short_path
94            for file in target_under_test[AidlGenInfo].transitive_srcs.to_list()
95        ],
96    )
97
98    return analysistest.end(env)
99
100empty_srcs_aggregates_deps_test = analysistest.make(_test_empty_srcs_aggregates_deps_impl)
101
102def _test_hdrs_are_only_in_transitive_srcs_impl(ctx):
103    env = analysistest.begin(ctx)
104    target_under_test = analysistest.target_under_test(env)
105
106    import_path = paths.join(
107        PACKAGE_ROOT,
108        "_virtual_imports",
109        target_under_test.label.name,
110    )
111
112    asserts.equals(
113        env,
114        expected = [
115            paths.join(import_path, "direct.aidl"),
116        ],
117        actual = [
118            file.short_path
119            for file in target_under_test[AidlGenInfo].srcs.to_list()
120        ],
121    )
122
123    asserts.equals(
124        env,
125        expected = [
126            paths.join(import_path, "header_direct.aidl"),
127        ],
128        actual = [
129            file.short_path
130            for file in target_under_test[AidlGenInfo].hdrs.to_list()
131        ],
132    )
133
134    return analysistest.end(env)
135
136hdrs_are_only_in_transitive_srcs_test = analysistest.make(_test_hdrs_are_only_in_transitive_srcs_impl)
137
138def _test_transitive_srcs_contains_direct_and_transitive_srcs_impl(ctx):
139    env = analysistest.begin(ctx)
140    target_under_test = analysistest.target_under_test(env)
141
142    import_path = paths.join(
143        PACKAGE_ROOT,
144        "_virtual_imports",
145    )
146
147    asserts.equals(
148        env,
149        expected = [
150            paths.join(import_path, target_under_test.label.name, "direct.aidl"),
151        ],
152        actual = [
153            file.short_path
154            for file in target_under_test[AidlGenInfo].srcs.to_list()
155        ],
156    )
157
158    asserts.equals(
159        env,
160        expected = [
161            paths.join(import_path, target_under_test.label.name + "_dependency2", "b.aidl"),
162            paths.join(import_path, target_under_test.label.name + "_dependency2", "header_b.aidl"),
163            paths.join(import_path, target_under_test.label.name + "_dependency3", "c.aidl"),
164            paths.join(import_path, target_under_test.label.name + "_dependency3", "header_c.aidl"),
165            paths.join(import_path, target_under_test.label.name + "_dependency1", "a.aidl"),
166            paths.join(import_path, target_under_test.label.name + "_dependency1", "header_a.aidl"),
167            paths.join(import_path, target_under_test.label.name, "direct.aidl"),
168            paths.join(import_path, target_under_test.label.name, "header_direct.aidl"),
169        ],
170        actual = [
171            file.short_path
172            for file in target_under_test[AidlGenInfo].transitive_srcs.to_list()
173        ],
174    )
175
176    return analysistest.end(env)
177
178transitive_srcs_contains_direct_and_transitive_srcs_test = analysistest.make(
179    _test_transitive_srcs_contains_direct_and_transitive_srcs_impl,
180)
181
182def _generate_test_targets(name):
183    aidl_dep1 = name + "_dependency1"
184    aidl_dep2 = name + "_dependency2"
185    aidl_dep3 = name + "_dependency3"
186    aidl_library(
187        name = aidl_dep1,
188        srcs = ["a.aidl"],
189        hdrs = ["header_a.aidl"],
190        deps = [
191            ":" + aidl_dep2,
192            ":" + aidl_dep3,
193        ],
194        tags = ["manual"],
195    )
196    aidl_library(
197        name = aidl_dep2,
198        srcs = ["b.aidl"],
199        hdrs = ["header_b.aidl"],
200        tags = ["manual"],
201    )
202    aidl_library(
203        name = aidl_dep3,
204        srcs = ["c.aidl"],
205        hdrs = ["header_c.aidl"],
206        tags = ["manual"],
207    )
208    return aidl_dep1
209
210def _test_empty_srcs_aggregates_deps():
211    test_base_name = "empty_srcs_aggregates_deps"
212    test_name = test_base_name + "_test"
213
214    aidl_dep1 = _generate_test_targets(test_base_name)
215    aidl_library(
216        name = test_base_name,
217        deps = [":" + aidl_dep1],
218        tags = ["manual"],
219    )
220    empty_srcs_aggregates_deps_test(
221        name = test_name,
222        target_under_test = test_base_name,
223    )
224    return [
225        test_name,
226    ]
227
228def _test_transitive_srcs_contains_direct_and_transitive_srcs():
229    test_base_name = "transitive_srcs_contains_direct_and_transitive_srcs"
230    srcs_test_name = test_base_name + "_srcs"
231    hdrs_test_name = test_base_name + "_hdrs"
232
233    aidl_dep1 = _generate_test_targets(test_base_name)
234    aidl_library(
235        name = test_base_name,
236        srcs = ["direct.aidl"],
237        hdrs = ["header_direct.aidl"],
238        deps = [":" + aidl_dep1],
239        tags = ["manual"],
240    )
241    transitive_srcs_contains_direct_and_transitive_srcs_test(
242        name = srcs_test_name,
243        target_under_test = test_base_name,
244    )
245    hdrs_are_only_in_transitive_srcs_test(
246        name = hdrs_test_name,
247        target_under_test = test_base_name,
248    )
249    return [
250        srcs_test_name,
251        hdrs_test_name,
252    ]
253
254def aidl_library_test_suite(name):
255    native.test_suite(
256        name = name,
257        tests = _test_include_dirs_transitivity() +
258                _test_transitive_srcs_contains_direct_and_transitive_srcs() +
259                _test_empty_srcs_aggregates_deps(),
260    )
261