plugins/tools/test_gameplay_resourcepack.py

66 lines
3 KiB
Python

import importlib.util
import json
from pathlib import Path
import tempfile
import unittest
import zipfile
ROOT = Path(__file__).resolve().parents[1]
spec = importlib.util.spec_from_file_location('gameplay', ROOT / 'tools/build-gameplay-resourcepack.py')
pack = importlib.util.module_from_spec(spec)
spec.loader.exec_module(pack)
class GameplayPackTests(unittest.TestCase):
def test_real_assets_in_final_zip_and_existing_gui_unchanged(self):
with tempfile.TemporaryDirectory() as folder:
baseline, output = Path(folder) / 'baseline.zip', Path(folder) / 'result.zip'
with zipfile.ZipFile(baseline, 'w') as archive:
archive.writestr('pack.mcmeta', '{"pack":{"pack_format":64}}')
archive.writestr('assets/gui/unchanged.bin', b'working GUI')
manifest = pack.build(baseline, output)
actual = pack.entries(output)
self.assertEqual(actual['assets/gui/unchanged.bin'], b'working GUI')
self.assertEqual(actual['pack.mcmeta'], b'{"pack":{"pack_format":64}}')
for path in pack.SHARED.rglob('*'):
if path.is_file():
self.assertEqual(actual[path.relative_to(pack.SHARED).as_posix()], path.read_bytes())
self.assertIn('assets/dunerealms/items/spy_module.json', actual)
self.assertIn('assets/dunerealms/items/currency/note_100.json', actual)
self.assertEqual(manifest['cardMappings'], len(json.loads(pack.GLYPHS.read_text())))
first = output.read_bytes()
pack.build(baseline, output)
self.assertEqual(first, output.read_bytes())
def test_missing_texture_or_glyph_fails(self):
files = {p.relative_to(pack.SHARED).as_posix(): p.read_bytes()
for p in pack.SHARED.rglob('*') if p.is_file()}
glyphs = json.loads(pack.GLYPHS.read_text())
missing = dict(files)
missing.pop('assets/dunerealms/textures/gui/card-art/atlas-00.png')
with self.assertRaises(ValueError):
pack.validate(missing, glyphs)
with self.assertRaises(ValueError):
pack.validate(files, {**glyphs, 'invalid': 65535})
def test_existing_assets_cannot_be_overwritten(self):
with tempfile.TemporaryDirectory() as folder:
baseline = Path(folder) / 'baseline.zip'
with zipfile.ZipFile(baseline, 'w') as archive:
archive.writestr('pack.mcmeta', '{}')
archive.writestr('assets/dunerealms/font/card_art.json', b'original')
with self.assertRaises(ValueError):
pack.build(baseline, Path(folder) / 'result.zip')
def test_unsafe_baseline_fails(self):
with tempfile.TemporaryDirectory() as folder:
path = Path(folder) / 'bad.zip'
with zipfile.ZipFile(path, 'w') as archive:
archive.writestr('pack.mcmeta', '{}')
archive.writestr('../unsafe', b'bad')
with self.assertRaises(ValueError):
pack.entries(path)
if __name__ == '__main__':
unittest.main()