Add test files
Some checks failed
CI / test (push) Has been cancelled

This commit is contained in:
2026-02-02 03:58:36 +00:00
parent 51f56399a4
commit af0b17c33e

149
tests/test_extract.py Normal file
View File

@@ -0,0 +1,149 @@
"""Tests for key extraction module."""
import tempfile
from pathlib import Path
from i18n_key_sync.extract import ExtractKeys, extract_keys
class TestExtractKeys:
"""Tests for ExtractKeys class."""
def test_extract_simple_keys(self, sample_python_code):
"""Test extracting simple i18n keys from Python code."""
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
f.write(sample_python_code)
f.flush()
extractor = ExtractKeys(["_", "t"])
keys = extractor.extract_from_file(Path(f.name))
assert "hello" in keys
assert "goodbye" in keys
assert "welcome_message" in keys
assert "save" in keys
assert "cancel" in keys
Path(f.name).unlink()
def test_extract_js_keys(self, sample_js_code):
"""Test extracting keys from JavaScript code."""
with tempfile.NamedTemporaryFile(mode="w", suffix=".js", delete=False) as f:
f.write(sample_js_code)
f.flush()
extractor = ExtractKeys(["_", "t", "i18n.t"])
keys = extractor.extract_from_file(Path(f.name))
assert "hello_world" in keys
assert "greeting" in keys
assert "welcome_message" in keys
assert "submit" in keys
Path(f.name).unlink()
def test_extract_multiple_patterns(self):
"""Test extracting keys using multiple patterns."""
code = '''
_("key1")
t("key2")
i18n.t("key3")
gettext("key4")
'''
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
f.write(code)
f.flush()
extractor = ExtractKeys(["_", "t", "i18n.t", "gettext"])
keys = extractor.extract_from_file(Path(f.name))
assert keys == {"key1", "key2", "key3", "key4"}
Path(f.name).unlink()
def test_extract_template_literals(self):
"""Test extracting keys from template literals."""
code = '''
const greeting = `Hello ${_("user_name")}, ${_("welcome_message")}`;
'''
with tempfile.NamedTemporaryFile(mode="w", suffix=".js", delete=False) as f:
f.write(code)
f.flush()
extractor = ExtractKeys(["_"])
keys = extractor.extract_from_file(Path(f.name))
assert "user_name" in keys
assert "welcome_message" in keys
Path(f.name).unlink()
def test_extract_no_keys(self):
"""Test extracting from code with no i18n calls."""
code = '''
def hello(name):
return "Hello " + name
'''
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
f.write(code)
f.flush()
extractor = ExtractKeys(["_", "t"])
keys = extractor.extract_from_file(Path(f.name))
assert keys == set()
Path(f.name).unlink()
def test_extract_from_files(self):
"""Test extracting keys from multiple files."""
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f1:
f1.write('_("key1")\n_("key2")')
f1_path = Path(f1.name)
with tempfile.NamedTemporaryFile(mode="w", suffix=".js", delete=False) as f2:
f2.write('t("key3")\nt("key4")')
f2_path = Path(f2.name)
try:
extractor = ExtractKeys(["_", "t"])
keys = extractor.extract_from_files([f1_path, f2_path])
assert keys == {"key1", "key2", "key3", "key4"}
finally:
f1_path.unlink()
f2_path.unlink()
class TestExtractKeysFunction:
"""Tests for the extract_keys function."""
def test_extract_keys_from_directory(self, temp_project_dir):
"""Test extracting keys from a directory."""
paths = (str(temp_project_dir / "src"),)
patterns = ["_", "t", "i18n.t"]
file_types = ["py", "js"]
keys = extract_keys(paths, patterns, file_types)
assert len(keys) > 0
assert "hello" in keys or "hello_world" in keys
def test_extract_keys_with_file_types_filter(self, temp_project_dir):
"""Test extracting keys with file type filtering."""
paths = (str(temp_project_dir / "src"),)
patterns = ["_", "t"]
file_types = ["py"]
keys = extract_keys(paths, patterns, file_types)
assert "goodbye" in keys
def test_extract_keys_nonexistent_path(self):
"""Test extracting from nonexistent path returns empty set."""
paths = ("/nonexistent/path",)
patterns = ["_", "t"]
file_types = ["py"]
keys = extract_keys(paths, patterns, file_types)
assert keys == set()