• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""Kotlin kt_jvm_import rule tests."""
16
17load("//kotlin:jvm_import.bzl", "kt_jvm_import")
18load("//kotlin:jvm_library.bzl", "kt_jvm_library")
19load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
20load("//tests/analysis:util.bzl", "ONLY_FOR_ANALYSIS_TEST_TAGS", "create_file")
21load(":assert_failure_test.bzl", "assert_failure_test")
22load("//:visibility.bzl", "RULES_KOTLIN")
23
24def _impl(ctx):
25    env = analysistest.begin(ctx)
26    asserts.true(
27        env,
28        JavaInfo in ctx.attr.target_under_test,
29        "kt_jvm_import did not produce JavaInfo provider.",
30    )
31    asserts.true(
32        env,
33        ProguardSpecProvider in ctx.attr.target_under_test,
34        "kt_jvm_import did not produce ProguardSpecProvider provider.",
35    )
36    return analysistest.end(env)
37
38_test = analysistest.make(_impl)
39
40def _test_kt_jvm_import():
41    test_name = "kt_jvm_import_test"
42    native.java_library(
43        name = "jar1",
44        srcs = [],
45    )
46    kt_jvm_import(
47        name = test_name + "_tut",
48        jars = [
49            "libjar1.jar",
50        ],
51        srcjar = "libjar1-src.jar",
52    )
53    _test(
54        name = test_name,
55        target_under_test = test_name + "_tut",
56    )
57    return test_name
58
59def _test_kt_jvm_import_no_srcjar():
60    test_name = "kt_jvm_import_no_srcjar_test"
61    native.java_library(
62        name = "jar3",
63        srcs = [],
64    )
65    kt_jvm_import(
66        name = test_name + "_tut",
67        jars = [
68            "libjar3.jar",
69        ],
70            )
71    _test(
72        name = test_name,
73        target_under_test = test_name + "_tut",
74    )
75    return test_name
76
77def _test_kt_jvm_import_with_srcjar_ext():
78    test_name = "kt_jvm_import_test_with_srcjar_ext"
79    native.java_library(
80        name = "jar2",
81        srcs = [],
82    )
83    native.genrule(
84        name = "gen_jar2_srcjar",
85        cmd = "touch $@",
86        outs = ["libjar2.srcjar"],
87    )
88    kt_jvm_import(
89        name = test_name + "_tut",
90        jars = [
91            "libjar2.jar",
92        ],
93        srcjar = ":libjar2.srcjar",
94    )
95    _test(
96        name = test_name,
97        target_under_test = test_name + "_tut",
98    )
99    return test_name
100
101def _test_kt_jvm_import_with_runtime_deps():
102    test_name = "kt_jvm_import_with_runtime_deps"
103    native.java_library(
104        name = test_name + "_dep",
105        srcs = [],
106    )
107    kt_jvm_import(
108        name = test_name + "_tut",
109        jars = [
110            "lib%s_dep.jar" % test_name,
111        ],
112        runtime_deps = [
113            test_name + "_dep",
114        ],
115    )
116    _test(
117        name = test_name,
118        target_under_test = test_name + "_tut",
119    )
120    return test_name
121
122def _test_kt_jvm_import_with_proguard_specs():
123    test_name = "kt_jvm_import_with_proguard_specs"
124    native.java_library(
125        name = test_name + "_jar",
126        srcs = [],
127    )
128
129    kt_jvm_import(
130        name = test_name + "_tut",
131        jars = [
132            "lib%s_jar.jar" % test_name,
133        ],
134        proguard_specs = [
135            create_file(
136                name = test_name + "/salutations.pgcfg",
137                content = """
138-keep class * {
139  *** greeting();
140}
141""",
142            ),
143        ],
144    )
145    _test(
146        name = test_name,
147        target_under_test = test_name + "_tut",
148    )
149    return test_name
150
151def _mock_jar(test_name, i):
152    """Creates a Jar named after the given inputs and returns its name."""
153    native.java_library(
154        name = "%s_mock%s" % (test_name, i),
155        srcs = [],
156    )
157    return "lib%s_mock%s.jar" % (test_name, i)
158
159def test_suite(name = None):
160    native.test_suite(
161        name = name,
162        tests = [
163            _test_kt_jvm_import(),
164            _test_kt_jvm_import_with_srcjar_ext(),
165            _test_kt_jvm_import_no_srcjar(),
166            _test_kt_jvm_import_with_runtime_deps(),
167            _test_kt_jvm_import_with_proguard_specs(),
168        ],
169    )
170