fork/mons

git clone https://git.julia.blue/fork/mons

coloursofnoiseUpdate mod resolve test and move logging setup out of global scopef9de773

fixes
10.8 KiB384 linesraw
1import click
2import pytest
3import yaml
4
5import mons.clickExt
6import mons.commands.mods
7from mons.commands.mods import resolve_dependencies
8from mons.commands.mods import resolve_exclusive_dependencies
9from mons.commands.mods import resolve_mods
10from mons.modmeta import ModMeta
11
12mod_db = {}
13dep_graph = {}
14
15
16@pytest.fixture(autouse=True)
17def fake_mod_db(monkeypatch):
18    monkeypatch.setattr(mons.commands.mods, "fetch_mod_db", lambda *args: mod_db)
19    monkeypatch.setattr(
20        mons.commands.mods, "fetch_dependency_graph", lambda *args: dep_graph
21    )
22
23
24@pytest.fixture(autouse=True)
25def stub_confirm_ext(monkeypatch):
26    def abort(*args, **kwargs):
27        raise click.Abort()
28
29    monkeypatch.setattr(mons.clickExt, "confirm_ext", abort)
30
31
32# better display names for parametrized tests
33def id_modmeta(val):
34    if isinstance(val, list) and all(isinstance(mod, ModMeta) for mod in val):
35        mod_names = [mod.Name for mod in val]
36        test_name = ""
37        for c, d in zip(max(mod_names, key=len), min(mod_names, key=len)):
38            if c != d:
39                break
40            test_name += c
41        return test_name.strip("_")
42    if isinstance(val, str):
43        return f'"{val}"'
44    if hasattr(val, "expected_exception"):
45        return val.expected_exception  # type:ignore
46
47
48def modmeta_for(name):
49    try:
50        return ModMeta(
51            {"Name": name, **mod_db.get(name, {"Version": "1.0.0"}), **dep_graph[name]}
52        )
53    except KeyError:
54        return name
55
56
57TEST_MOD = "TestMod"
58
59
60mod_db.update(
61    {
62        TEST_MOD: {
63            "URL": "https://gamebanana.com/mmdl/12345",
64            "MirrorURL": "https://testmirror.test",
65            "Version": "1.0.0",
66            "GameBananaId": 12345,
67            "LastUpdate": 0,
68        },
69        "TestHelper": {
70            "URL": "https://gamebanana.com/mmdl/54321",
71            "MirrorURL": "https://testmirror.test",
72            "Version": "1.0.0",
73            "GameBananaId": 54321,
74            "LastUpdate": 0,
75        },
76    }
77)
78
79
80dep_graph.update(
81    {
82        TEST_MOD: {
83            "Dependencies": [{"Name": "TestHelper", "Version": "1.0.0"}],
84            "OptionalDependencies": [],
85        },
86        "TestHelper": {},
87    }
88)
89
90
91@pytest.mark.parametrize(
92    ("input", "expect"),
93    [
94        pytest.param(TEST_MOD, TEST_MOD, id="Mod Name"),
95        pytest.param(
96            f"https://gamebanana.com/mods/{mod_db[TEST_MOD]['GameBananaId']}",
97            TEST_MOD,
98            id="GameBanana Page",
99        ),
100        pytest.param(
101            f"everest:{mod_db[TEST_MOD]['URL']},Mod,00000",
102            TEST_MOD,
103            id="1-Click Install",
104        ),
105        pytest.param(str(mod_db[TEST_MOD]["GameBananaId"]), TEST_MOD, id="Mod ID"),
106    ],
107)
108def test_resolve_mods(input, expect):
109    (resolved, unresolved) = resolve_mods(None, [input])
110    assert resolved and not unresolved
111    assert resolved[0].Meta.Name == expect
112
113
114@pytest.mark.data_file_zip(
115    {
116        "everest.yaml": yaml.safe_dump(
117            [
118                {
119                    "Name": TEST_MOD,
120                    "Version": "1.0.0",
121                }
122            ]
123        )
124    }
125)
126def test_resolve_mods_zip(data_file_zip):
127    (resolved, unresolved) = resolve_mods(None, data_file_zip)
128    assert resolved and not unresolved
129    assert resolved[0].Meta.Name == TEST_MOD
130
131
132@pytest.mark.parametrize(
133    ("input", "expect"),
134    [
135        pytest.param(
136            "https://drive.google.com/file/d/filename.zip",
137            "https://drive.google.com/uc?export=download&id=filename.zip",
138            id="Google Drive",
139        ),
140        pytest.param("fake://mod/path", "fake://mod/path", id="URL"),
141    ],
142)
143def test_resolve_mods_unresolved(input, expect):
144    (resolved, unresolved) = resolve_mods(None, [input])
145    assert unresolved and not resolved
146    assert unresolved[0] == expect
147
148
149def test_resolve_mods_fail(caplog):
150    fake_mod = "FakeMod"
151    with pytest.raises(click.Abort):
152        resolve_mods(None, (*mod_db.keys(), fake_mod))
153    assert f"Mod '{fake_mod}' could not be resolved" in caplog.text
154
155
156dep_graph.update(
157    {
158        "Simple": {},
159        "Nested": {
160            "Dependencies": [{"Name": "Nested_1", "Version": "1.0.0"}],
161            "OptionalDependencies": [],
162        },
163        **{
164            "Nested_1": {
165                "Dependencies": [{"Name": "Nested_2", "Version": "1.0.0"}],
166                "OptionalDependencies": [],
167            },
168            **{
169                "Nested_2": {
170                    "Dependencies": [],
171                    "OptionalDependencies": [],
172                },
173            },
174        },
175        "Version_Bump_Lower": {
176            "Dependencies": [{"Name": "Version_Bump_Dep", "Version": "1.2.0"}],
177            "OptionalDependencies": [],
178        },
179        "Version_Bump_Higher": {
180            "Dependencies": [{"Name": "Version_Bump_Dep", "Version": "1.5.0"}],
181            "OptionalDependencies": [],
182        },
183        "Version_Bump_Optional": {
184            "Dependencies": [{"Name": "Version_Bump_Optional_Dep", "Version": "1.5.0"}],
185            "OptionalDependencies": [
186                {"Name": "Version_Bump_Optional_Dep", "Version": "1.7.0"}
187            ],
188        },
189    }
190)
191
192
193@pytest.mark.parametrize(
194    ("mods", "expect"),
195    [
196        pytest.param([], "[]", id="Empty"),
197        pytest.param([modmeta_for("Simple")], "[]"),
198        pytest.param(
199            [modmeta_for("Nested")],
200            "[Nested_1: 1.0.0, Nested_2: 1.0.0]",
201        ),
202        pytest.param(
203            [
204                modmeta_for("Version_Bump_Lower"),
205                modmeta_for("Version_Bump_Higher"),
206                modmeta_for("Version_Bump_Lower"),  # make sure order doesn't matter
207            ],
208            "[Version_Bump_Dep: 1.5.0]",
209        ),
210    ],
211    ids=id_modmeta,
212)
213def test_resolve_dependencies(mods, expect):
214    dependencies, _opt_deps = resolve_dependencies(mods)
215    assert str(dependencies) == expect
216
217
218dep_graph.update(
219    {
220        "Opt_Deps_Nested": {
221            "Dependencies": [],
222            "OptionalDependencies": [{"Name": "Opt_Deps_Nested_1", "Version": "1.0.0"}],
223        },
224        **{
225            "Opt_Deps_Nested_1": {
226                "Dependencies": [{"Name": "Opt_Deps_Nested_Dep", "Version": "1.0.0"}],
227                "OptionalDependencies": [
228                    {"Name": "Opt_Deps_Nested_Opt", "Version": "1.0.0"}
229                ],
230            },
231            **{
232                "Opt_Deps_Nested_Dep": {},
233                "Opt_Deps_Nested_Opt": {},
234            },
235        },
236        "Opt_Deps_Shadowed": {
237            "Dependencies": [{"Name": "Opt_Deps_Shadowed_Dep", "Version": "1.3.0"}],
238            "OptionalDependencies": [
239                {"Name": "Opt_Deps_Shadowed_Dep", "Version": "1.6.2"}
240            ],
241        },
242    }
243)
244
245
246@pytest.mark.parametrize(
247    ("mods", "expect", "expect_opt"),
248    [
249        ([modmeta_for("Opt_Deps_Nested")], "[]", "[Opt_Deps_Nested_1: 1.0.0]"),
250        ([modmeta_for("Opt_Deps_Shadowed")], "[Opt_Deps_Shadowed_Dep: 1.6.2]", "[]"),
251    ],
252    ids=id_modmeta,
253)
254def test_resolve_dependencies_optional(mods, expect, expect_opt):
255    deps, opt_deps = resolve_dependencies(mods)
256    assert str(opt_deps) == expect_opt
257    assert str(deps) == expect
258
259
260dep_graph.update(
261    {
262        "Error_Major_Ver": {
263            "Dependencies": [
264                {"Name": "Error_Major_Ver_Dep", "Version": "1.5.0"},
265                {"Name": "Error_Major_Ver_Dep", "Version": "2.1.0"},
266            ],
267            "OptionalDependencies": [],
268        },
269        "Error_Major_Ver_Opt": {
270            "Dependencies": [{"Name": "Error_Major_Ver_Opt_Dep", "Version": "1.5.0"}],
271            "OptionalDependencies": [
272                {"Name": "Error_Major_Ver_Opt_Dep", "Version": "2.1.0"}
273            ],
274        },
275        "Error_Unfulfilled": {
276            "Dependencies": [{"Name": "Error_Unfulfilled", "Version": "1.5.0"}],
277            "OptionalDependencies": [],
278        },
279        "Error_Unfulfilled_Opt": {
280            "Dependencies": [],
281            "OptionalDependencies": [
282                {"Name": "Error_Unfulfilled_Opt", "Version": "1.5.0"}
283            ],
284        },
285    }
286)
287
288
289@pytest.mark.parametrize(
290    ("mods", "raises"),
291    [
292        (
293            [modmeta_for("Error_Major_Ver")],
294            pytest.raises(
295                ValueError, match=r"Incompatible dependencies.*different major version"
296            ),
297        ),
298        (
299            [modmeta_for("Error_Major_Ver_Opt")],
300            pytest.raises(
301                ValueError, match=r"Incompatible dependencies.*different major version"
302            ),
303        ),
304        (
305            [modmeta_for("Error_Unfulfilled")],
306            pytest.raises(
307                ValueError,
308                match=r"Incompatible dependencies.*\(explicit\) does not satisfy dependency",
309            ),
310        ),
311        (
312            [modmeta_for("Error_Unfulfilled_Opt")],
313            pytest.raises(
314                ValueError,
315                match=r"Incompatible dependencies.*\(explicit\) does not satisfy optional dependency",
316            ),
317        ),
318    ],
319    ids=id_modmeta,
320)
321def test_resolve_dependencies_fail(mods, raises):
322    with raises:
323        resolve_dependencies(mods)
324
325
326installed_mods = {
327    mod.Name: mod
328    for mod in [
329        ModMeta({"Name": "No_Deps", "Version": "1.0.0"}),
330        ModMeta(
331            {
332                "Name": "Shared_Deps",
333                "Version": "1.0.0",
334                "Dependencies": [
335                    {"Name": "Shared_Deps_Dep", "Version": "1.0.0"},
336                ],
337            }
338        ),
339        *[
340            ModMeta({"Name": "Shared_Deps_Dep", "Version": "1.0.0"}),
341        ],
342        ModMeta(
343            {
344                "Name": "Exclusive_Deps",
345                "Version": "1.0.0",
346                "Dependencies": [
347                    {"Name": "Exclusive_Deps_Dep", "Version": "1.0.0"},
348                ],
349            }
350        ),
351        *[ModMeta({"Name": "Exclusive_Deps_Dep", "Version": "1.0.0"})],
352        ModMeta(
353            {
354                "Name": "Shared_Exclusive_Deps",
355                "Version": "1.0.0",
356                "Dependencies": [
357                    {"Name": "Shared_Deps_Dep", "Version": "1.0.0"},
358                    {"Name": "Shared_Exclusive_Deps_Dep", "Version": "1.0.0"},
359                ],
360            }
361        ),
362        *[
363            ModMeta({"Name": "Shared_Exclusive_Deps_Dep", "Version": "1.0.0"}),
364        ],
365    ]
366}
367
368
369@pytest.mark.parametrize(
370    ("mods", "expect"),
371    [
372        ([installed_mods["No_Deps"]], "[]"),
373        ([installed_mods["Shared_Deps"]], "[]"),
374        ([installed_mods["Exclusive_Deps"]], "[Exclusive_Deps_Dep: 1.0.0]"),
375        (
376            [installed_mods["Shared_Exclusive_Deps"]],
377            "[Shared_Exclusive_Deps_Dep: 1.0.0]",
378        ),
379    ],
380    ids=id_modmeta,
381)
382def test_resolve_exclusive_dependencies(mods, expect):
383    deps = resolve_exclusive_dependencies(mods, installed_mods)
384    assert str(deps) == expect