aboutsummaryrefslogtreecommitdiff
path: root/kotlin/jvm/testing/jvm_library_analysis_test.bzl
blob: 6e05b286b87fb1e8d9fce1a35d06a5c752964f2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Copyright 2022 Google LLC. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the License);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""kt_jvm_library_analysis_test"""

load("//:visibility.bzl", "RULES_KOTLIN")
load("//kotlin/common/testing:analysis.bzl", "kt_analysis")
load("//kotlin/common/testing:asserts.bzl", "kt_asserts")
load("@bazel_skylib//lib:sets.bzl", "sets")
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")

visibility(RULES_KOTLIN)

kt_jvm_library_analysis_test = analysistest.make(
    impl = lambda ctx: _kt_jvm_library_analysis_test_impl(ctx),
    attrs = dict(
        expected_al_ruleset_names = attr.string_list(
            doc = "Android Lint rule JARs reported as run on the given target",
            default = kt_analysis.DEFAULT_LIST,
        ),
        expected_compile_jar_names = attr.string_list(
            doc = "Names of all JavaInfo::compile_jars for the given target",
            default = kt_analysis.DEFAULT_LIST,
        ),
        expected_exported_processor_jar_names = attr.string_list(
            doc = "Names of all JavaInfo::plugins JARs returned by the given target",
            default = kt_analysis.DEFAULT_LIST,
        ),
        expected_exported_processor_classes = attr.string_list(
            doc = "Annotation processors reported as to be run on depending targets",
        ),
        expected_processor_classes = attr.string_list(
            doc = "Annotation processors reported as run on the given target",
        ),
        expected_kotlinc_plugin_jar_names = attr.string_list(
            doc = "Names of all -Xplugin= JARs",
            default = kt_analysis.DEFAULT_LIST,
        ),
        expected_friend_jar_names = attr.string_list(
            doc = "Names of all -Xfriend-paths= JARs",
            default = kt_analysis.DEFAULT_LIST,
        ),
        expected_runfile_names = attr.string_list(
            doc = "Names of all runfiles",
            default = kt_analysis.DEFAULT_LIST,
        ),
        expect_jdeps = attr.bool(default = True),
        expect_processor_classpath = attr.bool(),
        expect_neverlink = attr.bool(),
        required_mnemonic_counts = attr.string_dict(
            doc = "Expected mnemonics to expected action count; unlisted mnemonics are ignored",
        ),
    ),
)

def _kt_jvm_library_analysis_test_impl(ctx):
    kt_analysis.check_endswith_test(ctx)

    env = analysistest.begin(ctx)
    actual = ctx.attr.target_under_test

    actions = analysistest.target_actions(env)
    kt_al_action = kt_analysis.get_action(actions, "KtAndroidLint")

    asserts.true(
        env,
        JavaInfo in actual,
        "kt_jvm_library did not produce JavaInfo provider.",
    )
    asserts.true(
        env,
        ProguardSpecProvider in actual,
        "Expected a ProguardSpecProvider provider.",
    )

    if ctx.attr.expected_runfile_names != kt_analysis.DEFAULT_LIST:
        asserts.set_equals(
            env,
            sets.make(ctx.attr.expected_runfile_names),
            sets.make([
                f.basename
                for f in actual[DefaultInfo].data_runfiles.files.to_list()
            ]),
        )

    if ctx.attr.expected_compile_jar_names != kt_analysis.DEFAULT_LIST:
        asserts.set_equals(
            env,
            sets.make(ctx.attr.expected_compile_jar_names),
            sets.make([f.basename for f in actual[JavaInfo].compile_jars.to_list()]),
            "kt_jvm_library JavaInfo::compile_jars",
        )

    if ctx.attr.expected_exported_processor_jar_names != kt_analysis.DEFAULT_LIST:
        asserts.set_equals(
            env,
            sets.make(ctx.attr.expected_exported_processor_jar_names),
            sets.make([f.basename for f in actual[JavaInfo].plugins.processor_jars.to_list()]),
        )

    asserts.set_equals(
        env,
        sets.make(ctx.attr.expected_exported_processor_classes),
        sets.make(actual[JavaInfo].plugins.processor_classes.to_list()),
    )

    kt_2_java_compile = kt_analysis.get_action(actions, "Kt2JavaCompile")

    if kt_2_java_compile:
        asserts.true(
            env,
            kt_2_java_compile.outputs.to_list()[0].basename.endswith(".jar"),
            "Expected first output to be a JAR (this affects the param file name).",
        )

    if ctx.attr.expected_friend_jar_names != kt_analysis.DEFAULT_LIST:
        friend_paths_arg = kt_analysis.get_arg(kt_2_java_compile, "-Xfriend-paths=")
        kt_asserts.list_matches(
            env,
            expected = ["/" + x for x in ctx.attr.expected_friend_jar_names],
            actual = friend_paths_arg.split(",") if friend_paths_arg else [],
            matcher = lambda expected, actual: actual.endswith(expected),
            items_name = "friend JARs",
        )

    if ctx.attr.expected_kotlinc_plugin_jar_names != kt_analysis.DEFAULT_LIST:
        kt_asserts.list_matches(
            env,
            expected = ["/" + x for x in ctx.attr.expected_kotlinc_plugin_jar_names],
            actual = kt_analysis.get_all_args(kt_2_java_compile, "-Xplugin="),
            matcher = lambda expected, actual: actual.endswith(expected),
            items_name = "kotlinc plugin JARs",
        )

    asserts.equals(
        env,
        ctx.attr.expect_neverlink,
        len(actual[JavaInfo].transitive_runtime_jars.to_list()) == 0,
        "Mismatch: Expected transitive_runtime_jars iff (neverlink == False)",
    )

    kt_asserts.required_mnemonic_counts(env, ctx.attr.required_mnemonic_counts, actions)

    return analysistest.end(env)