Files
horizon-airdef/tests/test_gen_version_consts.py
T
Ludovic Marie 9f385280ef Version facts in one generated file: crates/horizon-core/src/version.rs
tools/gen_version_consts.py writes what the pinned client jar's version.json
states (name, id, protocol, data version, pack formats, Java, stable, build
time) into horizon_core::version, and every Rust site reads it from there:
PROTOCOL_VERSION, GAME_VERSION, DATA_VERSION, the save's and the datafixer's
version name, /version, the crash report, the legacy ping, the dedicated
server's start line, the title screens, the default resource folder, the
resource pack format, and the launcher's and horizon-ctl's fallbacks. CI
checks the file against the pinned jar.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 12:22:20 +02:00

49 lines
2.1 KiB
Python

import json
from pathlib import Path
import re
import tempfile
import unittest
import zipfile
from tools import gen_version_consts as gen
from tools import lab
INFO = {
"id": "26.4", "name": "26.4", "world_version": 5100, "protocol_version": 780,
"pack_version": {"resource_major": 99, "resource_minor": 0, "data_major": 124, "data_minor": 2},
"build_time": "2026-12-01T09:05:07+00:00", "java_version": 25, "stable": False,
}
class GenVersionConstsTests(unittest.TestCase):
def test_renders_every_fact(self):
text = gen.render(INFO)
for line in ['pub const VERSION_ID: &str = "26.4";', 'pub const VANILLA_VERSION: &str = "26.4";',
"pub const PROTOCOL_VERSION: i32 = 780;", "pub const DATA_VERSION: i32 = 5100;",
"pub const RESOURCE_PACK_FORMAT: (i64, i64) = (99, 0);",
"pub const DATA_PACK_FORMAT: (i64, i64) = (124, 2);", "pub const JAVA_VERSION: u32 = 25;",
"pub const STABLE: bool = false;", 'pub const BUILD_TIME: &str = "Tue Dec 01 09:05:07 UTC 2026";']:
self.assertIn(line, text)
def test_build_time_is_printed_in_utc(self):
self.assertEqual(gen.java_date("2026-09-15T13:20:48+02:00"), "Tue Sep 15 11:20:48 UTC 2026")
def test_reads_the_jar(self):
with tempfile.TemporaryDirectory() as directory:
jar = Path(directory) / "client.jar"
with zipfile.ZipFile(jar, "w") as archive:
archive.writestr("version.json", json.dumps(INFO))
self.assertEqual(gen.jar_info(jar), INFO)
def test_committed_file_is_the_pinned_version(self):
"""A pin bump regenerates version.rs: the lock and the constants never disagree."""
text = gen.OUT.read_text()
pinned = json.loads(lab.LOCK.read_text())["minecraft"]
self.assertEqual(re.search(r'VANILLA_VERSION: &str = "([^"]+)"', text)[1], pinned["version"])
self.assertEqual(int(re.search(r"JAVA_VERSION: u32 = (\d+)", text)[1]), pinned["java_major"])
self.assertIn("Generated by tools/gen_version_consts.py", text)
if __name__ == "__main__":
unittest.main()