-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtest_zipapp.py
More file actions
119 lines (97 loc) · 4.03 KB
/
test_zipapp.py
File metadata and controls
119 lines (97 loc) · 4.03 KB
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
from __future__ import annotations
import shutil
import subprocess
from contextlib import suppress
from pathlib import Path
import pytest
from python_discovery import PythonInfo
from virtualenv.info import fs_supports_symlink
from virtualenv.run import cli_run
HERE = Path(__file__).parent
CURRENT = PythonInfo.current_system()
@pytest.fixture(scope="session")
def zipapp_build_env(tmp_path_factory):
create_env_path = None
if CURRENT.implementation not in {"PyPy", "GraalVM"}:
exe = CURRENT.executable # guaranteed to contain a recent enough pip (tox.ini)
else:
create_env_path = tmp_path_factory.mktemp("zipapp-create-env")
exe, found = None, False
# prefer CPython as builder as pypy is slow
for impl in ["cpython", ""]:
for threaded in ["", "t"]:
for version in range(11, 6, -1):
with suppress(Exception):
# create a virtual environment which is also guaranteed to contain a recent enough pip (bundled)
session = cli_run(
[
"-vvv",
"-p",
f"{impl}3.{version}{threaded}",
"--activators",
"",
str(create_env_path),
"--no-download",
"--no-periodic-update",
],
)
exe = str(session.creator.exe)
found = True
break
if found:
break
if found:
break
else:
msg = "could not find a python to build zipapp"
raise RuntimeError(msg)
cmd = [str(Path(exe).parent / "pip"), "install", "pip>=23", "packaging>=23"]
subprocess.run(cmd, check=True, timeout=300)
yield exe
if create_env_path is not None:
shutil.rmtree(str(create_env_path))
@pytest.fixture(scope="session")
def zipapp(zipapp_build_env, tmp_path_factory):
into = tmp_path_factory.mktemp("zipapp")
path = HERE.parent.parent / "tasks" / "make_zipapp.py"
filename = into / "virtualenv.pyz"
cmd = [zipapp_build_env, str(path), "--dest", str(filename)]
subprocess.run(cmd, check=True, timeout=300)
yield filename
shutil.rmtree(str(into))
@pytest.fixture(scope="session")
def zipapp_test_env(tmp_path_factory):
base_path = tmp_path_factory.mktemp("zipapp-test")
session = cli_run(["-v", "--activators", "", "--without-pip", str(base_path / "env"), "--no-periodic-update"])
yield session.creator.exe
shutil.rmtree(str(base_path))
@pytest.fixture
def call_zipapp(zipapp, tmp_path, zipapp_test_env, temp_app_data): # noqa: ARG001
def _run(*args) -> None:
cmd = [str(zipapp_test_env), str(zipapp), "-vv", str(tmp_path / "env"), *list(args)]
subprocess.run(cmd, check=True, timeout=120)
return _run
@pytest.fixture
def call_zipapp_symlink(zipapp, tmp_path, zipapp_test_env, temp_app_data): # noqa: ARG001
def _run(*args) -> None:
symlinked = zipapp.parent / "symlinked_virtualenv.pyz"
symlinked.symlink_to(str(zipapp))
cmd = [str(zipapp_test_env), str(symlinked), "-vv", str(tmp_path / "env"), *list(args)]
subprocess.run(cmd, check=True, timeout=120)
return _run
@pytest.mark.timeout(600)
@pytest.mark.skipif(not fs_supports_symlink(), reason="symlink not supported")
def test_zipapp_in_symlink(capsys, call_zipapp_symlink) -> None:
call_zipapp_symlink("--reset-app-data")
_out, err = capsys.readouterr()
assert not err
@pytest.mark.timeout(600)
def test_zipapp_help(call_zipapp, capsys) -> None:
call_zipapp("-h")
_out, err = capsys.readouterr()
assert not err
@pytest.mark.timeout(600)
@pytest.mark.slow
@pytest.mark.parametrize("seeder", ["app-data", "pip"])
def test_zipapp_create(call_zipapp, seeder) -> None:
call_zipapp("--seeder", seeder)