-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_brewprovider.py
More file actions
245 lines (212 loc) 路 8.98 KB
/
Copy pathtest_brewprovider.py
File metadata and controls
245 lines (212 loc) 路 8.98 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
from .conftest import (
_brew_formula_is_installed,
)
import logging
import shutil
import tempfile
from pathlib import Path
import pytest
from abxpkg import Binary, BrewProvider, SemVer
from abxpkg.exceptions import BinaryInstallError
class TestBrewProvider:
def test_shallow_repository_uses_homebrew_owned_version_floor(
self,
tmp_path,
test_machine,
):
host_brew = Path(test_machine.require_tool("brew")).resolve()
host_repository = host_brew.parent.parent
brew_repository = tmp_path / "homebrew"
brew_binary = brew_repository / "bin" / "brew"
brew_binary.parent.mkdir(parents=True)
shutil.copy2(host_brew, brew_binary)
git_dir = brew_repository / ".git"
(git_dir / "refs" / "heads").mkdir(parents=True)
(git_dir / "refs" / "tags").mkdir(parents=True)
(git_dir / "HEAD").write_text("ref: refs/heads/main\n")
(git_dir / "refs" / "heads" / "main").write_text("a" * 40 + "\n")
(git_dir / "refs" / "tags" / "99.0.0").write_text("b" * 40 + "\n")
brew_script = brew_repository / "Library" / "Homebrew" / "brew.sh"
brew_script.parent.mkdir(parents=True)
shutil.copy2(
host_repository / "Library" / "Homebrew" / "brew.sh",
brew_script,
)
version = BrewProvider().installer_version_handler(
"brew",
abspath=brew_binary,
)
assert version == SemVer("4.3.0")
def test_install_root_alias_symlinks_formula_into_requested_bin_dir(
self,
test_machine,
):
test_machine.require_tool("brew")
formula = test_machine.pick_missing_brew_formula()
with tempfile.TemporaryDirectory() as temp_dir:
install_root = Path(temp_dir) / "brew-root"
provider = BrewProvider(
install_root=install_root,
postinstall_scripts=True,
min_release_age=3,
)
installed = provider.install(formula, no_cache=True)
assert installed is not None
bin_dir = provider.bin_dir
assert bin_dir is not None
test_machine.assert_shallow_binary_loaded(installed)
assert provider.install_root == install_root.resolve()
assert bin_dir == (install_root / "bin").resolve()
assert installed.loaded_abspath is not None
assert installed.loaded_abspath == bin_dir / formula
assert installed.loaded_abspath.is_symlink()
assert installed.loaded_abspath.resolve() != installed.loaded_abspath
provider.uninstall(formula, quiet=True, no_cache=True)
assert not installed.loaded_abspath.exists()
def test_provider_direct_methods_exercise_real_lifecycle(self, test_machine):
test_machine.require_tool("brew")
formula = test_machine.pick_missing_brew_formula()
provider = BrewProvider(postinstall_scripts=True, min_release_age=3)
installed, _ = test_machine.exercise_provider_lifecycle(
provider,
bin_name=formula,
)
assert provider.install_root is not None
assert provider.bin_dir == provider.install_root / "bin"
assert installed.loaded_abspath is not None
assert installed.loaded_abspath.parent == provider.bin_dir
def test_unsupported_min_release_age_warns_and_continues(
self,
test_machine,
caplog,
):
test_machine.require_tool("brew")
formula = test_machine.pick_missing_brew_formula()
provider_for_cleanup = BrewProvider(
postinstall_scripts=False,
min_release_age=3,
)
try:
with caplog.at_level(logging.WARNING, logger="abxpkg.binprovider"):
installed = BrewProvider().install(
formula,
min_release_age=1,
)
test_machine.assert_shallow_binary_loaded(installed)
assert "ignoring unsupported min_release_age=1" in caplog.text
caplog.clear()
binary = Binary(
name=formula,
binproviders=[BrewProvider()],
postinstall_scripts=False,
min_release_age=1,
)
with caplog.at_level(logging.WARNING, logger="abxpkg.binprovider"):
installed = binary.install(no_cache=True)
test_machine.assert_shallow_binary_loaded(installed)
assert "ignoring unsupported min_release_age=1" in caplog.text
finally:
provider_for_cleanup.uninstall(formula, quiet=True, no_cache=True)
def test_postinstall_disable_is_live_and_min_version_is_enforced(
self,
test_machine,
):
test_machine.require_tool("brew")
formula = test_machine.pick_missing_brew_formula()
provider = BrewProvider(postinstall_scripts=True, min_release_age=3)
installed = provider.install(
formula,
postinstall_scripts=False,
min_release_age=3,
no_cache=True,
)
test_machine.assert_shallow_binary_loaded(installed)
updated = provider.update(
formula,
postinstall_scripts=False,
min_release_age=3,
no_cache=True,
)
test_machine.assert_shallow_binary_loaded(updated)
with pytest.raises(ValueError):
provider.update(
formula,
postinstall_scripts=True,
min_release_age=3,
min_version=SemVer("999.0.0"),
no_cache=True,
)
too_new = Binary(
name=formula,
binproviders=[
BrewProvider(postinstall_scripts=True, min_release_age=3),
],
postinstall_scripts=True,
min_release_age=3,
min_version=SemVer("999.0.0"),
)
with pytest.raises(BinaryInstallError):
too_new.install(no_cache=True)
def test_helper_install_args_used_by_native_brew_backend(self, test_machine):
test_machine.require_tool("brew")
# Keep this multi-package contract independent of the lifecycle
# candidate picker. ``watch`` pulls a comparatively large dependency
# closure on fresh macOS runners, while these two leaf formulae test
# the same native multi-argument install path.
primary = "hello"
extra = "fzy"
cleanup_provider = BrewProvider(
postinstall_scripts=True,
min_release_age=3,
)
for pkg in (primary, extra):
if _brew_formula_is_installed(cleanup_provider, pkg):
assert cleanup_provider.uninstall(pkg, no_cache=True) is True
assert not _brew_formula_is_installed(cleanup_provider, pkg)
provider = BrewProvider(
postinstall_scripts=True,
min_release_age=3,
).get_provider_with_overrides(
overrides={primary: {"install_args": [primary, extra]}},
)
installed = provider.install(primary, no_cache=True)
test_machine.assert_shallow_binary_loaded(installed)
assert _brew_formula_is_installed(provider, primary)
assert _brew_formula_is_installed(provider, extra)
assert provider.uninstall(primary, no_cache=True) is True
assert not _brew_formula_is_installed(provider, primary)
assert not _brew_formula_is_installed(provider, extra)
def test_binary_direct_methods_exercise_real_lifecycle(self, test_machine):
test_machine.require_tool("brew")
formula = test_machine.pick_missing_brew_formula()
binary = Binary(
name=formula,
binproviders=[
BrewProvider(postinstall_scripts=True, min_release_age=3),
],
postinstall_scripts=True,
min_release_age=3,
)
test_machine.exercise_binary_lifecycle(binary)
def test_provider_dry_run_does_not_install_formula(self, test_machine):
test_machine.require_tool("brew")
provider = BrewProvider(postinstall_scripts=False, min_release_age=3)
test_machine.exercise_provider_dry_run(
provider,
bin_name=test_machine.pick_missing_brew_formula(),
)
def test_search_finds_real_brew_formula_and_install_works(self, test_machine):
test_machine.require_tool("brew")
provider = BrewProvider(postinstall_scripts=True, min_release_age=3)
results = provider.search("jq")
assert results, "brew search jq should return matches"
names = [r.name for r in results]
assert "jq" in names
match = next(r for r in results if r.name == "jq")
assert match.overrides == {"brew": {"install_args": ["jq"]}}
assert match.loaded_abspath is None
assert match.loaded_version is None
provider.uninstall("jq", quiet=True, no_cache=True)
installed = match.install()
test_machine.assert_shallow_binary_loaded(installed)
assert installed.name == "jq"