-
-
Notifications
You must be signed in to change notification settings - Fork 418
/
test_installer.py
293 lines (241 loc) · 11.1 KB
/
test_installer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from __future__ import annotations
import logging
import os
import venv
from pathlib import Path
from typing import Callable
import pytest
from unearth import Link
from pdm import utils
from pdm.core import Core
from pdm.environments.base import BaseEnvironment
from pdm.environments.local import PythonLocalEnvironment
from pdm.environments.python import PythonEnvironment
from pdm.installers import InstallManager
from pdm.models.cached_package import CachedPackage
from pdm.models.candidates import Candidate
from pdm.models.requirements import parse_requirement
from pdm.project.core import Project
from tests import FIXTURES
pytestmark = pytest.mark.usefixtures("local_finder")
@pytest.fixture()
def supports_link(preferred: str | None, monkeypatch: pytest.MonkeyPatch) -> Callable[[str], bool]:
original = utils.fs_supports_link_method
def mocked_support(linker: str) -> bool:
if preferred is None:
return False
if preferred == "hardlink" and linker == "symlink":
return False
return original(linker)
monkeypatch.setattr(utils, "fs_supports_link_method", mocked_support)
return mocked_support
def _prepare_project_for_env(project: Project, env_cls: type[BaseEnvironment]):
project._saved_python = None
project._python = None
if env_cls is PythonEnvironment:
venv.create(project.root / ".venv", symlinks=True)
project.project_config["python.use_venv"] = True
@pytest.fixture(params=(PythonEnvironment, PythonLocalEnvironment), autouse=True)
def environment(request: pytest.RequestFixture, project: Project) -> type[BaseEnvironment]:
# Run all test against all environments as installation and cache behavior may differ
env_cls: type[BaseEnvironment] = request.param
_prepare_project_for_env(project, env_cls)
return env_cls
def test_install_wheel_with_inconsistent_dist_info(project):
req = parse_requirement("pyfunctional")
candidate = Candidate(
req,
link=Link("http://fixtures.test/artifacts/PyFunctional-1.4.3-py3-none-any.whl"),
)
installer = InstallManager(project.environment)
installer.install(candidate)
assert "pyfunctional" in project.environment.get_working_set()
def test_install_with_file_existing(project):
req = parse_requirement("demo")
candidate = Candidate(
req,
link=Link("http://fixtures.test/artifacts/demo-0.0.1-py2.py3-none-any.whl"),
)
lib_path = project.environment.get_paths()["purelib"]
os.makedirs(lib_path, exist_ok=True)
with open(os.path.join(lib_path, "demo.py"), "w") as fp:
fp.write("print('hello')\n")
installer = InstallManager(project.environment)
installer.install(candidate)
def test_uninstall_commit_rollback(project):
req = parse_requirement("demo")
candidate = Candidate(
req,
link=Link("http://fixtures.test/artifacts/demo-0.0.1-py2.py3-none-any.whl"),
)
installer = InstallManager(project.environment)
lib_path = project.environment.get_paths()["purelib"]
installer.install(candidate)
lib_file = os.path.join(lib_path, "demo.py")
assert os.path.exists(lib_file)
remove_paths = installer.get_paths_to_remove(project.environment.get_working_set()["demo"])
remove_paths.remove()
assert not os.path.exists(lib_file)
remove_paths.rollback()
assert os.path.exists(lib_file)
def test_rollback_after_commit(project, caplog):
caplog.set_level(logging.ERROR, logger="pdm.termui")
req = parse_requirement("demo")
candidate = Candidate(
req,
link=Link("http://fixtures.test/artifacts/demo-0.0.1-py2.py3-none-any.whl"),
)
installer = InstallManager(project.environment)
lib_path = project.environment.get_paths()["purelib"]
installer.install(candidate)
lib_file = os.path.join(lib_path, "demo.py")
assert os.path.exists(lib_file)
remove_paths = installer.get_paths_to_remove(project.environment.get_working_set()["demo"])
remove_paths.remove()
remove_paths.commit()
assert not os.path.exists(lib_file)
caplog.clear()
remove_paths.rollback()
assert not os.path.exists(lib_file)
assert any(record.message == "Can't rollback, not uninstalled yet" for record in caplog.records)
@pytest.mark.parametrize("use_install_cache", [False, True])
def test_uninstall_with_console_scripts(project, use_install_cache):
req = parse_requirement("celery")
candidate = Candidate(
req,
link=Link("http://fixtures.test/artifacts/celery-4.4.2-py2.py3-none-any.whl"),
)
installer = InstallManager(project.environment, use_install_cache=use_install_cache)
installer.install(candidate)
celery_script = os.path.join(
project.environment.get_paths()["scripts"],
"celery.exe" if os.name == "nt" else "celery",
)
assert os.path.exists(celery_script)
installer.uninstall(project.environment.get_working_set()["celery"])
assert not os.path.exists(celery_script)
@pytest.mark.parametrize("preferred", ["symlink", "hardlink", None])
def test_install_wheel_with_cache(project, pdm, supports_link):
req = parse_requirement("future-fstrings")
candidate = Candidate(
req,
link=Link("http://fixtures.test/artifacts/future_fstrings-1.2.0-py2.py3-none-any.whl"),
)
installer = InstallManager(project.environment, use_install_cache=True)
installer.install(candidate)
lib_path = project.environment.get_paths()["purelib"]
if supports_link("symlink"):
assert os.path.islink(os.path.join(lib_path, "future_fstrings.py"))
assert os.path.islink(os.path.join(lib_path, "aaaaa_future_fstrings.pth"))
else:
assert os.path.isfile(os.path.join(lib_path, "future_fstrings.py"))
assert os.path.isfile(os.path.join(lib_path, "aaaaa_future_fstrings.pth"))
for file in CachedPackage.cache_files:
assert not os.path.exists(os.path.join(lib_path, file))
cache_name = "future_fstrings-1.2.0-py2.py3-none-any.whl.cache"
assert any(p.path.name == cache_name for p in project.package_cache.iter_packages())
pdm(["run", "python", "-m", "site"], object=project)
r = pdm(["run", "python", "-c", "import future_fstrings"], obj=project)
assert r.exit_code == 0
pdm(["cache", "clear", "packages"], obj=project, strict=True)
assert supports_link("symlink") is any(p.path.name == cache_name for p in project.package_cache.iter_packages())
dist = project.environment.get_working_set()["future-fstrings"]
installer.uninstall(dist)
assert not os.path.exists(os.path.join(lib_path, "future_fstrings.py"))
assert not os.path.exists(os.path.join(lib_path, "aaaaa_future_fstrings.pth"))
assert not dist.read_text("direct_url.json")
pdm(["cache", "clear", "packages"], obj=project, strict=True)
assert not any(p.path.name == cache_name for p in project.package_cache.iter_packages())
@pytest.mark.parametrize("preferred", ["symlink", "hardlink", None])
def test_can_install_wheel_with_cache_in_multiple_projects(
project: Project, core: Core, supports_link, tmp_path_factory, environment
):
projects = []
for idx in range(3):
path: Path = tmp_path_factory.mktemp(f"project-{idx}")
p = core.create_project(path, global_config=project.global_config.config_file)
_prepare_project_for_env(p, environment)
projects.append(p)
req = parse_requirement("future-fstrings")
candidate = Candidate(
req,
link=Link("http://fixtures.test/artifacts/future_fstrings-1.2.0-py2.py3-none-any.whl"),
)
for p in projects:
installer = InstallManager(p.environment, use_install_cache=True)
installer.install(candidate)
lib_path = p.environment.get_paths()["purelib"]
if supports_link("symlink"):
assert os.path.islink(os.path.join(lib_path, "future_fstrings.py"))
assert os.path.islink(os.path.join(lib_path, "aaaaa_future_fstrings.pth"))
else:
assert os.path.isfile(os.path.join(lib_path, "future_fstrings.py"))
assert os.path.isfile(os.path.join(lib_path, "aaaaa_future_fstrings.pth"))
for file in CachedPackage.cache_files:
assert not os.path.exists(os.path.join(lib_path, file))
def test_url_requirement_is_not_cached(project):
req = parse_requirement(
"future-fstrings @ http://fixtures.test/artifacts/future_fstrings-1.2.0-py2.py3-none-any.whl"
)
candidate = Candidate(req)
installer = InstallManager(project.environment, use_install_cache=True)
installer.install(candidate)
cache_path = project.cache("packages") / "future_fstrings-1.2.0-py2.py3-none-any"
assert not cache_path.is_dir()
lib_path = project.environment.get_paths()["purelib"]
assert os.path.isfile(os.path.join(lib_path, "future_fstrings.py"))
assert os.path.isfile(os.path.join(lib_path, "aaaaa_future_fstrings.pth"))
dist = project.environment.get_working_set()["future-fstrings"]
assert dist.read_text("direct_url.json")
def test_editable_is_not_cached(project, tmp_path_factory):
editable_path: Path = tmp_path_factory.mktemp("editable-project")
editable_setup = editable_path / "setup.py"
editable_setup.write_text("""
from setuptools import setup
setup(name='editable-project',
version='0.1.0',
description='',
py_modules=['module'],
)
""")
editable_module = editable_path / "module.py"
editable_module.write_text("")
req = parse_requirement(f"file://{editable_path}#egg=editable-project", True)
candidate = Candidate(req)
installer = InstallManager(project.environment, use_install_cache=True)
installer.install(candidate)
cache_path = project.cache("packages") / "editable_project-0.1.0-0.editable-py3-none-any.whl.cache"
assert not cache_path.is_dir()
lib_path = Path(project.environment.get_paths()["purelib"])
for pth in lib_path.glob("*editable_project*.pth"):
assert pth.is_file()
assert not pth.is_symlink()
@pytest.mark.parametrize("use_install_cache", [False, True])
def test_install_wheel_with_data_scripts(project, use_install_cache):
req = parse_requirement("jmespath")
candidate = Candidate(
req,
link=Link("http://fixtures.test/artifacts/jmespath-0.10.0-py2.py3-none-any.whl"),
)
installer = InstallManager(project.environment, use_install_cache=use_install_cache)
installer.install(candidate)
bin_path = os.path.join(project.environment.get_paths()["scripts"], "jp.py")
assert os.path.isfile(bin_path)
if os.name != "nt":
assert os.stat(bin_path).st_mode & 0o100
dist = project.environment.get_working_set()["jmespath"]
installer.uninstall(dist)
assert not os.path.exists(bin_path)
def test_compress_file_list_for_rename():
from pdm.installers.uninstallers import compress_for_rename
project_root = str(FIXTURES / "projects")
paths = {
"test-removal/subdir",
"test-removal/subdir/__init__.py",
"test-removal/__init__.py",
"test-removal/bar.py",
"test-removal/foo.py",
"test-removal/non_exist.py",
}
abs_paths = {os.path.join(project_root, path) for path in paths}
assert sorted(compress_for_rename(abs_paths)) == [os.path.join(project_root, "test-removal" + os.sep)]