diff --git a/commitizen/cz/conventional_commits/.cache_multilanguage.txt b/commitizen/cz/conventional_commits/.cache_multilanguage.txt index cd95f6ca5..68e80fb65 100644 --- a/commitizen/cz/conventional_commits/.cache_multilanguage.txt +++ b/commitizen/cz/conventional_commits/.cache_multilanguage.txt @@ -148,3 +148,4 @@ footer_ko=Footer. Breaking Changes 및 이 커밋이 종료되는 참조 문제 body_en=Provide additional contextual information about the code changes: (press [enter] to skip) is_breaking_change_en=Is this a BREAKING CHANGE? Correlates with MAJOR in SemVer footer_en=Footer. Information about Breaking Changes and reference issues that this commit closes: (press [enter] to skip) +hello_fr=bonjour diff --git a/commitizen/cz/conventional_commits/translation_multilanguage.py b/commitizen/cz/conventional_commits/translation_multilanguage.py index 562f6e5c1..b8e5b231c 100644 --- a/commitizen/cz/conventional_commits/translation_multilanguage.py +++ b/commitizen/cz/conventional_commits/translation_multilanguage.py @@ -8,18 +8,20 @@ IS_TRANSLATING = True MULTILANGUAGE = {} -# test - -def load_multilanguage(): +def load_multilanguage(file=FILENAME): global MULTILANGUAGE MULTILANGUAGE = {} - with open(FILENAME) as file: - for line in file: + with open(file) as f: + for line in f: if not line.strip(): continue - key, value = line.strip().split("=", 1) - MULTILANGUAGE[key] = value + try: + key, value = line.strip().split("=", 1) + MULTILANGUAGE[key] = value + except ValueError: + print(f"Skipping malformed line: {line.strip()}") + return MULTILANGUAGE def generate_key(original_key, to_lang): diff --git a/tests/test_cz_conventional_commits.py b/tests/test_cz_conventional_commits.py index 04d052217..9b52e021e 100644 --- a/tests/test_cz_conventional_commits.py +++ b/tests/test_cz_conventional_commits.py @@ -1,3 +1,5 @@ +from unittest.mock import mock_open, patch + import pytest from commitizen.cz.conventional_commits.conventional_commits import ( @@ -5,6 +7,15 @@ parse_scope, parse_subject, ) +from commitizen.cz.conventional_commits.translation_multilanguage import ( + FILENAME, + MULTILANGUAGE, + generate_key, + load_multilanguage, + save_multilanguage, + translate_text, + translate_text_from_eng, +) from commitizen.cz.exceptions import AnswerRequiredError valid_scopes = ["", "simple", "dash-separated", "camelCase" "UPPERCASE"] @@ -153,3 +164,83 @@ def test_process_commit(commit_message, expected_message, config): conventional_commits = ConventionalCommitsCz(config) message = conventional_commits.process_commit(commit_message) assert message == expected_message + + +def test_load_multilanguage(): + mock_data = "hello_en=hello\nworld_fr=monde\n" + + file = "builtins.open" + with patch(file, mock_open(read_data=mock_data)): + MULTILANGUAGE = load_multilanguage(file) + + assert MULTILANGUAGE == {"hello_en": "hello", "world_fr": "monde"} + + +def test_save_multilanguage(): + key = "hello_fr" + value = "bonjour" + with patch("builtins.open", mock_open()) as mocked_file: + save_multilanguage(key, value) + mocked_file.assert_called_once_with(FILENAME, "a") + mocked_file().write.assert_called_once_with(f"{key}={value}\n") + + +def test_generate_key(): + original_key = "hello" + to_lang = "fr" + expected_key = "hello_fr" + assert generate_key(original_key, to_lang) == expected_key + + +def test_translate_text_error(): + with patch("translate.Translator") as MockTranslator: + mock_translator = MockTranslator.return_value + mock_translator.translate.return_value = "IS AN INVALID TARGET LANGUAGE" + + text = "hello" + from_lang = "en" + to_lang = "xx" # Langue invalid + translated = translate_text(text, from_lang, to_lang) + + assert translated == text + + +def test_translate_text_from_eng_default_language(): + text = "hello" + to_lang = "en" + key = "hello" + + translated = translate_text_from_eng(text, to_lang, key) + + # La langue de destination est l'anglais, donc le texte doit être renvoyé tel quel + assert translated == "hello" + + +def test_translate_text_with_is_translating_false(): + global IS_TRANSLATING + IS_TRANSLATING = False # Simuler que la traduction est désactivée + + text = "hello" + from_lang = "en" + to_lang = "fr" + + translated = translate_text(text, from_lang, to_lang) + + # IS_TRANSLATING est False, donc le texte doit être retourné sans modification + assert translated == text + + +def test_load_multilanguage_empty_file(): + with patch("builtins.open", mock_open(read_data="")): + load_multilanguage() + assert MULTILANGUAGE == {} + + +def test_load_multilanguage_malformed_file(): + malformed_data = "hello=bonjour\ninvalid-line\nworld=monde\n" + file = "builtins.open" + with patch(file, mock_open(read_data=malformed_data)): + MULTILANGUAGE = load_multilanguage(file) + + # Devrait ignorer les lignes malformées + assert MULTILANGUAGE == {"hello": "bonjour", "world": "monde"}