1# Copyright 2023 The Bazel Authors. All rights reserved. 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"""Tests common to py_test, py_binary, and py_library rules.""" 15 16load("@rules_testing//lib:analysis_test.bzl", "analysis_test") 17load("@rules_testing//lib:truth.bzl", "matching") 18load("@rules_testing//lib:util.bzl", "PREVENT_IMPLICIT_BUILDING_TAGS", rt_util = "util") 19load("//python:defs.bzl", "PyInfo") 20load("//tools/build_defs/python/tests:py_info_subject.bzl", "py_info_subject") 21load("//tools/build_defs/python/tests:util.bzl", pt_util = "util") 22 23_tests = [] 24 25def _produces_py_info_impl(ctx): 26 return [PyInfo(transitive_sources = depset(ctx.files.srcs))] 27 28_produces_py_info = rule( 29 implementation = _produces_py_info_impl, 30 attrs = {"srcs": attr.label_list(allow_files = True)}, 31) 32 33def _test_consumes_provider(name, config): 34 rt_util.helper_target( 35 config.base_test_rule, 36 name = name + "_subject", 37 deps = [name + "_produces_py_info"], 38 ) 39 rt_util.helper_target( 40 _produces_py_info, 41 name = name + "_produces_py_info", 42 srcs = [rt_util.empty_file(name + "_produce.py")], 43 ) 44 analysis_test( 45 name = name, 46 target = name + "_subject", 47 impl = _test_consumes_provider_impl, 48 ) 49 50def _test_consumes_provider_impl(env, target): 51 env.expect.that_target(target).provider( 52 PyInfo, 53 factory = py_info_subject, 54 ).transitive_sources().contains("{package}/{test_name}_produce.py") 55 56_tests.append(_test_consumes_provider) 57 58def _test_requires_provider(name, config): 59 rt_util.helper_target( 60 config.base_test_rule, 61 name = name + "_subject", 62 deps = [name + "_nopyinfo"], 63 ) 64 rt_util.helper_target( 65 native.filegroup, 66 name = name + "_nopyinfo", 67 ) 68 analysis_test( 69 name = name, 70 target = name + "_subject", 71 impl = _test_requires_provider_impl, 72 expect_failure = True, 73 ) 74 75def _test_requires_provider_impl(env, target): 76 env.expect.that_target(target).failures().contains_predicate( 77 matching.str_matches("mandatory*PyInfo"), 78 ) 79 80_tests.append(_test_requires_provider) 81 82def _test_data_sets_uses_shared_library(name, config): 83 rt_util.helper_target( 84 config.base_test_rule, 85 name = name + "_subject", 86 data = [rt_util.empty_file(name + "_dso.so")], 87 ) 88 analysis_test( 89 name = name, 90 target = name + "_subject", 91 impl = _test_data_sets_uses_shared_library_impl, 92 ) 93 94def _test_data_sets_uses_shared_library_impl(env, target): 95 env.expect.that_target(target).provider( 96 PyInfo, 97 factory = py_info_subject, 98 ).uses_shared_libraries().equals(True) 99 100_tests.append(_test_data_sets_uses_shared_library) 101 102def _test_tags_can_be_tuple(name, config): 103 # We don't use a helper because we want to ensure that value passed is 104 # a tuple. 105 config.base_test_rule( 106 name = name + "_subject", 107 tags = ("one", "two") + tuple(PREVENT_IMPLICIT_BUILDING_TAGS), 108 ) 109 analysis_test( 110 name = name, 111 target = name + "_subject", 112 impl = _test_tags_can_be_tuple_impl, 113 ) 114 115def _test_tags_can_be_tuple_impl(env, target): 116 env.expect.that_target(target).tags().contains_at_least([ 117 "one", 118 "two", 119 ]) 120 121_tests.append(_test_tags_can_be_tuple) 122 123def create_base_tests(config): 124 return pt_util.create_tests(_tests, config = config) 125