xref: /aosp_15_r20/build/bazel/rules/java/platform_compat_config_test.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1# Copyright (C) 2023 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(":library.bzl", "java_library")
17load(":platform_compat_config.bzl", "PlatformCompatConfigInfo", "platform_compat_config")
18
19def _platform_compat_config_test_impl(ctx):
20    env = analysistest.begin(ctx)
21
22    actions = analysistest.target_actions(env)
23    target = analysistest.target_under_test(env)
24
25    asserts.equals(
26        env,
27        1,
28        len(actions),
29        "Incorrect number of actions",
30    )
31
32    asserts.true(
33        env,
34        PlatformCompatConfigInfo in target,
35        "Expected PlatformCompatConfigInfo in platform_compat_config providers.",
36    )
37
38    action_outputs = actions[0].outputs.to_list()
39
40    asserts.equals(
41        env,
42        2,
43        len(action_outputs),
44        "Incorrect number of action outputs",
45    )
46
47    asserts.equals(
48        env,
49        ctx.attr.expected_config_filename,
50        action_outputs[0].basename,
51    )
52
53    asserts.equals(
54        env,
55        ctx.attr.expected_metadata_filename,
56        action_outputs[1].basename,
57    )
58
59    return analysistest.end(env)
60
61_platform_compat_config_test = analysistest.make(
62    _platform_compat_config_test_impl,
63    attrs = {
64        "expected_config_filename": attr.string(),
65        "expected_metadata_filename": attr.string(),
66    },
67)
68
69def test_generates_correct_outputs():
70    name = "test_generates_correct_outputs"
71    target_name = name + "_target"
72    src_library_name = name + "_src"
73
74    platform_compat_config(
75        name = target_name,
76        src = src_library_name,
77    )
78    java_library(
79        name = src_library_name,
80        sdk_version = "current",
81    )
82
83    _platform_compat_config_test(
84        name = name,
85        target_under_test = target_name,
86        expected_config_filename = target_name + ".xml",
87        expected_metadata_filename = target_name + "_meta.xml",
88    )
89    return name
90
91def platform_compat_config_test_suite(name):
92    native.test_suite(
93        name = name,
94        tests = [
95            test_generates_correct_outputs(),
96        ],
97    )
98