aboutsummaryrefslogtreecommitdiff
path: root/tests/analysis/jvm_import_test.bzl
blob: 6e56c028528a06df56d45f8b80b2b2d992c9a667 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# 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.

"""Kotlin kt_jvm_import rule tests."""

load("//kotlin:jvm_import.bzl", "kt_jvm_import")
load("//kotlin:jvm_library.bzl", "kt_jvm_library")
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
load("//tests/analysis:util.bzl", "ONLY_FOR_ANALYSIS_TEST_TAGS", "create_file")
load(":assert_failure_test.bzl", "assert_failure_test")
load("//:visibility.bzl", "RULES_KOTLIN")

def _impl(ctx):
    env = analysistest.begin(ctx)
    asserts.true(
        env,
        JavaInfo in ctx.attr.target_under_test,
        "kt_jvm_import did not produce JavaInfo provider.",
    )
    asserts.true(
        env,
        ProguardSpecProvider in ctx.attr.target_under_test,
        "kt_jvm_import did not produce ProguardSpecProvider provider.",
    )
    return analysistest.end(env)

_test = analysistest.make(_impl)

def _test_kt_jvm_import():
    test_name = "kt_jvm_import_test"
    native.java_library(
        name = "jar1",
        srcs = [],
    )
    kt_jvm_import(
        name = test_name + "_tut",
        jars = [
            "libjar1.jar",
        ],
        srcjar = "libjar1-src.jar",
    )
    _test(
        name = test_name,
        target_under_test = test_name + "_tut",
    )
    return test_name

def _test_kt_jvm_import_no_srcjar():
    test_name = "kt_jvm_import_no_srcjar_test"
    native.java_library(
        name = "jar3",
        srcs = [],
    )
    kt_jvm_import(
        name = test_name + "_tut",
        jars = [
            "libjar3.jar",
        ],
            )
    _test(
        name = test_name,
        target_under_test = test_name + "_tut",
    )
    return test_name

def _test_kt_jvm_import_with_srcjar_ext():
    test_name = "kt_jvm_import_test_with_srcjar_ext"
    native.java_library(
        name = "jar2",
        srcs = [],
    )
    native.genrule(
        name = "gen_jar2_srcjar",
        cmd = "touch $@",
        outs = ["libjar2.srcjar"],
    )
    kt_jvm_import(
        name = test_name + "_tut",
        jars = [
            "libjar2.jar",
        ],
        srcjar = ":libjar2.srcjar",
    )
    _test(
        name = test_name,
        target_under_test = test_name + "_tut",
    )
    return test_name

def _test_kt_jvm_import_with_runtime_deps():
    test_name = "kt_jvm_import_with_runtime_deps"
    native.java_library(
        name = test_name + "_dep",
        srcs = [],
    )
    kt_jvm_import(
        name = test_name + "_tut",
        jars = [
            "lib%s_dep.jar" % test_name,
        ],
        runtime_deps = [
            test_name + "_dep",
        ],
    )
    _test(
        name = test_name,
        target_under_test = test_name + "_tut",
    )
    return test_name

def _test_kt_jvm_import_with_proguard_specs():
    test_name = "kt_jvm_import_with_proguard_specs"
    native.java_library(
        name = test_name + "_jar",
        srcs = [],
    )

    kt_jvm_import(
        name = test_name + "_tut",
        jars = [
            "lib%s_jar.jar" % test_name,
        ],
        proguard_specs = [
            create_file(
                name = test_name + "/salutations.pgcfg",
                content = """
-keep class * {
  *** greeting();
}
""",
            ),
        ],
    )
    _test(
        name = test_name,
        target_under_test = test_name + "_tut",
    )
    return test_name

def _mock_jar(test_name, i):
    """Creates a Jar named after the given inputs and returns its name."""
    native.java_library(
        name = "%s_mock%s" % (test_name, i),
        srcs = [],
    )
    return "lib%s_mock%s.jar" % (test_name, i)

def test_suite(name = None):
    native.test_suite(
        name = name,
        tests = [
            _test_kt_jvm_import(),
            _test_kt_jvm_import_with_srcjar_ext(),
            _test_kt_jvm_import_no_srcjar(),
            _test_kt_jvm_import_with_runtime_deps(),
            _test_kt_jvm_import_with_proguard_specs(),
        ],
    )