From 22c3c6b6b56eac54804b9bd173a3c65a9ebea8db Mon Sep 17 00:00:00 2001 From: Lukas Juhrich Date: Sun, 17 Oct 2021 20:06:08 +0200 Subject: [PATCH] Add tests for `BadZipFile` handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Note that the functional test does not actually detect the behavioral change of throwing unhandled `BadZipFile` → throwing unhandled `InvalidWheel`, whereas the unit test does. --- .../packages/corruptwheel-1.0-py2.py3-none-any.whl | 1 + tests/functional/test_install_wheel.py | 11 +++++++++-- tests/unit/test_wheel.py | 9 +++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tests/data/packages/corruptwheel-1.0-py2.py3-none-any.whl diff --git a/tests/data/packages/corruptwheel-1.0-py2.py3-none-any.whl b/tests/data/packages/corruptwheel-1.0-py2.py3-none-any.whl new file mode 100644 index 00000000000..bf285f13f40 --- /dev/null +++ b/tests/data/packages/corruptwheel-1.0-py2.py3-none-any.whl @@ -0,0 +1 @@ +This is a corrupt wheel which _clearly_ is not a zip file. diff --git a/tests/functional/test_install_wheel.py b/tests/functional/test_install_wheel.py index a87fe293311..c7045a9dc5b 100644 --- a/tests/functional/test_install_wheel.py +++ b/tests/functional/test_install_wheel.py @@ -45,13 +45,20 @@ def test_install_from_future_wheel_version(script, tmpdir): result.assert_installed("futurewheel", without_egg_link=True, editable=False) -def test_install_from_broken_wheel(script, data): +@pytest.mark.parametrize( + "wheel_name", + [ + "brokenwheel-1.0-py2.py3-none-any.whl", + "corruptwheel-1.0-py2.py3-none-any.whl", + ], +) +def test_install_from_broken_wheel(script, data, wheel_name): """ Test that installing a broken wheel fails properly """ from tests.lib import TestFailure - package = data.packages.joinpath("brokenwheel-1.0-py2.py3-none-any.whl") + package = data.packages.joinpath(wheel_name) result = script.pip("install", package, "--no-index", expect_error=True) with pytest.raises(TestFailure): result.assert_installed("futurewheel", without_egg_link=True, editable=False) diff --git a/tests/unit/test_wheel.py b/tests/unit/test_wheel.py index 37b5974eb39..682027d152b 100644 --- a/tests/unit/test_wheel.py +++ b/tests/unit/test_wheel.py @@ -252,6 +252,15 @@ def test_wheel_root_is_purelib(text: str, expected: bool) -> None: assert wheel.wheel_root_is_purelib(message_from_string(text)) == expected +def test_dist_from_broken_wheel_fails(data) -> None: + from pip._internal.exceptions import InvalidWheel + from pip._internal.metadata import get_wheel_distribution, FilesystemWheel + + package = data.packages.joinpath("corruptwheel-1.0-py2.py3-none-any.whl") + with pytest.raises(InvalidWheel): + get_wheel_distribution(FilesystemWheel(package), "brokenwheel") + + class TestWheelFile: def test_unpack_wheel_no_flatten(self, tmpdir: Path) -> None: filepath = os.path.join(DATA_DIR, "packages", "meta-1.0-py2.py3-none-any.whl")