|
| 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 | + |
| 16 | +load("@rules_testing//lib:analysis_test.bzl", "analysis_test") |
| 17 | +load("@rules_testing//lib:truth.bzl", "matching") |
| 18 | +load("@rules_testing//lib:util.bzl", rt_util = "util") |
| 19 | +load("//python:defs.bzl", "PyInfo") |
| 20 | +load("//tools/build_defs/python/tests:py_info_subject.bzl", "py_info_subject") |
| 21 | +load("//tools/build_defs/python/tests:util.bzl", pt_util = "util") |
| 22 | + |
| 23 | +_tests = [] |
| 24 | + |
| 25 | +def _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 | + |
| 33 | +def _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 | + |
| 50 | +def _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 | + |
| 58 | +def _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 | + |
| 75 | +def _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 | + |
| 82 | +def _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 | + |
| 94 | +def _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 | + |
| 102 | +def create_base_tests(config): |
| 103 | + return pt_util.create_tests(_tests, config = config) |
0 commit comments