|
| 1 | +import json |
| 2 | +from pathlib import Path |
| 3 | +import shutil |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +import tempfile |
| 7 | +import unittest |
| 8 | + |
| 9 | +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) |
| 10 | +import translations |
| 11 | + |
| 12 | + |
| 13 | +def write_config(root, catalogs): |
| 14 | + path = root / ".tx/config" |
| 15 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 16 | + text = "[main]\nhost = https://www.transifex.com\n" |
| 17 | + for index, catalog in enumerate(catalogs): |
| 18 | + template = Path(catalog).with_suffix(".pot").as_posix() |
| 19 | + text += f"\n[resource{index}]\nsource_file = pot/{template}\n" |
| 20 | + path.write_text(text, encoding="utf-8") |
| 21 | + |
| 22 | + |
| 23 | +def write_po(root, name, translation): |
| 24 | + path = root / name |
| 25 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 26 | + path.write_text( |
| 27 | + 'msgid ""\nmsgstr "Content-Type: text/plain; charset=UTF-8\\n"\n\n' |
| 28 | + 'msgid "example"\nmsgstr ' + json.dumps(translation, ensure_ascii=False) + "\n", |
| 29 | + encoding="utf-8", |
| 30 | + ) |
| 31 | + return path |
| 32 | + |
| 33 | + |
| 34 | +class TranslationTests(unittest.TestCase): |
| 35 | + def setUp(self): |
| 36 | + temporary = tempfile.TemporaryDirectory() |
| 37 | + self.addCleanup(temporary.cleanup) |
| 38 | + self.root = Path(temporary.name) |
| 39 | + self.source = self.root / "source" |
| 40 | + self.destination = self.root / "destination" |
| 41 | + self.source.mkdir() |
| 42 | + self.destination.mkdir() |
| 43 | + subprocess.run( |
| 44 | + ["git", "init", "--quiet", str(self.destination)], check=True |
| 45 | + ) |
| 46 | + |
| 47 | + def track(self): |
| 48 | + subprocess.run( |
| 49 | + ["git", "-C", str(self.destination), "add", "."], check=True |
| 50 | + ) |
| 51 | + |
| 52 | + def test_sync_removes_old_path_and_preserves_untracked_files(self): |
| 53 | + catalogs = ["builtins/functions.po", "library/builtins.po", "bugs.po"] |
| 54 | + write_config(self.source, catalogs) |
| 55 | + download = self.source / "ja/LC_MESSAGES" |
| 56 | + for path in catalogs: |
| 57 | + write_po(download, path, "新しい訳") |
| 58 | + # A stale downloaded file must not be copied back into the repository. |
| 59 | + write_po(download, "library/functions.po", "古い訳") |
| 60 | + old = write_po(self.destination, "library/functions.po", "古い訳") |
| 61 | + write_po(self.destination, "library/builtins.po", "モジュール") |
| 62 | + self.track() |
| 63 | + scratch = write_po(self.destination, "scratch.po", "作業中") |
| 64 | + scratch_before = scratch.read_bytes() |
| 65 | + |
| 66 | + translations.sync(self.source, self.destination, "ja") |
| 67 | + |
| 68 | + self.assertFalse(old.exists()) |
| 69 | + for path in catalogs: |
| 70 | + self.assertEqual( |
| 71 | + (download / path).read_bytes(), |
| 72 | + (self.destination / path).read_bytes(), |
| 73 | + ) |
| 74 | + self.assertEqual(scratch_before, scratch.read_bytes()) |
| 75 | + self.assertEqual( |
| 76 | + (self.source / ".tx/config").read_bytes(), |
| 77 | + (self.destination / ".tx/config").read_bytes(), |
| 78 | + ) |
| 79 | + |
| 80 | + def test_incomplete_download_does_not_change_repository(self): |
| 81 | + write_config(self.source, ["builtins/functions.po", "bugs.po"]) |
| 82 | + write_po(self.source / "ja/LC_MESSAGES", "builtins/functions.po", "新しい訳") |
| 83 | + write_config(self.destination, ["library/functions.po"]) |
| 84 | + old = write_po(self.destination, "library/functions.po", "古い訳") |
| 85 | + self.track() |
| 86 | + before = old.read_bytes() |
| 87 | + config_before = (self.destination / ".tx/config").read_bytes() |
| 88 | + |
| 89 | + with self.assertRaises(FileNotFoundError): |
| 90 | + translations.sync(self.source, self.destination, "ja") |
| 91 | + |
| 92 | + self.assertEqual(before, old.read_bytes()) |
| 93 | + self.assertEqual(config_before, (self.destination / ".tx/config").read_bytes()) |
| 94 | + self.assertFalse((self.destination / "builtins/functions.po").exists()) |
| 95 | + |
| 96 | + def test_empty_config_does_not_delete_catalogs(self): |
| 97 | + write_config(self.source, []) |
| 98 | + old = write_po(self.destination, "library/functions.po", "古い訳") |
| 99 | + self.track() |
| 100 | + with self.assertRaises(ValueError): |
| 101 | + translations.sync(self.source, self.destination, "ja") |
| 102 | + self.assertTrue(old.exists()) |
| 103 | + |
| 104 | + def test_config_cannot_escape_catalog_directory(self): |
| 105 | + write_config(self.source, ["../outside.po"]) |
| 106 | + with self.assertRaises(ValueError): |
| 107 | + translations.active_catalogs(self.source / ".tx/config") |
| 108 | + |
| 109 | + @unittest.skipUnless(shutil.which("pomerge"), "pomerge is required") |
| 110 | + def test_merge_uses_current_translation_across_renamed_paths(self): |
| 111 | + write_config(self.source, ["builtins/functions.po"]) |
| 112 | + write_po(self.source, "builtins/functions.po", "新しい訳") |
| 113 | + write_po(self.source, "library/functions.po", "古い訳") |
| 114 | + old = write_po(self.destination, "library/functions.po", "以前の訳") |
| 115 | + self.track() |
| 116 | + |
| 117 | + translations.merge(self.source, self.destination) |
| 118 | + |
| 119 | + text = old.read_text(encoding="utf-8") |
| 120 | + self.assertIn('msgstr "新しい訳"', text) |
| 121 | + self.assertNotIn("古い訳", text) |
| 122 | + self.assertFalse((self.destination / "builtins/functions.po").exists()) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + unittest.main() |
0 commit comments