1# Copyright (C) 2022 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", "unittest", skylib_asserts = "asserts") 16load("//build/bazel/rules/test_common:asserts.bzl", roboleaf_asserts = "asserts") 17load(":cc_library_common.bzl", "CcAndroidMkInfo", "is_external_directory") 18 19asserts = skylib_asserts + roboleaf_asserts 20 21def _is_external_directory_test(ctx): 22 env = unittest.begin(ctx) 23 24 actual = is_external_directory(ctx.attr.path) 25 26 asserts.equals(env, ctx.attr.expected_value, actual, "expected {path}, to be {external}".format( 27 path = ctx.attr.path, 28 external = "external" if ctx.attr.expected_value else "non-external", 29 )) 30 31 return unittest.end(env) 32 33is_external_directory_test = unittest.make( 34 _is_external_directory_test, 35 attrs = { 36 "path": attr.string(), 37 "expected_value": attr.bool(), 38 }, 39) 40 41def _is_external_directory_tests(): 42 test_cases = { 43 "non_external": struct( 44 path = "path/to/package", 45 expected_value = False, 46 ), 47 "external": struct( 48 path = "external/path/to/package", 49 expected_value = True, 50 ), 51 "hardware": struct( 52 path = "hardware/path/to/package", 53 expected_value = True, 54 ), 55 "only_hardware": struct( 56 path = "hardware", 57 expected_value = True, 58 ), 59 "hardware_google": struct( 60 path = "hardware/google/path/to/package", 61 expected_value = False, 62 ), 63 "hardware_interfaces": struct( 64 path = "hardware/interfaces/path/to/package", 65 expected_value = False, 66 ), 67 "hardware_ril": struct( 68 path = "hardware/ril/path/to/package", 69 expected_value = False, 70 ), 71 "hardware_libhardware_dir": struct( 72 path = "hardware/libhardware/path/to/package", 73 expected_value = False, 74 ), 75 "hardware_libhardware_partial": struct( 76 path = "hardware/libhardware_legacy/path/to/package", 77 expected_value = False, 78 ), 79 "vendor": struct( 80 path = "vendor/path/to/package", 81 expected_value = True, 82 ), 83 "only_vendor": struct( 84 path = "vendor", 85 expected_value = True, 86 ), 87 "vendor_google": struct( 88 path = "vendor/google/path/to/package", 89 expected_value = False, 90 ), 91 "vendor_google_with_prefix": struct( 92 path = "vendor/pre_google/path/to/package", 93 expected_value = False, 94 ), 95 "vendor_google_with_postfix": struct( 96 path = "vendor/google_post/path/to/package", 97 expected_value = False, 98 ), 99 } 100 101 for name, test_case in test_cases.items(): 102 is_external_directory_test( 103 name = name, 104 path = test_case.path, 105 expected_value = test_case.expected_value, 106 ) 107 return test_cases.keys() 108 109def _target_provides_androidmk_info_test_impl(ctx): 110 env = analysistest.begin(ctx) 111 112 target_under_test = analysistest.target_under_test(env) 113 mkinfo = target_under_test[CcAndroidMkInfo] 114 asserts.list_equals( 115 env, 116 ctx.attr.expected_static_libs, 117 mkinfo.local_static_libs, 118 "expected static_libs to be %s, but got %s" % ( 119 ctx.attr.expected_static_libs, 120 mkinfo.local_static_libs, 121 ), 122 ) 123 asserts.list_equals( 124 env, 125 ctx.attr.expected_whole_static_libs, 126 mkinfo.local_whole_static_libs, 127 "expected whole_static_libs to be %s, but got %s" % ( 128 ctx.attr.expected_whole_static_libs, 129 mkinfo.local_whole_static_libs, 130 ), 131 ) 132 asserts.list_equals( 133 env, 134 ctx.attr.expected_shared_libs, 135 mkinfo.local_shared_libs, 136 "expected shared_libs to be %s, but got %s" % ( 137 ctx.attr.expected_shared_libs, 138 mkinfo.local_shared_libs, 139 ), 140 ) 141 142 return analysistest.end(env) 143 144target_provides_androidmk_info_test = analysistest.make( 145 _target_provides_androidmk_info_test_impl, 146 attrs = { 147 "expected_static_libs": attr.string_list(), 148 "expected_whole_static_libs": attr.string_list(), 149 "expected_shared_libs": attr.string_list(), 150 }, 151) 152 153def cc_library_common_test_suites(name): 154 native.test_suite( 155 name = name, 156 tests = _is_external_directory_tests(), 157 ) 158