71 lines
3.2 KiB
Python
71 lines
3.2 KiB
Python
import hashlib
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
|
|
from PIL import Image, ImageDraw
|
|
|
|
from plugin_supplied_item_art import read_pack
|
|
from plugin_wild_update import write_archive
|
|
|
|
|
|
def tool(name):
|
|
spec = importlib.util.spec_from_file_location(name, Path(__file__).with_name(name + ".py"))
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
compiler = tool("compile-realistic-stone-family")
|
|
assembler = tool("assemble-realistic-stone-pack")
|
|
|
|
|
|
class StoneFamilyTest(unittest.TestCase):
|
|
def test_complete_catalog_composites_ores_and_keeps_unrelated_files(self):
|
|
with tempfile.TemporaryDirectory() as location:
|
|
root = Path(location)
|
|
inputs = root / "sources"
|
|
inputs.mkdir()
|
|
for name in compiler.CORE + compiler.OPTIONAL:
|
|
Image.new("RGB", (256, 256), (122, 118, 110)).save(inputs / f"{name}-source.png")
|
|
ore = Image.new("RGBA", (260, 256))
|
|
ImageDraw.Draw(ore).rectangle((104, 104, 151, 151), fill=(200, 30, 55, 255))
|
|
for name in compiler.MINERALS + ("quartz", "ancient_debris"):
|
|
ore.save(inputs / f"{name}-deposit.png")
|
|
output = root / "export"
|
|
result = compiler.write_tiles(inputs, output, complete=True)
|
|
self.assertEqual(set(result["entries"]), set(json.loads(assembler.CATALOG.read_bytes())))
|
|
copper = compiler.TEXTURES / "copper_ore.png"
|
|
self.assertEqual(result["entries"][copper.as_posix()]["inputs"], ["stone-source.png", "copper-deposit.png"])
|
|
with Image.open(output / copper) as texture:
|
|
self.assertEqual(texture.getpixel((0, 0)), (122, 118, 110))
|
|
self.assertEqual(texture.getpixel((125, 125)), (200, 30, 55))
|
|
retained = {"pack.mcmeta": b"metadata", "assets/dunerealms/test": b"custom"}
|
|
baseline = root / "base.zip"
|
|
baseline.write_bytes(write_archive(retained))
|
|
original_pin = assembler.BASE["arrakis"]
|
|
assembler.BASE["arrakis"] = hashlib.sha256(baseline.read_bytes()).hexdigest()
|
|
try:
|
|
receipt = assembler.sha((output / "stone-family.json").read_bytes())
|
|
final = root / "tested.zip"
|
|
with self.assertRaisesRegex(ValueError, "receipt"):
|
|
assembler.assemble("arrakis", baseline, output, "0" * 64, final)
|
|
self.assertEqual(assembler.assemble("arrakis", baseline, output, receipt, final)["changed"], 81)
|
|
packed = read_pack(final)
|
|
self.assertEqual({name: packed[name] for name in retained}, retained)
|
|
self.assertEqual(set(packed) - set(retained), set(result["entries"]))
|
|
finally:
|
|
assembler.BASE["arrakis"] = original_pin
|
|
|
|
def test_opaque_ore_source_fails(self):
|
|
with tempfile.TemporaryDirectory() as location:
|
|
path = Path(location) / "opaque.png"
|
|
Image.new("RGB", (256, 256), (45, 80, 90)).save(path)
|
|
with self.assertRaisesRegex(ValueError, "transparent background"):
|
|
compiler.source_tile(path, "RGBA")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|

Staff