| 1 |
# |
| 2 |
# Copyright (C) 2007-2011 Edgewall Software, 2013-2025 the Babel team |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# This software is licensed as described in the file LICENSE, which |
| 6 |
# you should have received as part of this distribution. The terms |
| 7 |
# are also available at https://github.com/python-babel/babel/blob/master/LICENSE. |
| 8 |
# |
| 9 |
# This software consists of voluntary contributions made by many |
| 10 |
# individuals. For the exact contribution history, see the revision |
| 11 |
# history and logs, available at https://github.com/python-babel/babel/commits/master/. |
| 12 |
|
| 13 |
from __future__ import annotations |
| 14 |
|
| 15 |
import logging |
| 16 |
import os |
| 17 |
import shutil |
| 18 |
import sys |
| 19 |
import time |
| 20 |
from datetime import datetime, timedelta |
| 21 |
from io import StringIO |
| 22 |
|
| 23 |
import pytest |
| 24 |
from freezegun import freeze_time |
| 25 |
|
| 26 |
from babel import __version__ as VERSION |
| 27 |
from babel.dates import format_datetime |
| 28 |
from babel.messages import Catalog, frontend |
| 29 |
from babel.messages.frontend import BaseError |
| 30 |
from babel.messages.pofile import read_po, write_po |
| 31 |
from babel.util import LOCALTZ |
| 32 |
from tests.messages.consts import data_dir, get_po_file_path, i18n_dir |
| 33 |
|
| 34 |
|
| 35 |
@pytest.fixture |
| 36 |
def cli(monkeypatch, capsys): |
| 37 |
monkeypatch.chdir(data_dir) |
| 38 |
monkeypatch.setattr(sys, 'argv', ['pybabel']) |
| 39 |
_remove_log_handlers() |
| 40 |
yield frontend.CommandLineInterface() |
| 41 |
for dirname in ['lv_LV', 'ja_JP']: |
| 42 |
locale_dir = os.path.join(i18n_dir, dirname) |
| 43 |
if os.path.isdir(locale_dir): |
| 44 |
shutil.rmtree(locale_dir) |
| 45 |
_remove_log_handlers() |
| 46 |
|
| 47 |
|
| 48 |
def _remove_log_handlers(): |
| 49 |
# Logging handlers will be reused if possible (#227). This breaks the |
| 50 |
# implicit assumption that our newly created StringIO for sys.stderr |
| 51 |
# contains the console output. Removing the old handler ensures that a |
| 52 |
# new handler with our new StringIO instance will be used. |
| 53 |
log = logging.getLogger('babel') |
| 54 |
for handler in log.handlers: |
| 55 |
log.removeHandler(handler) |
| 56 |
|
| 57 |
|
| 58 |
def test_usage(cli): |
| 59 |
with pytest.raises(SystemExit) as ei: |
| 60 |
cli.run(["pybabel"]) |
| 61 |
assert ei.value.code == 2 |
| 62 |
assert sys.stderr.getvalue().lower() == """\ |
| 63 |
usage: pybabel command [options] [args] |
| 64 |
|
| 65 |
pybabel: error: no valid command or option passed. try the -h/--help option for more information. |
| 66 |
""" |
| 67 |
|
| 68 |
|
| 69 |
def test_list_locales(cli): |
| 70 |
""" |
| 71 |
Test the command with the --list-locales arg. |
| 72 |
""" |
| 73 |
result = cli.run(['pybabel', '--list-locales']) |
| 74 |
assert not result |
| 75 |
output = sys.stdout.getvalue() |
| 76 |
assert 'fr_CH' in output |
| 77 |
assert 'French (Switzerland)' in output |
| 78 |
assert "\nb'" not in output # No bytes repr markers in output |
| 79 |
|
| 80 |
|
| 81 |
def _run_init_catalog(cli): |
| 82 |
i18n_dir = os.path.join(data_dir, 'project', 'i18n') |
| 83 |
pot_path = os.path.join(data_dir, 'project', 'i18n', 'messages.pot') |
| 84 |
cli.run(['pybabel', 'init', '--locale', 'en_US', '-d', i18n_dir, '-i', pot_path]) |
| 85 |
|
| 86 |
|
| 87 |
def test_no_duplicated_output_for_multiple_runs(cli): |
| 88 |
_run_init_catalog(cli) |
| 89 |
first_output = sys.stderr.getvalue() |
| 90 |
_run_init_catalog(cli) |
| 91 |
second_output = sys.stderr.getvalue()[len(first_output):] |
| 92 |
|
| 93 |
# in case the log message is not duplicated we should get the same |
| 94 |
# output as before |
| 95 |
assert first_output == second_output |
| 96 |
|
| 97 |
|
| 98 |
def test_frontend_can_log_to_predefined_handler(cli): |
| 99 |
custom_stream = StringIO() |
| 100 |
log = logging.getLogger('babel') |
| 101 |
log.addHandler(logging.StreamHandler(custom_stream)) |
| 102 |
|
| 103 |
_run_init_catalog(cli) |
| 104 |
assert id(sys.stderr) != id(custom_stream) |
| 105 |
assert not sys.stderr.getvalue() |
| 106 |
assert custom_stream.getvalue() |
| 107 |
|
| 108 |
|
| 109 |
def test_help(cli): |
| 110 |
with pytest.raises(SystemExit) as ei: |
| 111 |
cli.run(['pybabel', '--help']) |
| 112 |
assert not ei.value.code |
| 113 |
content = sys.stdout.getvalue().lower() |
| 114 |
assert 'options:' in content |
| 115 |
assert all(command in content for command in ('init', 'update', 'compile', 'extract')) |
| 116 |
|
| 117 |
|
| 118 |
@freeze_time("1994-11-11") |
| 119 |
def test_extract_with_default_mapping(cli, pot_file): |
| 120 |
cli.run([ |
| 121 |
'pybabel', |
| 122 |
'extract', |
| 123 |
'--copyright-holder', 'FooBar, Inc.', |
| 124 |
'--project', 'TestProject', '--version', '0.1', |
| 125 |
'--msgid-bugs-address', '[email protected]', |
| 126 |
'-c', 'TRANSLATOR', '-c', 'TRANSLATORS:', |
| 127 |
'-o', pot_file, 'project', |
| 128 |
]) |
| 129 |
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') |
| 130 |
expected_content = fr"""# Translations template for TestProject. |
| 131 |
# Copyright (C) {time.strftime('%Y')} FooBar, Inc. |
| 132 |
# This file is distributed under the same license as the TestProject |
| 133 |
# project. |
| 134 |
# FIRST AUTHOR <EMAIL@ADDRESS>, {time.strftime('%Y')}. |
| 135 |
# |
| 136 |
#, fuzzy |
| 137 |
msgid "" |
| 138 |
msgstr "" |
| 139 |
"Project-Id-Version: TestProject 0.1\n" |
| 140 |
"Report-Msgid-Bugs-To: [email protected]\n" |
| 141 |
"POT-Creation-Date: {date}\n" |
| 142 |
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" |
| 143 |
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" |
| 144 |
"Language-Team: LANGUAGE <[email protected]>\n" |
| 145 |
"MIME-Version: 1.0\n" |
| 146 |
"Content-Type: text/plain; charset=utf-8\n" |
| 147 |
"Content-Transfer-Encoding: 8bit\n" |
| 148 |
"Generated-By: Babel {VERSION}\n" |
| 149 |
|
| 150 |
#. TRANSLATOR: This will be a translator coment, |
| 151 |
#. that will include several lines |
| 152 |
#: project/file1.py:8 |
| 153 |
msgid "bar" |
| 154 |
msgstr "" |
| 155 |
|
| 156 |
#: project/file2.py:9 |
| 157 |
msgid "foobar" |
| 158 |
msgid_plural "foobars" |
| 159 |
msgstr[0] "" |
| 160 |
msgstr[1] "" |
| 161 |
|
| 162 |
#: project/ignored/this_wont_normally_be_here.py:11 |
| 163 |
msgid "FooBar" |
| 164 |
msgid_plural "FooBars" |
| 165 |
msgstr[0] "" |
| 166 |
msgstr[1] "" |
| 167 |
|
| 168 |
""" |
| 169 |
assert expected_content == pot_file.read_text() |
| 170 |
|
| 171 |
|
| 172 |
@freeze_time("1994-11-11") |
| 173 |
def test_extract_with_mapping_file(cli, pot_file): |
| 174 |
cli.run([ |
| 175 |
'pybabel', |
| 176 |
'extract', |
| 177 |
'--copyright-holder', 'FooBar, Inc.', |
| 178 |
'--project', 'TestProject', '--version', '0.1', |
| 179 |
'--msgid-bugs-address', '[email protected]', |
| 180 |
'--mapping', os.path.join(data_dir, 'mapping.cfg'), |
| 181 |
'-c', 'TRANSLATOR', '-c', 'TRANSLATORS:', |
| 182 |
'-o', pot_file, 'project', |
| 183 |
]) |
| 184 |
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') |
| 185 |
expected_content = fr"""# Translations template for TestProject. |
| 186 |
# Copyright (C) {time.strftime('%Y')} FooBar, Inc. |
| 187 |
# This file is distributed under the same license as the TestProject |
| 188 |
# project. |
| 189 |
# FIRST AUTHOR <EMAIL@ADDRESS>, {time.strftime('%Y')}. |
| 190 |
# |
| 191 |
#, fuzzy |
| 192 |
msgid "" |
| 193 |
msgstr "" |
| 194 |
"Project-Id-Version: TestProject 0.1\n" |
| 195 |
"Report-Msgid-Bugs-To: [email protected]\n" |
| 196 |
"POT-Creation-Date: {date}\n" |
| 197 |
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" |
| 198 |
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" |
| 199 |
"Language-Team: LANGUAGE <[email protected]>\n" |
| 200 |
"MIME-Version: 1.0\n" |
| 201 |
"Content-Type: text/plain; charset=utf-8\n" |
| 202 |
"Content-Transfer-Encoding: 8bit\n" |
| 203 |
"Generated-By: Babel {VERSION}\n" |
| 204 |
|
| 205 |
#. TRANSLATOR: This will be a translator coment, |
| 206 |
#. that will include several lines |
| 207 |
#: project/file1.py:8 |
| 208 |
msgid "bar" |
| 209 |
msgstr "" |
| 210 |
|
| 211 |
#: project/file2.py:9 |
| 212 |
msgid "foobar" |
| 213 |
msgid_plural "foobars" |
| 214 |
msgstr[0] "" |
| 215 |
msgstr[1] "" |
| 216 |
|
| 217 |
""" |
| 218 |
assert expected_content == pot_file.read_text() |
| 219 |
|
| 220 |
|
| 221 |
@freeze_time("1994-11-11") |
| 222 |
def test_extract_with_exact_file(cli, pot_file): |
| 223 |
"""Tests that we can call extract with a particular file and only |
| 224 |
strings from that file get extracted. (Note the absence of strings from file1.py) |
| 225 |
""" |
| 226 |
file_to_extract = os.path.join(data_dir, 'project', 'file2.py') |
| 227 |
cli.run([ |
| 228 |
'pybabel', |
| 229 |
'extract', |
| 230 |
'--copyright-holder', 'FooBar, Inc.', |
| 231 |
'--project', 'TestProject', '--version', '0.1', |
| 232 |
'--msgid-bugs-address', '[email protected]', |
| 233 |
'--mapping', os.path.join(data_dir, 'mapping.cfg'), |
| 234 |
'-c', 'TRANSLATOR', '-c', 'TRANSLATORS:', |
| 235 |
'-o', pot_file, file_to_extract, |
| 236 |
]) |
| 237 |
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') |
| 238 |
expected_content = fr"""# Translations template for TestProject. |
| 239 |
# Copyright (C) {time.strftime('%Y')} FooBar, Inc. |
| 240 |
# This file is distributed under the same license as the TestProject |
| 241 |
# project. |
| 242 |
# FIRST AUTHOR <EMAIL@ADDRESS>, {time.strftime('%Y')}. |
| 243 |
# |
| 244 |
#, fuzzy |
| 245 |
msgid "" |
| 246 |
msgstr "" |
| 247 |
"Project-Id-Version: TestProject 0.1\n" |
| 248 |
"Report-Msgid-Bugs-To: [email protected]\n" |
| 249 |
"POT-Creation-Date: {date}\n" |
| 250 |
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" |
| 251 |
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" |
| 252 |
"Language-Team: LANGUAGE <[email protected]>\n" |
| 253 |
"MIME-Version: 1.0\n" |
| 254 |
"Content-Type: text/plain; charset=utf-8\n" |
| 255 |
"Content-Transfer-Encoding: 8bit\n" |
| 256 |
"Generated-By: Babel {VERSION}\n" |
| 257 |
|
| 258 |
#: project/file2.py:9 |
| 259 |
msgid "foobar" |
| 260 |
msgid_plural "foobars" |
| 261 |
msgstr[0] "" |
| 262 |
msgstr[1] "" |
| 263 |
|
| 264 |
""" |
| 265 |
assert expected_content == pot_file.read_text() |
| 266 |
|
| 267 |
|
| 268 |
@freeze_time("1994-11-11") |
| 269 |
def test_init_with_output_dir(cli): |
| 270 |
po_file = get_po_file_path('en_US') |
| 271 |
cli.run([ |
| 272 |
'pybabel', |
| 273 |
'init', |
| 274 |
'--locale', 'en_US', |
| 275 |
'-d', os.path.join(i18n_dir), |
| 276 |
'-i', os.path.join(i18n_dir, 'messages.pot'), |
| 277 |
]) |
| 278 |
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') |
| 279 |
expected_content = fr"""# English (United States) translations for TestProject. |
| 280 |
# Copyright (C) 2007 FooBar, Inc. |
| 281 |
# This file is distributed under the same license as the TestProject |
| 282 |
# project. |
| 283 |
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007. |
| 284 |
# |
| 285 |
msgid "" |
| 286 |
msgstr "" |
| 287 |
"Project-Id-Version: TestProject 0.1\n" |
| 288 |
"Report-Msgid-Bugs-To: [email protected]\n" |
| 289 |
"POT-Creation-Date: 2007-04-01 15:30+0200\n" |
| 290 |
"PO-Revision-Date: {date}\n" |
| 291 |
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" |
| 292 |
"Language: en_US\n" |
| 293 |
"Language-Team: en_US <[email protected]>\n" |
| 294 |
"Plural-Forms: nplurals=2; plural=(n != 1);\n" |
| 295 |
"MIME-Version: 1.0\n" |
| 296 |
"Content-Type: text/plain; charset=utf-8\n" |
| 297 |
"Content-Transfer-Encoding: 8bit\n" |
| 298 |
"Generated-By: Babel {VERSION}\n" |
| 299 |
|
| 300 |
#. This will be a translator coment, |
| 301 |
#. that will include several lines |
| 302 |
#: project/file1.py:8 |
| 303 |
msgid "bar" |
| 304 |
msgstr "" |
| 305 |
|
| 306 |
#: project/file2.py:9 |
| 307 |
msgid "foobar" |
| 308 |
msgid_plural "foobars" |
| 309 |
msgstr[0] "" |
| 310 |
msgstr[1] "" |
| 311 |
|
| 312 |
""" |
| 313 |
with open(po_file) as f: |
| 314 |
actual_content = f.read() |
| 315 |
assert expected_content == actual_content |
| 316 |
|
| 317 |
|
| 318 |
@freeze_time("1994-11-11") |
| 319 |
def test_init_singular_plural_forms(cli): |
| 320 |
po_file = get_po_file_path('ja_JP') |
| 321 |
cli.run([ |
| 322 |
'pybabel', |
| 323 |
'init', |
| 324 |
'--locale', 'ja_JP', |
| 325 |
'-d', os.path.join(i18n_dir), |
| 326 |
'-i', os.path.join(i18n_dir, 'messages.pot'), |
| 327 |
]) |
| 328 |
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') |
| 329 |
expected_content = fr"""# Japanese (Japan) translations for TestProject. |
| 330 |
# Copyright (C) 2007 FooBar, Inc. |
| 331 |
# This file is distributed under the same license as the TestProject |
| 332 |
# project. |
| 333 |
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007. |
| 334 |
# |
| 335 |
msgid "" |
| 336 |
msgstr "" |
| 337 |
"Project-Id-Version: TestProject 0.1\n" |
| 338 |
"Report-Msgid-Bugs-To: [email protected]\n" |
| 339 |
"POT-Creation-Date: 2007-04-01 15:30+0200\n" |
| 340 |
"PO-Revision-Date: {date}\n" |
| 341 |
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" |
| 342 |
"Language: ja_JP\n" |
| 343 |
"Language-Team: ja_JP <[email protected]>\n" |
| 344 |
"Plural-Forms: nplurals=1; plural=0;\n" |
| 345 |
"MIME-Version: 1.0\n" |
| 346 |
"Content-Type: text/plain; charset=utf-8\n" |
| 347 |
"Content-Transfer-Encoding: 8bit\n" |
| 348 |
"Generated-By: Babel {VERSION}\n" |
| 349 |
|
| 350 |
#. This will be a translator coment, |
| 351 |
#. that will include several lines |
| 352 |
#: project/file1.py:8 |
| 353 |
msgid "bar" |
| 354 |
msgstr "" |
| 355 |
|
| 356 |
#: project/file2.py:9 |
| 357 |
msgid "foobar" |
| 358 |
msgid_plural "foobars" |
| 359 |
msgstr[0] "" |
| 360 |
|
| 361 |
""" |
| 362 |
with open(po_file) as f: |
| 363 |
actual_content = f.read() |
| 364 |
assert expected_content == actual_content |
| 365 |
|
| 366 |
|
| 367 |
@freeze_time("1994-11-11") |
| 368 |
def test_init_more_than_2_plural_forms(cli): |
| 369 |
po_file = get_po_file_path('lv_LV') |
| 370 |
cli.run([ |
| 371 |
'pybabel', |
| 372 |
'init', |
| 373 |
'--locale', 'lv_LV', |
| 374 |
'-d', i18n_dir, |
| 375 |
'-i', os.path.join(i18n_dir, 'messages.pot'), |
| 376 |
]) |
| 377 |
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en') |
| 378 |
expected_content = fr"""# Latvian (Latvia) translations for TestProject. |
| 379 |
# Copyright (C) 2007 FooBar, Inc. |
| 380 |
# This file is distributed under the same license as the TestProject |
| 381 |
# project. |
| 382 |
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007. |
| 383 |
# |
| 384 |
msgid "" |
| 385 |
msgstr "" |
| 386 |
"Project-Id-Version: TestProject 0.1\n" |
| 387 |
"Report-Msgid-Bugs-To: [email protected]\n" |
| 388 |
"POT-Creation-Date: 2007-04-01 15:30+0200\n" |
| 389 |
"PO-Revision-Date: {date}\n" |
| 390 |
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" |
| 391 |
"Language: lv_LV\n" |
| 392 |
"Language-Team: lv_LV <[email protected]>\n" |
| 393 |
"Plural-Forms: nplurals=3; plural=(n % 10 == 0 || n % 100 >= 11 && n % 100" |
| 394 |
" <= 19 ? 0 : n % 10 == 1 && n % 100 != 11 ? 1 : 2);\n" |
| 395 |
"MIME-Version: 1.0\n" |
| 396 |
"Content-Type: text/plain; charset=utf-8\n" |
| 397 |
"Content-Transfer-Encoding: 8bit\n" |
| 398 |
"Generated-By: Babel {VERSION}\n" |
| 399 |
|
| 400 |
#. This will be a translator coment, |
| 401 |
#. that will include several lines |
| 402 |
#: project/file1.py:8 |
| 403 |
msgid "bar" |
| 404 |
msgstr "" |
| 405 |
|
| 406 |
#: project/file2.py:9 |
| 407 |
msgid "foobar" |
| 408 |
msgid_plural "foobars" |
| 409 |
msgstr[0] "" |
| 410 |
msgstr[1] "" |
| 411 |
msgstr[2] "" |
| 412 |
|
| 413 |
""" |
| 414 |
with open(po_file) as f: |
| 415 |
actual_content = f.read() |
| 416 |
assert expected_content == actual_content |
| 417 |
|
| 418 |
|
| 419 |
def test_compile_catalog(cli): |
| 420 |
po_file = get_po_file_path('de_DE') |
| 421 |
mo_file = po_file.replace('.po', '.mo') |
| 422 |
cli.run(['pybabel', 'compile', '--locale', 'de_DE', '-d', i18n_dir]) |
| 423 |
assert not os.path.isfile(mo_file), f'Expected no file at {mo_file!r}' |
| 424 |
assert sys.stderr.getvalue() == f'catalog {po_file} is marked as fuzzy, skipping\n' |
| 425 |
|
| 426 |
|
| 427 |
def test_compile_fuzzy_catalog(cli): |
| 428 |
po_file = get_po_file_path('de_DE') |
| 429 |
mo_file = po_file.replace('.po', '.mo') |
| 430 |
try: |
| 431 |
cli.run(['pybabel', 'compile', '--locale', 'de_DE', '--use-fuzzy', '-d', i18n_dir]) |
| 432 |
assert os.path.isfile(mo_file) |
| 433 |
assert sys.stderr.getvalue() == f'compiling catalog {po_file} to {mo_file}\n' |
| 434 |
finally: |
| 435 |
if os.path.isfile(mo_file): |
| 436 |
os.unlink(mo_file) |
| 437 |
|
| 438 |
|
| 439 |
def test_compile_catalog_with_more_than_2_plural_forms(cli): |
| 440 |
po_file = get_po_file_path('ru_RU') |
| 441 |
mo_file = po_file.replace('.po', '.mo') |
| 442 |
try: |
| 443 |
cli.run(['pybabel', 'compile', '--locale', 'ru_RU', '--use-fuzzy', '-d', i18n_dir]) |
| 444 |
assert os.path.isfile(mo_file) |
| 445 |
assert sys.stderr.getvalue() == f'compiling catalog {po_file} to {mo_file}\n' |
| 446 |
finally: |
| 447 |
if os.path.isfile(mo_file): |
| 448 |
os.unlink(mo_file) |
| 449 |
|
| 450 |
|
| 451 |
def test_compile_catalog_multidomain(cli): |
| 452 |
po_foo = os.path.join(i18n_dir, 'de_DE', 'LC_MESSAGES', 'foo.po') |
| 453 |
po_bar = os.path.join(i18n_dir, 'de_DE', 'LC_MESSAGES', 'bar.po') |
| 454 |
mo_foo = po_foo.replace('.po', '.mo') |
| 455 |
mo_bar = po_bar.replace('.po', '.mo') |
| 456 |
try: |
| 457 |
cli.run([ |
| 458 |
'pybabel', 'compile', |
| 459 |
'--locale', 'de_DE', |
| 460 |
'--domain', 'foo bar', |
| 461 |
'--use-fuzzy', |
| 462 |
'-d', i18n_dir, |
| 463 |
]) |
| 464 |
for mo_file in [mo_foo, mo_bar]: |
| 465 |
assert os.path.isfile(mo_file) |
| 466 |
assert sys.stderr.getvalue() == ( |
| 467 |
f'compiling catalog {po_foo} to {mo_foo}\n' |
| 468 |
f'compiling catalog {po_bar} to {mo_bar}\n' |
| 469 |
) |
| 470 |
|
| 471 |
finally: |
| 472 |
for mo_file in [mo_foo, mo_bar]: |
| 473 |
if os.path.isfile(mo_file): |
| 474 |
os.unlink(mo_file) |
| 475 |
|
| 476 |
|
| 477 |
def test_update(cli): |
| 478 |
template = Catalog() |
| 479 |
template.add("1") |
| 480 |
template.add("2") |
| 481 |
template.add("3") |
| 482 |
tmpl_file = os.path.join(i18n_dir, 'temp-template.pot') |
| 483 |
with open(tmpl_file, "wb") as outfp: |
| 484 |
write_po(outfp, template) |
| 485 |
po_file = os.path.join(i18n_dir, 'temp1.po') |
| 486 |
cli.run(['pybabel', 'init', '-l', 'fi', '-o', po_file, '-i', tmpl_file]) |
| 487 |
with open(po_file) as infp: |
| 488 |
catalog = read_po(infp) |
| 489 |
assert len(catalog) == 3 |
| 490 |
|
| 491 |
# Add another entry to the template |
| 492 |
|
| 493 |
template.add("4") |
| 494 |
|
| 495 |
with open(tmpl_file, "wb") as outfp: |
| 496 |
write_po(outfp, template) |
| 497 |
|
| 498 |
cli.run(['pybabel', 'update', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file]) |
| 499 |
|
| 500 |
with open(po_file) as infp: |
| 501 |
catalog = read_po(infp) |
| 502 |
assert len(catalog) == 4 # Catalog was updated |
| 503 |
|
| 504 |
|
| 505 |
def test_update_pot_creation_date(cli): |
| 506 |
template = Catalog() |
| 507 |
template.add("1") |
| 508 |
template.add("2") |
| 509 |
template.add("3") |
| 510 |
tmpl_file = os.path.join(i18n_dir, 'temp-template.pot') |
| 511 |
with open(tmpl_file, "wb") as outfp: |
| 512 |
write_po(outfp, template) |
| 513 |
po_file = os.path.join(i18n_dir, 'temp1.po') |
| 514 |
cli.run(['pybabel', 'init', '-l', 'fi', '-o', po_file, '-i', tmpl_file]) |
| 515 |
with open(po_file) as infp: |
| 516 |
catalog = read_po(infp) |
| 517 |
assert len(catalog) == 3 |
| 518 |
original_catalog_creation_date = catalog.creation_date |
| 519 |
|
| 520 |
# Update the template creation date |
| 521 |
template.creation_date -= timedelta(minutes=3) |
| 522 |
with open(tmpl_file, "wb") as outfp: |
| 523 |
write_po(outfp, template) |
| 524 |
|
| 525 |
cli.run(['pybabel', 'update', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file]) |
| 526 |
|
| 527 |
with open(po_file) as infp: |
| 528 |
catalog = read_po(infp) |
| 529 |
# We didn't ignore the creation date, so expect a diff |
| 530 |
assert catalog.creation_date != original_catalog_creation_date |
| 531 |
|
| 532 |
# Reset the "original" |
| 533 |
original_catalog_creation_date = catalog.creation_date |
| 534 |
|
| 535 |
# Update the template creation date again |
| 536 |
# This time, pass the ignore flag and expect the times are different |
| 537 |
template.creation_date -= timedelta(minutes=5) |
| 538 |
with open(tmpl_file, "wb") as outfp: |
| 539 |
write_po(outfp, template) |
| 540 |
|
| 541 |
cli.run(['pybabel', 'update', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file, '--ignore-pot-creation-date']) |
| 542 |
|
| 543 |
with open(po_file) as infp: |
| 544 |
catalog = read_po(infp) |
| 545 |
# We ignored creation date, so it should not have changed |
| 546 |
assert catalog.creation_date == original_catalog_creation_date |
| 547 |
|
| 548 |
|
| 549 |
def test_check(cli): |
| 550 |
template = Catalog() |
| 551 |
template.add("1") |
| 552 |
template.add("2") |
| 553 |
template.add("3") |
| 554 |
tmpl_file = os.path.join(i18n_dir, 'temp-template.pot') |
| 555 |
with open(tmpl_file, "wb") as outfp: |
| 556 |
write_po(outfp, template) |
| 557 |
po_file = os.path.join(i18n_dir, 'temp1.po') |
| 558 |
cli.run(['pybabel', 'init', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file]) |
| 559 |
|
| 560 |
# Update the catalog file |
| 561 |
cli.run(['pybabel', 'update', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file]) |
| 562 |
|
| 563 |
# Run a check without introducing any changes to the template |
| 564 |
cli.run(['pybabel', 'update', '--check', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file]) |
| 565 |
|
| 566 |
# Add a new entry and expect the check to fail |
| 567 |
template.add("4") |
| 568 |
with open(tmpl_file, "wb") as outfp: |
| 569 |
write_po(outfp, template) |
| 570 |
|
| 571 |
with pytest.raises(BaseError): |
| 572 |
cli.run(['pybabel', 'update', '--check', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file]) |
| 573 |
|
| 574 |
# Write the latest changes to the po-file |
| 575 |
cli.run(['pybabel', 'update', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file]) |
| 576 |
|
| 577 |
# Update an entry and expect the check to fail |
| 578 |
template.add("4", locations=[("foo.py", 1)]) |
| 579 |
with open(tmpl_file, "wb") as outfp: |
| 580 |
write_po(outfp, template) |
| 581 |
|
| 582 |
with pytest.raises(BaseError): |
| 583 |
cli.run(['pybabel', 'update', '--check', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file]) |
| 584 |
|
| 585 |
|
| 586 |
def test_check_pot_creation_date(cli): |
| 587 |
template = Catalog() |
| 588 |
template.add("1") |
| 589 |
template.add("2") |
| 590 |
template.add("3") |
| 591 |
tmpl_file = os.path.join(i18n_dir, 'temp-template.pot') |
| 592 |
with open(tmpl_file, "wb") as outfp: |
| 593 |
write_po(outfp, template) |
| 594 |
po_file = os.path.join(i18n_dir, 'temp1.po') |
| 595 |
cli.run(['pybabel', 'init', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file]) |
| 596 |
|
| 597 |
# Update the catalog file |
| 598 |
cli.run(['pybabel', 'update', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file]) |
| 599 |
|
| 600 |
# Run a check without introducing any changes to the template |
| 601 |
cli.run(['pybabel', 'update', '--check', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file]) |
| 602 |
|
| 603 |
# Run a check after changing the template creation date |
| 604 |
template.creation_date = datetime.now() - timedelta(minutes=5) |
| 605 |
with open(tmpl_file, "wb") as outfp: |
| 606 |
write_po(outfp, template) |
| 607 |
|
| 608 |
# Should fail without --ignore-pot-creation-date flag |
| 609 |
with pytest.raises(BaseError): |
| 610 |
cli.run(['pybabel', 'update', '--check', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file]) |
| 611 |
# Should pass with --ignore-pot-creation-date flag |
| 612 |
cli.run([ |
| 613 |
'pybabel', 'update', |
| 614 |
'--check', |
| 615 |
'-l', 'fi_FI', |
| 616 |
'-o', po_file, |
| 617 |
'-i', tmpl_file, |
| 618 |
'--ignore-pot-creation-date', |
| 619 |
]) |
| 620 |
|
| 621 |
|
| 622 |
def test_update_init_missing(cli): |
| 623 |
template = Catalog() |
| 624 |
template.add("1") |
| 625 |
template.add("2") |
| 626 |
template.add("3") |
| 627 |
tmpl_file = os.path.join(i18n_dir, 'temp2-template.pot') |
| 628 |
with open(tmpl_file, "wb") as outfp: |
| 629 |
write_po(outfp, template) |
| 630 |
po_file = os.path.join(i18n_dir, 'temp2.po') |
| 631 |
|
| 632 |
cli.run(['pybabel', 'update', '--init-missing', '-l', 'fi', '-o', po_file, '-i', tmpl_file]) |
| 633 |
|
| 634 |
with open(po_file) as infp: |
| 635 |
catalog = read_po(infp) |
| 636 |
assert len(catalog) == 3 |
| 637 |
|
| 638 |
# Add another entry to the template |
| 639 |
|
| 640 |
template.add("4") |
| 641 |
|
| 642 |
with open(tmpl_file, "wb") as outfp: |
| 643 |
write_po(outfp, template) |
| 644 |
|
| 645 |
cli.run(['pybabel', 'update', '--init-missing', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file]) |
| 646 |
|
| 647 |
with open(po_file) as infp: |
| 648 |
catalog = read_po(infp) |
| 649 |
assert len(catalog) == 4 # Catalog was updated |
| 650 |
|
| 651 |
|
| 652 |
def test_update_init_missing_creates_dest_dir(cli, tmp_path): |
| 653 |
template = Catalog() |
| 654 |
template.add("xyzzy") |
| 655 |
template.add("ferg") |
| 656 |
tmpl_file = tmp_path / 'temp.pot' |
| 657 |
with tmpl_file.open("wb") as outfp: |
| 658 |
write_po(outfp, template) |
| 659 |
|
| 660 |
dest_dir = tmp_path / 'newdir' / 'hierarchy' |
| 661 |
assert not dest_dir.exists() |
| 662 |
po_file = dest_dir / 'temp.po' |
| 663 |
|
| 664 |
cli.run(['pybabel', 'update', '--init-missing', '-l', 'ja', '-o', po_file, '-i', tmpl_file]) |
| 665 |
assert dest_dir.exists() |
| 666 |
|
| 667 |
with po_file.open() as infp: |
| 668 |
assert len(read_po(infp)) == 2 |