Skip to content

Commit e153dcd

Browse files
committed
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
1 parent 7d7b968 commit e153dcd

File tree

3 files changed

+101
-7
lines changed

3 files changed

+101
-7
lines changed

commitizen/cz/conventional_commits/.cache_multilanguage.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,4 @@ footer_ko=Footer. Breaking Changes 및 이 커밋이 종료되는 참조 문제
148148
body_en=Provide additional contextual information about the code changes: (press [enter] to skip)
149149
is_breaking_change_en=Is this a BREAKING CHANGE? Correlates with MAJOR in SemVer
150150
footer_en=Footer. Information about Breaking Changes and reference issues that this commit closes: (press [enter] to skip)
151+
hello_fr=bonjour

commitizen/cz/conventional_commits/translation_multilanguage.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@
88
IS_TRANSLATING = True
99
MULTILANGUAGE = {}
1010

11-
# test
1211

13-
14-
def load_multilanguage():
12+
def load_multilanguage(file=FILENAME):
1513
global MULTILANGUAGE
1614
MULTILANGUAGE = {}
17-
with open(FILENAME) as file:
18-
for line in file:
15+
with open(file) as f:
16+
for line in f:
1917
if not line.strip():
2018
continue
21-
key, value = line.strip().split("=", 1)
22-
MULTILANGUAGE[key] = value
19+
try:
20+
key, value = line.strip().split("=", 1)
21+
MULTILANGUAGE[key] = value
22+
except ValueError:
23+
print(f"Skipping malformed line: {line.strip()}")
24+
return MULTILANGUAGE
2325

2426

2527
def generate_key(original_key, to_lang):

tests/test_cz_conventional_commits.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1+
from unittest.mock import mock_open, patch
2+
13
import pytest
24

35
from commitizen.cz.conventional_commits.conventional_commits import (
46
ConventionalCommitsCz,
57
parse_scope,
68
parse_subject,
79
)
10+
from commitizen.cz.conventional_commits.translation_multilanguage import (
11+
FILENAME,
12+
MULTILANGUAGE,
13+
generate_key,
14+
load_multilanguage,
15+
save_multilanguage,
16+
translate_text,
17+
translate_text_from_eng,
18+
)
819
from commitizen.cz.exceptions import AnswerRequiredError
920

1021
valid_scopes = ["", "simple", "dash-separated", "camelCase" "UPPERCASE"]
@@ -153,3 +164,83 @@ def test_process_commit(commit_message, expected_message, config):
153164
conventional_commits = ConventionalCommitsCz(config)
154165
message = conventional_commits.process_commit(commit_message)
155166
assert message == expected_message
167+
168+
169+
def test_load_multilanguage():
170+
mock_data = "hello_en=hello\nworld_fr=monde\n"
171+
172+
file = "builtins.open"
173+
with patch(file, mock_open(read_data=mock_data)):
174+
MULTILANGUAGE = load_multilanguage(file)
175+
176+
assert MULTILANGUAGE == {"hello_en": "hello", "world_fr": "monde"}
177+
178+
179+
def test_save_multilanguage():
180+
key = "hello_fr"
181+
value = "bonjour"
182+
with patch("builtins.open", mock_open()) as mocked_file:
183+
save_multilanguage(key, value)
184+
mocked_file.assert_called_once_with(FILENAME, "a")
185+
mocked_file().write.assert_called_once_with(f"{key}={value}\n")
186+
187+
188+
def test_generate_key():
189+
original_key = "hello"
190+
to_lang = "fr"
191+
expected_key = "hello_fr"
192+
assert generate_key(original_key, to_lang) == expected_key
193+
194+
195+
def test_translate_text_error():
196+
with patch("translate.Translator") as MockTranslator:
197+
mock_translator = MockTranslator.return_value
198+
mock_translator.translate.return_value = "IS AN INVALID TARGET LANGUAGE"
199+
200+
text = "hello"
201+
from_lang = "en"
202+
to_lang = "xx" # Langue invalid
203+
translated = translate_text(text, from_lang, to_lang)
204+
205+
assert translated == text
206+
207+
208+
def test_translate_text_from_eng_default_language():
209+
text = "hello"
210+
to_lang = "en"
211+
key = "hello"
212+
213+
translated = translate_text_from_eng(text, to_lang, key)
214+
215+
# La langue de destination est l'anglais, donc le texte doit être renvoyé tel quel
216+
assert translated == "hello"
217+
218+
219+
def test_translate_text_with_is_translating_false():
220+
global IS_TRANSLATING
221+
IS_TRANSLATING = False # Simuler que la traduction est désactivée
222+
223+
text = "hello"
224+
from_lang = "en"
225+
to_lang = "fr"
226+
227+
translated = translate_text(text, from_lang, to_lang)
228+
229+
# IS_TRANSLATING est False, donc le texte doit être retourné sans modification
230+
assert translated == text
231+
232+
233+
def test_load_multilanguage_empty_file():
234+
with patch("builtins.open", mock_open(read_data="")):
235+
load_multilanguage()
236+
assert MULTILANGUAGE == {}
237+
238+
239+
def test_load_multilanguage_malformed_file():
240+
malformed_data = "hello=bonjour\ninvalid-line\nworld=monde\n"
241+
file = "builtins.open"
242+
with patch(file, mock_open(read_data=malformed_data)):
243+
MULTILANGUAGE = load_multilanguage(file)
244+
245+
# Devrait ignorer les lignes malformées
246+
assert MULTILANGUAGE == {"hello": "bonjour", "world": "monde"}

0 commit comments

Comments
 (0)