|
| 1 | +"""Test that pip index versions handles invalid (non-PEP 440) wheel filenames. |
| 2 | +
|
| 3 | +This test was added for robustness after legacy wheel filename support |
| 4 | +was removed in pip 25.3. |
| 5 | +""" |
| 6 | + |
| 7 | +import json |
| 8 | +import textwrap |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +from tests.lib import PipTestEnvironment |
| 12 | +from tests.lib.wheel import make_wheel |
| 13 | + |
| 14 | + |
| 15 | +def _create_test_index_with_invalid_wheels( |
| 16 | + tmpdir: Path, package_name: str = "pkg" |
| 17 | +) -> Path: |
| 18 | + """Create a test index with both valid and invalid wheel filenames. |
| 19 | +
|
| 20 | + Returns the path to the index directory. |
| 21 | + """ |
| 22 | + # Create test index |
| 23 | + index_dir = tmpdir / "test_index" |
| 24 | + index_dir.mkdir() |
| 25 | + |
| 26 | + (index_dir / "index.html").write_text( |
| 27 | + textwrap.dedent( |
| 28 | + f"""\ |
| 29 | + <!DOCTYPE html> |
| 30 | + <html> |
| 31 | + <body><a href="{package_name}/index.html">{package_name}</a></body> |
| 32 | + </html> |
| 33 | + """ |
| 34 | + ) |
| 35 | + ) |
| 36 | + |
| 37 | + pkg_dir = index_dir / package_name |
| 38 | + pkg_dir.mkdir() |
| 39 | + |
| 40 | + valid_wheels = [ |
| 41 | + (f"{package_name}-1.0.0-py3-none-any.whl", "1.0.0"), |
| 42 | + (f"{package_name}-2.0.0-py3-none-any.whl", "2.0.0"), |
| 43 | + ] |
| 44 | + invalid_wheels = [ |
| 45 | + (f"{package_name}-3.0_1-py3-none-any.whl", "3.0"), # underscore in version |
| 46 | + (f"{package_name}-_bad_-py3-none-any.wh", "1.0.0"), # no version |
| 47 | + ( |
| 48 | + f"{package_name}-5.0.0_build1-py3-none-any.whl", |
| 49 | + "5.0.0", |
| 50 | + ), # underscore in build tag |
| 51 | + ] |
| 52 | + |
| 53 | + all_wheels = valid_wheels + invalid_wheels |
| 54 | + for wheel_name, version in all_wheels: |
| 55 | + wheel = make_wheel(name=package_name, version=version) |
| 56 | + wheel.save_to(pkg_dir / wheel_name) |
| 57 | + |
| 58 | + # Create package index |
| 59 | + links = [ |
| 60 | + f'<a href="{wheel_name}">{wheel_name}</a><br/>' for wheel_name, _ in all_wheels |
| 61 | + ] |
| 62 | + (pkg_dir / "index.html").write_text( |
| 63 | + textwrap.dedent( |
| 64 | + f"""\ |
| 65 | + <!DOCTYPE html> |
| 66 | + <html> |
| 67 | + <body> |
| 68 | + {''.join(links)} |
| 69 | + </body> |
| 70 | + </html> |
| 71 | + """ |
| 72 | + ) |
| 73 | + ) |
| 74 | + |
| 75 | + return index_dir |
| 76 | + |
| 77 | + |
| 78 | +def test_index_versions_ignores_invalid_wheel_names( |
| 79 | + script: PipTestEnvironment, |
| 80 | + tmpdir: Path, |
| 81 | +) -> None: |
| 82 | + """Test that pip index versions ignores invalid wheel names.""" |
| 83 | + index_dir = _create_test_index_with_invalid_wheels(tmpdir) |
| 84 | + |
| 85 | + # Run pip index versions with JSON output |
| 86 | + result = script.pip( |
| 87 | + "index", "versions", "pkg", "--json", "--index-url", index_dir.as_uri() |
| 88 | + ) |
| 89 | + |
| 90 | + assert result.returncode == 0 |
| 91 | + |
| 92 | + output = json.loads(result.stdout) |
| 93 | + assert output["name"] == "pkg" |
| 94 | + assert output["latest"] == "2.0.0" |
| 95 | + |
| 96 | + expected_versions = ["2.0.0", "1.0.0"] |
| 97 | + assert output["versions"] == expected_versions |
| 98 | + |
| 99 | + |
| 100 | +def test_install_ignores_invalid_wheel_names( |
| 101 | + script: PipTestEnvironment, |
| 102 | + tmpdir: Path, |
| 103 | +) -> None: |
| 104 | + """Test that pip install ignores invalid wheel names and installs valid ones.""" |
| 105 | + index_dir = _create_test_index_with_invalid_wheels(tmpdir) |
| 106 | + |
| 107 | + # Run pip install - should ignore invalid wheels and install the latest valid one |
| 108 | + result = script.pip( |
| 109 | + "install", "pkg", "--no-cache-dir", "--index-url", index_dir.as_uri() |
| 110 | + ) |
| 111 | + |
| 112 | + assert result.returncode == 0 |
| 113 | + script.assert_installed(pkg="2.0.0") |
0 commit comments