Skip to content

Commit

Permalink
fix for invalid json file
Browse files Browse the repository at this point in the history
  • Loading branch information
Mips2648 committed Feb 5, 2025
1 parent acdfd8d commit 574540e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@
INPUT_USE_CORE_TRANSLATIONS,
INPUT_GENERATE_SOURCE_LANGUAGE_TRANSLATIONS,
INPUT_DEBUG,
TRANSLATIONS_FILES_PATH,
)


class TestTranslatePlugin():
class TestPluginTranslator():
# Arrange
@pytest.fixture(scope="session", autouse=True)
def current_working_dir(self, tmp_path_factory) -> Path:
_cwd = tmp_path_factory.mktemp("plugin_root")
return _cwd

@pytest.fixture(autouse=True)
def os_config(self):
def setup_os_env(self):
os.environ[INPUT_SOURCE_LANGUAGE] = FR_FR
os.environ[INPUT_TARGET_LANGUAGES] = f'{EN_US},{ES_ES},{DE_DE}'
os.environ[INPUT_INCLUDE_EMPTY_TRANSLATION] = 'False'
Expand All @@ -36,7 +37,8 @@ def os_config(self):
os.environ[INPUT_DEBUG] = 'True'

@pytest.fixture(autouse=True)
def info_json(self, current_working_dir: Path):
def setup_info_json(self, current_working_dir: Path):
self.__plugin_root = current_working_dir/PLUGIN_ROOT
plugin_info_root = current_working_dir/PLUGIN_ROOT/"plugin_info"
plugin_info_root.mkdir(parents=True, exist_ok=True)
assert plugin_info_root.exists()
Expand All @@ -48,7 +50,21 @@ def info_json(self, current_working_dir: Path):
info_json_file = plugin_info_root/'info.json'
info_json_file.write_text(json.dumps(info_json_content, ensure_ascii=False, indent='\t'), encoding="UTF-8")

def test_translate_plugin(self, current_working_dir):
def test_init(self, current_working_dir):
self._test_translate = PluginTranslator(current_working_dir)
assert self._test_translate is not None
assert isinstance(self._test_translate, PluginTranslator)

def test_get_plugin_translations(self, current_working_dir):
# Arrange
self._test_translate = PluginTranslator(current_working_dir)

translation_path = self.__plugin_root/TRANSLATIONS_FILES_PATH
translation_path.mkdir(parents=True, exist_ok=True)
translation_file = translation_path/f"{EN_US}.json"
translation_file.touch()

# Act
self._test_translate.get_plugin_translations()
# Assert
assert 1
12 changes: 7 additions & 5 deletions plugintranslations/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,13 @@ def _get_translations_from_json_files(self, dir: Path):
if not file.exists():
self.__logger.info(f"file {file.as_posix()} not found !?")
continue

data = json.loads(file.read_text(encoding="UTF-8"))
for path in data:
for text in data[path]:
self.__existing_translations.add_translation(language, text, data[path][text])
try:
data = json.loads(file.read_text(encoding="UTF-8"))
for path in data:
for text in data[path]:
self.__existing_translations.add_translation(language, text, data[path][text])
except json.JSONDecodeError as e:
self.__logger.error(f"Error while reading {file.as_posix()}: {e}")

def write_plugin_translations(self):
self.__logger.info("Write translations files...")
Expand Down

0 comments on commit 574540e

Please sign in to comment.