"""Offline tests for gen_shader_profiles.py (synthetic packs; no game).""" import contextlib import io import sys import tempfile import unittest import zipfile from pathlib import Path HERE = Path(__file__).resolve().parent sys.path.insert(0, str(HERE.parent)) import gen_shader_profiles as g # noqa: E402 PROPS = ( "# Profiles\r\n" " profile.BASE = A=1 !B C D:4 \\\r\n" " E=x\r\n" "profile.LOW = profile.BASE A=2 !C\r\n" "profile.TOP = profile.LOW B E=y\r\n" "screen= A B\r\n" "#if A == 1\r\n" " program.world0/shadow.enabled=false\r\n" "#endif\r\n" ) class Parse(unittest.TestCase): def test_tokens_and_continuation(self): p = g.parse_profiles(PROPS) self.assertEqual(sorted(p), ["BASE", "LOW", "TOP"]) self.assertEqual(p["BASE"], ["A=1", "!B", "C", "D:4", "E=x"]) def test_resolve_semantics(self): p = g.parse_profiles(PROPS) opts, prog = g.resolve("BASE", p) self.assertEqual(opts, {"A": "1", "B": "false", "C": "true", "D": "4", "E": "x"}) self.assertEqual(prog, []) opts, _ = g.resolve("TOP", p) # includes recurse; later tokens win self.assertEqual(opts, {"D": "4", "A": "2", "C": "false", "B": "true", "E": "y"}) def test_errors(self): p = {"X": ["profile.Y"], "Y": ["profile.X"]} with self.assertRaisesRegex(g.ProfileError, "cycle"): g.resolve("X", p) with self.assertRaisesRegex(g.ProfileError, "unknown profile"): g.resolve("NOPE", g.parse_profiles(PROPS)) with self.assertRaisesRegex(g.ProfileError, "conditional"): g.parse_profiles("#ifdef MC_OS_MAC\nprofile.M = A=1\n#endif\n") with self.assertRaisesRegex(g.ProfileError, "escaping"): g.render({"A": "has space"}) def test_programs_collected(self): _, prog = g.resolve("P", {"P": ["!program.world0/shadow", "A"]}) self.assertEqual(prog, ["world0/shadow"]) class Generate(unittest.TestCase): def setUp(self): self.tmp = Path(tempfile.mkdtemp()) self.zip = self.tmp / "Pack_1.0.zip" with zipfile.ZipFile(self.zip, "w") as z: z.writestr("shaders/shaders.properties", PROPS) z.writestr("shaders/lib/settings.glsl", "#define A 1 // [1 2]\n//#define B\n#define C\nconst int D = 4; // [4 8]\n") def test_file_and_unknowns(self): text, n, unknown = g.generate(self.zip, "LOW") self.assertEqual(n, 5) self.assertEqual(unknown, ["E"]) body = [ln for ln in text.splitlines() if not ln.startswith("#")] self.assertEqual(body, ["B=false", "D=4", "E=x", "A=2", "C=false"]) self.assertIn("profile.LOW of Pack_1.0.zip", text) self.assertEqual(g.out_name("Pack_1.0.zip", "LOW"), "Pack_1.0.LOW.txt") def test_program_disabling_profile_rejected(self): z2 = self.tmp / "P2.zip" with zipfile.ZipFile(z2, "w") as z: z.writestr("shaders/shaders.properties", "profile.X = !program.composite A\n") with self.assertRaisesRegex(g.ProfileError, "disables programs"): g.generate(z2, "X") def test_cli_write_then_check(self): out = self.tmp / "out" args = ["--packs", str(self.tmp), "--out", str(out), "--target", "Pack_1.0.zip:TOP"] with contextlib.redirect_stdout(io.StringIO()): self.assertEqual(g.main(args), 0) self.assertTrue((out / "Pack_1.0.TOP.txt").is_file()) self.assertEqual(g.main(args + ["--check"]), 0) (out / "Pack_1.0.TOP.txt").write_text("stale\n") self.assertEqual(g.main(args + ["--check"]), 1) def test_matrix_targets(self): t = g.matrix_targets() self.assertIn(("MakeUp-UltraFast-9.5f.zip", "medium"), t) self.assertIn(("ComplementaryReimagined_r5.9.3.zip", "LOW"), t) if __name__ == "__main__": unittest.main()