|
3 | 3 | import os |
4 | 4 | import subprocess |
5 | 5 | import sys |
| 6 | +from shutil import copytree |
6 | 7 | from typing import TYPE_CHECKING, Final |
| 8 | +from zipfile import ZipFile |
7 | 9 |
|
8 | 10 | import pytest |
9 | 11 |
|
| 12 | +from virtualenv.info import fs_supports_symlink |
10 | 13 | from virtualenv.run import cli_run |
11 | | -from virtualenv.seed.wheels.embed import get_embed_wheel |
| 14 | +from virtualenv.seed.wheels.embed import BUNDLE_FOLDER, BUNDLE_SUPPORT, get_embed_wheel |
| 15 | +from virtualenv.util.path import safe_delete |
12 | 16 |
|
13 | 17 | if TYPE_CHECKING: |
14 | 18 | from pathlib import Path |
15 | 19 |
|
| 20 | + from pytest_mock import MockerFixture |
| 21 | + |
| 22 | + from virtualenv.run.session import Session |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.skipif(not fs_supports_symlink(), reason="symlinks are unavailable") |
| 26 | +def test_pip_reseed_keeps_shared_image(linked_seed: Session) -> None: |
| 27 | + package: Final = linked_seed.creator.purelib / "pip" |
| 28 | + image: Final = package.resolve().parent |
| 29 | + before: Final = { |
| 30 | + str(path.relative_to(image)): (path.read_bytes(), path.stat().st_mode) |
| 31 | + for path in image.rglob("*") |
| 32 | + if path.is_file() |
| 33 | + } |
| 34 | + cli_run([str(linked_seed.creator.dest), "--seeder", "pip", "--pip", "embed", "--no-periodic-update"]) |
| 35 | + assert { |
| 36 | + str(path.relative_to(image)): (path.read_bytes(), path.stat().st_mode) |
| 37 | + for path in image.rglob("*") |
| 38 | + if path.is_file() |
| 39 | + } == before |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.skipif(not fs_supports_symlink(), reason="symlinks are unavailable") |
| 43 | +def test_pip_reseed_installs_local_package(linked_seed: Session) -> None: |
| 44 | + cli_run([str(linked_seed.creator.dest), "--seeder", "pip", "--pip", "embed", "--no-periodic-update"]) |
| 45 | + result: Final = subprocess.run( |
| 46 | + [str(linked_seed.creator.exe), "-I", "-m", "pip", "--version"], |
| 47 | + capture_output=True, |
| 48 | + text=True, |
| 49 | + encoding="utf-8", |
| 50 | + check=True, |
| 51 | + ) |
| 52 | + assert not (linked_seed.creator.purelib / "pip").is_symlink() |
| 53 | + assert os.path.normcase(str(linked_seed.creator.purelib)) in os.path.normcase(result.stdout) |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.skipif(not fs_supports_symlink(), reason="symlinks are unavailable") |
| 57 | +def test_pip_reseed_keeps_unrelated_launcher(linked_seed: Session) -> None: |
| 58 | + unrelated: Final = linked_seed.creator.script_dir / "unrelated-tool" |
| 59 | + unrelated.symlink_to(linked_seed.creator.dest / "missing-tool") |
| 60 | + cli_run([str(linked_seed.creator.dest), "--seeder", "pip", "--pip", "embed", "--no-periodic-update"]) |
| 61 | + assert (unrelated.is_symlink(), unrelated.exists()) == (True, False) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.skipif(not fs_supports_symlink(), reason="symlinks are unavailable") |
| 65 | +@pytest.mark.parametrize("name", [pytest.param("__main__.py", id="file"), pytest.param("_internal", id="directory")]) |
| 66 | +def test_pip_reseed_replaces_nested_link(linked_seed: Session, name: str) -> None: |
| 67 | + package: Final = linked_seed.creator.purelib / "pip" |
| 68 | + source: Final = package.resolve() |
| 69 | + package.unlink() |
| 70 | + copytree(source, package) |
| 71 | + for path in package.rglob("*"): |
| 72 | + if path.is_file(): |
| 73 | + path.chmod(0o600) |
| 74 | + entry: Final = package / name |
| 75 | + directory: Final = entry.is_dir() |
| 76 | + if directory: |
| 77 | + safe_delete(entry) |
| 78 | + else: |
| 79 | + entry.unlink() |
| 80 | + entry.symlink_to(source / name, target_is_directory=directory) |
| 81 | + cli_run([str(linked_seed.creator.dest), "--seeder", "pip", "--pip", "embed", "--no-periodic-update"]) |
| 82 | + assert (entry.exists(), entry.is_symlink()) == (True, False) |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.skipif(not fs_supports_symlink(), reason="symlinks are unavailable") |
| 86 | +def test_pip_reseed_replaces_launcher_link(linked_seed: Session) -> None: |
| 87 | + launcher: Final = linked_seed.creator.script_dir / ("pip.exe" if os.name == "nt" else "pip") |
| 88 | + launcher.unlink() |
| 89 | + missing: Final = linked_seed.creator.dest / "missing-launcher" |
| 90 | + launcher.symlink_to(missing) |
| 91 | + cli_run([str(linked_seed.creator.dest), "--seeder", "pip", "--pip", "embed", "--no-periodic-update"]) |
| 92 | + assert (launcher.is_file(), launcher.is_symlink(), missing.exists()) == (True, False, False) |
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.parametrize( |
| 96 | + "invalid", [pytest.param("entry", id="package-name"), pytest.param("script", id="script-name")] |
| 97 | +) |
| 98 | +def test_pip_reseed_rejects_invalid_wheel_names(tmp_path: Path, invalid: str) -> None: |
| 99 | + with ZipFile(tmp_path / "pip-99.0-py3-none-any.whl", "w") as archive: |
| 100 | + archive.writestr("pip-99.0.dist-info/METADATA", "Name: pip\nVersion: 99.0\n") |
| 101 | + if invalid == "entry": |
| 102 | + archive.writestr("./", "") |
| 103 | + else: |
| 104 | + archive.writestr("pip-99.0.dist-info/entry_points.txt", "[console_scripts]\n. = pip:main\n") |
| 105 | + with pytest.raises(RuntimeError, match=f"invalid seed wheel {invalid}"): |
| 106 | + cli_run([ |
| 107 | + str(tmp_path / "env"), |
| 108 | + "--seeder", |
| 109 | + "pip", |
| 110 | + "--pip", |
| 111 | + "99.0", |
| 112 | + "--extra-search-dir", |
| 113 | + str(tmp_path), |
| 114 | + "--no-download", |
| 115 | + "--no-periodic-update", |
| 116 | + ]) |
| 117 | + |
| 118 | + |
| 119 | +def test_pip_reseed_reports_process_failure(tmp_path: Path, mocker: MockerFixture) -> None: |
| 120 | + process: Final = mocker.patch("virtualenv.seed.embed.pip_invoke.Popen", autospec=True) |
| 121 | + process.return_value.configure_mock(returncode=7) |
| 122 | + with pytest.raises(RuntimeError, match="failed seed with code 7"): |
| 123 | + cli_run([str(tmp_path / "env"), "--seeder", "pip", "--pip", "embed", "--no-periodic-update"]) |
| 124 | + |
| 125 | + |
| 126 | +def test_pip_reseed_reports_missing_wheel(tmp_path: Path) -> None: |
| 127 | + with pytest.raises(RuntimeError, match="could not get wheel for distribution pip"): |
| 128 | + cli_run([str(tmp_path / "env"), "--seeder", "pip", "--pip", "99.1234", "--no-download", "--no-periodic-update"]) |
| 129 | + |
| 130 | + |
| 131 | +@pytest.fixture |
| 132 | +def linked_seed(tmp_path: Path, current_fastest: str) -> Session: |
| 133 | + return cli_run([ |
| 134 | + str(tmp_path / "env"), |
| 135 | + "--creator", |
| 136 | + current_fastest, |
| 137 | + "--pip", |
| 138 | + "embed", |
| 139 | + "--no-periodic-update", |
| 140 | + "--symlink-app-data", |
| 141 | + "--app-data", |
| 142 | + str(tmp_path / "cache"), |
| 143 | + ]) |
| 144 | + |
16 | 145 |
|
17 | 146 | @pytest.mark.parametrize("safe_path", [pytest.param("", id="default"), pytest.param("1", id="safe-path")]) |
18 | 147 | def test_pip_invoke_uses_selected_wheel( |
@@ -102,3 +231,26 @@ def test_pip_invoke_freeze_uses_version_pin(tmp_path: Path) -> None: |
102 | 231 | ) |
103 | 232 | version: Final = get_embed_wheel("pip", f"{sys.version_info.major}.{sys.version_info.minor}").version |
104 | 233 | assert f"pip=={version}" in result.stdout.splitlines() |
| 234 | + |
| 235 | + |
| 236 | +@pytest.mark.skipif(not fs_supports_symlink(), reason="symlinks are unavailable") |
| 237 | +@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires two compatible bundled pip versions") |
| 238 | +def test_pip_reseed_removes_previous_version_metadata(tmp_path: Path) -> None: |
| 239 | + old: Final = BUNDLE_SUPPORT["3.9"]["pip"].split("-")[1] |
| 240 | + session: Final = cli_run([ |
| 241 | + str(tmp_path / "env"), |
| 242 | + "--pip", |
| 243 | + old, |
| 244 | + "--extra-search-dir", |
| 245 | + str(BUNDLE_FOLDER), |
| 246 | + "--symlink-app-data", |
| 247 | + "--no-periodic-update", |
| 248 | + "--app-data", |
| 249 | + str(tmp_path / "cache"), |
| 250 | + ]) |
| 251 | + cli_run([str(session.creator.dest), "--seeder", "pip", "--pip", "embed", "--no-periodic-update"]) |
| 252 | + expected: Final = BUNDLE_SUPPORT[f"{sys.version_info.major}.{sys.version_info.minor}"]["pip"].split("-")[1] |
| 253 | + assert sorted(entry.name for entry in session.creator.purelib.glob("pip-*.dist-info")) == [ |
| 254 | + f"pip-{expected}.dist-info", |
| 255 | + ] |
| 256 | + assert not list(session.creator.purelib.glob(f"pip-{old}.virtualenv")) |
0 commit comments