xref: /aosp_15_r20/external/wayland-protocols/bazel/gensrcs_test.bzl (revision 6c119a463dd5c45dd05bbe67429293292dde15ee)
1"""
2Copyright 2023 The Android Open Source Project
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15"""
16
17load("@bazel_skylib//lib:new_sets.bzl", "sets")
18load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
19load("@bazel_skylib//lib:paths.bzl", "paths")
20load(":gensrcs.bzl", "gensrcs")
21
22SRCS = [
23    "texts/src1.txt",
24    "texts/src2.txt",
25    "src3.txt",
26]
27
28# ==== Check the output paths created by gensrcs ====
29
30def _test_output_path_expansion_impl(ctx):
31    env = analysistest.begin(ctx)
32    target = analysistest.target_under_test(env)
33    actions = analysistest.target_actions(env)
34
35    # Expect an action for each input/output file pair.
36    asserts.equals(
37        env,
38        expected = len(ctx.attr.expected_outputs),
39        actual = len(actions),
40    )
41
42    # Expect the correct set of output files.
43    asserts.set_equals(
44        env,
45        expected = sets.make([
46            paths.join(
47                ctx.genfiles_dir.path,
48                paths.dirname(ctx.build_file_path),
49                out,
50            )
51            for out in ctx.attr.expected_outputs
52        ]),
53        actual = sets.make(
54            [file.path for file in target.files.to_list()],
55        ),
56    )
57
58    return analysistest.end(env)
59
60output_path_expansion_test = analysistest.make(
61    _test_output_path_expansion_impl,
62    attrs = {
63        "expected_outputs": attr.string_list(
64            doc = "The expected list of output files",
65        ),
66    },
67)
68
69def _test_output_expansion_base():
70    name = "gensrcs_output_expansion_base"
71    test_name = name + "_test"
72
73    gensrcs(
74        name = name,
75        cmd = "cat $(SRC) > $(OUT)",
76        srcs = SRCS,
77        output = "prefix_$(SRC:BASE)_suffix",
78        tags = ["manual"],  # make sure it's not built using `:all`
79    )
80
81    output_path_expansion_test(
82        name = test_name,
83        target_under_test = name,
84        expected_outputs = [
85            "prefix_src1_suffix",
86            "prefix_src2_suffix",
87            "prefix_src3_suffix",
88        ],
89    )
90    return test_name
91
92def _test_output_expansion_base_ext():
93    name = "gensrcs_output_expansion_base_ext"
94    test_name = name + "_test"
95
96    gensrcs(
97        name = name,
98        cmd = "cat $(SRC) > $(OUT)",
99        srcs = SRCS,
100        output = "prefix_$(SRC:BASE.EXT)_suffix",
101        tags = ["manual"],  # make sure it's not built using `:all`
102    )
103
104    output_path_expansion_test(
105        name = test_name,
106        target_under_test = name,
107        expected_outputs = [
108            "prefix_src1.txt_suffix",
109            "prefix_src2.txt_suffix",
110            "prefix_src3.txt_suffix",
111        ],
112    )
113    return test_name
114
115def _test_output_expansion_path_base():
116    name = "gensrcs_output_expansion_path_base"
117    test_name = name + "_test"
118
119    gensrcs(
120        name = name,
121        cmd = "cat $(SRC) > $(OUT)",
122        srcs = SRCS,
123        output = "prefix_$(SRC:PATH/BASE)_suffix",
124        tags = ["manual"],  # make sure it's not built using `:all`
125    )
126
127    output_path_expansion_test(
128        name = test_name,
129        target_under_test = name,
130        expected_outputs = [
131            "prefix_texts/src1_suffix",
132            "prefix_texts/src2_suffix",
133            "prefix_src3_suffix",
134        ],
135    )
136    return test_name
137
138def _test_output_expansion_path_base_ext():
139    name = "gensrcs_output_expansion_path_base_ext"
140    test_name = name + "_test"
141
142    gensrcs(
143        name = name,
144        cmd = "cat $(SRC) > $(OUT)",
145        srcs = SRCS,
146        output = "prefix_$(SRC:PATH/BASE.EXT)_suffix",
147        tags = ["manual"],  # make sure it's not built using `:all`
148    )
149
150    output_path_expansion_test(
151        name = test_name,
152        target_under_test = name,
153        expected_outputs = [
154            "prefix_texts/src1.txt_suffix",
155            "prefix_texts/src2.txt_suffix",
156            "prefix_src3.txt_suffix",
157        ],
158    )
159    return test_name
160
161def _test_output_expansion_pkg_path_base():
162    name = "gensrcs_output_expansion_pkg_path_base"
163    test_name = name + "_test"
164
165    gensrcs(
166        name = name,
167        cmd = "cat $(SRC) > $(OUT)",
168        srcs = SRCS,
169        output = "prefix_$(SRC:PKG/PATH/BASE)_suffix",
170        tags = ["manual"],  # make sure it's not built using `:all`
171    )
172
173    output_path_expansion_test(
174        name = test_name,
175        target_under_test = name,
176        expected_outputs = [
177            "prefix_external/wayland-protocols/bazel/texts/src1_suffix",
178            "prefix_external/wayland-protocols/bazel/texts/src2_suffix",
179            "prefix_external/wayland-protocols/bazel/src3_suffix",
180        ],
181    )
182    return test_name
183
184def _test_output_expansion_pkg_path_base_ext():
185    name = "gensrcs_output_expansion_pkg_path_base_ext"
186    test_name = name + "_test"
187
188    gensrcs(
189        name = name,
190        cmd = "cat $(SRC) > $(OUT)",
191        srcs = SRCS,
192        output = "prefix_$(SRC:PKG/PATH/BASE.EXT)_suffix",
193        tags = ["manual"],  # make sure it's not built using `:all`
194    )
195
196    output_path_expansion_test(
197        name = test_name,
198        target_under_test = name,
199        expected_outputs = [
200            "prefix_external/wayland-protocols/bazel/texts/src1.txt_suffix",
201            "prefix_external/wayland-protocols/bazel/texts/src2.txt_suffix",
202            "prefix_external/wayland-protocols/bazel/src3.txt_suffix",
203        ],
204    )
205    return test_name
206
207def _test_output_expansion_default():
208    name = "gensrcs_output_expansion_default"
209    test_name = name + "_test"
210
211    gensrcs(
212        name = name,
213        cmd = "cat $(SRC) > $(OUT)",
214        srcs = SRCS,
215        tags = ["manual"],  # make sure it's not built using `:all`
216    )
217
218    output_path_expansion_test(
219        name = test_name,
220        target_under_test = name,
221        expected_outputs = [
222            "texts/src1.txt",
223            "texts/src2.txt",
224            "src3.txt",
225        ],
226    )
227    return test_name
228
229# ==== test suite ====
230
231def gensrcs_test_suite(name):
232    """Creates test targets for gensrcs.bzl"""
233    native.test_suite(
234        name = name,
235        tests = [
236            _test_output_expansion_base(),
237            _test_output_expansion_base_ext(),
238            _test_output_expansion_path_base(),
239            _test_output_expansion_path_base_ext(),
240            _test_output_expansion_pkg_path_base(),
241            _test_output_expansion_pkg_path_base_ext(),
242            _test_output_expansion_default(),
243        ],
244    )
245