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 15""" 16Tests for java_sysprop_library.bzl 17""" 18 19load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite") 20load("@rules_testing//lib:truth.bzl", "matching") 21load("//build/bazel/rules/sysprop:sysprop_library.bzl", "sysprop_library") 22load( 23 "//build/bazel/rules/test_common:paths.bzl", 24 "get_output_and_package_dir_based_path_rt", 25 "get_package_dir_based_path", 26) 27load(":java_sysprop_library.bzl", "java_sysprop_library") 28 29def _create_targets_under_test(name): 30 sysprop_name = name + "_sysprop" 31 sysprop_library( 32 name = sysprop_name, 33 srcs = [ 34 "foo.sysprop", 35 "bar.sysprop", 36 ], 37 tags = ["manual"], 38 ) 39 java_sysprop_library( 40 name = name, 41 dep = sysprop_name, 42 tags = ["manual"], 43 ) 44 45def _java_sysprop_library_target_test(name): 46 target_name = name + "_subject" 47 _create_targets_under_test(target_name) 48 49 analysis_test(name, target = target_name, impl = _target_test_impl) 50 51def _target_test_impl(env, target): 52 target_subject = env.expect.that_target(target) 53 54 output_jar_source_path = get_package_dir_based_path( 55 env, 56 "java_sysprop_library_target_test_subject.jar", 57 ) 58 target_subject.default_outputs().contains_at_least([output_jar_source_path]) 59 target_subject.runfiles().contains_at_least([ 60 "__main__/" + output_jar_source_path, 61 "__main__/system/tools/sysprop/libsysprop-library-stub-platform_private.jar", 62 ]) 63 target_subject.has_provider(JavaInfo) 64 65def _java_sysprop_library_java_action_test(name): 66 target_name = name + "_subject" 67 _create_targets_under_test(target_name) 68 69 analysis_test(name, target = target_name, impl = _java_action_test_impl) 70 71def _java_action_test_impl(env, target): 72 stubs_prefix = "system/tools/sysprop/libsysprop-library-stub-platform_private-hjar" 73 74 env.expect.that_target(target).action_named("Javac").inputs().contains_at_least([ 75 "build/bazel/rules/java/foo.sysprop.srcjar", 76 "build/bazel/rules/java/bar.sysprop.srcjar", 77 "{}.jar".format(stubs_prefix), 78 "{}.jdeps".format(stubs_prefix), 79 ]) 80 81def _java_sysprop_library_gen_action_args_test(name): 82 target_name = name + "_subject" 83 _create_targets_under_test(target_name) 84 85 analysis_test(name, target = target_name, impl = _gen_action_args_test_impl) 86 87def _gen_action_args_test_impl(env, target): 88 actions = target.actions 89 90 gen_actions = [] 91 for action in actions: 92 if action.mnemonic == "SyspropJava": 93 gen_actions.append(action) 94 95 for action in gen_actions: 96 name = "" 97 if "foo.sysprop" in action.argv[2]: 98 name = "foo" 99 elif "bar.sysprop" in action.argv[2]: 100 name = "bar" 101 else: 102 fail("neither expected source file was found in an action") 103 104 action_cmds = action.argv[2].split("&&") 105 106 rm_cmd_subject = env.expect.that_collection( 107 action_cmds[0].strip().split(" "), 108 expr = "rm command for {}".format(name), 109 ) 110 rm_cmd_subject.contains_exactly([ 111 "rm", 112 "-rf", 113 get_output_and_package_dir_based_path_rt( 114 target, 115 "{}.sysprop.srcjar.tmp".format(name), 116 ), 117 ]).in_order() 118 119 mkdir_cmd_subject = env.expect.that_collection( 120 action_cmds[1].strip().split(" "), 121 expr = "mkdir command for {}".format(name), 122 ) 123 mkdir_cmd_subject.contains_exactly([ 124 "mkdir", 125 "-p", 126 get_output_and_package_dir_based_path_rt( 127 target, 128 "{}.sysprop.srcjar.tmp".format(name), 129 ), 130 ]).in_order() 131 132 sysprop_cmd_subject = env.expect.that_collection( 133 action_cmds[2].strip().split(" "), 134 expr = "sysprop command for {}".format(name), 135 ) 136 sysprop_cmd_subject.contains_exactly_predicates([ 137 matching.str_endswith( 138 "bin/system/tools/sysprop/bin/sysprop_java/sysprop_java", 139 ), 140 matching.equals_wrapper("--scope"), 141 matching.equals_wrapper("internal"), 142 matching.equals_wrapper("--java-output-dir"), 143 matching.equals_wrapper(get_output_and_package_dir_based_path_rt( 144 target, 145 "{}.sysprop.srcjar.tmp".format(name), 146 )), 147 matching.equals_wrapper(get_package_dir_based_path( 148 env, 149 "{}.sysprop".format(name), 150 )), 151 ]).in_order() 152 153 expected_out_tmp_dir_path = get_output_and_package_dir_based_path_rt( 154 target, 155 "{}.sysprop.srcjar.tmp".format(name), 156 ) 157 zip_cmd_subject = env.expect.that_collection( 158 action_cmds[3].strip().split(" "), 159 expr = "soong_zip command for {}".format(name), 160 ) 161 zip_cmd_subject.contains_exactly_predicates([ 162 matching.str_endswith( 163 "/build/soong/zip/cmd/soong_zip_/soong_zip", 164 ), 165 matching.equals_wrapper("-jar"), 166 matching.equals_wrapper("-o"), 167 matching.equals_wrapper(get_output_and_package_dir_based_path_rt( 168 target, 169 "{}.sysprop.srcjar".format(name), 170 )), 171 matching.equals_wrapper("-C"), 172 matching.equals_wrapper(expected_out_tmp_dir_path), 173 matching.equals_wrapper("-D"), 174 matching.equals_wrapper(expected_out_tmp_dir_path), 175 ]) 176 177def _java_sysprop_library_sdk_setting_test(name): 178 target_name = name + "_subject" 179 _create_targets_under_test(target_name) 180 181 analysis_test(name, target = target_name, impl = _sdk_setting_test_impl) 182 183def _sdk_setting_test_impl(env, target): 184 # The argument this check searches for is part of bootclasspath, and is 185 # currently the only result of the sdk version transition visible in the 186 # java compilation action. 187 # core_10000 is the part that reflects the required sdk version, 188 # core_current 189 # TODO: b/303596698 - Find a better way to test this 190 env.expect.that_target(target).action_named( 191 "Javac", 192 ).argv().contains_at_least_predicates([ 193 matching.str_endswith( 194 "core_10000_android_jar_private/prebuilts/sdk/current/core/android-ijar.jar", 195 ), 196 ]) 197 198def java_sysprop_library_test_suite(name): 199 test_suite( 200 name = name, 201 tests = [ 202 _java_sysprop_library_gen_action_args_test, 203 _java_sysprop_library_java_action_test, 204 _java_sysprop_library_target_test, 205 _java_sysprop_library_sdk_setting_test, 206 ], 207 ) 208