1# Copyright (C) 2023 The Android Open Source Project 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 15load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") 16load(":library.bzl", "java_library") 17 18ActionArgsInfo = provider( 19 fields = { 20 "argv_map": "A dict with compile action arguments keyed by the target label", 21 }, 22) 23 24def _library_compile_actions_aspect_impl(target, ctx): 25 argv_map = {} 26 if ctx.rule.kind == "java_library_sdk_transition": 27 if len(ctx.rule.attr.exports) > 1: 28 fail("multiple exports is not supported.") 29 for export in ctx.rule.attr.exports: 30 label_name = export.label.name 31 action = export[ActionArgsInfo].argv_map.get(label_name, None) 32 if action: 33 argv_map[target.label.name] = action 34 else: 35 argv = [] 36 for action in target.actions: 37 if action.mnemonic == "Javac": 38 argv.extend(action.argv) 39 argv_map[target.label.name] = argv 40 41 return ActionArgsInfo( 42 argv_map = argv_map, 43 ) 44 45library_compile_actions_aspect = aspect( 46 implementation = _library_compile_actions_aspect_impl, 47 attr_aspects = ["exports"], 48) 49 50def _compile_test_impl(ctx): 51 env = analysistest.begin(ctx) 52 if len(ctx.attr.args_to_check) == 0: 53 return analysistest.end(env) 54 target = analysistest.target_under_test(env) 55 argv = target[ActionArgsInfo].argv_map[target.label.name] 56 expected_args = ctx.attr.args_to_check 57 first_arg = expected_args[0] 58 for (i, arg) in enumerate(argv): 59 if arg == first_arg: 60 asserts.true(env, len(argv) >= i + len(expected_args), "expected enough at least %d args based on # of expected args (%d), got %d" % (i + len(expected_args), len(expected_args), len(argv))) 61 asserts.equals(env, expected_args, argv[i:i + len(expected_args)]) 62 break 63 return analysistest.end(env) 64 65java_library_compile_test = analysistest.make( 66 _compile_test_impl, 67 attrs = { 68 "args_to_check": attr.string_list(), 69 }, 70 extra_target_under_test_aspects = [library_compile_actions_aspect], 71) 72 73def _host_java_library_has_correct_java_version(): 74 basename = "host_java_library_has_correct_java_version" 75 test_name = basename + "_test" 76 77 java_library( 78 name = basename, 79 srcs = ["foo.java"], 80 sdk_version = "21", 81 java_version = "1.7", 82 tags = ["manual"], 83 ) 84 85 java_library_compile_test( 86 name = test_name, 87 target_under_test = basename, 88 args_to_check = [ 89 "-source", 90 "7", 91 "-target", 92 "7", 93 ], 94 ) 95 96 return test_name 97 98def java_library_test_suite(name): 99 native.test_suite( 100 name = name, 101 tests = [ 102 _host_java_library_has_correct_java_version(), 103 ], 104 ) 105