|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import importlib.util |
| 4 | +import os |
3 | 5 | import shutil |
4 | 6 | import subprocess |
| 7 | +import sys |
5 | 8 | import zipfile |
6 | 9 | from contextlib import suppress |
7 | 10 | from pathlib import Path |
| 11 | +from typing import Final |
8 | 12 |
|
9 | 13 | import pytest |
10 | 14 | from python_discovery import PythonInfo |
11 | 15 |
|
| 16 | +from virtualenv.app_data import AppDataDiskFolder |
12 | 17 | from virtualenv.info import fs_supports_symlink |
13 | 18 | from virtualenv.run import cli_run |
| 19 | +from virtualenv.seed.wheels.periodic_update import trigger_update |
14 | 20 |
|
15 | 21 | HERE = Path(__file__).parent |
16 | 22 | CURRENT = PythonInfo.current_system() |
| 23 | +# Repackaged sdists omit the zipapp build sources. |
| 24 | +_ZIPAPP_MAIN: Final = pytest.importorskip("tasks.__main__zipapp") |
17 | 25 |
|
18 | 26 |
|
19 | 27 | @pytest.fixture(scope="session") |
@@ -84,6 +92,75 @@ def zipapp_test_env(tmp_path_factory): |
84 | 92 | shutil.rmtree(str(base_path)) |
85 | 93 |
|
86 | 94 |
|
| 95 | +@pytest.mark.timeout(600) |
| 96 | +def test_zipapp_loader_context(zipapp: Path, monkeypatch: pytest.MonkeyPatch) -> None: |
| 97 | + monkeypatch.setattr("tasks.__main__zipapp.ABS_HERE", str(zipapp)) |
| 98 | + monkeypatch.delitem(sys.modules, "filelock", raising=False) |
| 99 | + with _ZIPAPP_MAIN.VersionedFindLoad(): |
| 100 | + spec: Final = importlib.util.find_spec("filelock") |
| 101 | + assert spec is not None |
| 102 | + assert spec.origin is not None |
| 103 | + assert spec.origin.startswith(str(zipapp)) |
| 104 | + restored: Final = importlib.util.find_spec("filelock") |
| 105 | + assert restored is not None |
| 106 | + assert restored.origin is not None |
| 107 | + assert not restored.origin.startswith(str(zipapp)) |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.timeout(600) |
| 111 | +def test_zipapp_version(zipapp: Path, monkeypatch: pytest.MonkeyPatch) -> None: |
| 112 | + monkeypatch.setattr("tasks.__main__zipapp.ABS_HERE", str(zipapp)) |
| 113 | + monkeypatch.setattr(sys, "argv", [str(zipapp), "--version"]) |
| 114 | + with pytest.raises(SystemExit) as context: |
| 115 | + _ZIPAPP_MAIN.run() |
| 116 | + assert context.value.code == 0 |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.timeout(600) |
| 120 | +def test_zipapp_update_command( |
| 121 | + zipapp: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str] |
| 122 | +) -> None: |
| 123 | + monkeypatch.setattr("virtualenv.seed.wheels.periodic_update.IS_ZIPAPP", True) |
| 124 | + monkeypatch.setattr("virtualenv.seed.wheels.periodic_update.ROOT", str(zipapp)) |
| 125 | + monkeypatch.setenv("PIP_NO_INDEX", "1") |
| 126 | + trigger_update( |
| 127 | + "pip", |
| 128 | + f"{sys.version_info.major}.{sys.version_info.minor}", |
| 129 | + None, |
| 130 | + [], |
| 131 | + AppDataDiskFolder(str(tmp_path / "data")), |
| 132 | + {"_VIRTUALENV_PERIODIC_UPDATE_INLINE": "1"}, |
| 133 | + True, |
| 134 | + ) |
| 135 | + assert "done pip" in capfd.readouterr().out |
| 136 | + |
| 137 | + |
| 138 | +@pytest.mark.timeout(600) |
| 139 | +def test_zipapp_update_without_installed_package(zipapp: Path, zipapp_test_env: Path, tmp_path: Path) -> None: |
| 140 | + result: Final = subprocess.run( |
| 141 | + [ |
| 142 | + str(zipapp_test_env), |
| 143 | + "-I", |
| 144 | + "-c", |
| 145 | + ( |
| 146 | + f"import runpy\nwith runpy.run_path({str(zipapp)!r}, run_name='__loader__')['VersionedFindLoad']():\n" |
| 147 | + " import sys\n" |
| 148 | + " from virtualenv.app_data import AppDataDiskFolder\n" |
| 149 | + " from virtualenv.seed.wheels.periodic_update import trigger_update\n" |
| 150 | + f" trigger_update('pip', f'{{sys.version_info.major}}.{{sys.version_info.minor}}', None, [], " |
| 151 | + f"AppDataDiskFolder({str(tmp_path / 'data')!r}), {{'_VIRTUALENV_PERIODIC_UPDATE_INLINE': '1'}}, True)\n" |
| 152 | + ), |
| 153 | + ], |
| 154 | + env={**os.environ, "PIP_NO_INDEX": "1"}, |
| 155 | + capture_output=True, |
| 156 | + text=True, |
| 157 | + encoding="utf-8", |
| 158 | + timeout=60, |
| 159 | + check=True, |
| 160 | + ) |
| 161 | + assert "done pip" in result.stdout |
| 162 | + |
| 163 | + |
87 | 164 | @pytest.fixture |
88 | 165 | def call_zipapp(zipapp, tmp_path, zipapp_test_env, temp_app_data): # ruff:ignore[unused-function-argument] |
89 | 166 | def _run(*args) -> None: |
|
0 commit comments