fork/mons

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

coloursofnoiseAdd integration tests for mod management commandsf0f1db8

misc-fixes
9.6 KiB335 linesraw
1import os
2import typing as t
3import zipfile
4
5import pytest
6import yaml
7
8import mons.commands.mods
9import mons.install
10from mons.mons import cli as mons_cli
11
12
13@pytest.fixture(autouse=True)
14def temp_mod_folder(monkeypatch, tmp_path):
15    mod_folder = os.path.join(tmp_path, "Mods")
16    os.mkdir(mod_folder)
17    monkeypatch.setattr(mons.install.Install, "mod_folder", mod_folder)
18    return mod_folder
19
20
21mod_db = {}
22dep_graph = {}
23installed_mods: t.Dict[str, t.Dict[str, str]] = {}
24
25
26@pytest.fixture(autouse=True)
27def fake_mod_db(monkeypatch, temp_mod_folder):
28    monkeypatch.setattr(mons.commands.mods, "fetch_mod_db", lambda *args: mod_db)
29    monkeypatch.setattr(
30        mons.commands.mods, "fetch_dependency_graph", lambda *args: dep_graph
31    )
32
33
34def fake_mod_db_request(request, temp_mod_folder, monkeypatch):
35    monkeypatch.setattr(mons.commands.mods, "fetch_mod_db", lambda *args: request.param)
36
37
38@pytest.fixture
39def fake_mods(temp_mod_folder):
40    for filename, data in installed_mods.items():
41        mod_file = os.path.join(temp_mod_folder, filename)
42        with zipfile.ZipFile(mod_file, "w") as zip:
43            for filename, filedata in data.items():
44                zip.writestr(filename, filedata)
45    return None
46
47
48@pytest.fixture
49def fake_mods_request(request, temp_mod_folder):
50    mods = request.param
51    for mod in mods:
52        filepath = os.path.join(temp_mod_folder, f"{mod}.zip")
53        with zipfile.ZipFile(filepath, "w") as zip:
54            zip.writestr(
55                "everest.yaml",
56                yaml.dump(
57                    [
58                        {
59                            "Name": mod,
60                            "Version": "2.2.2",
61                        }
62                    ]
63                ),
64            )
65    return mods
66
67
68mod_db.update(
69    {
70        "Installed": {
71            "URL": "",
72            "Version": "1.0.0",
73            "GameBananaId": 12345,
74            "LastUpdate": 0,
75        },
76        "Outdated": {
77            "URL": "",
78            "Version": "1.0.0",
79            "GameBananaId": 12345,
80            "LastUpdate": 0,
81        },
82        "Missing": {
83            "URL": "",
84            "Version": "1.0.0",
85            "GameBananaId": 12345,
86            "LastUpdate": 0,
87        },
88    }
89)
90
91
92def test_mods_add_none(runner_result, test_install):
93    with runner_result(mons_cli, ["mods", "add", test_install, "--yes"]) as result:
94        assert result.exit_code == 0
95        assert "No mods to add." in result.output
96
97
98@pytest.mark.parametrize(
99    "data_file_zip",
100    [
101        {
102            "everest.yaml": yaml.dump(
103                [
104                    {
105                        "Name": "TestMod",
106                        "Version": "1.0.0",
107                    }
108                ]
109            )
110        }
111    ],
112    indirect=("data_file_zip",),
113)
114def test_mods_add_zip(runner_result, test_install, data_file_zip, temp_mod_folder):
115    with runner_result(
116        mons_cli, ["mods", "add", test_install, *data_file_zip, "--yes"]
117    ) as result:
118        assert result.exit_code == 0
119        assert "TestMod" in result.output
120        assert "Installed mods" in result.output
121
122    assert os.path.isfile(os.path.join(temp_mod_folder, "TestMod.zip")), os.listdir(
123        temp_mod_folder
124    )
125
126
127@pytest.mark.data_file_zip(
128    {
129        "everest.yaml": yaml.dump(
130            [
131                {
132                    "Name": "TestMod",
133                    "Version": "1.0.0",
134                }
135            ]
136        )
137    }
138)
139def test_mods_add_stdin(runner_result, test_install, data_file_zip, temp_mod_folder):
140    with open(data_file_zip[0]) as stdin, runner_result(
141        mons_cli, ["mods", "add", test_install, "-", "--yes"], input=stdin
142    ) as result:
143        assert result.exit_code == 0
144        assert "TestMod" in result.output
145        assert "Installed mods" in result.output
146
147    assert os.path.isfile(os.path.join(temp_mod_folder, "TestMod.zip")), os.listdir(
148        temp_mod_folder
149    )
150
151
152@pytest.mark.data_file_zip(
153    {
154        "everest.yaml": yaml.dump(
155            [
156                {
157                    "Name": "TestMod",
158                    "Version": "1.0.0",
159                }
160            ]
161        )
162    }
163)
164def test_mods_add_blacklisted(
165    runner_result, test_install, data_file_zip, temp_mod_folder
166):
167    blacklist = os.path.join(temp_mod_folder, "blacklist.txt")
168    with open(blacklist, "w") as blacklist_file:
169        blacklist_file.write("TestMod.zip")
170
171    with runner_result(
172        mons_cli, ["mods", "add", test_install, *data_file_zip, "--yes"]
173    ) as result:
174        assert result.exit_code == 0
175        assert (
176            "The following mods will be automatically removed from the blacklist"
177            in result.output
178        )
179
180    with open(blacklist) as blacklist_file:
181        contents = blacklist_file.read()
182        assert "#TestMod.zip" in contents, contents
183
184
185@pytest.mark.data_file_zip(
186    {
187        "everest.yaml": yaml.dump(
188            [
189                {
190                    "Name": "TestMod",
191                    "Version": "1.0.0",
192                }
193            ]
194        )
195    }
196)
197def test_mods_add_skip_unzipped(
198    runner_result, test_install, data_file_zip, temp_mod_folder
199):
200    mod_dir = os.path.join(temp_mod_folder, "TestMod")
201    os.mkdir(mod_dir)
202    with open(os.path.join(mod_dir, "everest.yaml"), "w") as testmodyaml:
203        testmodyaml.write(yaml.dump([{"Name": "TestMod", "Version": "1.0.0"}]))
204
205    with runner_result(
206        mons_cli, ["mods", "add", test_install, *data_file_zip, "--yes"]
207    ) as result:
208        assert result.exit_code == 0
209        assert "Unzipped mods will not be updated" in result.output
210
211
212@pytest.mark.data_file_zip({"everest.yaml": yaml.dump("hi!")})
213def test_mods_add_invalid_mod(
214    runner_result, test_install, data_file_zip, temp_mod_folder
215):
216    with runner_result(
217        mons_cli,
218        ["mods", "add", test_install, f"file://{data_file_zip[0]}", "--force"],
219        input="NotAMod.zip\n",
220    ) as result:
221        assert result.exit_code == 0
222        assert "The following mods could not be resolved" in result.output
223
224    mod_added = os.path.isfile(os.path.join(temp_mod_folder, "NotAMod.zip"))
225    assert mod_added, os.listdir(temp_mod_folder)
226
227
228installed_mods.update(
229    {
230        "Remove.zip": {
231            "everest.yaml": yaml.dump(
232                [
233                    {
234                        "Name": "Remove",
235                        "Version": "1.0.0",
236                    }
237                ]
238            )
239        },
240        "Remove_Recurse.zip": {
241            "everest.yaml": yaml.dump(
242                [
243                    {
244                        "Name": "Remove_Recurse",
245                        "Version": "1.0.0",
246                        "Dependencies": [
247                            {"Name": "Remove_Recurse_Dep", "Version": "1.0.0"},
248                            {"Name": "Remove_Recurse_Shared", "Version": "1.0.0"},
249                        ],
250                    }
251                ]
252            )
253        },
254        "Remove_Recurse_Dep.zip": {
255            "everest.yaml": yaml.dump(
256                [
257                    {
258                        "Name": "Remove_Recurse_Dep",
259                        "Version": "1.0.0",
260                    }
261                ]
262            )
263        },
264        "Remove_Recurse_Shared.zip": {
265            "everest.yaml": yaml.dump(
266                [
267                    {
268                        "Name": "Remove_Recurse_Shared",
269                        "Version": "1.0.0",
270                    }
271                ]
272            )
273        },
274        "Remove_Other.zip": {
275            "everest.yaml": yaml.dump(
276                [
277                    {
278                        "Name": "Remove_Other",
279                        "Version": "1.0.0",
280                        "Dependencies": [
281                            {"Name": "Remove_Recurse_Shared", "Version": "1.0.0"}
282                        ],
283                    }
284                ]
285            )
286        },
287    }
288)
289
290
291def test_mods_remove(runner_result, test_install, temp_mod_folder, fake_mods):
292    with runner_result(
293        mons_cli, ["mods", "remove", test_install, "Remove", "--force"]
294    ) as result:
295        assert result.exit_code == 0
296
297    assert not os.path.exists(os.path.join(temp_mod_folder, "Remove.zip")), os.listdir(
298        temp_mod_folder
299    )
300
301
302def test_mods_remove_recurse(runner_result, test_install, temp_mod_folder, fake_mods):
303    with runner_result(
304        mons_cli,
305        ["mods", "remove", test_install, "--recurse", "Remove_Recurse", "--force"],
306    ) as result:
307        assert result.exit_code == 0
308
309    for filename in ["Remove_Recurse.zip", "Remove_Recurse_Dep.zip"]:
310        filepath = os.path.join(temp_mod_folder, filename)
311        assert not os.path.exists(filepath), os.listdir(temp_mod_folder)
312
313    filepath = os.path.join(temp_mod_folder, "Remove_Recurse_Shared.zip")
314    assert os.path.exists(filepath), os.listdir(temp_mod_folder)
315
316
317def test_mods_remove_none(runner_result, test_install, temp_mod_folder, fake_mods):
318    with runner_result(
319        mons_cli, ["mods", "remove", test_install, "Non-Existent-Mod", "--force"]
320    ) as result:
321        assert result.exit_code == 0
322        assert "No mods to remove." in result.output
323
324
325def test_mods_update(runner_result, test_install, temp_mod_folder, fake_mods):
326    with runner_result(mons_cli, ["mods", "update", test_install]) as result:
327        assert result.exit_code == 0
328        assert "All mods up to date" in result.output
329
330
331def test_mods_list(runner_result, test_install, temp_mod_folder, fake_mods):
332    with runner_result(mons_cli, ["mods", "list", test_install]) as result:
333        assert result.exit_code == 0
334        # strip .zip from filenames
335        assert all(mod[:-4] in result.output for mod in installed_mods.keys())