From 99d4ecab5c0a129560fd50f35d8081fdeacacb50 Mon Sep 17 00:00:00 2001 From: Cory Francis Myers Date: Thu, 28 Sep 2023 12:10:58 -0700 Subject: [PATCH] test: don't use "i18n_tool.py" Instead, generate test fixtures by calling "pybabel" directly. --- securedrop/tests/test_i18n.py | 64 +++++++++++++++-------- securedrop/tests/test_template_filters.py | 20 +++---- 2 files changed, 53 insertions(+), 31 deletions(-) diff --git a/securedrop/tests/test_i18n.py b/securedrop/tests/test_i18n.py index 0dca558df0..1d457527b5 100644 --- a/securedrop/tests/test_i18n.py +++ b/securedrop/tests/test_i18n.py @@ -21,7 +21,6 @@ from typing import List import i18n -import i18n_tool import journalist_app as journalist_app_module import pytest import source_app @@ -247,22 +246,31 @@ def test_i18n(): i18n_dir = Path(__file__).absolute().parent / "i18n" sources = [str(i18n_dir / "code.py"), str(i18n_dir / "template.html")] - i18n_tool.I18NTool().main( + pot = i18n_dir / "messages.pot" + subprocess.check_call( [ - "--verbose", - "translate-messages", + "pybabel", + "extract", "--mapping", str(i18n_dir / "babel.cfg"), - "--translations-dir", - str(translation_dirs), - "--sources", - ",".join(sources), - "--extract-update", + "--output", + pot, + *sources, ] ) - pot = translation_dirs / "messages.pot" - subprocess.check_call(["pybabel", "init", "-i", pot, "-d", translation_dirs, "-l", "en_US"]) + subprocess.check_call( + [ + "pybabel", + "init", + "--input-file", + pot, + "--output-dir", + translation_dirs, + "--locale", + "en_US", + ] + ) for (locale, translated_msg) in ( ("fr_FR", "code bonjour"), @@ -271,7 +279,18 @@ def test_i18n(): ("nb_NO", "code norwegian"), ("es_ES", "code spanish"), ): - subprocess.check_call(["pybabel", "init", "-i", pot, "-d", translation_dirs, "-l", locale]) + subprocess.check_call( + [ + "pybabel", + "init", + "--input-file", + pot, + "--output-dir", + translation_dirs, + "--locale", + locale, + ] + ) # Populate the po file with a translation po_file = translation_dirs / locale / "LC_MESSAGES" / "messages.po" @@ -281,15 +300,18 @@ def test_i18n(): msgstr=translated_msg, ) - i18n_tool.I18NTool().main( - [ - "--verbose", - "translate-messages", - "--translations-dir", - str(translation_dirs), - "--compile", - ] - ) + subprocess.check_call( + [ + "pybabel", + "compile", + "--directory", + translation_dirs, + "--locale", + locale, + "--input-file", + po_file, + ] + ) # Use our config (and not an app fixture) because the i18n module # grabs values at init time and we can't inject them later. diff --git a/securedrop/tests/test_template_filters.py b/securedrop/tests/test_template_filters.py index e4d5b094a9..797ebd4220 100644 --- a/securedrop/tests/test_template_filters.py +++ b/securedrop/tests/test_template_filters.py @@ -2,7 +2,6 @@ from datetime import datetime, timedelta from pathlib import Path -import i18n_tool import journalist_app import source_app import template_filters @@ -87,23 +86,24 @@ def do_test(create_app): test_config = create_config_for_i18n_test(supported_locales=["en_US", "fr_FR"]) i18n_dir = Path(__file__).absolute().parent / "i18n" - i18n_tool.I18NTool().main( + pot = Path(test_config.TEMP_DIR) / "messages.pot" + subprocess.check_call( [ - "--verbose", - "translate-messages", + "pybabel", + "extract", "--mapping", str(i18n_dir / "babel.cfg"), - "--translations-dir", - str(test_config.TEMP_DIR), - "--sources", + "--output", + pot, str(i18n_dir / "code.py"), - "--extract-update", - "--compile", ] ) + # To be able to test template filters for a given language, its message + # catalog must exist, but it doesn't have to contain any actual + # translations. So we can just initialize it based on the template created + # by "pybabel extract". for lang in ("en_US", "fr_FR"): - pot = Path(test_config.TEMP_DIR) / "messages.pot" subprocess.check_call( ["pybabel", "init", "-i", pot, "-d", test_config.TEMP_DIR, "-l", lang] )