"""Exercise the real CMake resource functions without requiring a Qt configure.""" from pathlib import Path import os import re import subprocess import tempfile import unittest import xml.etree.ElementTree as ET ROOT = Path(__file__).resolve().parents[1] SOURCE = ROOT / "freecad-source/cMake/FreeCAD_Helpers/SetupQt.cmake" class TranslationResourceStability(unittest.TestCase): def test_content_changes_update_resources_but_reconfigure_preserves_timestamps(self): source = SOURCE.read_text() for name, prefix in (("qt_create_resource_file", "translations/"), ("qt_create_resource_file_prefix", "")): with self.subTest(name=name), tempfile.TemporaryDirectory(prefix="freecad-qrc-") as temporary: directory = Path(temporary) body = re.search(rf"function\({name} outfile\).*?endfunction\(\)", source, re.S) self.assertIsNotNone(body) script = directory / "test.cmake" output = directory / "with spaces" / "translations.qrc" def run(files): arguments = " ".join(f'"/inputs/{value}"' for value in files) script.write_text(f'{body.group()}\n{name}("{output.as_posix()}" {arguments})\n') subprocess.run(["cmake", "-P", str(script)], check=True, capture_output=True, text=True, timeout=10) run(["Base_en.qm", "Base_zh_CN.qm"]) expected = output.read_bytes() os.utime(output, ns=(1_600_000_000_000_000_000, 1_600_000_000_000_000_000)) first_mtime = output.stat().st_mtime_ns run(["Base_en.qm", "Base_zh_CN.qm"]) self.assertEqual(output.read_bytes(), expected) self.assertEqual(output.stat().st_mtime_ns, first_mtime) resource = ET.fromstring(expected).find("qresource") self.assertEqual([item.text for item in resource], [prefix + "Base_en.qm", prefix + "Base_zh_CN.qm"]) self.assertEqual(resource.get("prefix"), "/translations" if not prefix else None) run(["Base_zh_CN.qm", "Base_fr.qm"]) self.assertNotEqual(output.stat().st_mtime_ns, first_mtime) self.assertEqual([item.text for item in ET.fromstring(output.read_bytes()).find("qresource")], [prefix + "Base_zh_CN.qm", prefix + "Base_fr.qm"]) run([]) self.assertEqual(list(ET.fromstring(output.read_bytes()).find("qresource")), []) if __name__ == "__main__": unittest.main()