From e153dcdcbd2d9bd369ed346308c753ff472ee8f0 Mon Sep 17 00:00:00 2001 From: Willem Adnet Date: Thu, 2 Jan 2025 13:10:27 +0100 Subject: [PATCH] feat(file): adding tests for the multilanguage version test to be sure that all the case are looked. The language option give the oppotunity to choose on wich language you want to display infomation during the commit --- .../.cache_multilanguage.txt | 1 + .../translation_multilanguage.py | 16 ++-- tests/test_cz_conventional_commits.py | 91 +++++++++++++++++++ 3 files changed, 101 insertions(+), 7 deletions(-) diff --git a/commitizen/cz/conventional_commits/.cache_multilanguage.txt b/commitizen/cz/conventional_commits/.cache_multilanguage.txt index cd95f6ca52..68e80fb65c 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 562f6e5c1a..b8e5b231ce 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 04d0522174..9b52e021e1 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"}