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:new_sets.bzl", "sets") 16load("@bazel_skylib//lib:paths.bzl", "paths") 17load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") 18load("//build/bazel/rules:gensrcs.bzl", "gensrcs") 19 20SRCS = [ 21 "texts/src1.txt", 22 "texts/src2.txt", 23 "src3.txt", 24] 25 26OUTPUT_EXTENSION = "out" 27 28EXPECTED_OUTS = [ 29 "texts/src1.out", 30 "texts/src2.out", 31 "src3.out", 32] 33 34# ==== Check the actions created by gensrcs ==== 35 36def _test_actions_impl(ctx): 37 env = analysistest.begin(ctx) 38 target = analysistest.target_under_test(env) 39 actions = analysistest.target_actions(env) 40 41 # Expect an action for each pair of input/output file 42 asserts.equals(env, expected = len(SRCS), actual = len(actions)) 43 44 asserts.set_equals( 45 env, 46 sets.make([ 47 # given an input file build/bazel/rules/texts/src1.txt 48 # the corresponding output file is 49 # <GENDIR>/build/bazel/rules/build/bazel/rules/texts/src1.out 50 # the second "build/bazel/rules" is to accomodate the srcs from 51 # external package 52 paths.join( 53 ctx.genfiles_dir.path, 54 "build/bazel/rules", 55 "build/bazel/rules", 56 out, 57 ) 58 for out in EXPECTED_OUTS 59 ]), 60 sets.make([file.path for file in target.files.to_list()]), 61 ) 62 63 return analysistest.end(env) 64 65actions_test = analysistest.make(_test_actions_impl) 66 67def _test_actions(): 68 name = "gensrcs_output_paths" 69 test_name = name + "_test" 70 71 # Rule under test 72 gensrcs( 73 name = name, 74 cmd = "cat $(SRC) > $(OUT)", 75 srcs = SRCS, 76 output_extension = OUTPUT_EXTENSION, 77 tags = ["manual"], # make sure it's not built using `:all` 78 ) 79 80 actions_test( 81 name = test_name, 82 target_under_test = name, 83 ) 84 return test_name 85 86# ==== Check the output file when out_extension is unset ==== 87 88def _test_unset_output_extension_impl(ctx): 89 env = analysistest.begin(ctx) 90 91 actions = analysistest.target_actions(env) 92 asserts.equals(env, expected = 1, actual = len(actions)) 93 action = actions[0] 94 asserts.equals( 95 env, 96 expected = "input.", 97 actual = action.outputs.to_list()[0].basename, 98 ) 99 100 return analysistest.end(env) 101 102unset_output_extension_test = analysistest.make(_test_unset_output_extension_impl) 103 104def _test_unset_output_extension(): 105 name = "unset_output_extension" 106 test_name = name + "_test" 107 108 # Rule under test 109 gensrcs( 110 name = "TSTSS", 111 cmd = "cat $(SRC) > $(OUT)", 112 srcs = ["input.txt"], 113 tags = ["manual"], # make sure it's not built using `:all` 114 ) 115 116 unset_output_extension_test( 117 name = test_name, 118 target_under_test = "TSTSS", 119 ) 120 return test_name 121 122TOOL_FILE_NAME = "out.sh" 123 124def _test_gensrcs_tool_builds_for_host_impl(ctx): 125 env = analysistest.begin(ctx) 126 actions = analysistest.target_actions(env) 127 asserts.equals(env, expected = 1, actual = len(actions), msg = "expected actions") 128 129 action = actions[0] 130 inputs = action.inputs.to_list() 131 asserts.equals(env, expected = 2, actual = len(inputs), msg = "expected inputs") 132 133 input_map = {} 134 for i in inputs: 135 input_map[i.basename] = i 136 tool = input_map[TOOL_FILE_NAME] 137 asserts.true( 138 env, 139 # because we set --experimental_platform_in_output_dir, we expect the 140 # platform to be in the output path of a generated file 141 "linux" in tool.path, # host platform 142 "expected 'linux' in tool path, got '%s'" % tool.path, 143 ) 144 145 outputs = action.outputs.to_list() 146 asserts.equals(env, expected = 1, actual = len(outputs), msg = "expected outputs %s" % outputs) 147 output = outputs[0] 148 asserts.true( 149 env, 150 # because we set --experimental_platform_in_output_dir, we expect the 151 # platform to be in the output path of a generated file. However, the platform 152 # will be the android product name, like aosp_arm, so we can't check if anything 153 # in particular is in the path. Check that linux is not in the path instead. 154 "linux" not in output.path, # target platform 155 "expected 'linux' to not be in output path, got '%s'" % output.path, 156 ) 157 158 return analysistest.end(env) 159 160__gensrcs_tool_builds_for_host_test = analysistest.make( 161 _test_gensrcs_tool_builds_for_host_impl, 162) 163 164def _gensrcs_tool_builds_for_host_test(**kwargs): 165 __gensrcs_tool_builds_for_host_test( 166 target_compatible_with = ["//build/bazel/platforms/os:android"], # ensure target != host so there is a transition 167 **kwargs 168 ) 169 170def _test_gensrcs_tool_builds_for_host(): 171 native.genrule( 172 name = "gensrcs_test_bin", 173 outs = [TOOL_FILE_NAME], 174 executable = True, 175 cmd = "touch $@", 176 target_compatible_with = select({ 177 # only supported OS is that specified as host_platform 178 "//build/bazel/platforms/os:linux": [], 179 "//conditions:default": ["@platforms//:incompatible"], 180 }), 181 tags = ["manual"], 182 ) 183 184 gensrcs( 185 name = "gensrcs_test_tool_builds_for_host", 186 tools = [":gensrcs_test_bin"], 187 srcs = ["input.txt"], 188 output_extension = OUTPUT_EXTENSION, 189 cmd = "", 190 tags = ["manual"], 191 ) 192 193 test_name = "gensrcs_tools_build_for_host_test" 194 _gensrcs_tool_builds_for_host_test( 195 name = test_name, 196 target_under_test = ":gensrcs_test_tool_builds_for_host", 197 ) 198 return test_name 199 200def gensrcs_tests_suite(name): 201 """Creates test targets for gensrcs.bzl""" 202 native.test_suite( 203 name = name, 204 tests = [ 205 _test_actions(), 206 _test_unset_output_extension(), 207 _test_gensrcs_tool_builds_for_host(), 208 ], 209 ) 210