diff --git a/docs/pypi/patch.md b/docs/pypi/patch.md index 7e3cb41981..57320702a4 100644 --- a/docs/pypi/patch.md +++ b/docs/pypi/patch.md @@ -8,3 +8,32 @@ Sometimes the wheels have to be patched to: * Include certain PRs of your choice on top of wheels and avoid building from sdist. You can patch the wheels by using the {attr}`pip.override.patches` attribute. + +To patch a wheel file, use {attr}`pip.override.file`: + +```starlark +pip.override( + file = "requests-2.25.1-py2.py3-none-any.whl", + patch_strip = 1, + patches = ["//patches:requests.patch"], +) +``` + +To patch all of the wheels of a package, e.g. the wheels for each platform or +the wheels that are built from an sdist, use {attr}`pip.override.package`. It +can be limited to a version (`foo@1.2.3`) and to a hub +({attr}`pip.override.hub_name`): + +```starlark +pip.override( + package = "matplotlib", + hub_name = "my_deps", + patch_strip = 1, + patches = ["//patches:matplotlib.patch"], +) +``` + +:::{note} +The patches are applied with `patch_strip = 0` by default, so use +`patch_strip = 1` for patches created with `git diff`. +::: diff --git a/docs/pypi/rules-pycross-comparison.md b/docs/pypi/rules-pycross-comparison.md index 86181b75d3..54a30406b9 100644 --- a/docs/pypi/rules-pycross-comparison.md +++ b/docs/pypi/rules-pycross-comparison.md @@ -91,16 +91,9 @@ pip.whl_mods( data_exclude_glob = ["site-packages/tests/**"], ) -# planned: patch a package by name -# pip.override( -# hub_name = "uv_deps", -# package = "matplotlib", -# patches = ["//:matplotlib.patch"], -# patch_strip = 1, -# ) -# Today: one `pip.override` per wheel file, e.g. one for each platform. pip.override( - file = "matplotlib-3.10.9-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", + hub_name = "uv_deps", + package = "matplotlib", patches = ["//:matplotlib.patch"], patch_strip = 1, ) @@ -121,7 +114,7 @@ The `BUILD.bazel` files keep using `@uv_deps//ipython`, | `ignore_dependencies` | none | planned | | `extra_dependencies` | {attr}`pip.whl_mods.data` (runfiles only) | partial, planned | | `install_exclude_globs` | {attr}`pip.whl_mods.srcs_exclude_glob`, {attr}`pip.whl_mods.data_exclude_glob`, {attr}`pip.parse.pip_data_exclude` | have; the globs start with `site-packages/` | -| `post_install_patches` | {attr}`pip.override.patches` for a wheel file name | partial, per-package: planned | +| `post_install_patches` | {attr}`pip.override.package` with {attr}`pip.override.patches` | have; the wheel is patched before it is extracted | | `site_paths`, `bin_paths`, `data_paths`, `include_paths` | a fixed `site-packages`, `bin`, `data`, `include` layout | not needed | | `wheel_library_tags` | none | planned | | `precompile` | the {flag}`--precompile` flag for all packages | partial | @@ -216,7 +209,7 @@ module. Do not depend on the same distribution from both in one binary. | extras targets | requires {gh-pr}`4490` and {gh-pr}`4523` | have | | `ignore_dependencies`, `extra_dependencies`, `wheel_library_tags` | none | planned | | per-package `precompile` | global flags | partial | -| `post_install_patches` | {attr}`pip.override.patches` per wheel file | partial | +| `post_install_patches` | {attr}`pip.override.package` | have | | `install_exclude_globs` | {obj}`pip.whl_mods`, {attr}`pip.parse.pip_data_exclude` | have | | `build_mode` | {attr}`pip.parse.download_only` for `"never"` | partial | | `build_target` | none | planned | diff --git a/news/pip-override-package.added.md b/news/pip-override-package.added.md new file mode 100644 index 0000000000..a53d0ec582 --- /dev/null +++ b/news/pip-override-package.added.md @@ -0,0 +1,5 @@ +(pypi) The {obj}`pip.override` tag class now accepts a +{attr}`pip.override.package` (optionally with a `@version`) instead of a wheel +{attr}`pip.override.file` to patch all of the wheels of a package, including +the wheels built from an sdist, and a {attr}`pip.override.hub_name` to limit it +to one hub (bzlmod only). diff --git a/python/private/pypi/BUILD.bazel b/python/private/pypi/BUILD.bazel index e34579d741..7f67eb9fbc 100644 --- a/python/private/pypi/BUILD.bazel +++ b/python/private/pypi/BUILD.bazel @@ -159,7 +159,6 @@ bzl_library( ":hub_builder", ":hub_exposure", ":hub_repository", - ":parse_whl_name", ":pep508_env", ":pip_repository_attrs", ":platform", @@ -167,6 +166,7 @@ bzl_library( ":simpleapi_download", ":unified_hub_repo", ":whl_library", + ":whl_overrides", "//python/private:auth", "//python/private:normalize_name", "//python/private:pyproject_utils", @@ -574,6 +574,15 @@ bzl_library( ], ) +bzl_library( + name = "whl_overrides", + srcs = ["whl_overrides.bzl"], + deps = [ + ":parse_whl_name", + "//python/private:normalize_name", + ], +) + bzl_library( name = "whl_repo_name", srcs = ["whl_repo_name.bzl"], diff --git a/python/private/pypi/extension.bzl b/python/private/pypi/extension.bzl index 8ddc3c5847..d064d71aa5 100644 --- a/python/private/pypi/extension.bzl +++ b/python/private/pypi/extension.bzl @@ -26,7 +26,6 @@ load(":default_platforms.bzl", "default_platforms") load(":hub_builder.bzl", "hub_builder") load(":hub_exposure.bzl", "add_extras", "hub_exposure") load(":hub_repository.bzl", "hub_repository", "whl_config_settings_to_json") -load(":parse_whl_name.bzl", "parse_whl_name") load(":pep508_env.bzl", "env") load(":pip_repository_attrs.bzl", "ATTRS") load(":platform.bzl", _plat = "platform") @@ -34,6 +33,7 @@ load(":pypi_cache.bzl", "pypi_cache") load(":simpleapi_download.bzl", "simpleapi_download") load(":unified_hub_repo.bzl", "unified_hub_repo") load(":whl_library.bzl", "whl_library") +load(":whl_overrides.bzl", "parse_whl_overrides", "unmatched_whl_overrides", "whl_overrides_for_hub") def _whl_mods_impl(whl_mods_dict): """Implementation of the pip.whl_mods tag class. @@ -214,36 +214,7 @@ You cannot use both the additive_build_content and additive_build_content_file a config = build_config(module_ctx = module_ctx, enable_pipstar_extract = enable_pipstar_extract) - # TODO @aignas 2025-06-03: Merge override API with the builder? - _overriden_whl_set = {} - whl_overrides = {} - for module in module_ctx.modules: - for attr in module.tags.override: - if not module.is_root: - # Overrides are only supported in root modules. Silently - # ignore the override: - continue - - if not attr.file.endswith(".whl"): - fail("Only whl overrides are supported at this time") - - whl_name = normalize_name(parse_whl_name(attr.file).distribution) - - if attr.file in _overriden_whl_set: - fail("Duplicate module overrides for '{}'".format(attr.file)) - _overriden_whl_set[attr.file] = None - - for patch in attr.patches: - if whl_name not in whl_overrides: - whl_overrides[whl_name] = {} - - if patch not in whl_overrides[whl_name]: - whl_overrides[whl_name][patch] = struct( - patch_strip = attr.patch_strip, - whls = [], - ) - - whl_overrides[whl_name][patch].whls.append(attr.file) + whl_overrides = parse_whl_overrides(module_ctx.modules) # Used to track all the different pip hubs and the spoke pip Python # versions. @@ -256,6 +227,10 @@ You cannot use both the additive_build_content and additive_build_content_file a is_pypi_hub_reserved = module_ctx.getenv("RULES_PYTHON_PYPI_HUB_RESERVED", "0") == "1" renamed_default_hub = None + # The hub names as declared by the users, before the reserved `pypi` hub is + # renamed. + declared_hub_names = {} + for mod in module_ctx.modules: for pip_attr in mod.tags.parse: python_version = pip_attr.python_version @@ -296,12 +271,13 @@ You cannot use both the additive_build_content and additive_build_content_file a ), ) # buildifier: disable=print + declared_hub_names[hub_name] = pip_attr.hub_name if hub_name not in pip_hub_map: builder = hub_builder( name = hub_name, module_name = mod.name, config = config, - whl_overrides = whl_overrides, + whl_overrides = whl_overrides_for_hub(whl_overrides, pip_attr.hub_name), simpleapi_download_fn = simpleapi_download, simpleapi_cache = simpleapi_cache, # TODO @aignas 2025-09-06: do not use kwargs @@ -374,6 +350,13 @@ You cannot use both the additive_build_content and additive_build_content_file a packages = out.whl_map.keys(), ) + errors = unmatched_whl_overrides(whl_overrides, { + declared_hub_names.get(name, name): hub.packages + for name, hub in hubs.items() + }) + if errors: + fail("\n".join(errors)) + exposure = hub_exposure( module_ctx, hubs = hubs, @@ -1072,8 +1055,36 @@ _override_tag = tag_class( doc = """\ The Python distribution file name which needs to be patched. This will be applied to all repositories that setup this distribution via the pip.parse tag -class.""", - mandatory = True, +class. + +Exactly one of `file` or {attr}`pip.override.package` needs to be set. + +:::{versionchanged} VERSION_NEXT_FEATURE +It is no longer mandatory, use {attr}`pip.override.package` to patch all of the +wheels of a package. +:::""", + ), + "hub_name": attr.string( + doc = """\ +The hub that the {attr}`pip.override.package` override applies to. If empty, +it applies to all of the hubs. + +:::{versionadded} VERSION_NEXT_FEATURE +:::""", + ), + "package": attr.string( + doc = """\ +The package which needs to be patched, e.g. `foo`, or a specific version of it, +e.g. `foo@1.2.3`. The version is compared with the version in the wheel file +name using the PEP 440 `==` semantics, so `foo@1.2` matches `foo-1.2.0+cpu`. A +version that matches no wheel is ignored. The patches apply to all of the wheels +of the package, including the wheels that are built from an sdist, so that a +single override works on all of the platforms. + +Exactly one of {attr}`pip.override.file` or `package` needs to be set. + +:::{versionadded} VERSION_NEXT_FEATURE +:::""", ), "patch_strip": attr.int( default = 0, diff --git a/python/private/pypi/patch_whl.bzl b/python/private/pypi/patch_whl.bzl index 7910808863..49187e4b06 100644 --- a/python/private/pypi/patch_whl.bzl +++ b/python/private/pypi/patch_whl.bzl @@ -21,6 +21,7 @@ the RECORD file natively in Starlark and repack using Bazel's zipper """ load("//python/private:repo_utils.bzl", "repo_utils") +load("//python/private:version.bzl", pep440 = "version") load(":parse_whl_name.bzl", "parse_whl_name") _DUMMY_SHA256 = "sha256=0" @@ -62,17 +63,40 @@ def get_whl_patches(whl_patches, filename): """Select the patches that apply to a wheel filename. Args: - whl_patches: Patch labels mapped to JSON-encoded wheel names and strip levels. + whl_patches: Patch labels mapped to JSON-encoded args with the strip + level (`patch_strip`) and the wheel file names (`whls`) or the + package versions (`versions`, `*` for all versions) that the patch + applies to. The versions are compared with the PEP 440 `==` + semantics, so `1.2` matches `1.2.0` and `1.2.0+local`. filename: The wheel filename to match. Returns: A dictionary of matching patch labels mapped to strip levels. """ patches = {} + version = None for patch_file, json_args in whl_patches.items(): - patch_dst = struct(**json.decode(json_args)) - if filename in patch_dst.whls: - patches[patch_file] = patch_dst.patch_strip + patch_dst = json.decode(json_args) + if filename in patch_dst.get("whls", []): + patches[patch_file] = patch_dst["patch_strip"] + continue + + versions = patch_dst.get("versions", []) + if not versions: + continue + + if "*" in versions: + patches[patch_file] = patch_dst["patch_strip"] + continue + + if version == None: + version = parse_whl_name(filename).version + whl_version = pep440.parse(version) + for v in versions: + want = pep440.parse(v) + if whl_version and want and pep440.is_eq(whl_version, want) or v == version: + patches[patch_file] = patch_dst["patch_strip"] + break return patches def patch_whl(rctx, *, whl_path, patches): diff --git a/python/private/pypi/whl_archive.bzl b/python/private/pypi/whl_archive.bzl index 3ba32b1aa1..aea89ba34e 100644 --- a/python/private/pypi/whl_archive.bzl +++ b/python/private/pypi/whl_archive.bzl @@ -72,9 +72,9 @@ A label-keyed-string dict with patch files as keys and json-strings as values. The keys are labels to the patch file to apply. The values describe what to apply the patch to and how to apply it. -It is encoded as `json.encode(struct([whls], patch_strip])`, -where `whls` is a `list[str`] of wheel filenames, and `patch_strip` -is a number. +It is encoded as `json.encode(struct([whls], [versions], patch_strip])`, +where `whls` is a `list[str`] of wheel filenames, `versions` is a `list[str]` +of package versions (`*` for all versions) and `patch_strip` is a number. So it will look something like this: ``` diff --git a/python/private/pypi/whl_overrides.bzl b/python/private/pypi/whl_overrides.bzl new file mode 100644 index 0000000000..83e31fdcea --- /dev/null +++ b/python/private/pypi/whl_overrides.bzl @@ -0,0 +1,154 @@ +"""Parse the `pip.override` tags into the patches for each hub.""" + +load("//python/private:normalize_name.bzl", "normalize_name") +load(":parse_whl_name.bzl", "parse_whl_name") + +def parse_whl_overrides(modules, *, _fail = fail): + """Collect the `pip.override` tags of the root module. + + Args: + modules: {type}`list[module]` the `module_ctx.modules`. + _fail: {type}`callable` the function to call on errors, for tests. + + Returns: + {type}`struct` with `files` - the overrides by the wheel file name, and + `packages` - the overrides by the package name, keyed by the hub name + (an empty string for all hubs). + """ + files = {} + packages = {} + seen_files = {} + seen_packages = {} + for module in modules: + for attr in module.tags.override: + if not module.is_root: + # Overrides are only supported in root modules. Silently + # ignore the override: + continue + + if bool(attr.file) == bool(attr.package): + _fail("pip.override needs exactly one of 'file' or 'package', got: file = '{}', package = '{}'".format(attr.file, attr.package)) + continue + + if attr.file: + if attr.hub_name: + _fail("pip.override(file = '{}') applies to all hubs and does not support 'hub_name'".format(attr.file)) + continue + + if not attr.file.endswith(".whl"): + _fail("Only whl overrides are supported at this time") + continue + + whl_name = normalize_name(parse_whl_name(attr.file).distribution) + + if attr.file in seen_files: + _fail("Duplicate module overrides for '{}'".format(attr.file)) + continue + seen_files[attr.file] = None + + for patch in attr.patches: + patches = files.setdefault(whl_name, {}) + if patch not in patches: + patches[patch] = struct( + patch_strip = attr.patch_strip, + whls = [], + ) + + patches[patch].whls.append(attr.file) + continue + + name, sep, version = attr.package.partition("@") + if (sep and not version) or not name or [c for c in "=<>!~ ;[".elems() if c in attr.package]: + _fail("pip.override(package = '{}') needs to be a package name or '@'".format(attr.package)) + continue + whl_name = normalize_name(name) + key = (attr.hub_name, whl_name, version) + if key in seen_packages: + _fail("Duplicate module overrides for package '{}'{}".format( + attr.package, + " in hub '{}'".format(attr.hub_name) if attr.hub_name else "", + )) + continue + seen_packages[key] = None + + patches = packages.setdefault(attr.hub_name, {}).setdefault(whl_name, {}) + for patch in attr.patches: + if patch in patches and patches[patch].patch_strip != attr.patch_strip: + _fail("The patch '{}' for package '{}' is used with different 'patch_strip' values, use a copy of the patch file".format(patch, attr.package)) + continue + patches.setdefault(patch, struct( + patch_strip = attr.patch_strip, + versions = [], + )).versions.append(version or "*") + + return struct( + files = files, + packages = packages, + ) + +def whl_overrides_for_hub(overrides, hub_name, *, _fail = fail): + """Get the patches of the packages in a hub. + + Args: + overrides: {type}`struct` the result of {func}`parse_whl_overrides`. + hub_name: {type}`str` the name of the hub. + _fail: {type}`callable` the function to call on errors, for tests. + + Returns: + {type}`dict[str, dict[Label, struct]]` the patches for each package + name with the `patch_strip` and the wheel file names (`whls`) or the + versions (`versions`, `*` for all) that they apply to. + """ + ret = {} + for whl_name, patches in overrides.files.items(): + ret[whl_name] = dict(patches) + + for key in ["", hub_name]: + for whl_name, patches in overrides.packages.get(key, {}).items(): + whl_patches = ret.setdefault(whl_name, {}) + for patch, args in patches.items(): + existing = whl_patches.get(patch) + if not existing: + whl_patches[patch] = args + continue + + if existing.patch_strip != args.patch_strip: + _fail("The patch '{}' for package '{}' is used with different 'patch_strip' values, use a copy of the patch file".format(patch, whl_name)) + continue + + merged = { + "patch_strip": existing.patch_strip, + "versions": getattr(existing, "versions", []) + args.versions, + } + if getattr(existing, "whls", None): + merged["whls"] = existing.whls + whl_patches[patch] = struct(**merged) + + return ret + +def unmatched_whl_overrides(overrides, hub_packages): + """Find the package overrides that do not match any package. + + Args: + overrides: {type}`struct` the result of {func}`parse_whl_overrides`. + hub_packages: {type}`dict[str, list[str]]` the normalized package names of + each hub. + + Returns: + {type}`list[str]` the error messages, empty if all overrides match. + """ + errors = [] + for hub_name, packages in sorted(overrides.packages.items()): + if hub_name and hub_name not in hub_packages: + errors.append("pip.override(hub_name = '{}') refers to a hub that does not exist".format(hub_name)) + continue + + hubs = [hub_name] if hub_name else hub_packages.keys() + for whl_name in sorted(packages): + if not [h for h in hubs if whl_name in hub_packages[h]]: + errors.append("pip.override(package = '{}') does not match any package{}".format( + whl_name, + " in hub '{}'".format(hub_name) if hub_name else "", + )) + + return errors diff --git a/tests/pypi/patch_whl/patch_whl_tests.bzl b/tests/pypi/patch_whl/patch_whl_tests.bzl index 9577605c89..467c6ff169 100644 --- a/tests/pypi/patch_whl/patch_whl_tests.bzl +++ b/tests/pypi/patch_whl/patch_whl_tests.bzl @@ -18,6 +18,7 @@ load("@rules_testing//lib:test_suite.bzl", "test_suite") load( "//python/private/pypi:patch_whl.bzl", "fix_record_content", + "get_whl_patches", "patched_whl_name", "zipper_params_content", ) # buildifier: disable=bzl-visibility @@ -156,6 +157,32 @@ def _test_zipper_params_unsupported_names(env): _tests.append(_test_zipper_params_unsupported_names) +def _test_get_whl_patches(env): + whl_patches = { + "//:all.patch": json.encode({"patch_strip": 1, "versions": ["*"]}), + "//:file.patch": json.encode({"patch_strip": 0, "whls": ["foo-1.0-py3-none-any.whl"]}), + "//:v2.patch": json.encode({"patch_strip": 2, "versions": ["2.0"]}), + } + + for filename, want in { + "foo-1.0-cp313-cp313-manylinux_2_17_x86_64.whl": {"//:all.patch": 1}, + "foo-1.0-py3-none-any.whl": {"//:all.patch": 1, "//:file.patch": 0}, + "foo-2.0+cpu-py3-none-any.whl": {"//:all.patch": 1, "//:v2.patch": 2}, + "foo-2.0-py3-none-any.whl": {"//:all.patch": 1, "//:v2.patch": 2}, + # The versions are compared with the PEP 440 `==` semantics + "foo-2.0.0-py3-none-any.whl": {"//:all.patch": 1, "//:v2.patch": 2}, + "foo-2.0.1-py3-none-any.whl": {"//:all.patch": 1}, + }.items(): + env.expect.where(filename = filename).that_dict(get_whl_patches(whl_patches, filename)).contains_exactly(want) + + # The args written by older versions only have `whls` + env.expect.that_dict(get_whl_patches( + {"//:file.patch": json.encode({"patch_strip": 0, "whls": ["foo-1.0-py3-none-any.whl"]})}, + "foo-1.0-py3-none-any.whl", + )).contains_exactly({"//:file.patch": 0}) + +_tests.append(_test_get_whl_patches) + def patch_whl_test_suite(name): """Create the test suite. diff --git a/tests/pypi/whl_overrides/BUILD.bazel b/tests/pypi/whl_overrides/BUILD.bazel new file mode 100644 index 0000000000..7ee9af7ef3 --- /dev/null +++ b/tests/pypi/whl_overrides/BUILD.bazel @@ -0,0 +1,3 @@ +load(":whl_overrides_tests.bzl", "whl_overrides_test_suite") + +whl_overrides_test_suite(name = "whl_overrides_tests") diff --git a/tests/pypi/whl_overrides/whl_overrides_tests.bzl b/tests/pypi/whl_overrides/whl_overrides_tests.bzl new file mode 100644 index 0000000000..092be75aa4 --- /dev/null +++ b/tests/pypi/whl_overrides/whl_overrides_tests.bzl @@ -0,0 +1,121 @@ +"" + +load("@rules_testing//lib:test_suite.bzl", "test_suite") +load("//python/private/pypi:whl_overrides.bzl", "parse_whl_overrides", "unmatched_whl_overrides", "whl_overrides_for_hub") # buildifier: disable=bzl-visibility + +_tests = [] + +def _override(*, file = "", package = "", hub_name = "", patches = [], patch_strip = 0): + return struct( + file = file, + hub_name = hub_name, + package = package, + patch_strip = patch_strip, + patches = patches, + ) + +def _mod(*overrides, is_root = True): + return struct( + is_root = is_root, + tags = struct(override = list(overrides)), + ) + +def _parse(*modules): + failures = [] + got = parse_whl_overrides(list(modules), _fail = failures.append) + return got, failures + +def _encode(overrides): + """Make the result comparable.""" + return { + whl: {patch: json.decode(json.encode(args)) for patch, args in patches.items()} + for whl, patches in overrides.items() + } + +def _test_file_and_package(env): + got, failures = _parse( + _mod( + _override(file = "foo-1.0-py3-none-any.whl", patches = ["//:file.patch"], patch_strip = 1), + _override(package = "Foo", patches = ["//:all.patch"]), + _override(package = "foo@2.0", hub_name = "pypi", patches = ["//:all.patch", "//:v2.patch"]), + _override(package = "bar", hub_name = "other", patches = ["//:bar.patch"]), + ), + # Overrides from other modules are ignored + _mod(_override(package = "baz", patches = ["//:baz.patch"]), is_root = False), + ) + env.expect.that_collection(failures).contains_exactly([]) + + env.expect.that_dict(_encode(whl_overrides_for_hub(got, "pypi"))).contains_exactly({ + "foo": { + "//:all.patch": {"patch_strip": 0, "versions": ["*", "2.0"]}, + "//:file.patch": {"patch_strip": 1, "whls": ["foo-1.0-py3-none-any.whl"]}, + "//:v2.patch": {"patch_strip": 0, "versions": ["2.0"]}, + }, + }) + env.expect.that_dict(_encode(whl_overrides_for_hub(got, "other"))).contains_exactly({ + "bar": { + "//:bar.patch": {"patch_strip": 0, "versions": ["*"]}, + }, + "foo": { + "//:all.patch": {"patch_strip": 0, "versions": ["*"]}, + "//:file.patch": {"patch_strip": 1, "whls": ["foo-1.0-py3-none-any.whl"]}, + }, + }) + + env.expect.that_collection(unmatched_whl_overrides(got, { + "other": ["bar"], + "pypi": ["foo"], + })).contains_exactly([]) + env.expect.that_collection(unmatched_whl_overrides(got, { + "other": ["foo"], + })).contains_exactly([ + "pip.override(package = 'bar') does not match any package in hub 'other'", + "pip.override(hub_name = 'pypi') refers to a hub that does not exist", + ]) + +_tests.append(_test_file_and_package) + +def _test_errors(env): + for overrides, want in [ + ([_override(patches = ["//:a.patch"])], "pip.override needs exactly one of 'file' or 'package'"), + ([_override(file = "foo-1.0-py3-none-any.whl", package = "foo")], "pip.override needs exactly one of 'file' or 'package'"), + ([_override(file = "foo-1.0-py3-none-any.whl", hub_name = "pypi")], "does not support 'hub_name'"), + ([_override(file = "foo-1.0.tar.gz")], "Only whl overrides are supported"), + ([_override(package = "foo@", patches = ["//:a.patch"])], "needs to be a package name or '@'"), + ([_override(package = "foo==1.0", patches = ["//:a.patch"])], "needs to be a package name or '@'"), + ( + [_override(package = "foo", patches = ["//:a.patch"]), _override(package = "foo", patches = ["//:b.patch"])], + "Duplicate module overrides for package 'foo'", + ), + ( + [ + _override(package = "foo", patches = ["//:a.patch"]), + _override(package = "foo@1.0", patches = ["//:a.patch"], patch_strip = 1), + ], + "The patch '//:a.patch' for package 'foo@1.0' is used with different 'patch_strip' values", + ), + ]: + _, failures = _parse(_mod(*overrides)) + env.expect.where(overrides = overrides).that_int(len(failures)).equals(1) + env.expect.that_str(failures[0]).contains(want) + + # The strip level of the same patch for a file and a package must match + got, failures = _parse(_mod( + _override(file = "foo-1.0-py3-none-any.whl", patches = ["//:a.patch"], patch_strip = 1), + _override(package = "foo", patches = ["//:a.patch"]), + )) + env.expect.that_collection(failures).contains_exactly([]) + whl_overrides_for_hub(got, "pypi", _fail = failures.append) + env.expect.that_collection(failures).contains_exactly([ + "The patch '//:a.patch' for package 'foo' is used with different 'patch_strip' values, use a copy of the patch file", + ]) + +_tests.append(_test_errors) + +def whl_overrides_test_suite(name): + """Create the test suite. + + Args: + name: the name of the test suite + """ + test_suite(name = name, basic_tests = _tests)