|
| 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 | + |
| 15 | +"" |
| 16 | + |
| 17 | +load("@rules_testing//lib:test_suite.bzl", "test_suite") |
| 18 | +load("//python/private/pypi:extension.bzl", "parse_modules") # buildifier: disable=bzl-visibility |
| 19 | + |
| 20 | +_tests = [] |
| 21 | + |
| 22 | +def _mock_mctx(*modules, environ = {}, read = None): |
| 23 | + return struct( |
| 24 | + os = struct( |
| 25 | + environ = environ, |
| 26 | + name = "unittest", |
| 27 | + arch = "exotic", |
| 28 | + ), |
| 29 | + read = read or {}.get, |
| 30 | + modules = [ |
| 31 | + struct( |
| 32 | + name = modules[0].name, |
| 33 | + tags = modules[0].tags, |
| 34 | + is_root = modules[0].is_root, |
| 35 | + ), |
| 36 | + ] + [ |
| 37 | + struct( |
| 38 | + name = mod.name, |
| 39 | + tags = mod.tags, |
| 40 | + is_root = False, |
| 41 | + ) |
| 42 | + for mod in modules[1:] |
| 43 | + ], |
| 44 | + ) |
| 45 | + |
| 46 | +def _mod(*, name, parse = [], override = [], whl_mods = [], is_root = True): |
| 47 | + return struct( |
| 48 | + name = name, |
| 49 | + tags = struct( |
| 50 | + parse = parse, |
| 51 | + override = override, |
| 52 | + whl_mods = whl_mods, |
| 53 | + ), |
| 54 | + is_root = is_root, |
| 55 | + ) |
| 56 | + |
| 57 | +def _parse( |
| 58 | + *, |
| 59 | + hub_name, |
| 60 | + python_version, |
| 61 | + experimental_index_url = "", |
| 62 | + requirements_lock = None, |
| 63 | + **kwargs): |
| 64 | + return struct( |
| 65 | + hub_name = hub_name, |
| 66 | + python_version = python_version, |
| 67 | + requirements_lock = requirements_lock, |
| 68 | + # Constants for now |
| 69 | + # TODO @aignas 2024-10-21: cover with tests |
| 70 | + auth_patterns = {}, |
| 71 | + download_only = False, |
| 72 | + enable_implicit_namespace_pkgs = False, |
| 73 | + environment = {}, |
| 74 | + envsubst = {}, |
| 75 | + experimental_index_url = experimental_index_url, |
| 76 | + experimental_requirement_cycles = {}, |
| 77 | + experimental_target_platforms = [], |
| 78 | + extra_pip_args = [], |
| 79 | + isolated = False, |
| 80 | + netrc = None, |
| 81 | + pip_data_exclude = None, |
| 82 | + python_interpreter = None, |
| 83 | + python_interpreter_target = None, |
| 84 | + quiet = False, |
| 85 | + requirements_by_platform = {}, |
| 86 | + requirements_darwin = None, |
| 87 | + requirements_linux = None, |
| 88 | + requirements_windows = None, |
| 89 | + timeout = 42, |
| 90 | + whl_modifications = {}, |
| 91 | + _evaluate_markers_srcs = [], |
| 92 | + **kwargs |
| 93 | + ) |
| 94 | + |
| 95 | +def _test_simple(env): |
| 96 | + pypi = parse_modules( |
| 97 | + module_ctx = _mock_mctx( |
| 98 | + _mod(name = "rules_python", parse = [_parse( |
| 99 | + hub_name = "pypi", |
| 100 | + python_version = "3.15", |
| 101 | + requirements_lock = "requirements.txt", |
| 102 | + )]), |
| 103 | + read = { |
| 104 | + "requirements.txt": "", |
| 105 | + }.get, |
| 106 | + ), |
| 107 | + available_interpreters = { |
| 108 | + "python_3_15_host": "unit_test_interpreter_target", |
| 109 | + }, |
| 110 | + ) |
| 111 | + |
| 112 | + env.expect.that_dict(pypi.exposed_packages).contains_exactly({"pypi": []}) |
| 113 | + env.expect.that_dict(pypi.hub_group_map).contains_exactly({"pypi": {}}) |
| 114 | + env.expect.that_dict(pypi.hub_whl_map).contains_exactly({}) |
| 115 | + env.expect.that_bool(pypi.is_reproducible).equals(True) |
| 116 | + env.expect.that_dict(pypi.whl_libraries).contains_exactly({}) |
| 117 | + env.expect.that_dict(pypi.whl_mods).contains_exactly({}) |
| 118 | + |
| 119 | +_tests.append(_test_simple) |
| 120 | + |
| 121 | +def extension_test_suite(name): |
| 122 | + """Create the test suite. |
| 123 | +
|
| 124 | + Args: |
| 125 | + name: the name of the test suite |
| 126 | + """ |
| 127 | + test_suite(name = name, basic_tests = _tests) |
0 commit comments