|
6 | 6 | import gettext |
7 | 7 | import importlib |
8 | 8 | import shutil |
| 9 | +import struct |
9 | 10 | import subprocess |
10 | 11 | from pathlib import Path |
11 | 12 |
|
|
20 | 21 | NOW = dt.datetime.now(tz=dt.UTC) |
21 | 22 |
|
22 | 23 |
|
| 24 | +def test_activate_uses_catalog_from_requested_path(tmp_path: Path) -> None: |
| 25 | + def write_catalog(root: Path, translated: str) -> None: |
| 26 | + directory = root / "zz" / "LC_MESSAGES" |
| 27 | + directory.mkdir(parents=True) |
| 28 | + original = b"one" |
| 29 | + translation = translated.encode("utf-8") |
| 30 | + data = struct.pack("<7I", 0x950412DE, 0, 1, 28, 36, 0, 0) |
| 31 | + data += struct.pack("<2I", len(original), 44) |
| 32 | + data += struct.pack("<2I", len(translation), 48) |
| 33 | + data += original + b"\0" + translation + b"\0" |
| 34 | + (directory / "humanize.mo").write_bytes(data) |
| 35 | + |
| 36 | + first = tmp_path / "first" |
| 37 | + second = tmp_path / "second" |
| 38 | + write_catalog(first, "first") |
| 39 | + write_catalog(second, "second") |
| 40 | + try: |
| 41 | + first_translation = humanize.activate("zz", first) |
| 42 | + assert humanize.apnumber(1) == "first" |
| 43 | + second_translation = humanize.activate("zz", second) |
| 44 | + assert humanize.apnumber(1) == "second" |
| 45 | + assert first_translation is not second_translation |
| 46 | + humanize.activate("zz", first) |
| 47 | + assert humanize.apnumber(1) == "first" |
| 48 | + finally: |
| 49 | + humanize.deactivate() |
| 50 | + |
| 51 | + |
23 | 52 | @pytest.mark.parametrize("locale, one", [("de_DE", "eins"), ("fr_FR", "un")]) |
24 | 53 | def test_update_translations( |
25 | 54 | tmp_path: Path, monkeypatch: pytest.MonkeyPatch, locale: str, one: str |
|
0 commit comments