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"""Test for py_wheel.""" 15 16load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite") 17load("@rules_testing//lib:util.bzl", rt_util = "util") 18load("//python:packaging.bzl", "py_wheel") 19 20_tests = [] 21 22def _test_metadata(name): 23 rt_util.helper_target( 24 py_wheel, 25 name = name + "_subject", 26 distribution = "mydist_" + name, 27 version = "0.0.0", 28 ) 29 analysis_test( 30 name = name, 31 impl = _test_metadata_impl, 32 target = name + "_subject", 33 ) 34 35def _test_metadata_impl(env, target): 36 action = env.expect.that_target(target).action_generating( 37 "{package}/{name}.metadata.txt", 38 ) 39 action.content().split("\n").contains_exactly([ 40 env.expect.meta.format_str("Name: mydist_{test_name}"), 41 "Metadata-Version: 2.1", 42 "", 43 ]) 44 45_tests.append(_test_metadata) 46 47def _test_content_type_from_attr(name): 48 rt_util.helper_target( 49 py_wheel, 50 name = name + "_subject", 51 distribution = "mydist_" + name, 52 version = "0.0.0", 53 description_content_type = "text/x-rst", 54 ) 55 analysis_test( 56 name = name, 57 impl = _test_content_type_from_attr_impl, 58 target = name + "_subject", 59 ) 60 61def _test_content_type_from_attr_impl(env, target): 62 action = env.expect.that_target(target).action_generating( 63 "{package}/{name}.metadata.txt", 64 ) 65 action.content().split("\n").contains( 66 "Description-Content-Type: text/x-rst", 67 ) 68 69_tests.append(_test_content_type_from_attr) 70 71def _test_content_type_from_description(name): 72 rt_util.helper_target( 73 py_wheel, 74 name = name + "_subject", 75 distribution = "mydist_" + name, 76 version = "0.0.0", 77 description_file = "desc.md", 78 ) 79 analysis_test( 80 name = name, 81 impl = _test_content_type_from_description_impl, 82 target = name + "_subject", 83 ) 84 85def _test_content_type_from_description_impl(env, target): 86 action = env.expect.that_target(target).action_generating( 87 "{package}/{name}.metadata.txt", 88 ) 89 action.content().split("\n").contains( 90 "Description-Content-Type: text/markdown", 91 ) 92 93_tests.append(_test_content_type_from_description) 94 95def py_wheel_test_suite(name): 96 test_suite( 97 name = name, 98 tests = _tests, 99 ) 100