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:paths.bzl", "paths") 16load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") 17load(":api_domain.bzl", "api_domain") 18load(":cc_api_contribution.bzl", "cc_api_contribution") 19load(":java_api_contribution.bzl", "java_api_contribution") 20 21# Check that a .json file is created 22def _json_output_test_impl(ctx): 23 env = analysistest.begin(ctx) 24 actions = analysistest.target_actions(env) 25 asserts.equals( 26 env, 27 expected = 9, # union of cc and java api surfaces 28 actual = len(actions), 29 ) 30 asserts.equals( 31 env, 32 expected = 1, 33 actual = len(actions[0].outputs.to_list()), 34 ) 35 asserts.equals( 36 env, 37 expected = "json", 38 actual = actions[0].outputs.to_list()[0].extension, 39 ) 40 return analysistest.end(env) 41 42json_output_test = analysistest.make(_json_output_test_impl) 43 44def _json_output_test(): 45 test_name = "json_output_test" 46 subject_name = test_name + "_subject" 47 api_domain( 48 name = subject_name, 49 cc_api_contributions = [], 50 tags = ["manual"], 51 ) 52 json_output_test( 53 name = test_name, 54 target_under_test = subject_name, 55 ) 56 return test_name 57 58# Check that output contains contribution information 59# e.g. cc_libraries, java_libraries 60def _json_output_contains_contributions_test_impl(ctx): 61 env = analysistest.begin(ctx) 62 actions = analysistest.target_actions(env) 63 asserts.equals( 64 env, 65 expected = 9, # union of cc and java api surfaces 66 actual = len(actions), 67 ) 68 69 output = json.decode(actions[0].content.replace("'", "")) # Trim the surrounding ' 70 71 # cc 72 asserts.true(env, "cc_libraries" in output) 73 cc_contributions_in_output = output.get("cc_libraries") 74 asserts.equals( 75 env, 76 expected = 1, 77 actual = len(cc_contributions_in_output), 78 ) 79 test_contribution = cc_contributions_in_output[0] 80 asserts.equals( 81 env, 82 expected = ctx.attr.expected_cc_library_name, 83 actual = test_contribution.get("name"), 84 ) 85 asserts.equals( 86 env, 87 expected = paths.join( 88 paths.dirname(ctx.build_file_path), 89 ctx.attr.expected_symbolfile, 90 ), 91 actual = test_contribution.get("api"), 92 ) 93 94 # java 95 asserts.true(env, "java_libraries" in output) 96 java_contributions_in_output = output.get("java_libraries") 97 asserts.equals( 98 env, 99 expected = 1, 100 actual = len(java_contributions_in_output), 101 ) 102 test_java_contribution = java_contributions_in_output[0] 103 asserts.equals( 104 env, 105 expected = paths.join( 106 paths.dirname(ctx.build_file_path), 107 ctx.attr.expected_java_apifile, 108 ), 109 actual = test_java_contribution.get("api"), 110 ) 111 return analysistest.end(env) 112 113json_output_contains_contributions_test = analysistest.make( 114 impl = _json_output_contains_contributions_test_impl, 115 attrs = { 116 "expected_cc_library_name": attr.string(), 117 "expected_symbolfile": attr.string(), 118 "expected_java_apifile": attr.string(), 119 }, 120) 121 122def _json_output_contains_contributions_test(): 123 test_name = "json_output_contains_cc_test" 124 subject_name = test_name + "_subject" 125 cc_subject_name = subject_name + "_cc" 126 java_subject_name = subject_name + "_java" 127 symbolfile = "libfoo.map.txt" 128 java_apifile = "current.txt" 129 cc_api_contribution( 130 name = cc_subject_name, 131 api = symbolfile, 132 tags = ["manual"], 133 ) 134 java_api_contribution( 135 name = java_subject_name, 136 api = java_apifile, 137 tags = ["manual"], 138 ) 139 api_domain( 140 name = subject_name, 141 cc_api_contributions = [cc_subject_name], 142 java_api_contributions = [java_subject_name], 143 tags = ["manual"], 144 ) 145 json_output_contains_contributions_test( 146 name = test_name, 147 target_under_test = subject_name, 148 expected_cc_library_name = cc_subject_name, 149 expected_symbolfile = symbolfile, 150 expected_java_apifile = java_apifile, 151 ) 152 return test_name 153 154def api_domain_test_suite(name): 155 native.test_suite( 156 name = name, 157 tests = [ 158 _json_output_test(), 159 _json_output_contains_contributions_test(), 160 ], 161 ) 162