mirror of
https://github.com/Second-Hand-Friends/kleinanzeigen-bot.git
synced 2026-03-12 10:31:50 +01:00
test: don't require translations for debug messages
This commit is contained in:
@@ -62,6 +62,7 @@ def _get_function_name(node: ast.AST) -> str:
|
|||||||
Returns:
|
Returns:
|
||||||
The function name or "module" for module-level code
|
The function name or "module" for module-level code
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def find_parent_context(n: ast.AST) -> tuple[str | None, str | None]:
|
def find_parent_context(n: ast.AST) -> tuple[str | None, str | None]:
|
||||||
"""Find the containing class and function names."""
|
"""Find the containing class and function names."""
|
||||||
class_name = None
|
class_name = None
|
||||||
@@ -83,7 +84,7 @@ def _get_function_name(node: ast.AST) -> str:
|
|||||||
return "module" # For module-level code
|
return "module" # For module-level code
|
||||||
|
|
||||||
|
|
||||||
def _extract_log_messages(file_path: str) -> MessageDict:
|
def _extract_log_messages(file_path: str, exclude_debug:bool = False) -> MessageDict:
|
||||||
"""
|
"""
|
||||||
Extract all translatable messages from a Python file with their function context.
|
Extract all translatable messages from a Python file with their function context.
|
||||||
|
|
||||||
@@ -93,8 +94,8 @@ def _extract_log_messages(file_path: str) -> MessageDict:
|
|||||||
Returns:
|
Returns:
|
||||||
Dictionary mapping function names to their messages
|
Dictionary mapping function names to their messages
|
||||||
"""
|
"""
|
||||||
with open(file_path, 'r', encoding='utf-8') as file:
|
with open(file_path, 'r', encoding = 'utf-8') as file:
|
||||||
tree = ast.parse(file.read(), filename=file_path)
|
tree = ast.parse(file.read(), filename = file_path)
|
||||||
|
|
||||||
# Add parent references for context tracking
|
# Add parent references for context tracking
|
||||||
for parent in ast.walk(tree):
|
for parent in ast.walk(tree):
|
||||||
@@ -127,7 +128,7 @@ def _extract_log_messages(file_path: str) -> MessageDict:
|
|||||||
if (isinstance(node.func, ast.Attribute) and
|
if (isinstance(node.func, ast.Attribute) and
|
||||||
isinstance(node.func.value, ast.Name) and
|
isinstance(node.func.value, ast.Name) and
|
||||||
node.func.value.id in {'LOG', 'logger', 'logging'} and
|
node.func.value.id in {'LOG', 'logger', 'logging'} and
|
||||||
node.func.attr in {'debug', 'info', 'warning', 'error', 'critical'}):
|
node.func.attr in {None if exclude_debug else 'debug', 'info', 'warning', 'error', 'critical'}):
|
||||||
if node.args:
|
if node.args:
|
||||||
msg = extract_string_value(node.args[0])
|
msg = extract_string_value(node.args[0])
|
||||||
if msg:
|
if msg:
|
||||||
@@ -154,7 +155,7 @@ def _extract_log_messages(file_path: str) -> MessageDict:
|
|||||||
return messages
|
return messages
|
||||||
|
|
||||||
|
|
||||||
def _get_all_log_messages() -> dict[str, MessageDict]:
|
def _get_all_log_messages(exclude_debug:bool = False) -> dict[str, MessageDict]:
|
||||||
"""
|
"""
|
||||||
Get all translatable messages from all Python files in the project.
|
Get all translatable messages from all Python files in the project.
|
||||||
|
|
||||||
@@ -191,7 +192,7 @@ def _get_all_log_messages() -> dict[str, MessageDict]:
|
|||||||
relative_path = os.path.relpath(file_path, src_dir)
|
relative_path = os.path.relpath(file_path, src_dir)
|
||||||
if relative_path.startswith('resources/'):
|
if relative_path.startswith('resources/'):
|
||||||
continue
|
continue
|
||||||
messages = _extract_log_messages(file_path)
|
messages = _extract_log_messages(file_path, exclude_debug)
|
||||||
if messages:
|
if messages:
|
||||||
module_path = os.path.join('kleinanzeigen_bot', relative_path)
|
module_path = os.path.join('kleinanzeigen_bot', relative_path)
|
||||||
module_path = module_path.replace(os.sep, '/')
|
module_path = module_path.replace(os.sep, '/')
|
||||||
@@ -226,7 +227,7 @@ def _get_translations_for_language(lang: str) -> TranslationDict:
|
|||||||
Returns:
|
Returns:
|
||||||
Dictionary containing all translations for the language
|
Dictionary containing all translations for the language
|
||||||
"""
|
"""
|
||||||
yaml = YAML(typ='safe')
|
yaml = YAML(typ = 'safe')
|
||||||
translation_file = f"translations.{lang}.yaml"
|
translation_file = f"translations.{lang}.yaml"
|
||||||
print(f"Loading translations from {translation_file}")
|
print(f"Loading translations from {translation_file}")
|
||||||
content = files(resources).joinpath(translation_file).read_text()
|
content = files(resources).joinpath(translation_file).read_text()
|
||||||
@@ -323,7 +324,7 @@ def test_all_log_messages_have_translations(lang: str) -> None:
|
|||||||
|
|
||||||
This test ensures that no untranslated messages exist in the codebase.
|
This test ensures that no untranslated messages exist in the codebase.
|
||||||
"""
|
"""
|
||||||
messages_by_file = _get_all_log_messages()
|
messages_by_file = _get_all_log_messages(exclude_debug = True)
|
||||||
translations = _get_translations_for_language(lang)
|
translations = _get_translations_for_language(lang)
|
||||||
|
|
||||||
missing_translations = []
|
missing_translations = []
|
||||||
@@ -373,7 +374,7 @@ def test_no_obsolete_translations(lang: str) -> None:
|
|||||||
function:
|
function:
|
||||||
"original message": "translated message"
|
"original message": "translated message"
|
||||||
"""
|
"""
|
||||||
messages_by_file = _get_all_log_messages()
|
messages_by_file = _get_all_log_messages(exclude_debug = False)
|
||||||
translations = _get_translations_for_language(lang)
|
translations = _get_translations_for_language(lang)
|
||||||
obsolete_items: list[tuple[str, str, str]] = []
|
obsolete_items: list[tuple[str, str, str]] = []
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user