Debian Codegraph @

tests/tox_env/python/test_pylock.py

raw
1 from __future__ import annotations
2
3 import sys
4 from textwrap import dedent
5 from typing import TYPE_CHECKING
6
7 import pytest
8
9 from tox.tox_env.errors import Fail
10 from tox.tox_env.python.pylock import Pylock
11
12 if TYPE_CHECKING:
13 from pathlib import Path
14
15 from tox.pytest import ToxProjectCreator
16
17 PYLOCK_TOML = dedent("""\
18 lock-version = "1.0"
19 created-by = "test-tool"
20
21 [[packages]]
22 name = "alpha"
23 version = "1.0.0"
24
25 [[packages.wheels]]
26 url = "https://files.example.com/alpha-1.0.0-py3-none-any.whl"
27
28 [packages.wheels.hashes]
29 sha256 = "abc123"
30
31 [[packages]]
32 name = "beta"
33 version = "2.0.0"
34
35 [packages.sdist]
36 url = "https://files.example.com/beta-2.0.0.tar.gz"
37
38 [packages.sdist.hashes]
39 sha256 = "def456"
40 """)
41
42
43 def test_pylock_install(tox_project: ToxProjectCreator) -> None:
44 project = tox_project(
45 {
46 "tox.toml": """
47 [env_run_base]
48 skip_install = true
49 pylock = "pylock.toml"
50 """,
51 "pylock.toml": PYLOCK_TOML,
52 },
53 )
54 execute_calls = project.patch_execute()
55 result = project.run("r", "-e", "py")
56
57 result.assert_success()
58
59 req_file = str(project.path / ".tox" / "py" / "pylock.txt")
60 found_calls = [(i[0][0].conf.name, i[0][3].run_id, i[0][3].cmd) for i in execute_calls.call_args_list]
61 assert found_calls == [
62 ("py", "install_pylock", ["python", "-I", "-m", "pip", "install", "--no-deps", "-r", req_file]),
63 ]
64 assert (project.path / ".tox" / "py" / "pylock.txt").read_text() == "alpha==1.0.0\nbeta==2.0.0"
65
66
67 def test_pylock_empty(tox_project: ToxProjectCreator) -> None:
68 project = tox_project(
69 {
70 "tox.toml": """
71 [env_run_base]
72 skip_install = true
73 """,
74 },
75 )
76 execute_calls = project.patch_execute()
77 result = project.run("r", "-e", "py")
78
79 result.assert_success()
80 assert not execute_calls.call_args_list
81
82
83 def test_pylock_not_found(tox_project: ToxProjectCreator) -> None:
84 project = tox_project(
85 {
86 "tox.toml": """
87 [env_run_base]
88 skip_install = true
89 pylock = "missing.toml"
90 """,
91 },
92 )
93 result = project.run("r", "-e", "py")
94
95 result.assert_failed()
96 assert "pylock file 'missing.toml' not found" in result.out
97
98
99 def test_pylock_invalid_toml(tox_project: ToxProjectCreator) -> None:
100 project = tox_project(
101 {
102 "tox.toml": """
103 [env_run_base]
104 skip_install = true
105 pylock = "pylock.toml"
106 """,
107 "pylock.toml": dedent("""\
108 lock-version = "1.0"
109 created-by = "test-tool"
110 packages = "not-a-list"
111 """),
112 },
113 )
114 result = project.run("r", "-e", "py")
115
116 result.assert_failed()
117 assert "invalid pylock file" in result.out
118
119
120 def test_pylock_mutually_exclusive_with_deps(tox_project: ToxProjectCreator) -> None:
121 project = tox_project(
122 {
123 "tox.toml": """
124 [env_run_base]
125 skip_install = true
126 deps = ["pytest"]
127 pylock = "pylock.toml"
128 """,
129 "pylock.toml": PYLOCK_TOML,
130 },
131 )
132 result = project.run("r", "-e", "py")
133
134 result.assert_failed()
135 assert "cannot use both 'deps' and 'pylock'" in result.out
136
137
138 def test_pylock_recreate_on_change(tox_project: ToxProjectCreator) -> None:
139 project = tox_project(
140 {
141 "tox.toml": """
142 [env_run_base]
143 skip_install = true
144 pylock = "pylock.toml"
145 """,
146 "pylock.toml": PYLOCK_TOML,
147 },
148 )
149 execute_calls = project.patch_execute()
150 result = project.run("r", "-e", "py")
151 result.assert_success()
152
153 (project.path / "pylock.toml").write_text(
154 dedent("""\
155 lock-version = "1.0"
156 created-by = "test-tool"
157
158 [[packages]]
159 name = "alpha"
160 version = "1.0.0"
161
162 [[packages.wheels]]
163 url = "https://files.example.com/alpha-1.0.0-py3-none-any.whl"
164
165 [packages.wheels.hashes]
166 sha256 = "abc123"
167 """),
168 )
169 execute_calls.reset_mock()
170 result_second = project.run("r", "-e", "py")
171 result_second.assert_success()
172 assert "py: recreate env because" in result_second.out
173
174
175 def test_pylock_no_reinstall_on_rerun(tox_project: ToxProjectCreator) -> None:
176 project = tox_project(
177 {
178 "tox.toml": """
179 [env_run_base]
180 skip_install = true
181 pylock = "pylock.toml"
182 """,
183 "pylock.toml": PYLOCK_TOML,
184 },
185 )
186 execute_calls = project.patch_execute()
187 result = project.run("r", "-e", "py")
188 result.assert_success()
189 assert len(execute_calls.call_args_list) == 1
190
191 execute_calls.reset_mock()
192 result_second = project.run("r", "-e", "py")
193 result_second.assert_success()
194 assert not execute_calls.call_args_list
195
196
197 def test_pylock_filters_by_extras(tox_project: ToxProjectCreator) -> None:
198 project = tox_project(
199 {
200 "tox.toml": """
201 [env_run_base]
202 skip_install = true
203 pylock = "pylock.toml"
204 extras = ["docs"]
205 """,
206 "pylock.toml": dedent("""\
207 lock-version = "1.0"
208 created-by = "test-tool"
209 extras = ["docs", "test"]
210
211 [[packages]]
212 name = "alpha"
213 version = "1.0.0"
214
215 [[packages.wheels]]
216 url = "https://files.example.com/alpha-1.0.0-py3-none-any.whl"
217
218 [packages.wheels.hashes]
219 sha256 = "abc"
220
221 [[packages]]
222 name = "sphinx"
223 version = "7.0.0"
224 marker = "'docs' in extras"
225
226 [[packages.wheels]]
227 url = "https://files.example.com/sphinx-7.0.0-py3-none-any.whl"
228
229 [packages.wheels.hashes]
230 sha256 = "def"
231
232 [[packages]]
233 name = "pytest"
234 version = "8.0.0"
235 marker = "'test' in extras"
236
237 [[packages.wheels]]
238 url = "https://files.example.com/pytest-8.0.0-py3-none-any.whl"
239
240 [packages.wheels.hashes]
241 sha256 = "ghi"
242 """),
243 },
244 )
245 project.patch_execute()
246 result = project.run("r", "-e", "py")
247
248 result.assert_success()
249 assert (project.path / ".tox" / "py" / "pylock.txt").read_text() == "alpha==1.0.0\nsphinx==7.0.0"
250
251
252 def test_pylock_filters_by_dependency_groups(tox_project: ToxProjectCreator) -> None:
253 project = tox_project(
254 {
255 "tox.toml": """
256 [env_run_base]
257 skip_install = true
258 pylock = "pylock.toml"
259 dependency_groups = ["dev"]
260 """,
261 "pylock.toml": dedent("""\
262 lock-version = "1.0"
263 created-by = "test-tool"
264 dependency-groups = ["dev", "ci"]
265
266 [[packages]]
267 name = "alpha"
268 version = "1.0.0"
269
270 [[packages.wheels]]
271 url = "https://files.example.com/alpha-1.0.0-py3-none-any.whl"
272
273 [packages.wheels.hashes]
274 sha256 = "abc"
275
276 [[packages]]
277 name = "ruff"
278 version = "0.5.0"
279 marker = "'dev' in dependency_groups"
280
281 [[packages.wheels]]
282 url = "https://files.example.com/ruff-0.5.0-py3-none-any.whl"
283
284 [packages.wheels.hashes]
285 sha256 = "def"
286
287 [[packages]]
288 name = "coverage"
289 version = "7.0.0"
290 marker = "'ci' in dependency_groups"
291
292 [[packages.wheels]]
293 url = "https://files.example.com/coverage-7.0.0-py3-none-any.whl"
294
295 [packages.wheels.hashes]
296 sha256 = "ghi"
297 """),
298 },
299 )
300 project.patch_execute()
301 result = project.run("r", "-e", "py")
302
303 result.assert_success()
304 assert (project.path / ".tox" / "py" / "pylock.txt").read_text() == "alpha==1.0.0\nruff==0.5.0"
305
306
307 def test_pylock_filters_by_platform_marker(tox_project: ToxProjectCreator) -> None:
308 project = tox_project(
309 {
310 "tox.toml": """
311 [env_run_base]
312 skip_install = true
313 pylock = "pylock.toml"
314 """,
315 "pylock.toml": dedent("""\
316 lock-version = "1.0"
317 created-by = "test-tool"
318
319 [[packages]]
320 name = "alpha"
321 version = "1.0.0"
322
323 [[packages.wheels]]
324 url = "https://files.example.com/alpha-1.0.0-py3-none-any.whl"
325
326 [packages.wheels.hashes]
327 sha256 = "abc"
328
329 [[packages]]
330 name = "linuxonly"
331 version = "1.0.0"
332 marker = "sys_platform == 'linux'"
333
334 [[packages.wheels]]
335 url = "https://files.example.com/linuxonly-1.0.0-py3-none-any.whl"
336
337 [packages.wheels.hashes]
338 sha256 = "def"
339
340 [[packages]]
341 name = "winonly"
342 version = "1.0.0"
343 marker = "sys_platform == 'win32'"
344
345 [[packages.wheels]]
346 url = "https://files.example.com/winonly-1.0.0-py3-none-any.whl"
347
348 [packages.wheels.hashes]
349 sha256 = "ghi"
350 """),
351 },
352 )
353 project.patch_execute()
354 result = project.run("r", "-e", "py")
355
356 result.assert_success()
357 content = (project.path / ".tox" / "py" / "pylock.txt").read_text()
358 assert "alpha==1.0.0" in content
359 if sys.platform == "win32":
360 assert "winonly==1.0.0" in content
361 assert "linuxonly" not in content
362 elif sys.platform == "linux":
363 assert "linuxonly==1.0.0" in content
364 assert "winonly" not in content
365 else:
366 assert "linuxonly" not in content
367 assert "winonly" not in content
368
369
370 def test_pylock_requirements(tmp_path: Path) -> None:
371 lock_file = tmp_path / "pylock.toml"
372 lock_file.write_text(PYLOCK_TOML)
373 pylock = Pylock(path=lock_file)
374
375 result = [str(r) for r in pylock.requirements()]
376
377 assert result == ["alpha==1.0.0", "beta==2.0.0"]
378
379
380 def test_pylock_requirements_filters_extras(tmp_path: Path) -> None:
381 lock_file = tmp_path / "pylock.toml"
382 lock_file.write_text(
383 dedent("""\
384 lock-version = "1.0"
385 created-by = "test-tool"
386 extras = ["docs"]
387
388 [[packages]]
389 name = "alpha"
390 version = "1.0.0"
391
392 [[packages.wheels]]
393 url = "https://files.example.com/alpha-1.0.0-py3-none-any.whl"
394
395 [packages.wheels.hashes]
396 sha256 = "abc"
397
398 [[packages]]
399 name = "sphinx"
400 version = "7.0.0"
401 marker = "'docs' in extras"
402
403 [[packages.wheels]]
404 url = "https://files.example.com/sphinx-7.0.0-py3-none-any.whl"
405
406 [packages.wheels.hashes]
407 sha256 = "def"
408 """),
409 )
410
411 with_extras = [str(r) for r in Pylock(path=lock_file, extras=frozenset({"docs"})).requirements()]
412 assert with_extras == ["alpha==1.0.0", "sphinx==7.0.0"]
413
414 without_extras = [str(r) for r in Pylock(path=lock_file).requirements()]
415 assert without_extras == ["alpha==1.0.0"]
416
417
418 def test_pylock_requirements_invalid(tmp_path: Path) -> None:
419 lock_file = tmp_path / "pylock.toml"
420 lock_file.write_text(
421 dedent("""\
422 lock-version = "1.0"
423 created-by = "test-tool"
424 packages = "not-a-list"
425 """),
426 )
427 pylock = Pylock(path=lock_file)
428
429 with pytest.raises(Fail, match="invalid pylock file"):
430 pylock.requirements()
431
432
433 @pytest.mark.slow
434 @pytest.mark.integration
435 def test_pylock_install_integration(
436 tox_project: ToxProjectCreator,
437 enable_pip_pypi_access: str | None, # noqa: ARG001
438 ) -> None:
439 project = tox_project(
440 {
441 "tox.toml": """
442 [env_run_base]
443 skip_install = true
444 pylock = "pylock.toml"
445 commands = [["python", "-c", "import six; print(six.__version__)"]]
446 """,
447 "pylock.toml": dedent("""\
448 lock-version = "1.0"
449 created-by = "test-tool"
450
451 [[packages]]
452 name = "six"
453 version = "1.17.0"
454
455 [[packages.wheels]]
456 url = "https://files.pythonhosted.org/packages/b7/ce/149a00dd41f10bc29e5921b496af8b574d8413afcd5e30f4c7e48bb4cb87/six-1.17.0-py2.py3-none-any.whl"
457
458 [packages.wheels.hashes]
459 sha256 = "4721f391ed90541fddacab5acf947aa0d3dc7d27b2e1e8ez91c67d35ad282f3"
460 """),
461 },
462 )
463 result = project.run("r", "-e", "py")
464
465 result.assert_success()
466 assert "1.17.0" in result.out