|
| 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 | +"""Tests for construction of Python version matching config settings.""" |
| 15 | + |
| 16 | +load("@bazel_skylib//lib:selects.bzl", "selects") |
| 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:truth.bzl", "subjects") |
| 20 | +load("@rules_testing//lib:util.bzl", rt_util = "util") |
| 21 | +load("//python/private:pip_config_settings.bzl", "pip_config_settings") |
| 22 | +load("//python/private:whl_select_dict.bzl", "whl_select_dict") |
| 23 | + |
| 24 | +_PLATFORMS = [ |
| 25 | + "manylinux1_x86_64", |
| 26 | + "manylinux_2_27_aarch64", |
| 27 | + "musllinux_1_1_x86_64", |
| 28 | + "musllinux_1_1_aarch64", |
| 29 | + "macosx_12_0_universal2", |
| 30 | + "macosx_12_0_aarch64", |
| 31 | + "macosx_12_0_x86_64", |
| 32 | + "win_amd64", |
| 33 | +] |
| 34 | + |
| 35 | +_tests = [] |
| 36 | + |
| 37 | +def _subject_impl(ctx): |
| 38 | + _ = ctx # @unused |
| 39 | + return [DefaultInfo()] |
| 40 | + |
| 41 | +_subject = rule( |
| 42 | + implementation = _subject_impl, |
| 43 | + attrs = { |
| 44 | + "all_dists_match": attr.string(), |
| 45 | + "any_match": attr.string(), |
| 46 | + "sdist_match": attr.string(), |
| 47 | + }, |
| 48 | +) |
| 49 | + |
| 50 | +def _test_library_matching(name, test): |
| 51 | + select_values = {} |
| 52 | + dists = [ |
| 53 | + ( |
| 54 | + "foo-0.0.1{}".format(suffix), |
| 55 | + [ |
| 56 | + ("3.11", None), |
| 57 | + ("3.12", None), |
| 58 | + ] if "-cp311-" not in suffix else [ |
| 59 | + ("3.11", None), |
| 60 | + ], |
| 61 | + ) |
| 62 | + for suffix in [ |
| 63 | + ".tar.gz", |
| 64 | + "-py3-none-any.whl", |
| 65 | + #"-py3-abi3-any.whl", |
| 66 | + ] + [ |
| 67 | + "-" + "-".join([py, abi_tag, platform + ".whl"]) |
| 68 | + for py in ["py3"] |
| 69 | + # Let's simulate not all whls being available on cp312 |
| 70 | + for abi_tag in ["none", "abi3", "cp311"] |
| 71 | + for platform in _PLATFORMS |
| 72 | + ] |
| 73 | + ] |
| 74 | + |
| 75 | + attr_dists = { |
| 76 | + "all_dists_match": dists, |
| 77 | + "any_match": [ |
| 78 | + ( |
| 79 | + "foo-0.0.1-py2.py3-none-any.whl", |
| 80 | + [ |
| 81 | + ("3.11", None), |
| 82 | + ], |
| 83 | + ), |
| 84 | + ( |
| 85 | + "foo-0.0.1.tar.gz", |
| 86 | + [ |
| 87 | + ("3.11", None), |
| 88 | + ], |
| 89 | + ), |
| 90 | + ], |
| 91 | + "sdist_match": [ |
| 92 | + ( |
| 93 | + "foo-0.0.1.tar.gz", |
| 94 | + [ |
| 95 | + ("3.11", None), |
| 96 | + ], |
| 97 | + ), |
| 98 | + ], |
| 99 | + } |
| 100 | + for attr, dists in attr_dists.items(): |
| 101 | + if attr not in test.attrs: |
| 102 | + continue |
| 103 | + select_value, _ = whl_select_dict( |
| 104 | + hub_name = "hub", |
| 105 | + default_version = "3.11", |
| 106 | + target_name = "pkg", |
| 107 | + dists = dists, |
| 108 | + condition_package = Label(":unused").package, |
| 109 | + ) |
| 110 | + select_values[attr] = select_value |
| 111 | + |
| 112 | + rt_util.helper_target( |
| 113 | + _subject, |
| 114 | + name = name + "_subject", |
| 115 | + **{ |
| 116 | + k: selects.with_or(v) |
| 117 | + for k, v in select_values.items() |
| 118 | + } |
| 119 | + ) |
| 120 | + |
| 121 | + config_settings = { |
| 122 | + "cp311_linux_aarch64": { |
| 123 | + Label("//python/config_settings:python_version"): "3.11.1", |
| 124 | + "//command_line_option:platforms": Label("//tests/support:linux_aarch64"), |
| 125 | + }, |
| 126 | + "cp311_linux_aarch64_sdist": { |
| 127 | + Label(":whl"): "no", |
| 128 | + Label("//python/config_settings:python_version"): "3.11.1", |
| 129 | + "//command_line_option:platforms": Label("//tests/support:linux_aarch64"), |
| 130 | + }, |
| 131 | + "cp312_osx_aarch64": { |
| 132 | + Label("//python/config_settings:python_version"): "3.12.1", |
| 133 | + "//command_line_option:platforms": Label("//tests/support:osx_aarch64"), |
| 134 | + }, |
| 135 | + "linux_aarch64": { |
| 136 | + "//command_line_option:platforms": Label("//tests/support:linux_aarch64"), |
| 137 | + }, |
| 138 | + "linux_aarch64_musl": { |
| 139 | + Label(":whl_linux_libc"): "musl", |
| 140 | + "//command_line_option:platforms": Label("//tests/support:linux_aarch64"), |
| 141 | + }, |
| 142 | + "osx_aarch64_any": { |
| 143 | + Label(":whl_plat"): "no", |
| 144 | + "//command_line_option:platforms": Label("//tests/support:osx_aarch64"), |
| 145 | + }, |
| 146 | + "osx_aarch64_any_abi_none": { |
| 147 | + Label(":whl_abi3"): "no", |
| 148 | + Label(":whl_cpxy"): "no", |
| 149 | + Label(":whl_plat"): "no", |
| 150 | + "//command_line_option:platforms": Label("//tests/support:osx_aarch64"), |
| 151 | + }, |
| 152 | + "osx_aarch64_multiarch": { |
| 153 | + Label(":whl_osx_arch"): "multiarch", |
| 154 | + "//command_line_option:platforms": Label("//tests/support:osx_aarch64"), |
| 155 | + }, |
| 156 | + } |
| 157 | + |
| 158 | + analysis_test( |
| 159 | + name = name, |
| 160 | + target = name + "_subject", |
| 161 | + impl = test.expect, |
| 162 | + config_settings = { |
| 163 | + str(k): str(v) |
| 164 | + for k, v in config_settings[test.setting].items() |
| 165 | + }, |
| 166 | + ) |
| 167 | + |
| 168 | +def _match(env, target, expect): |
| 169 | + target = env.expect.that_target(target) |
| 170 | + for attr, want in expect.items(): |
| 171 | + target.attr(attr, factory = subjects.str).equals(want) |
| 172 | + |
| 173 | +def _test_matching_defaults(name): |
| 174 | + _test_library_matching( |
| 175 | + name = name, |
| 176 | + test = struct( |
| 177 | + name = "selecting an any wheel by default", |
| 178 | + setting = "linux_aarch64", |
| 179 | + attrs = [ |
| 180 | + "any_match", |
| 181 | + "all_dists_match", |
| 182 | + "sdist_match", |
| 183 | + ], |
| 184 | + expect = lambda env, target: _match(env, target, { |
| 185 | + "all_dists_match": "@hub_foo_0_0_1_py3_cp311_manylinux_2_27_aarch64//:pkg", |
| 186 | + "any_match": "@hub_foo_0_0_1_py3_none_any//:pkg", |
| 187 | + "sdist_match": "@hub_foo_0_0_1_sdist//:pkg", |
| 188 | + }), |
| 189 | + ), |
| 190 | + ) |
| 191 | + |
| 192 | +_tests.append(_test_matching_defaults) |
| 193 | + |
| 194 | +def _test_matching_with_py_version(name): |
| 195 | + _test_library_matching( |
| 196 | + name = name, |
| 197 | + test = struct( |
| 198 | + name = "selecting an any wheel by default", |
| 199 | + setting = "cp311_linux_aarch64", |
| 200 | + attrs = [ |
| 201 | + "any_match", |
| 202 | + "sdist_match", |
| 203 | + ], |
| 204 | + expect = lambda env, target: _match(env, target, { |
| 205 | + "any_match": "@hub_foo_0_0_1_py3_none_any//:pkg", |
| 206 | + "sdist_match": "@hub_foo_0_0_1_sdist//:pkg", |
| 207 | + }), |
| 208 | + ), |
| 209 | + ) |
| 210 | + |
| 211 | +_tests.append(_test_matching_with_py_version) |
| 212 | + |
| 213 | +def _test_using_sdists(name): |
| 214 | + _test_library_matching( |
| 215 | + name = name, |
| 216 | + test = struct( |
| 217 | + setting = "cp311_linux_aarch64_sdist", |
| 218 | + attrs = [ |
| 219 | + "all_dists_match", |
| 220 | + "sdist_match", |
| 221 | + ], |
| 222 | + expect = lambda env, target: _match(env, target, { |
| 223 | + "all_dists_match": "@hub_foo_0_0_1_sdist//:pkg", |
| 224 | + "sdist_match": "@hub_foo_0_0_1_sdist//:pkg", |
| 225 | + }), |
| 226 | + ), |
| 227 | + ) |
| 228 | + |
| 229 | +_tests.append(_test_using_sdists) |
| 230 | + |
| 231 | +def _test_using_osx_fat_wheels(name): |
| 232 | + _test_library_matching( |
| 233 | + name = name, |
| 234 | + test = struct( |
| 235 | + setting = "osx_aarch64_multiarch", |
| 236 | + attrs = [ |
| 237 | + "any_match", |
| 238 | + "all_dists_match", |
| 239 | + "sdist_match", |
| 240 | + ], |
| 241 | + expect = lambda env, target: _match(env, target, { |
| 242 | + "all_dists_match": "@hub_foo_0_0_1_py3_cp311_macosx_12_0_universal2//:pkg", |
| 243 | + "any_match": "@hub_foo_0_0_1_py3_none_any//:pkg", |
| 244 | + "sdist_match": "@hub_foo_0_0_1_sdist//:pkg", |
| 245 | + }), |
| 246 | + ), |
| 247 | + ) |
| 248 | + |
| 249 | +_tests.append(_test_using_osx_fat_wheels) |
| 250 | + |
| 251 | +def _test_prefer_any_whls(name): |
| 252 | + _test_library_matching( |
| 253 | + name = name, |
| 254 | + test = struct( |
| 255 | + setting = "osx_aarch64_any", |
| 256 | + attrs = [ |
| 257 | + "any_match", |
| 258 | + "all_dists_match", |
| 259 | + "sdist_match", |
| 260 | + ], |
| 261 | + expect = lambda env, target: _match(env, target, { |
| 262 | + "all_dists_match": "@hub_foo_0_0_1_py3_none_any//:pkg", |
| 263 | + "any_match": "@hub_foo_0_0_1_py3_none_any//:pkg", |
| 264 | + "sdist_match": "@hub_foo_0_0_1_sdist//:pkg", |
| 265 | + }), |
| 266 | + ), |
| 267 | + ) |
| 268 | + |
| 269 | +_tests.append(_test_prefer_any_whls) |
| 270 | + |
| 271 | +def _test_using_musl_wheels(name): |
| 272 | + _test_library_matching( |
| 273 | + name = name, |
| 274 | + test = struct( |
| 275 | + setting = "linux_aarch64_musl", |
| 276 | + attrs = [ |
| 277 | + "any_match", |
| 278 | + "all_dists_match", |
| 279 | + "sdist_match", |
| 280 | + ], |
| 281 | + expect = lambda env, target: _match(env, target, { |
| 282 | + "all_dists_match": "@hub_foo_0_0_1_py3_cp311_musllinux_1_1_aarch64//:pkg", |
| 283 | + "any_match": "@hub_foo_0_0_1_py3_none_any//:pkg", |
| 284 | + "sdist_match": "@hub_foo_0_0_1_sdist//:pkg", |
| 285 | + }), |
| 286 | + ), |
| 287 | + ) |
| 288 | + |
| 289 | +_tests.append(_test_using_musl_wheels) |
| 290 | + |
| 291 | +def _test_prefer_abi_none_whls(name): |
| 292 | + _test_library_matching( |
| 293 | + name = name, |
| 294 | + test = struct( |
| 295 | + setting = "osx_aarch64_any_abi_none", |
| 296 | + attrs = [ |
| 297 | + "any_match", |
| 298 | + "all_dists_match", |
| 299 | + "sdist_match", |
| 300 | + ], |
| 301 | + expect = lambda env, target: _match(env, target, { |
| 302 | + "all_dists_match": "@hub_foo_0_0_1_py3_none_any//:pkg", |
| 303 | + "any_match": "@hub_foo_0_0_1_py3_none_any//:pkg", |
| 304 | + "sdist_match": "@hub_foo_0_0_1_sdist//:pkg", |
| 305 | + }), |
| 306 | + ), |
| 307 | + ) |
| 308 | + |
| 309 | +_tests.append(_test_prefer_abi_none_whls) |
| 310 | + |
| 311 | +def _test_new_version_of_python(name): |
| 312 | + _test_library_matching( |
| 313 | + name = name, |
| 314 | + test = struct( |
| 315 | + setting = "cp312_osx_aarch64", |
| 316 | + debug = True, |
| 317 | + attrs = [ |
| 318 | + "any_match", |
| 319 | + "all_dists_match", |
| 320 | + "sdist_match", |
| 321 | + ], |
| 322 | + expect = lambda env, target: _match(env, target, { |
| 323 | + "all_dists_match": "@hub_foo_0_0_1_py3_abi3_macosx_12_0_aarch64//:pkg", |
| 324 | + "any_match": "@hub_foo_0_0_1_py3_none_any//:pkg", |
| 325 | + "sdist_match": "@hub_foo_0_0_1_sdist//:pkg", |
| 326 | + }), |
| 327 | + ), |
| 328 | + ) |
| 329 | + |
| 330 | +_tests.append(_test_new_version_of_python) |
| 331 | + |
| 332 | +def pip_config_settings_test_suite(name): # buildifier: disable=function-docstring |
| 333 | + test_suite( |
| 334 | + name = name, |
| 335 | + tests = _tests, |
| 336 | + ) |
| 337 | + pip_config_settings( |
| 338 | + name = "config", |
| 339 | + python_versions = ["3.11", "3.12"], |
| 340 | + whl_platforms = _PLATFORMS, |
| 341 | + ) |
0 commit comments