|
| 1 | +# Generated by Google Antigravity (Gemini 4 Neon) without human creative authorship. |
| 2 | +# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + |
| 4 | +import importlib |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import tempfile |
| 8 | +import types |
| 9 | +import unittest |
| 10 | +from unittest import mock |
| 11 | + |
| 12 | +ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)) |
| 13 | +PYTHON_DIR = os.path.join(ROOT, "python") |
| 14 | +if PYTHON_DIR not in sys.path: |
| 15 | + sys.path.insert(0, PYTHON_DIR) |
| 16 | + |
| 17 | + |
| 18 | +def _import_rime_ime_with_mock_librime(appdata_dir): |
| 19 | + """Import input_methods.rime.rime_ime with a stubbed librime module.""" |
| 20 | + init_calls = [] |
| 21 | + |
| 22 | + stub_librime = types.ModuleType("input_methods.rime.librime") |
| 23 | + stub_librime.RIME = "Rime" |
| 24 | + stub_librime.ENC = "utf-8" |
| 25 | + stub_librime.RimeNotificationHandler = lambda fn: fn |
| 26 | + stub_librime.rime = mock.MagicMock() |
| 27 | + |
| 28 | + def fake_rime_init(**kwargs): |
| 29 | + init_calls.append(kwargs) |
| 30 | + |
| 31 | + stub_librime.rimeInit = fake_rime_init |
| 32 | + stub_librime.__all__ = [ |
| 33 | + "RIME", |
| 34 | + "ENC", |
| 35 | + "RimeNotificationHandler", |
| 36 | + "rime", |
| 37 | + "rimeInit", |
| 38 | + ] |
| 39 | + |
| 40 | + mod_name = "input_methods.rime.rime_ime" |
| 41 | + saved = { |
| 42 | + k: sys.modules.pop(k) |
| 43 | + for k in list(sys.modules) |
| 44 | + if k in ("input_methods.rime.librime", mod_name) |
| 45 | + } |
| 46 | + sys.modules["input_methods.rime.librime"] = stub_librime |
| 47 | + try: |
| 48 | + with mock.patch.dict(os.environ, {"APPDATA": appdata_dir}): |
| 49 | + mod = importlib.import_module(mod_name) |
| 50 | + return mod, init_calls |
| 51 | + finally: |
| 52 | + sys.modules.pop("input_methods.rime.librime", None) |
| 53 | + sys.modules.pop(mod_name, None) |
| 54 | + sys.modules.update(saved) |
| 55 | + |
| 56 | + |
| 57 | +class RimeDeployTests(unittest.TestCase): |
| 58 | + def test_needs_rime_deploy_missing_dir(self): |
| 59 | + with tempfile.TemporaryDirectory() as tmp: |
| 60 | + mod, init_calls = _import_rime_ime_with_mock_librime(tmp) |
| 61 | + self.assertTrue(mod.needs_rime_deploy(os.path.join(tmp, "missing"))) |
| 62 | + self.assertEqual(len(init_calls), 1) |
| 63 | + self.assertTrue(init_calls[0]["fullcheck"]) |
| 64 | + |
| 65 | + def test_needs_rime_deploy_dir_without_bin(self): |
| 66 | + with tempfile.TemporaryDirectory() as tmp: |
| 67 | + rime_dir = os.path.join(tmp, "PIME", "Rime") |
| 68 | + os.makedirs(os.path.join(rime_dir, "build"), exist_ok=True) |
| 69 | + with open(os.path.join(rime_dir, "default.yaml"), "w", encoding="utf-8") as f: |
| 70 | + f.write("config_version: 1\n") |
| 71 | + |
| 72 | + mod, init_calls = _import_rime_ime_with_mock_librime(tmp) |
| 73 | + self.assertTrue(mod.needs_rime_deploy(rime_dir)) |
| 74 | + self.assertEqual(len(init_calls), 1) |
| 75 | + self.assertTrue(init_calls[0]["fullcheck"]) |
| 76 | + |
| 77 | + def test_needs_rime_deploy_dir_with_bin(self): |
| 78 | + with tempfile.TemporaryDirectory() as tmp: |
| 79 | + rime_dir = os.path.join(tmp, "PIME", "Rime") |
| 80 | + os.makedirs(rime_dir, exist_ok=True) |
| 81 | + with open(os.path.join(rime_dir, "luna_pinyin.prism.bin"), "wb") as f: |
| 82 | + f.write(b"prism") |
| 83 | + |
| 84 | + mod, init_calls = _import_rime_ime_with_mock_librime(tmp) |
| 85 | + self.assertFalse(mod.needs_rime_deploy(rime_dir)) |
| 86 | + self.assertEqual(len(init_calls), 1) |
| 87 | + self.assertFalse(init_calls[0]["fullcheck"]) |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + unittest.main() |
0 commit comments