aboutsummaryrefslogtreecommitdiff
path: root/tests/analysis/jvm_library_test.bzl
blob: 6435cc555b42e782a0dc1dcab94a8b369d9e4388 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# 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_library rule tests."""

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

_DEFAULT_LIST = ["__default__"]

def _test_impl(ctx):
    env = analysistest.begin(ctx)
    actions = analysistest.target_actions(env)
    actual = ctx.attr.target_under_test
    expected = ctx.attr.expected

    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 "data" in expected:
        expected_data = expected["data"]
        actual_data = _extract_data_runfiles(actual)

        asserts.new_set_equals(
            env,
            sets.make(expected_data),
            sets.make(actual_data),
            """
            FAIL: kt_jvm_library did not produce the expected data dependencies.
            EXPECTED: %s
            ACTUAL: %s
            """ % (expected_data, actual_data),
        )

    expected_exports = []
    for target in ctx.attr.expected_exports:
        asserts.equals(
            env,
            1,
            len(target[JavaInfo].full_compile_jars.to_list()),
            "Not a single compile-time Jar: %s" % target.label,
        )
        expected_exports.extend(target[JavaInfo].full_compile_jars.to_list())
    actual_exports = actual[JavaInfo].full_compile_jars.to_list()

    # TODO: fail if there are *un*expected exports, maybe by making sure
    # that the actual exports are exactly the expected ones plus the Jar(s)
    # produced by this JavaInfo.
    for expected_export in expected_exports:
        asserts.true(
            env,
            expected_export in actual_exports,
            """
            kt_jvm_library did not export %s
            actual: %s
            """ % (expected_export, actual_exports),
        )

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

    if ctx.attr.expected_friend_jar_names != _DEFAULT_LIST:
        friend_paths_arg = get_action_arg(actions, "Kt2JavaCompile", "-Xfriend-paths=")
        friend_jar_names = [p.rsplit("/", 1)[1] for p in friend_paths_arg.split(",")] if friend_paths_arg else []
        asserts.set_equals(env, sets.make(ctx.attr.expected_friend_jar_names), sets.make(friend_jar_names))

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

    return analysistest.end(env)

_test = analysistest.make(
    impl = _test_impl,
    attrs = dict(
        expected = attr.string_list_dict(),
        expected_exports = attr.label_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_friend_jar_names = attr.string_list(
            doc = "Names of all -Xfriend-paths= JARs",
            default = _DEFAULT_LIST,
        ),
        expect_processor_classpath = attr.bool(),
        expect_neverlink = attr.bool(),
    ),
)

jvm_library_test = _test

def _coverage_test_impl(ctx):
    env = analysistest.begin(ctx)
    target_under_test = analysistest.target_under_test(env)
    instrumented_files_info = target_under_test[InstrumentedFilesInfo]
    instrumented_files = instrumented_files_info.instrumented_files.to_list()
    asserts.equals(
        env,
        ctx.attr.expected_instrumented_file_basenames,
        [file.basename for file in instrumented_files],
    )
    return analysistest.end(env)

_coverage_test = analysistest.make(
    impl = _coverage_test_impl,
    attrs = {
        "expected_instrumented_file_basenames": attr.string_list(),
    },
    config_settings = {
        "//command_line_option:collect_code_coverage": "1",
        "//command_line_option:instrument_test_targets": "1",
        "//command_line_option:instrumentation_filter": "+tests/analysis[:/]",
    },
)

def _extract_data_runfiles(target):
    return [f.basename for f in target[DefaultInfo].data_runfiles.files.to_list()]

def _test_kt_jvm_library_with_proguard_specs():
    test_name = "kt_jvm_library_with_proguard_specs_test"
    create_file(
        name = test_name + "/Salutations.kt",
        content = """
package test

fun greeting(): String = "Hello World!"
""",
    )
    create_file(
        name = test_name + "/salutations.pgcfg",
        content = """
-keep class * {
  *** greeting();
}
""",
    )
    kt_jvm_library(
        name = test_name + "_tut",
        srcs = [
            test_name + "/Salutations.kt",
        ],
        proguard_specs = [
            test_name + "/salutations.pgcfg",
        ],
    )
    _test(
        name = test_name,
        target_under_test = test_name + "_tut",
    )
    return test_name

def _test_kt_jvm_library_with_resources():
    test_name = "kt_jvm_library_with_resources_test"
    create_file(
        name = test_name + "/Salutations.kt",
        content = """
package test

fun greeting(): String = "Hello World!"
""",
    )
    create_file(
        name = test_name + "/salutations.txt",
        content = """
Hi!
""",
    )
    kt_jvm_library(
        name = test_name + "_tut",
        srcs = [
            test_name + "/Salutations.kt",
            "testinputs/Foo.java",
        ],
        resources = [
            test_name + "/salutations.txt",
        ],
    )
    _test(
        name = test_name,
        target_under_test = test_name + "_tut",
    )
    return test_name

def _test_kt_jvm_library_with_plugin():
    test_name = "kt_jvm_library_with_plugin_test"
    create_file(
        name = test_name + "/Salutations.kt",
        content = """
package test

fun greeting(): String = "Hello World!"
""",
    )

    kt_jvm_library(
        name = test_name + "_tut",
        srcs = [
            test_name + "/Salutations.kt",
        ],
        # Need a working plugin so it can run for the test.
        plugins = ["//bazel:auto_value_plugin"],
    )

    _test(
        name = test_name,
        target_under_test = test_name + "_tut",
        expected_processor_classes = ["com.google.auto.value.processor.AutoValueProcessor"],
        expect_processor_classpath = True,
    )
    return test_name

def _test_kt_jvm_library_no_kt_srcs_with_plugin():
    test_name = "kt_jvm_library_no_kt_srcs_with_plugin_test"
    native.java_plugin(
        name = "%s_plugin" % test_name,
        processor_class = test_name,
        srcs = ["testinputs/Foo.java"],  # induce processor_classpath
    )
    kt_jvm_library(
        name = test_name + "_tut",
        srcs = ["testinputs/Bar.java"],
        plugins = [":%s_plugin" % test_name],
        tags = ONLY_FOR_ANALYSIS_TEST_TAGS,
    )
    _test(
        name = test_name,
        target_under_test = test_name + "_tut",
        expected_processor_classes = [test_name],
        expect_processor_classpath = True,
    )
    return test_name

def _test_kt_jvm_library_with_non_processor_plugin():
    test_name = "kt_jvm_library_with_non_processor_plugin_test"
    create_file(
        name = test_name + "/Salutations.kt",
        content = """
package test

fun greeting(): String = "Hello World!"
""",
    )

    native.java_plugin(
        # no processor_class
        name = "%s_plugin" % test_name,
        srcs = ["testinputs/Foo.java"],
    )
    kt_jvm_library(
        name = test_name + "_tut",
        srcs = [
            test_name + "/Salutations.kt",
        ],
        plugins = [":%s_plugin" % test_name],
    )

    _test(
        name = test_name,
        target_under_test = test_name + "_tut",
        expected_processor_classes = [],  # no processor class so no processing
        expect_processor_classpath = True,  # expect java_plugin's Jar
    )
    return test_name

def _test_kt_jvm_library_with_exported_plugin():
    test_name = "kt_jvm_library_with_exported_plugin_test"
    create_file(
        name = test_name + "/Salutations.kt",
        content = """
package test

fun greeting(): String = "Hello World!"
""",
    )
    native.java_plugin(
        name = "%s_plugin" % test_name,
        processor_class = test_name,
    )
    kt_jvm_library(
        name = test_name + "_tut",
        srcs = [
            test_name + "/Salutations.kt",
        ],
        exported_plugins = [":%s_plugin" % test_name],
    )

    _test(
        name = test_name,
        target_under_test = test_name + "_tut",
        expected_exported_processor_classes = [test_name],
        expected_processor_classes = [],  # exported plugin should *not* run on _tut itself
    )
    return test_name

def _test_kt_jvm_library_dep_on_exported_plugin():
    test_name = "kt_jvm_library_dep_on_exported_plugin_test"
    create_file(
        name = test_name + "/Salutations.kt",
        content = """
package test

fun greeting(): String = "Hello World!"
""",
    )

    native.java_plugin(
        name = "%s_plugin" % test_name,
        processor_class = test_name,
        srcs = ["testinputs/Foo.java"],  # induce processor_classpath
    )
    kt_jvm_library(
        name = "%s_exports_plugin" % test_name,
        srcs = [test_name + "/Salutations.kt"],
        exported_plugins = [":%s_plugin" % test_name],
    )
    kt_jvm_library(
        name = test_name + "_tut",
        srcs = [
            test_name + "/Salutations.kt",
        ],
        deps = [":%s_exports_plugin" % test_name],
        tags = ONLY_FOR_ANALYSIS_TEST_TAGS,
    )

    _test(
        name = test_name,
        target_under_test = test_name + "_tut",
        expected_processor_classes = [test_name],
        expect_processor_classpath = True,
    )
    return test_name

def _test_kt_jvm_library_java_dep_on_exported_plugin():
    test_name = "kt_jvm_library_java_dep_on_exported_plugin_test"
    create_file(
        name = test_name + "/Salutations.kt",
        content = """
package test

fun greeting(): String = "Hello World!"
""",
    )
    native.java_plugin(
        name = "%s_plugin" % test_name,
        processor_class = test_name,
        srcs = ["testinputs/Foo.java"],  # induce processor_classpath
    )
    native.java_library(
        name = "%s_exports_plugin" % test_name,
        exported_plugins = [":%s_plugin" % test_name],
    )
    kt_jvm_library(
        name = test_name + "_tut",
        srcs = [
            test_name + "/Salutations.kt",
        ],
        deps = [":%s_exports_plugin" % test_name],
        tags = ONLY_FOR_ANALYSIS_TEST_TAGS,
    )

    _test(
        name = test_name,
        target_under_test = test_name + "_tut",
        expected_processor_classes = [test_name],
        expect_processor_classpath = True,
    )
    return test_name

def _test_kt_jvm_library_with_exports():
    test_name = "kt_jvm_library_with_exports_test"
    create_file(
        name = test_name + "/Salutations.kt",
        content = """
package test

fun greeting(): String = "Hello World!"
""",
    )
    kt_jvm_library(
        name = test_name + "_exp",
        srcs = [test_name + "/Salutations.kt"],
    )
    native.java_library(
        name = test_name + "_javaexp",
        srcs = ["testinputs/Foo.java"],  # need file here so we get a Jar
    )
    kt_jvm_library(
        name = test_name + "_tut",
        srcs = [
            test_name + "/Salutations.kt",
        ],
        exports = [
            ":%s_exp" % test_name,
            ":%s_javaexp" % test_name,
        ],
    )
    _test(
        name = test_name,
        target_under_test = test_name + "_tut",
        expected_exports = [
            ":%s_exp" % test_name,
            ":%s_javaexp" % test_name,
        ],
    )
    return test_name

def _test_kt_jvm_library_with_export_that_exports_plugin():
    test_name = "kt_jvm_library_with_export_that_exports_plugin_test"
    create_file(
        name = test_name + "/Salutations.kt",
        content = """
package test

fun greeting(): String = "Hello World!"
""",
    )
    native.java_plugin(
        name = "%s_plugin" % test_name,
        processor_class = test_name,
        srcs = ["testinputs/Foo.java"],  # induce processor_classpath
    )
    kt_jvm_library(
        name = "%s_exports_plugin" % test_name,
        exported_plugins = [":%s_plugin" % test_name],
        srcs = [test_name + "/Salutations.kt"],
    )
    kt_jvm_library(
        name = test_name + "_tut",
        srcs = [
            test_name + "/Salutations.kt",
        ],
        exports = [":%s_exports_plugin" % test_name],
    )
    _test(
        name = test_name,
        target_under_test = test_name + "_tut",
        expected_exports = [":%s_exports_plugin" % test_name],
        expected_exported_processor_classes = [test_name],
    )
    return test_name

def _test_kt_jvm_library_with_java_export_that_exports_plugin():
    test_name = "kt_jvm_library_with_java_export_that_exports_plugin_test"
    create_file(
        name = test_name + "/Salutations.kt",
        content = """
package test

fun greeting(): String = "Hello World!"
""",
    )
    native.java_plugin(
        name = "%s_plugin" % test_name,
        processor_class = test_name,
        srcs = ["testinputs/Foo.java"],  # induce processor_classpath
    )
    native.java_library(
        name = "%s_exports_plugin" % test_name,
        exported_plugins = [":%s_plugin" % test_name],
    )
    kt_jvm_library(
        name = test_name + "_tut",
        srcs = [
            test_name + "/Salutations.kt",
        ],
        exports = [":%s_exports_plugin" % test_name],
    )
    _test(
        name = test_name,
        target_under_test = test_name + "_tut",
        expected_exports = [],  # _exports_plugin has no compile/runtime Jars
        expected_exported_processor_classes = [test_name],
    )
    return test_name

def _test_forbidden_nano_dep():
    test_name = "kt_jvm_library_forbidden_nano_test"

    kt_jvm_library(
        name = test_name + "_tut",
        srcs = [test_name + "/Ignored.kt"],
        deps = [test_name + "_fake_nano_proto_lib"],
                tags = [
            "manual",
            "nobuilder",
        ],
    )
    native.java_library(
        name = test_name + "_fake_nano_proto_lib",
        srcs = [],
                tags = ["nano_proto_library"],
    )
    assert_failure_test(
        name = test_name,
        target_under_test = test_name + "_tut",
        msg_contains = test_name + "_fake_nano_proto_lib : nano_proto_library",
    )
    return test_name

def _test_forbidden_nano_export():
    test_name = "kt_jvm_library_forbidden_nano_export_test"

    kt_jvm_library(
        name = test_name + "_tut",
        srcs = [test_name + "/Ignored.kt"],
        deps = [test_name + "_export"],
                tags = [
            "manual",
            "nobuilder",
        ],
    )
    native.java_library(
        name = test_name + "_export",
        exports = [test_name + "_fake_nano_proto_lib"],
            )
    native.java_library(
        name = test_name + "_fake_nano_proto_lib",
        srcs = [],
                tags = ["nano_proto_library"],
    )
    assert_failure_test(
        name = test_name,
        target_under_test = test_name + "_tut",
        msg_contains = test_name + "_fake_nano_proto_lib : nano_proto_library",
    )
    return test_name

def _test_kt_jvm_library_with_no_sources():
    test_name = "kt_jvm_library_with_no_sources_test"

    kt_jvm_library(
        name = test_name + "_tut",
        tags = [
            "manual",
            "nobuilder",
        ],
    )
    tut_label = str(Label("//tests/analysis:kt_jvm_library_with_no_sources_test_tut"))
    assert_failure_test(
        name = test_name,
        target_under_test = test_name + "_tut",
        msg_contains = "One of {srcs, common_srcs, exports, exported_plugins} of target " + tut_label + " must be non empty",
    )
    return test_name

def _test_kt_jvm_library_coverage():
    test_name = "kt_jvm_library_coverage"
    kt_jvm_library(
        name = test_name + "_tut",
        srcs = ["testinputs/Srcs.kt"],
        common_srcs = ["testinputs/CommonSrcs.kt"],
        deps = [":{}_deps".format(test_name)],
        runtime_deps = [":{}_runtime_deps".format(test_name)],
        data = [":{}_data".format(test_name)],
        resources = [":{}_resources".format(test_name)],
        testonly = True,
    )
    native.java_library(
        name = test_name + "_deps",
        srcs = ["testinputs/Deps.java"],
        testonly = True,
    )
    native.java_library(
        name = test_name + "_runtime_deps",
        srcs = ["testinputs/RuntimeDeps.java"],
        testonly = True,
    )
    native.java_binary(
        name = test_name + "_data",
        main_class = "Data",
        srcs = ["testinputs/Data.java"],
        testonly = True,
    )
    native.java_binary(
        name = test_name + "_resources",
        main_class = "Resources",
        srcs = ["testinputs/Resources.java"],
        testonly = True,
    )
    _coverage_test(
        name = test_name,
        target_under_test = test_name + "_tut",
        expected_instrumented_file_basenames = [
            "Data.java",
            "Deps.java",
            "Resources.java",
            "RuntimeDeps.java",
            "Srcs.kt",
            "CommonSrcs.kt",
        ],
    )
    return test_name

def test_suite(name):
    native.test_suite(
        name = name,
        tests = [
            _test_forbidden_nano_dep(),
            _test_forbidden_nano_export(),
            _test_kt_jvm_library_dep_on_exported_plugin(),
            _test_kt_jvm_library_java_dep_on_exported_plugin(),
            _test_kt_jvm_library_no_kt_srcs_with_plugin(),
            _test_kt_jvm_library_with_export_that_exports_plugin(),
            _test_kt_jvm_library_with_exported_plugin(),
            _test_kt_jvm_library_with_exports(),
            _test_kt_jvm_library_with_java_export_that_exports_plugin(),
            _test_kt_jvm_library_with_no_sources(),
            _test_kt_jvm_library_with_non_processor_plugin(),
            _test_kt_jvm_library_with_plugin(),
            _test_kt_jvm_library_with_proguard_specs(),
            _test_kt_jvm_library_with_resources(),
            _test_kt_jvm_library_coverage(),
        ],
    )