-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_binary.py
More file actions
439 lines (394 loc) · 15.3 KB
/
test_binary.py
File metadata and controls
439 lines (394 loc) · 15.3 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import tempfile
from pathlib import Path
import pytest
from abxpkg import (
Binary,
BinProvider,
EnvProvider,
NpmProvider,
PipProvider,
SemVer,
UvProvider,
)
from abxpkg.exceptions import (
BinaryLoadError,
BinaryInstallError,
BinaryUninstallError,
BinaryUpdateError,
)
class TestBinary:
def test_short_aliases_match_loaded_field_names(self):
binary = Binary(
name="python",
binproviders=[
EnvProvider(postinstall_scripts=True, min_release_age=0),
],
).load(no_cache=True)
assert binary.binproviders
assert binary.binprovider == binary.loaded_binprovider
assert binary.abspath == binary.loaded_abspath
assert binary.abspaths == binary.loaded_abspaths
assert binary.version == binary.loaded_version
assert binary.sha256 == binary.loaded_sha256
assert binary.mtime == binary.loaded_mtime
assert binary.euid == binary.loaded_euid
def test_get_binprovider_applies_overrides_and_provider_filtering(
self,
test_machine,
):
with tempfile.TemporaryDirectory() as tmpdir:
# Point EnvProvider at an empty PATH and install_root so the test
# stays hermetic even when ``black`` is already installed
# elsewhere on the host or cached from a prior test run.
empty_bin_dir = Path(tmpdir) / "empty_bin"
empty_bin_dir.mkdir()
env_provider = EnvProvider(
PATH=str(empty_bin_dir),
install_root=Path(tmpdir) / "empty_env",
)
pip_provider = PipProvider(
install_root=Path(tmpdir) / "venv",
postinstall_scripts=True,
min_release_age=0,
)
binary = Binary(
name="black",
binproviders=[env_provider, pip_provider],
overrides={"pip": {"install_args": ["black"]}},
postinstall_scripts=True,
min_release_age=0,
)
overridden_provider = binary.get_binprovider("pip")
assert overridden_provider.get_install_args("black") == ("black",)
with pytest.raises(KeyError):
binary.get_binprovider("brew")
installed = binary.install()
assert installed.loaded_binprovider is not None
assert installed.loaded_binprovider.name == "pip"
with pytest.raises(BinaryLoadError):
test_machine.unloaded_binary(binary).load(
binproviders=["env"],
no_cache=True,
)
loaded = test_machine.unloaded_binary(binary).load(
binproviders=["pip"],
no_cache=True,
)
test_machine.assert_shallow_binary_loaded(loaded)
def test_get_binprovider_applies_provider_field_patches_from_binary_overrides(
self,
):
binary = Binary(
name="forum-dl",
binproviders=[
PipProvider(
postinstall_scripts=False,
min_release_age=7,
),
],
overrides={
"pip": {
"install_args": ["forum-dl", "cchardet==2.2.0a2"],
"postinstall_scripts": True,
"min_release_age": 0,
},
},
)
provider = binary.get_binprovider("pip")
assert provider.postinstall_scripts is True
assert provider.min_release_age == 0
assert provider.get_install_args("forum-dl") == (
"forum-dl",
"cchardet==2.2.0a2",
)
def test_min_version_rejection_paths_raise_public_errors(self):
binary = Binary(
name="python",
binproviders=[
EnvProvider(postinstall_scripts=True, min_release_age=0),
],
min_version=SemVer("999.0.0"),
postinstall_scripts=True,
min_release_age=0,
)
with pytest.raises(BinaryLoadError):
binary.load()
with pytest.raises(BinaryInstallError):
binary.install()
with pytest.raises(BinaryUpdateError):
binary.update()
with pytest.raises(BinaryUninstallError):
binary.uninstall()
def test_install_and_update_upgrade_real_installed_version(
self,
test_machine,
):
with tempfile.TemporaryDirectory() as tmpdir:
venv_path = Path(tmpdir) / "venv"
old_binary = Binary(
name="black",
binproviders=[
PipProvider(
install_root=venv_path,
postinstall_scripts=True,
min_release_age=0,
),
],
postinstall_scripts=True,
min_release_age=0,
overrides={"pip": {"install_args": ["black==23.1.0"]}},
)
old_installed = old_binary.install()
assert old_installed.loaded_version is not None
required_version = SemVer.parse("24.0.0")
assert required_version is not None
assert tuple(old_installed.loaded_version) < tuple(required_version)
upgraded = Binary(
name="black",
binproviders=[
PipProvider(
install_root=venv_path,
postinstall_scripts=True,
min_release_age=0,
),
],
postinstall_scripts=True,
min_release_age=0,
min_version=SemVer("24.0.0"),
).install()
test_machine.assert_shallow_binary_loaded(
upgraded,
expected_version=SemVer("24.0.0"),
)
updated = Binary(
name="black",
binproviders=[
PipProvider(
install_root=venv_path,
postinstall_scripts=True,
min_release_age=0,
),
],
postinstall_scripts=True,
min_release_age=0,
min_version=SemVer("24.0.0"),
).update()
test_machine.assert_shallow_binary_loaded(
updated,
expected_version=SemVer("24.0.0"),
)
removed = updated.uninstall()
assert removed.loaded_abspath is None
assert removed.loaded_mtime is None
assert removed.loaded_euid is None
def test_empty_binprovider_filter_returns_binary_unchanged(self):
binary = Binary(
name="python",
binproviders=[
EnvProvider(postinstall_scripts=True, min_release_age=0),
],
postinstall_scripts=True,
min_release_age=0,
)
assert binary.install(binproviders=[]) == binary
assert binary.load(binproviders=[]) == binary
assert binary.update(binproviders=[]) == binary
assert binary.uninstall(binproviders=[]) == binary
def test_binary_params_override_provider_defaults_and_binary_overrides_win(
self,
test_machine,
):
with tempfile.TemporaryDirectory() as tmpdir:
provider = PipProvider(
install_root=Path(tmpdir) / "venv",
postinstall_scripts=False,
min_release_age=36500,
).get_provider_with_overrides(
overrides={"black": {"install_args": ["black"]}},
)
binary = Binary(
name="black",
binproviders=[provider],
postinstall_scripts=True,
min_release_age=0,
overrides={"pip": {"install_args": ["black==23.1.0"]}},
)
resolved_provider = binary.get_binprovider("pip")
assert resolved_provider.get_install_args("black") == ("black==23.1.0",)
installed = binary.install()
assert installed.loaded_version == SemVer("23.1.0")
upgraded = Binary(
name="black",
binproviders=[
PipProvider(
install_root=Path(tmpdir) / "venv",
postinstall_scripts=False,
min_release_age=36500,
),
],
postinstall_scripts=True,
min_release_age=0,
min_version=SemVer("24.0.0"),
).install()
test_machine.assert_shallow_binary_loaded(
upgraded,
expected_version=SemVer("24.0.0"),
)
def test_binary_install_works_with_provider_install_root_alias(self, test_machine):
with tempfile.TemporaryDirectory() as tmpdir:
install_root = Path(tmpdir) / "pip-root"
providers: list[BinProvider] = [
PipProvider.model_validate(
{
"install_root": install_root,
"postinstall_scripts": True,
"min_release_age": 0,
},
),
]
binary = Binary(
name="black",
binproviders=providers,
postinstall_scripts=True,
min_release_age=0,
)
installed = binary.install()
test_machine.assert_shallow_binary_loaded(installed)
assert installed.loaded_abspath is not None
provider = binary.get_binprovider("pip")
assert provider.install_root == install_root
assert provider.bin_dir == install_root / "venv" / "bin"
assert installed.loaded_abspath.parent == provider.bin_dir
def test_binary_dry_run_passes_through_to_provider_without_installing(self):
with tempfile.TemporaryDirectory() as tmpdir:
provider = PipProvider(
install_root=Path(tmpdir) / "venv",
postinstall_scripts=True,
min_release_age=0,
)
binary = Binary(
name="black",
binproviders=[provider],
postinstall_scripts=True,
min_release_age=0,
)
installed = binary.install(dry_run=True)
assert installed.loaded_version == SemVer("999.999.999")
assert provider.load("black", quiet=True, no_cache=True) is None
ensured = binary.install(dry_run=True)
assert ensured.loaded_version == SemVer("999.999.999")
assert provider.load("black", quiet=True, no_cache=True) is None
updated = binary.update(dry_run=True)
assert updated.loaded_version == SemVer("999.999.999")
assert provider.load("black", quiet=True, no_cache=True) is None
with pytest.raises(BinaryUninstallError):
binary.uninstall(dry_run=True)
assert provider.load("black", quiet=True, no_cache=True) is None
def test_binary_dry_run_install_does_not_update_stale_existing_binary(self):
with tempfile.TemporaryDirectory() as tmpdir:
venv_path = Path(tmpdir) / "venv"
old_binary = Binary(
name="black",
binproviders=[
PipProvider(
install_root=venv_path,
postinstall_scripts=True,
min_release_age=0,
),
],
postinstall_scripts=True,
min_release_age=0,
overrides={"pip": {"install_args": ["black==23.1.0"]}},
)
old_installed = old_binary.install()
assert old_installed.loaded_version == SemVer("23.1.0")
binary = Binary(
name="black",
binproviders=[
PipProvider(
install_root=venv_path,
postinstall_scripts=True,
min_release_age=0,
),
],
postinstall_scripts=True,
min_release_age=0,
min_version=SemVer("24.0.0"),
)
dry_installed = binary.install(dry_run=True)
assert dry_installed.loaded_version == SemVer("999.999.999")
loaded_after_dry_run = binary.get_binprovider("pip").load(
"black",
quiet=True,
no_cache=True,
)
assert loaded_after_dry_run is not None
assert loaded_after_dry_run.loaded_version == SemVer("23.1.0")
def test_binary_install_no_cache_bypasses_already_loaded_short_circuit(self):
with tempfile.TemporaryDirectory() as tmpdir:
binary = Binary(
name="black",
binproviders=[
PipProvider(
install_root=Path(tmpdir) / "venv",
postinstall_scripts=True,
min_release_age=0,
),
],
postinstall_scripts=True,
min_release_age=0,
)
installed = binary.install()
assert installed.is_valid
forced = installed.install(dry_run=True, no_cache=True)
assert forced.loaded_version == SemVer("999.999.999")
def test_binary_uninstall_prioritizes_provider_with_cached_install_record(self):
with tempfile.TemporaryDirectory() as tmpdir:
tmp_path = Path(tmpdir)
npm_provider = NpmProvider(
install_root=tmp_path / "npm",
postinstall_scripts=True,
min_release_age=0,
)
installed = npm_provider.install("zx")
assert installed is not None
assert npm_provider.load("zx", no_cache=True) is not None
binary = Binary(
name="zx",
binproviders=[
UvProvider(
install_root=tmp_path / "uv",
postinstall_scripts=True,
min_release_age=0,
),
npm_provider,
],
postinstall_scripts=True,
min_release_age=0,
)
removed = binary.uninstall()
assert removed.loaded_abspath is None
assert npm_provider.load("zx", no_cache=True) is None
def test_binary_action_args_override_binary_and_provider_defaults(
self,
test_machine,
):
with tempfile.TemporaryDirectory() as tmpdir:
provider = PipProvider(
install_root=Path(tmpdir) / "venv",
dry_run=True,
postinstall_scripts=False,
min_release_age=36500,
)
binary = Binary(
name="black",
binproviders=[provider],
postinstall_scripts=False,
min_release_age=36500,
)
installed = binary.install(
dry_run=False,
postinstall_scripts=True,
min_release_age=0,
)
test_machine.assert_shallow_binary_loaded(installed)