Skip to content

Commit

Permalink
test: don't use "i18n_tool.py"
Browse files Browse the repository at this point in the history
Instead, generate test fixtures by calling "pybabel" directly.
  • Loading branch information
cfm committed Sep 29, 2023
1 parent 1bfed59 commit 99d4eca
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 31 deletions.
64 changes: 43 additions & 21 deletions securedrop/tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"),
Expand All @@ -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"
Expand All @@ -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.
Expand Down
20 changes: 10 additions & 10 deletions securedrop/tests/test_template_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
)
Expand Down

0 comments on commit 99d4eca

Please sign in to comment.