From 574540e3b204be4d5d56459179bf3c8669dcce40 Mon Sep 17 00:00:00 2001 From: Mips2648 Date: Wed, 5 Feb 2025 19:28:04 +0100 Subject: [PATCH] fix for invalid json file --- ...te_plugin.py => test_plugin_translator.py} | 24 +++++++++++++++---- plugintranslations/translator.py | 12 ++++++---- 2 files changed, 27 insertions(+), 9 deletions(-) rename plugintranslations/tests/{test_translate_plugin.py => test_plugin_translator.py} (70%) diff --git a/plugintranslations/tests/test_translate_plugin.py b/plugintranslations/tests/test_plugin_translator.py similarity index 70% rename from plugintranslations/tests/test_translate_plugin.py rename to plugintranslations/tests/test_plugin_translator.py index 4e8a518..012a899 100644 --- a/plugintranslations/tests/test_translate_plugin.py +++ b/plugintranslations/tests/test_plugin_translator.py @@ -16,10 +16,11 @@ 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: @@ -27,7 +28,7 @@ def current_working_dir(self, tmp_path_factory) -> Path: 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' @@ -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() @@ -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 diff --git a/plugintranslations/translator.py b/plugintranslations/translator.py index 9f0dca9..3bfb6f0 100644 --- a/plugintranslations/translator.py +++ b/plugintranslations/translator.py @@ -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...")