xref: /aosp_15_r20/external/bazelbuild-kotlin-rules/kotlin/jvm/testing/jvm_library_analysis_test.bzl (revision 3a22c0a33dd99bcca39a024d43e6fbcc55c2806e)
1# Copyright 2022 Google LLC. All rights reserved.
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
15"""kt_jvm_library_analysis_test"""
16
17load("//:visibility.bzl", "RULES_KOTLIN")
18load("//kotlin/common/testing:analysis.bzl", "kt_analysis")
19load("//kotlin/common/testing:asserts.bzl", "kt_asserts")
20load("@bazel_skylib//lib:sets.bzl", "sets")
21load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
22
23visibility(RULES_KOTLIN)
24
25kt_jvm_library_analysis_test = analysistest.make(
26    impl = lambda ctx: _kt_jvm_library_analysis_test_impl(ctx),
27    attrs = dict(
28        expected_al_ruleset_names = attr.string_list(
29            doc = "Android Lint rule JARs reported as run on the given target",
30            default = kt_analysis.DEFAULT_LIST,
31        ),
32        expected_compile_jar_names = attr.string_list(
33            doc = "Names of all JavaInfo::compile_jars for the given target",
34            default = kt_analysis.DEFAULT_LIST,
35        ),
36        expected_exported_processor_jar_names = attr.string_list(
37            doc = "Names of all JavaInfo::plugins JARs returned by the given target",
38            default = kt_analysis.DEFAULT_LIST,
39        ),
40        expected_exported_processor_classes = attr.string_list(
41            doc = "Annotation processors reported as to be run on depending targets",
42        ),
43        expected_processor_classes = attr.string_list(
44            doc = "Annotation processors reported as run on the given target",
45        ),
46        expected_kotlinc_plugin_jar_names = attr.string_list(
47            doc = "Names of all -Xplugin= JARs",
48            default = kt_analysis.DEFAULT_LIST,
49        ),
50        expected_friend_jar_names = attr.string_list(
51            doc = "Names of all -Xfriend-paths= JARs",
52            default = kt_analysis.DEFAULT_LIST,
53        ),
54        expected_runfile_names = attr.string_list(
55            doc = "Names of all runfiles",
56            default = kt_analysis.DEFAULT_LIST,
57        ),
58        expect_jdeps = attr.bool(default = True),
59        expect_processor_classpath = attr.bool(),
60        expect_neverlink = attr.bool(),
61        required_mnemonic_counts = attr.string_dict(
62            doc = "Expected mnemonics to expected action count; unlisted mnemonics are ignored",
63        ),
64    ),
65)
66
67def _kt_jvm_library_analysis_test_impl(ctx):
68    kt_analysis.check_endswith_test(ctx)
69
70    env = analysistest.begin(ctx)
71    actual = ctx.attr.target_under_test
72
73    actions = analysistest.target_actions(env)
74    kt_al_action = kt_analysis.get_action(actions, "KtAndroidLint")
75
76    asserts.true(
77        env,
78        JavaInfo in actual,
79        "kt_jvm_library did not produce JavaInfo provider.",
80    )
81    asserts.true(
82        env,
83        ProguardSpecProvider in actual,
84        "Expected a ProguardSpecProvider provider.",
85    )
86
87    if ctx.attr.expected_runfile_names != kt_analysis.DEFAULT_LIST:
88        asserts.set_equals(
89            env,
90            sets.make(ctx.attr.expected_runfile_names),
91            sets.make([
92                f.basename
93                for f in actual[DefaultInfo].data_runfiles.files.to_list()
94            ]),
95        )
96
97    if ctx.attr.expected_compile_jar_names != kt_analysis.DEFAULT_LIST:
98        asserts.set_equals(
99            env,
100            sets.make(ctx.attr.expected_compile_jar_names),
101            sets.make([f.basename for f in actual[JavaInfo].compile_jars.to_list()]),
102            "kt_jvm_library JavaInfo::compile_jars",
103        )
104
105    if ctx.attr.expected_exported_processor_jar_names != kt_analysis.DEFAULT_LIST:
106        asserts.set_equals(
107            env,
108            sets.make(ctx.attr.expected_exported_processor_jar_names),
109            sets.make([f.basename for f in actual[JavaInfo].plugins.processor_jars.to_list()]),
110        )
111
112    asserts.set_equals(
113        env,
114        sets.make(ctx.attr.expected_exported_processor_classes),
115        sets.make(actual[JavaInfo].plugins.processor_classes.to_list()),
116    )
117
118    kt_2_java_compile = kt_analysis.get_action(actions, "Kt2JavaCompile")
119
120    if kt_2_java_compile:
121        asserts.true(
122            env,
123            kt_2_java_compile.outputs.to_list()[0].basename.endswith(".jar"),
124            "Expected first output to be a JAR (this affects the param file name).",
125        )
126
127    if ctx.attr.expected_friend_jar_names != kt_analysis.DEFAULT_LIST:
128        friend_paths_arg = kt_analysis.get_arg(kt_2_java_compile, "-Xfriend-paths=")
129        kt_asserts.list_matches(
130            env,
131            expected = ["/" + x for x in ctx.attr.expected_friend_jar_names],
132            actual = friend_paths_arg.split(",") if friend_paths_arg else [],
133            matcher = lambda expected, actual: actual.endswith(expected),
134            items_name = "friend JARs",
135        )
136
137    if ctx.attr.expected_kotlinc_plugin_jar_names != kt_analysis.DEFAULT_LIST:
138        kt_asserts.list_matches(
139            env,
140            expected = ["/" + x for x in ctx.attr.expected_kotlinc_plugin_jar_names],
141            actual = kt_analysis.get_all_args(kt_2_java_compile, "-Xplugin="),
142            matcher = lambda expected, actual: actual.endswith(expected),
143            items_name = "kotlinc plugin JARs",
144        )
145
146    asserts.equals(
147        env,
148        ctx.attr.expect_neverlink,
149        len(actual[JavaInfo].transitive_runtime_jars.to_list()) == 0,
150        "Mismatch: Expected transitive_runtime_jars iff (neverlink == False)",
151    )
152
153    kt_asserts.required_mnemonic_counts(env, ctx.attr.required_mnemonic_counts, actions)
154
155    return analysistest.end(env)
156