|
1 | 1 | #!/usr/bin/env python3
|
| 2 | +import os |
| 3 | +import shutil |
| 4 | +from pathlib import Path |
2 | 5 | from unittest.mock import patch
|
3 | 6 |
|
4 | 7 | import pytest
|
5 | 8 |
|
6 | 9 | from ammo.controller.mod import ModController
|
7 | 10 |
|
| 11 | +EXTRACT_CACHE = Path("/tmp/ammo_extract_cache/") |
| 12 | + |
| 13 | + |
| 14 | +def get_cached_archive(self, index: int, download: Path) -> Path: |
| 15 | + """ |
| 16 | + See if an extracted archive is cached. If it is, copy it to |
| 17 | + /tmp/ammo_test/MockGame/mods/. If it's not, extract it, cache it, |
| 18 | + then copy it to /tmp/ammo_test/MockGame/mods/. |
| 19 | +
|
| 20 | + Return Path("/tmp/ammo_test/MockGame/mods/<extracted_archive>") |
| 21 | + """ |
| 22 | + extract_to_name = "".join( |
| 23 | + [i for i in download.location.stem.replace(" ", "_") if i.isalnum() or i == "_"] |
| 24 | + ).strip() |
| 25 | + |
| 26 | + extract_to = self.game.ammo_mods_dir / extract_to_name |
| 27 | + if extract_to.exists(): |
| 28 | + raise Warning( |
| 29 | + f"Extraction of {index} failed since mod '{extract_to.name}' exists." |
| 30 | + ) |
| 31 | + |
| 32 | + archive_at = EXTRACT_CACHE / extract_to_name |
| 33 | + if not archive_at.exists(): |
| 34 | + os.system(f"7z x '{download.location}' -o'{archive_at}'") |
| 35 | + |
| 36 | + os.system(f"cp -r {archive_at} {extract_to}") |
| 37 | + |
| 38 | + return extract_to |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture(scope="session", autouse=True) |
| 42 | +def mock_extract_archive(): |
| 43 | + try: |
| 44 | + EXTRACT_CACHE.mkdir(parents=True, exist_ok=True) |
| 45 | + with patch.object(ModController, "extract_archive", get_cached_archive): |
| 46 | + yield |
| 47 | + finally: |
| 48 | + shutil.rmtree(EXTRACT_CACHE, ignore_errors=True) |
| 49 | + |
8 | 50 |
|
9 | 51 | @pytest.fixture
|
10 | 52 | def mock_has_extra_folder():
|
|
0 commit comments