1"""Copyright (C) 2023 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("@rules_testing//lib:analysis_test.bzl", "analysis_test", rt_test_suite = "test_suite")
17load("@rules_testing//lib:truth.bzl", "matching")
18load("@rules_testing//lib:util.bzl", "util")
19load("//build/bazel/rules/aconfig:aconfig_declarations.bzl", "aconfig_declarations")
20load("//build/bazel/rules/aconfig:aconfig_value_set.bzl", "aconfig_value_set")
21load("//build/bazel/rules/aconfig:aconfig_values.bzl", "aconfig_values")
22load("//build/bazel/rules/java:java_aconfig_library.bzl", "java_aconfig_library")
23load(
24    "//build/bazel/rules/test_common:flags.bzl",
25    "action_flags_present_for_mnemonic_nonexclusive_test",
26    "input_output_verification_test",
27)
28
29def test_java_aconfig_library_action():
30    name = "java_aconfig_library_action"
31    package = "com.android.aconfig.test"
32    aconfig_declarations_name = name + "_aconfig_declarations"
33    target_under_test = name
34
35    aconfig_value_set(
36        name = "aconfig.test.value_set",
37        values = [":aconfig.test.values"],
38        tags = ["manual"],
39    )
40
41    aconfig_values(
42        name = "aconfig.test.values",
43        package = package,
44        srcs = [
45            "test.textproto",
46        ],
47        tags = ["manual"],
48    )
49
50    aconfig_declarations(
51        name = aconfig_declarations_name,
52        package = package,
53        srcs = ["test.aconfig"],
54        tags = ["manual"],
55    )
56
57    java_aconfig_library(
58        name = name,
59        aconfig_declarations = ":" + aconfig_declarations_name,
60        target_compatible_with = ["//build/bazel_common_rules/platforms/os:android"],
61        tags = ["manual"],
62    )
63
64    test_name_compile_flags = name + "_test_compile_flags"
65    action_flags_present_for_mnemonic_nonexclusive_test(
66        name = test_name_compile_flags,
67        target_under_test = target_under_test,
68        mnemonics = [
69            "AconfigCreateJavaLib",
70        ],
71        expected_flags = [
72            "create-java-lib",
73            "--cache",
74            "--out",
75            "--mode",
76        ],
77    )
78
79    test_name_zip_flags = name + "_test_zip_flags"
80    action_flags_present_for_mnemonic_nonexclusive_test(
81        name = test_name_zip_flags,
82        target_under_test = target_under_test,
83        mnemonics = [
84            "AconfigZipJavaLib",
85        ],
86        expected_flags = [
87            "-write_if_changed",
88            "-jar",
89            "-o",
90            "-C",
91            "-D",
92            "-symlinks=false",
93        ],
94    )
95
96    test_name_compile_input_output = name + "_test_compile_input_output"
97    input_output_verification_test(
98        name = test_name_compile_input_output,
99        target_under_test = target_under_test,
100        mnemonic = "AconfigCreateJavaLib",
101        input_files = [
102            "java_aconfig_library_action_aconfig_declarations/intermediate.pb",
103        ],
104        output_files = [
105            "java_aconfig_library_action/gen/tmp",
106        ],
107    )
108
109    test_name_zip_input_output = name + "_test_zip_input_output"
110    input_output_verification_test(
111        name = test_name_zip_input_output,
112        target_under_test = target_under_test,
113        mnemonic = "AconfigZipJavaLib",
114        input_files = [
115            "java_aconfig_library_action/gen/tmp",
116        ],
117        output_files = [
118            "java_aconfig_library_action/gen/java_aconfig_library_action.srcjar",
119        ],
120    )
121
122    return [
123        test_name_compile_flags,
124        test_name_zip_flags,
125        test_name_compile_input_output,
126        test_name_zip_input_output,
127    ]
128
129def _test_java_aconfig_library_rule_rt(name):
130    aconfig_declarations_name = name + "_aconfig_declarations"
131    package = "com.android.aconfig.test"
132    target = name + "_target"
133
134    util.helper_target(
135        aconfig_declarations,
136        name = aconfig_declarations_name,
137        package = package,
138        srcs = ["test.aconfig"],
139        tags = ["manual"],
140    )
141    util.helper_target(
142        java_aconfig_library,
143        name = target,
144        aconfig_declarations = ":" + aconfig_declarations_name,
145        target_compatible_with = ["//build/bazel_common_rules/platforms/os:android"],
146        tags = ["manual"],
147    )
148    analysis_test(
149        name = name,
150        impl = _test_java_aconfig_library_rule_rt_impl,
151        target = target,
152    )
153
154def _test_java_aconfig_library_rule_rt_impl(env, target):
155    for mnemonic in [
156        "AconfigCreateJavaLib",
157        "AconfigZipJavaLib",
158    ]:
159        env.expect.that_target(target).action_named(mnemonic).mnemonic().equals(mnemonic)
160
161    env.expect.that_target(target).default_outputs().contains_predicate(
162        matching.file_basename_equals(target.label.name + ".jar"),
163    )
164
165    # Providers
166    env.expect.that_target(target).has_provider(JavaInfo)
167    env.expect.that_target(target).output_group("srcjar").contains_predicate(
168        matching.file_basename_equals(target.label.name + ".srcjar"),
169    )
170
171def java_aconfig_library_test_suite(name):
172    native.test_suite(
173        name = name,
174        tests = test_java_aconfig_library_action(),
175    )
176
177def java_aconfig_library_rt_test_suite(name):
178    rt_test_suite(
179        name = name,
180        tests = [_test_java_aconfig_library_rule_rt],
181    )
182