quorate/services/minecraft/test_publish.py

58 lines
2.4 KiB
Python

from __future__ import annotations
import hashlib
import io
import tempfile
import unittest
from pathlib import Path
from typing import Any
import publish
class MemoryStore:
def __init__(self) -> None:
self.objects: dict[str, bytes] = {}
def put_object(self, *, Bucket: str, Key: str, Body: Any) -> None:
assert Bucket == "artifacts"
self.objects[Key] = Body if isinstance(Body, bytes) else Body.read()
def get_object(self, *, Bucket: str, Key: str) -> dict[str, io.BytesIO]:
assert Bucket == "artifacts"
return {"Body": io.BytesIO(self.objects[Key])}
class PublishTest(unittest.TestCase):
def test_publish(self) -> None:
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
source, tools = root / "pack", root / "tools"
for group in ("config", "defaultconfigs", "kubejs", "libraries", "libraries-integratedscripting", "mods"):
(source / group).mkdir(parents=True)
(source / group / "file").write_text(group)
(source / "mods/test.jar").write_bytes(b"mod")
(source / "user_jvm_args.txt").write_text("-Xmx1G")
ranks = source / "world/serverconfig/ftbranks/ranks.snbt"
ranks.parent.mkdir(parents=True)
ranks.write_text("ranks")
(ranks.parent / "players.snbt").write_text("test player")
(source / "server.properties").write_text("test credential")
(tools / "amd64").mkdir(parents=True)
(tools / "amd64/mc-monitor").write_bytes(b"monitor")
exporter = root / "exporter.jar"
exporter.write_bytes(b"exporter")
files = publish.files_for(source, tools, exporter)
client = MemoryStore()
publish.publish(client, files)
self.assertEqual(set(files["mods"]), {"test.jar", "exporter.jar"})
self.assertNotIn("minecraft/server.properties", client.objects)
self.assertNotIn("minecraft/world/serverconfig/ftbranks/players.snbt", client.objects)
self.assertIn("minecraft/world/serverconfig/ftbranks/ranks.snbt", client.objects)
self.assertEqual(client.objects["minecraft/world-datapacks.sha256"], b"")
expected = hashlib.sha256(b"mod").hexdigest()
self.assertIn(f"{expected} test.jar\n".encode(), client.objects["minecraft/mods.sha256"])
if __name__ == "__main__":
unittest.main()