forked from pyscaffold/pyscaffold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_install.py
274 lines (220 loc) · 8.39 KB
/
test_install.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
"""Unit tests of everything related to retrieving the version
There are four tree states we want to check:
A: sitting on the 1.0 tag
B: dirtying the tree after 1.0
C: a commit after a tag, clean tree
D: a commit after a tag, dirty tree
Each test will run inside a different venv in a temporary directory, so they
can execute in parallel and not interfere with each other.
"""
import json
import os
import shutil
from contextlib import contextmanager
from datetime import datetime, timezone
from pathlib import Path
from shutil import copyfile
import pytest
from pyscaffold import shell
from pyscaffold.cli import main as putup
from pyscaffold.extensions import venv
from pyscaffold.file_system import chdir
from pyscaffold.shell import git
__location__ = Path(__file__).parent
pytestmark = pytest.mark.slow
@pytest.fixture
def demoapp(tmpfolder):
app = DemoApp(tmpfolder)
with chdir(app.pkg_path):
yield app
@pytest.fixture
def demoapp_data(tmpfolder):
app = DemoApp(tmpfolder, data=True)
with chdir(app.pkg_path):
yield app
class DemoApp:
def __init__(self, tmpdir, data=None):
self.name = "demoapp"
if data:
self.name += "_data"
self.pkg_path = Path(str(tmpdir), self.name)
self.built = False
self.installed = False
self.venv_path = Path(str(tmpdir), ".venv")
venv.create(self.venv_path)
assert self.venv_path.exists()
self._cmd_opts = {
"shell": os.name == "nt", # needed on Windows to pass env vars correctly.
"include_path": False, # avoid leaking executables outside the venv
}
self.python = shell.get_command("python", self.venv_path, **self._cmd_opts)
# ^ Python inside the venv...
# Different from ``shell.python`` which is the one running the tests (.tox)
self._cli = None
self.data = data
self.dist = None
with chdir(str(tmpdir)):
self._generate()
def _generate(self):
putup([self.name])
with chdir(self.name):
demoapp_src_dir = __location__ / self.name
demoapp_dst_root = self.pkg_path
demoapp_dst_pkg = demoapp_dst_root / "src" / self.name
copyfile(demoapp_src_dir / "runner.py", demoapp_dst_pkg / "runner.py")
git("add", demoapp_dst_pkg / "runner.py")
for file in "setup.cfg setup.py pyproject.toml".split():
copyfile(demoapp_src_dir / file, demoapp_dst_root / file)
git("add", demoapp_dst_root / file)
if self.data:
data_src_dir = demoapp_src_dir / "data"
data_dst_dir = demoapp_dst_pkg / "data"
os.mkdir(data_dst_dir)
pkg_file = data_dst_dir / "__init__.py"
pkg_file.write_text("")
git("add", pkg_file)
for file in "hello_world.txt".split():
copyfile(data_src_dir / file, data_dst_dir / file)
git("add", data_dst_dir / file)
git("commit", "-m", "Added basic application logic")
# setuptools-scm is used for tests
if os.name == "os":
# Windows lacks some certificates
self.pip("install", "-q", "certifi", "setuptools_scm")
else:
self.pip("install", "-q", "setuptools_scm")
def pip(self, *cmd, **kwargs):
return self.python("-m", "pip", *cmd, **kwargs)
def check_not_installed(self):
installed = [
e["name"] for e in json.loads(list(self.pip("list", "--format", "json"))[0])
]
dirty = [self.name, "UNKNOWN"]
app_list = [x for x in dirty if x in installed]
if app_list:
msg = f"Dirty virtual environment:\n{', '.join(app_list)} found"
raise RuntimeError(msg)
@contextmanager
def guard(self, attr):
if getattr(self, attr):
raise RuntimeError("For simplicity, just build/install once per package")
yield
setattr(self, attr, True)
@property
def cli(self):
if self._cli is None:
self._cli = shell.get_command(self.name, self.venv_path, **self._cmd_opts)
return self._cli
def get_version(self):
out = "".join(self.cli("--version"))
return out.replace(self.name, "").strip()
def build(self, dist="wheel", cli_opts=()):
with self.guard("built"), chdir(self.pkg_path):
# For the sake of speed, we use the same Python running the tests
# (inside .tox) and skip isolation (no extra venv just for building)
args = [self.pkg_path, *cli_opts]
shell.python("-m", "build", "--no-isolation", f"--{dist}", *args)
self.dist = dist
return self
@property
def dist_file(self):
return next((self.pkg_path / "dist").glob(self.name + "*"))
def install(self, edit=False):
with self.guard("installed"), chdir(self.pkg_path):
self.check_not_installed()
if edit or self.dist is None:
self.pip("install", "-e", ".")
else:
self.pip("install", self.dist_file)
return self
def installed_path(self):
if not self.installed:
return None
cmd = f"import {self.name}; print({self.name}.__path__[0])"
return Path(next(self.python("-c", cmd)))
def make_dirty_tree(self):
dirty_file = self.pkg_path / "src" / self.name / "runner.py"
with open(dirty_file, "a") as fh:
fh.write("\n\ndirty_variable = 69\n")
return self
def make_commit(self):
with chdir(self.pkg_path):
git("commit", "-a", "-m", "message")
return self
def rm_git_tree(self):
git_path = self.pkg_path / ".git"
shutil.rmtree(git_path)
return self
def tag(self, name, message):
with chdir(self.pkg_path):
git("tag", "-a", name, "-m", message)
return self
def check_version(version, exp_version, dirty=False):
today = datetime.now(timezone.utc).strftime("%Y%m%d")
dirty_tag = f".d{today}"
# ^ this depends on the local strategy configured for setuptools_scm...
# the default 'node-and-date'
sep = "+" if "+" in version else "_"
# ^ for some setuptools version a directory with + is generated, sometimes _
if dirty:
ver, local = version.split(sep)
assert local.endswith(dirty_tag)
assert ver == exp_version
else:
ver = version.split(sep)
if len(ver) > 1:
assert not ver[1].endswith(dirty_tag)
assert ver[0] == exp_version
def test_sdist_install(demoapp):
(demoapp.build("sdist").install())
exp = "0.0.post1.dev2"
check_version(demoapp.get_version(), exp, dirty=False)
def test_sdist_install_dirty(demoapp):
(
demoapp.tag("v0.1", "first release")
.make_dirty_tree()
.make_commit()
.make_dirty_tree()
.build("sdist")
.install()
)
exp = "0.1.post1.dev1"
check_version(demoapp.get_version(), exp, dirty=True)
def test_sdist_install_with_1_0_tag(demoapp):
(
demoapp.make_dirty_tree()
.make_commit()
.tag("v1.0", "final release")
.build("sdist")
.install()
)
exp = "1.0"
check_version(demoapp.get_version(), exp, dirty=False)
def test_sdist_install_with_1_0_tag_dirty(demoapp):
demoapp.tag("v1.0", "final release").make_dirty_tree().build("sdist").install()
exp = "1.0.post1.dev0"
check_version(demoapp.get_version(), exp, dirty=True)
def test_wheel_install(demoapp):
demoapp.build("wheel").install()
exp = "0.0.post1.dev2"
check_version(demoapp.get_version(), exp, dirty=False)
def test_sdist_install_with_data(demoapp_data):
demoapp_data.build("sdist").install()
out = "".join(demoapp_data.cli())
exp = "Hello World"
assert out.startswith(exp)
def test_wheel_install_with_data(demoapp_data):
demoapp_data.build("wheel").install()
path = demoapp_data.installed_path()
assert path.exists()
assert (path / "data/__init__.py").exists()
assert (path / "data/hello_world.txt").exists()
assert (path / "runner.py").exists()
out = "".join(demoapp_data.cli())
exp = "Hello World"
assert out.startswith(exp)
def test_edit_install_with_data(demoapp_data):
demoapp_data.install(edit=True)
out = "".join(demoapp_data.cli())
exp = "Hello World"
assert out.startswith(exp)