74 lines
2.5 KiB
Python
74 lines
2.5 KiB
Python
import pytest
|
|
from pathlib import Path
|
|
import re
|
|
|
|
# Base directory for language files
|
|
LANG_DIR = Path(__file__).resolve().parent.parent / "app" / "lang"
|
|
|
|
def get_structure(file_path: Path):
|
|
"""
|
|
Returns a list of (line_number, key/header) for a TOML file.
|
|
Only captures keys and section headers, ignoring the values.
|
|
"""
|
|
structure = []
|
|
# Regex to capture "key" = or [header]
|
|
key_pattern = re.compile(r'^\s*"?([^"\s=]+)"?\s*=')
|
|
header_pattern = re.compile(r'^\s*\[([^\]]+)\]')
|
|
|
|
with open(file_path, "r", encoding="utf-8") as f:
|
|
for i, line in enumerate(f, 1):
|
|
line = line.strip()
|
|
if not line:
|
|
structure.append((i, "<empty>"))
|
|
continue
|
|
|
|
# Check for header [section]
|
|
header_match = header_pattern.match(line)
|
|
if header_match:
|
|
structure.append((i, f"[{header_match.group(1)}]"))
|
|
continue
|
|
|
|
# Check for key "name" =
|
|
key_match = key_pattern.match(line)
|
|
if key_match:
|
|
structure.append((i, key_match.group(1)))
|
|
continue
|
|
|
|
# Comments or anything else
|
|
structure.append((i, "<other/comment>"))
|
|
|
|
return structure
|
|
|
|
def test_i18n_files_exist():
|
|
assert LANG_DIR.exists(), f"Language directory {LANG_DIR} not found"
|
|
en_file = LANG_DIR / "en.toml"
|
|
assert en_file.exists(), "English language file (en.toml) must exist as baseline"
|
|
|
|
def test_i18n_synchronization():
|
|
"""
|
|
Ensures all language files have the same keys/headers on the same lines
|
|
as the baseline en.toml.
|
|
"""
|
|
en_path = LANG_DIR / "en.toml"
|
|
en_structure = get_structure(en_path)
|
|
|
|
other_files = list(LANG_DIR.glob("*.toml"))
|
|
other_files.remove(en_path)
|
|
|
|
for lang_file in other_files:
|
|
lang_name = lang_file.name
|
|
lang_structure = get_structure(lang_file)
|
|
|
|
# Check line count
|
|
assert len(lang_structure) == len(en_structure), \
|
|
f"{lang_name} length mismatch: expected {len(en_structure)} lines, got {len(lang_structure)}"
|
|
|
|
# Check line-by-line sync
|
|
for (en_line, en_key), (lang_line, lang_key) in zip(en_structure, lang_structure):
|
|
assert en_key == lang_key, \
|
|
f"Sync error at {lang_name}:{lang_line}. Expected '{en_key}', found '{lang_key}'"
|
|
|
|
if __name__ == "__main__":
|
|
# Allow running directly as a script
|
|
pytest.main([__file__])
|