|
| 1 | +# Copyright 2024 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 | +"""py_common tests.""" |
| 15 | + |
| 16 | +load("@rules_python_internal//:rules_python_config.bzl", "config") |
| 17 | +load("@rules_testing//lib:analysis_test.bzl", "analysis_test") |
| 18 | +load("@rules_testing//lib:test_suite.bzl", "test_suite") |
| 19 | +load("@rules_testing//lib:util.bzl", rt_util = "util") |
| 20 | +load("//python/api:api.bzl", _py_common = "py_common") |
| 21 | +load("//tests/support:py_info_subject.bzl", "py_info_subject") |
| 22 | + |
| 23 | +_tests = [] |
| 24 | + |
| 25 | +def _test_merge_py_infos(name): |
| 26 | + rt_util.helper_target( |
| 27 | + native.filegroup, |
| 28 | + name = name + "_subject", |
| 29 | + srcs = ["f1.py", "f1.pyc", "f2.py", "f2.pyc"], |
| 30 | + ) |
| 31 | + analysis_test( |
| 32 | + name = name, |
| 33 | + impl = _test_merge_py_infos_impl, |
| 34 | + target = name + "_subject", |
| 35 | + attrs = _py_common.API_ATTRS, |
| 36 | + ) |
| 37 | + |
| 38 | +def _test_merge_py_infos_impl(env, target): |
| 39 | + f1_py, f1_pyc, f2_py, f2_pyc = target[DefaultInfo].files.to_list() |
| 40 | + |
| 41 | + py_common = _py_common.get(env.ctx) |
| 42 | + |
| 43 | + py1 = py_common.PyInfoBuilder() |
| 44 | + if config.enable_pystar: |
| 45 | + py1.direct_pyc_files.add(f1_pyc) |
| 46 | + py1.transitive_sources.add(f1_py) |
| 47 | + |
| 48 | + py2 = py_common.PyInfoBuilder() |
| 49 | + if config.enable_pystar: |
| 50 | + py1.direct_pyc_files.add(f2_pyc) |
| 51 | + py2.transitive_sources.add(f2_py) |
| 52 | + |
| 53 | + actual = py_info_subject( |
| 54 | + py_common.merge_py_infos([py2.build()], direct = [py1.build()]), |
| 55 | + meta = env.expect.meta, |
| 56 | + ) |
| 57 | + |
| 58 | + actual.transitive_sources().contains_exactly([f1_py.path, f2_py.path]) |
| 59 | + if config.enable_pystar: |
| 60 | + actual.direct_pyc_files().contains_exactly([f1_pyc.path, f2_pyc.path]) |
| 61 | + |
| 62 | +_tests.append(_test_merge_py_infos) |
| 63 | + |
| 64 | +def py_common_test_suite(name): |
| 65 | + test_suite( |
| 66 | + name = name, |
| 67 | + tests = _tests, |
| 68 | + ) |
0 commit comments