Skip to content

Commit

Permalink
feat(file): adding tests for the multilanguage version
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Vlor999 committed Jan 2, 2025
1 parent 7d7b968 commit e153dcd
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 9 additions & 7 deletions commitizen/cz/conventional_commits/translation_multilanguage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
91 changes: 91 additions & 0 deletions tests/test_cz_conventional_commits.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
from unittest.mock import mock_open, patch

import pytest

from commitizen.cz.conventional_commits.conventional_commits import (
ConventionalCommitsCz,
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"]
Expand Down Expand Up @@ -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"}

0 comments on commit e153dcd

Please sign in to comment.