-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_pkg_management.py
318 lines (237 loc) · 11.3 KB
/
test_pkg_management.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
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
from __future__ import annotations
from pathlib import Path
import aiohttp
import aiohttp.web
import attrs
import pytest
from instawow import pkg_management
from instawow.definitions import Defn, Strategy
from instawow.pkg_models import Pkg
from instawow.results import (
InternalError,
PkgAlreadyInstalled,
PkgConflictsWithInstalled,
PkgConflictsWithUnreconciled,
PkgInstalled,
PkgNonexistent,
PkgNotInstalled,
PkgRemoved,
PkgSourceInvalid,
PkgStrategiesUnsupported,
PkgUpdated,
PkgUpToDate,
)
from instawow.shared_ctx import ConfigBoundCtx
from .fixtures.http import ResponsesMockServer, Route
@pytest.mark.usefixtures('_iw_web_client_ctx')
async def test_pinning_supported_pkg(iw_config_ctx: ConfigBoundCtx):
defn = Defn('curse', 'molinari')
install_result = (await pkg_management.install(iw_config_ctx, [defn], replace_folders=False))[
defn
]
assert type(install_result) is PkgInstalled
for new_defn in (defn.with_version(install_result.pkg.version), defn):
pin_result = (await pkg_management.pin(iw_config_ctx, [new_defn]))[new_defn]
assert type(pin_result) is PkgInstalled
assert pin_result.pkg.options.version_eq is bool(new_defn.strategies[Strategy.VersionEq])
assert install_result.pkg.version == pin_result.pkg.version
@pytest.mark.usefixtures('_iw_web_client_ctx')
async def test_pinning_unsupported_pkg(iw_config_ctx: ConfigBoundCtx):
molinari_defn = Defn('wowi', '13188')
await pkg_management.install(iw_config_ctx, [molinari_defn], replace_folders=False)
installed_pkg = pkg_management.get_pkg(iw_config_ctx, molinari_defn)
assert installed_pkg is not None
assert installed_pkg.options.version_eq is False
result = (await pkg_management.pin(iw_config_ctx, [molinari_defn]))[molinari_defn]
assert type(result) is PkgStrategiesUnsupported
assert result.strategies == {Strategy.VersionEq}
assert installed_pkg.options.version_eq is False
async def test_pinning_nonexistent_pkg(iw_config_ctx: ConfigBoundCtx):
molinari_defn = Defn('curse', 'molinari')
result = await pkg_management.pin(iw_config_ctx, [molinari_defn])
assert type(result[molinari_defn]) is PkgNotInstalled
async def test_pinning_unsupported_nonexistent_pkg(iw_config_ctx: ConfigBoundCtx):
molinari_defn = Defn('wowi', '13188')
result = await pkg_management.pin(iw_config_ctx, [molinari_defn])
assert type(result[molinari_defn]) is PkgStrategiesUnsupported
@pytest.mark.parametrize('exception', [ValueError('foo'), aiohttp.ClientError('bar')])
@pytest.mark.usefixtures('_iw_web_client_ctx')
async def test_resolve_rewraps_exception_appropriately_from_resolve(
monkeypatch: pytest.MonkeyPatch,
iw_config_ctx: ConfigBoundCtx,
exception: Exception,
):
async def resolve_one(self, defn, metadata):
raise exception
monkeypatch.setattr('instawow.resolvers.BaseResolver.resolve_one', resolve_one)
defn = Defn('curse', 'molinari')
result = (await pkg_management.resolve(iw_config_ctx, [defn]))[defn]
assert type(result) is InternalError
assert result.message == f'internal error: "{exception}"'
@pytest.mark.usefixtures('_iw_web_client_ctx')
async def test_resolve_invalid_source(iw_config_ctx: ConfigBoundCtx):
defn = Defn('bar', 'baz')
results = await pkg_management.resolve(iw_config_ctx, [defn])
assert type(results[defn]) is PkgSourceInvalid
@pytest.mark.usefixtures('_iw_web_client_ctx')
async def test_resolve_plugin_hook_source(iw_config_ctx: ConfigBoundCtx):
pytest.importorskip('instawow_test_plugin')
defn = Defn('me', 'bar')
results = await pkg_management.resolve(iw_config_ctx, [defn])
assert type(results[defn]) is Pkg
@pytest.mark.usefixtures('_iw_web_client_ctx')
async def test_install_can_replace_unreconciled_folders(
iw_config_ctx: ConfigBoundCtx,
):
molinari = iw_config_ctx.config.addon_dir / 'Molinari'
molinari.mkdir()
defn = Defn('curse', 'molinari')
result = await pkg_management.install(iw_config_ctx, [defn], replace_folders=False)
assert type(result[defn]) is PkgConflictsWithUnreconciled
assert not any(molinari.iterdir())
result = await pkg_management.install(iw_config_ctx, [defn], replace_folders=True)
assert type(result[defn]) is PkgInstalled
assert any(molinari.iterdir())
@pytest.mark.usefixtures('_iw_web_client_ctx')
async def test_install_cannot_replace_reconciled_folders(iw_config_ctx: ConfigBoundCtx):
curse_defn = Defn('curse', 'molinari')
wowi_defn = Defn('wowi', '13188-molinari')
result = await pkg_management.install(iw_config_ctx, [curse_defn], replace_folders=False)
assert type(result[curse_defn]) is PkgInstalled
result = await pkg_management.install(iw_config_ctx, [wowi_defn], replace_folders=False)
assert type(result[wowi_defn]) is PkgConflictsWithInstalled
result = await pkg_management.install(iw_config_ctx, [wowi_defn], replace_folders=True)
assert type(result[wowi_defn]) is PkgConflictsWithInstalled
@pytest.mark.usefixtures('_iw_web_client_ctx')
async def test_install_recognises_renamed_pkg_from_id(
monkeypatch: pytest.MonkeyPatch,
iw_aresponses: ResponsesMockServer,
iw_config_ctx: ConfigBoundCtx,
):
iw_aresponses.add(
Route(
r'//api\.github\.com/repos/p3lim-wow/molinarifico/?.*',
aiohttp.web.Response(status=404),
)
)
old_defn = Defn('github', 'p3lim-wow/molinari')
new_defn = Defn('github', 'p3lim-wow/molinarifico')
result = await pkg_management.install(iw_config_ctx, [old_defn], replace_folders=False)
assert type(result[old_defn]) is PkgInstalled
result = await pkg_management.install(iw_config_ctx, [old_defn], replace_folders=False)
assert type(result[old_defn]) is PkgAlreadyInstalled
result = await pkg_management.install(iw_config_ctx, [new_defn], replace_folders=False)
assert type(result[new_defn]) is PkgNonexistent
async def resolve(config_ctx, defns, with_deps=False):
result = await orig_resolve(config_ctx, [old_defn])
pkg = attrs.evolve(result[old_defn], slug=new_defn.alias)
return {new_defn: pkg}
orig_resolve = pkg_management.resolve
monkeypatch.setattr(pkg_management, 'resolve', resolve)
result = await pkg_management.install(iw_config_ctx, [new_defn], replace_folders=False)
assert type(result[new_defn]) is PkgAlreadyInstalled
@pytest.mark.usefixtures('_iw_web_client_ctx')
async def test_update_lifecycle_with_strategy_switch(iw_config_ctx: ConfigBoundCtx):
defn = Defn('curse', 'molinari')
versioned_defn = defn.with_version('100005.97-Release')
result = (await pkg_management.install(iw_config_ctx, [defn], replace_folders=False))[defn]
assert type(result) is PkgInstalled
result = (await pkg_management.update(iw_config_ctx, [defn]))[defn]
assert type(result) is PkgUpToDate
assert result.is_pinned is False
result = (await pkg_management.update(iw_config_ctx, [versioned_defn]))[versioned_defn]
assert type(result) is PkgUpdated
assert result.new_pkg.options.version_eq is True
result = (await pkg_management.update(iw_config_ctx, [defn]))[defn]
assert type(result) is PkgUpToDate
assert result.is_pinned is True
@pytest.mark.usefixtures('_iw_web_client_ctx')
async def test_update_reinstalls_corrupted_pkgs(
iw_config_ctx: ConfigBoundCtx,
):
defn = Defn('curse', 'molinari')
results = await pkg_management.install(iw_config_ctx, [defn], replace_folders=False)
result = results[defn]
assert type(result) is PkgInstalled
folders = [iw_config_ctx.config.addon_dir / f.name for f in result.pkg.folders]
first_folder = folders[0]
first_folder.rename(first_folder.with_name('foo'))
assert not all(f.is_dir() for f in folders)
results = await pkg_management.update(iw_config_ctx, [defn])
assert type(results[defn]) is PkgUpdated
assert all(f.is_dir() for f in folders)
@pytest.mark.parametrize('keep_folders', [True, False])
@pytest.mark.usefixtures('_iw_web_client_ctx')
async def test_deleting_and_retaining_folders_on_remove(
iw_config_ctx: ConfigBoundCtx,
keep_folders: bool,
):
defn = Defn('curse', 'molinari')
await pkg_management.install(iw_config_ctx, [defn], replace_folders=False)
pkg = pkg_management.get_pkg(iw_config_ctx, defn)
assert pkg
folders = [iw_config_ctx.config.addon_dir / f.name for f in pkg.folders]
assert all(f.is_dir() for f in folders)
results = await pkg_management.remove(iw_config_ctx, [defn], keep_folders=keep_folders)
assert type(results[defn]) is PkgRemoved
assert not pkg_management.get_pkg(iw_config_ctx, defn)
if keep_folders:
assert all(f.is_dir() for f in folders)
else:
assert not any(f.is_dir() for f in folders)
@pytest.mark.parametrize('keep_folders', [True, False])
@pytest.mark.usefixtures('_iw_web_client_ctx')
async def test_removing_pkg_with_missing_folders(
iw_config_ctx: ConfigBoundCtx,
keep_folders: bool,
):
defn = Defn('curse', 'molinari')
results = await pkg_management.install(iw_config_ctx, [defn], replace_folders=False)
result = results[defn]
assert type(result) is PkgInstalled
folders = [iw_config_ctx.config.addon_dir / f.name for f in result.pkg.folders]
for folder in folders:
folder.rename(folder.with_name(f'Not_{folder.name}'))
assert not any(f.is_dir() for f in folders)
results = await pkg_management.remove(iw_config_ctx, [defn], keep_folders=keep_folders)
assert type(results[defn]) is PkgRemoved
assert not pkg_management.get_pkg(iw_config_ctx, defn)
@pytest.mark.usefixtures('_iw_web_client_ctx')
async def test_replace_pkg(
iw_config_ctx: ConfigBoundCtx,
):
old_defn = Defn('curse', 'molinari')
new_defn = Defn('github', 'p3lim-wow/molinari')
results = await pkg_management.install(iw_config_ctx, [old_defn], replace_folders=False)
assert type(results[old_defn]) is PkgInstalled
results = await pkg_management.replace(iw_config_ctx, {old_defn: new_defn})
assert len(results) == 2
assert type(results[old_defn]) is PkgRemoved
assert type(results[new_defn]) is PkgInstalled
async def test_get_changelog_from_empty_data_url(iw_config_ctx: ConfigBoundCtx):
assert (await pkg_management.get_changelog(iw_config_ctx, 'github', 'data:,')) == ''
async def test_get_changelog_from_url_encoded_data_url(iw_config_ctx: ConfigBoundCtx):
assert (
await pkg_management.get_changelog(iw_config_ctx, 'github', 'data:,foo%20bar')
) == 'foo bar'
async def test_get_malformed_changelog(iw_config_ctx: ConfigBoundCtx):
with pytest.raises(ValueError, match='Unsupported URI with scheme'):
await pkg_management.get_changelog(iw_config_ctx, 'github', '')
async def test_get_changelog_from_file_uri(
iw_config_ctx: ConfigBoundCtx,
tmp_path: Path,
):
changelog = tmp_path / 'changelog.txt'
changelog.write_text('test')
assert (
await pkg_management.get_changelog(iw_config_ctx, 'github', changelog.as_uri())
) == 'test'
@pytest.mark.usefixtures('_iw_web_client_ctx')
async def test_get_changelog_from_web_url(iw_config_ctx: ConfigBoundCtx):
assert (
await pkg_management.get_changelog(
iw_config_ctx,
'curse',
'https://api.curseforge.com/v1/mods/20338/files/3657564/changelog',
)
).startswith('<h3>Changes in')