"""Pytest configuration and fixtures."""
import json
import pytest
from i18n_key_sync.locale import LocaleParser
@pytest.fixture
def sample_python_code():
"""Sample Python code with i18n keys."""
return '''
from gettext import gettext as _
def greet(name):
return _("hello") + " " + name
def farewell():
return _("goodbye")
messages = {
"welcome": _("welcome_message"),
"login": t("login_button"),
}
# Inline usage
print(_("save"))
print(_("cancel"))
'''
@pytest.fixture
def sample_js_code():
"""Sample JavaScript code with i18n keys."""
return '''
import { t } from './i18n';
const message = _("hello_world");
const greeting = t("greeting");
function showWelcome() {
return i18n.t("welcome_message");
}
// Template usage
const text = `Hello ${name}, ${_("welcome_message")}`;
// JSX usage
return
{_("submit")}
;
'''
@pytest.fixture
def sample_locale_json():
"""Sample JSON locale file."""
return {
"hello": "Hello",
"goodbye": "Goodbye",
"welcome_message": "Welcome!",
"save": "Save",
"cancel": "Cancel",
"unused_key": "This key is not used",
}
@pytest.fixture
def sample_locale_yaml():
"""Sample YAML locale file."""
return {
"hello": "Hello",
"goodbye": "Goodbye",
"welcome_message": "Welcome!",
"save": "Save",
"cancel": "Cancel",
"unused_key": "This key is not used",
"nested": {
"key": "Nested value",
"deep": {
"key": "Deep nested value",
},
},
}
@pytest.fixture
def temp_locale_dir(tmp_path, sample_locale_json):
"""Create a temporary locale directory with sample files."""
locale_dir = tmp_path / "locales"
locale_dir.mkdir()
en_file = locale_dir / "en.json"
en_file.write_text(json.dumps(sample_locale_json, indent=2))
es_file = locale_dir / "es.json"
es_data = sample_locale_json.copy()
es_data["hello"] = "Hola"
es_file.write_text(json.dumps(es_data, indent=2))
return locale_dir
@pytest.fixture
def temp_locale_dir_yaml(tmp_path, sample_locale_yaml):
"""Create a temporary locale directory with YAML files."""
import yaml
locale_dir = tmp_path / "locales_yaml"
locale_dir.mkdir()
en_file = locale_dir / "en.yaml"
en_file.write_text(yaml.dump(sample_locale_yaml, default_flow_style=False))
return locale_dir
@pytest.fixture
def locale_parser():
"""Create a LocaleParser instance."""
return LocaleParser()
@pytest.fixture
def temp_project_dir(tmp_path, sample_python_code, sample_js_code, temp_locale_dir):
"""Create a temporary project directory with code and locales."""
src_dir = tmp_path / "src"
src_dir.mkdir()
python_file = src_dir / "main.py"
python_file.write_text(sample_python_code)
js_file = src_dir / "app.js"
js_file.write_text(sample_js_code)
return tmp_path