-
Notifications
You must be signed in to change notification settings - Fork 430
/
test_frontend.py
1626 lines (1393 loc) · 54.4 KB
/
test_frontend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright (C) 2007-2011 Edgewall Software, 2013-2023 the Babel team
# All rights reserved.
#
# This software is licensed as described in the file LICENSE, which
# you should have received as part of this distribution. The terms
# are also available at http://babel.edgewall.org/wiki/License.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://babel.edgewall.org/log/.
import logging
import os
import shlex
import shutil
import sys
import time
import unittest
from datetime import datetime, timedelta
from io import BytesIO, StringIO
import pytest
from freezegun import freeze_time
from setuptools import Distribution
from babel import __version__ as VERSION
from babel.dates import format_datetime
from babel.messages import Catalog, extract, frontend
from babel.messages.frontend import (
BaseError,
CommandLineInterface,
OptionError,
extract_messages,
update_catalog,
)
from babel.messages.pofile import read_po, write_po
from babel.util import LOCALTZ
TEST_PROJECT_DISTRIBUTION_DATA = {
"name": "TestProject",
"version": "0.1",
"packages": ["project"],
}
this_dir = os.path.abspath(os.path.dirname(__file__))
data_dir = os.path.join(this_dir, 'data')
project_dir = os.path.join(data_dir, 'project')
i18n_dir = os.path.join(project_dir, 'i18n')
pot_file = os.path.join(i18n_dir, 'temp.pot')
def _po_file(locale):
return os.path.join(i18n_dir, locale, 'LC_MESSAGES', 'messages.po')
class CompileCatalogTestCase(unittest.TestCase):
def setUp(self):
self.olddir = os.getcwd()
os.chdir(data_dir)
self.dist = Distribution(TEST_PROJECT_DISTRIBUTION_DATA)
self.cmd = frontend.compile_catalog(self.dist)
self.cmd.initialize_options()
def tearDown(self):
os.chdir(self.olddir)
def test_no_directory_or_output_file_specified(self):
self.cmd.locale = 'en_US'
self.cmd.input_file = 'dummy'
with pytest.raises(OptionError):
self.cmd.finalize_options()
def test_no_directory_or_input_file_specified(self):
self.cmd.locale = 'en_US'
self.cmd.output_file = 'dummy'
with pytest.raises(OptionError):
self.cmd.finalize_options()
class ExtractMessagesTestCase(unittest.TestCase):
def setUp(self):
self.olddir = os.getcwd()
os.chdir(data_dir)
self.dist = Distribution(TEST_PROJECT_DISTRIBUTION_DATA)
self.cmd = frontend.extract_messages(self.dist)
self.cmd.initialize_options()
def tearDown(self):
if os.path.isfile(pot_file):
os.unlink(pot_file)
os.chdir(self.olddir)
def assert_pot_file_exists(self):
assert os.path.isfile(pot_file)
def test_neither_default_nor_custom_keywords(self):
self.cmd.output_file = 'dummy'
self.cmd.no_default_keywords = True
with pytest.raises(OptionError):
self.cmd.finalize_options()
def test_no_output_file_specified(self):
with pytest.raises(OptionError):
self.cmd.finalize_options()
def test_both_sort_output_and_sort_by_file(self):
self.cmd.output_file = 'dummy'
self.cmd.sort_output = True
self.cmd.sort_by_file = True
with pytest.raises(OptionError):
self.cmd.finalize_options()
def test_invalid_file_or_dir_input_path(self):
self.cmd.input_paths = 'nonexistent_path'
self.cmd.output_file = 'dummy'
with pytest.raises(OptionError):
self.cmd.finalize_options()
def test_input_paths_is_treated_as_list(self):
self.cmd.input_paths = data_dir
self.cmd.output_file = pot_file
self.cmd.finalize_options()
self.cmd.run()
with open(pot_file) as f:
catalog = read_po(f)
msg = catalog.get('bar')
assert len(msg.locations) == 1
assert ('file1.py' in msg.locations[0][0])
def test_input_paths_handle_spaces_after_comma(self):
self.cmd.input_paths = f"{this_dir}, {data_dir}"
self.cmd.output_file = pot_file
self.cmd.finalize_options()
assert self.cmd.input_paths == [this_dir, data_dir]
def test_input_dirs_is_alias_for_input_paths(self):
self.cmd.input_dirs = this_dir
self.cmd.output_file = pot_file
self.cmd.finalize_options()
# Gets listified in `finalize_options`:
assert self.cmd.input_paths == [self.cmd.input_dirs]
def test_input_dirs_is_mutually_exclusive_with_input_paths(self):
self.cmd.input_dirs = this_dir
self.cmd.input_paths = this_dir
self.cmd.output_file = pot_file
with pytest.raises(OptionError):
self.cmd.finalize_options()
@freeze_time("1994-11-11")
def test_extraction_with_default_mapping(self):
self.cmd.copyright_holder = 'FooBar, Inc.'
self.cmd.msgid_bugs_address = 'bugs.address@email.tld'
self.cmd.output_file = 'project/i18n/temp.pot'
self.cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:'
self.cmd.finalize_options()
self.cmd.run()
self.assert_pot_file_exists()
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
expected_content = fr"""# Translations template for TestProject.
# Copyright (C) {time.strftime('%Y')} FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, {time.strftime('%Y')}.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
"POT-Creation-Date: {date}\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel {VERSION}\n"
#. TRANSLATOR: This will be a translator coment,
#. that will include several lines
#: project/file1.py:8
msgid "bar"
msgstr ""
#: project/file2.py:9
msgid "foobar"
msgid_plural "foobars"
msgstr[0] ""
msgstr[1] ""
#: project/ignored/this_wont_normally_be_here.py:11
msgid "FooBar"
msgid_plural "FooBars"
msgstr[0] ""
msgstr[1] ""
"""
with open(pot_file) as f:
actual_content = f.read()
assert expected_content == actual_content
@freeze_time("1994-11-11")
def test_extraction_with_mapping_file(self):
self.cmd.copyright_holder = 'FooBar, Inc.'
self.cmd.msgid_bugs_address = 'bugs.address@email.tld'
self.cmd.mapping_file = 'mapping.cfg'
self.cmd.output_file = 'project/i18n/temp.pot'
self.cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:'
self.cmd.finalize_options()
self.cmd.run()
self.assert_pot_file_exists()
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
expected_content = fr"""# Translations template for TestProject.
# Copyright (C) {time.strftime('%Y')} FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, {time.strftime('%Y')}.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
"POT-Creation-Date: {date}\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel {VERSION}\n"
#. TRANSLATOR: This will be a translator coment,
#. that will include several lines
#: project/file1.py:8
msgid "bar"
msgstr ""
#: project/file2.py:9
msgid "foobar"
msgid_plural "foobars"
msgstr[0] ""
msgstr[1] ""
"""
with open(pot_file) as f:
actual_content = f.read()
assert expected_content == actual_content
@freeze_time("1994-11-11")
def test_extraction_with_mapping_dict(self):
self.dist.message_extractors = {
'project': [
('**/ignored/**.*', 'ignore', None),
('**.py', 'python', None),
]
}
self.cmd.copyright_holder = 'FooBar, Inc.'
self.cmd.msgid_bugs_address = 'bugs.address@email.tld'
self.cmd.output_file = 'project/i18n/temp.pot'
self.cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:'
self.cmd.finalize_options()
self.cmd.run()
self.assert_pot_file_exists()
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
expected_content = fr"""# Translations template for TestProject.
# Copyright (C) {time.strftime('%Y')} FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, {time.strftime('%Y')}.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
"POT-Creation-Date: {date}\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel {VERSION}\n"
#. TRANSLATOR: This will be a translator coment,
#. that will include several lines
#: project/file1.py:8
msgid "bar"
msgstr ""
#: project/file2.py:9
msgid "foobar"
msgid_plural "foobars"
msgstr[0] ""
msgstr[1] ""
"""
with open(pot_file) as f:
actual_content = f.read()
assert expected_content == actual_content
def test_extraction_add_location_file(self):
self.dist.message_extractors = {
'project': [
('**/ignored/**.*', 'ignore', None),
('**.py', 'python', None),
]
}
self.cmd.output_file = 'project/i18n/temp.pot'
self.cmd.add_location = 'file'
self.cmd.omit_header = True
self.cmd.finalize_options()
self.cmd.run()
self.assert_pot_file_exists()
expected_content = r"""#: project/file1.py
msgid "bar"
msgstr ""
#: project/file2.py
msgid "foobar"
msgid_plural "foobars"
msgstr[0] ""
msgstr[1] ""
"""
with open(pot_file) as f:
actual_content = f.read()
assert expected_content == actual_content
class InitCatalogTestCase(unittest.TestCase):
def setUp(self):
self.olddir = os.getcwd()
os.chdir(data_dir)
self.dist = Distribution(TEST_PROJECT_DISTRIBUTION_DATA)
self.cmd = frontend.init_catalog(self.dist)
self.cmd.initialize_options()
def tearDown(self):
for dirname in ['en_US', 'ja_JP', 'lv_LV']:
locale_dir = os.path.join(i18n_dir, dirname)
if os.path.isdir(locale_dir):
shutil.rmtree(locale_dir)
os.chdir(self.olddir)
def test_no_input_file(self):
self.cmd.locale = 'en_US'
self.cmd.output_file = 'dummy'
with pytest.raises(OptionError):
self.cmd.finalize_options()
def test_no_locale(self):
self.cmd.input_file = 'dummy'
self.cmd.output_file = 'dummy'
with pytest.raises(OptionError):
self.cmd.finalize_options()
@freeze_time("1994-11-11")
def test_with_output_dir(self):
self.cmd.input_file = 'project/i18n/messages.pot'
self.cmd.locale = 'en_US'
self.cmd.output_dir = 'project/i18n'
self.cmd.finalize_options()
self.cmd.run()
po_file = _po_file('en_US')
assert os.path.isfile(po_file)
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
expected_content = fr"""# English (United States) translations for TestProject.
# Copyright (C) 2007 FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
#
msgid ""
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
"POT-Creation-Date: 2007-04-01 15:30+0200\n"
"PO-Revision-Date: {date}\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: en_US\n"
"Language-Team: en_US <LL@li.org>\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel {VERSION}\n"
#. This will be a translator coment,
#. that will include several lines
#: project/file1.py:8
msgid "bar"
msgstr ""
#: project/file2.py:9
msgid "foobar"
msgid_plural "foobars"
msgstr[0] ""
msgstr[1] ""
"""
with open(po_file) as f:
actual_content = f.read()
assert expected_content == actual_content
@freeze_time("1994-11-11")
def test_keeps_catalog_non_fuzzy(self):
self.cmd.input_file = 'project/i18n/messages_non_fuzzy.pot'
self.cmd.locale = 'en_US'
self.cmd.output_dir = 'project/i18n'
self.cmd.finalize_options()
self.cmd.run()
po_file = _po_file('en_US')
assert os.path.isfile(po_file)
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
expected_content = fr"""# English (United States) translations for TestProject.
# Copyright (C) 2007 FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
#
msgid ""
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
"POT-Creation-Date: 2007-04-01 15:30+0200\n"
"PO-Revision-Date: {date}\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: en_US\n"
"Language-Team: en_US <LL@li.org>\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel {VERSION}\n"
#. This will be a translator coment,
#. that will include several lines
#: project/file1.py:8
msgid "bar"
msgstr ""
#: project/file2.py:9
msgid "foobar"
msgid_plural "foobars"
msgstr[0] ""
msgstr[1] ""
"""
with open(po_file) as f:
actual_content = f.read()
assert expected_content == actual_content
@freeze_time("1994-11-11")
def test_correct_init_more_than_2_plurals(self):
self.cmd.input_file = 'project/i18n/messages.pot'
self.cmd.locale = 'lv_LV'
self.cmd.output_dir = 'project/i18n'
self.cmd.finalize_options()
self.cmd.run()
po_file = _po_file('lv_LV')
assert os.path.isfile(po_file)
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
expected_content = fr"""# Latvian (Latvia) translations for TestProject.
# Copyright (C) 2007 FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
#
msgid ""
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
"POT-Creation-Date: 2007-04-01 15:30+0200\n"
"PO-Revision-Date: {date}\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: lv_LV\n"
"Language-Team: lv_LV <LL@li.org>\n"
"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 :"
" 2);\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel {VERSION}\n"
#. This will be a translator coment,
#. that will include several lines
#: project/file1.py:8
msgid "bar"
msgstr ""
#: project/file2.py:9
msgid "foobar"
msgid_plural "foobars"
msgstr[0] ""
msgstr[1] ""
msgstr[2] ""
"""
with open(po_file) as f:
actual_content = f.read()
assert expected_content == actual_content
@freeze_time("1994-11-11")
def test_correct_init_singular_plural_forms(self):
self.cmd.input_file = 'project/i18n/messages.pot'
self.cmd.locale = 'ja_JP'
self.cmd.output_dir = 'project/i18n'
self.cmd.finalize_options()
self.cmd.run()
po_file = _po_file('ja_JP')
assert os.path.isfile(po_file)
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='ja_JP')
expected_content = fr"""# Japanese (Japan) translations for TestProject.
# Copyright (C) 2007 FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
#
msgid ""
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
"POT-Creation-Date: 2007-04-01 15:30+0200\n"
"PO-Revision-Date: {date}\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: ja_JP\n"
"Language-Team: ja_JP <LL@li.org>\n"
"Plural-Forms: nplurals=1; plural=0;\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel {VERSION}\n"
#. This will be a translator coment,
#. that will include several lines
#: project/file1.py:8
msgid "bar"
msgstr ""
#: project/file2.py:9
msgid "foobar"
msgid_plural "foobars"
msgstr[0] ""
"""
with open(po_file) as f:
actual_content = f.read()
assert expected_content == actual_content
@freeze_time("1994-11-11")
def test_supports_no_wrap(self):
self.cmd.input_file = 'project/i18n/long_messages.pot'
self.cmd.locale = 'en_US'
self.cmd.output_dir = 'project/i18n'
long_message = '"' + 'xxxxx ' * 15 + '"'
with open('project/i18n/messages.pot', 'rb') as f:
pot_contents = f.read().decode('latin-1')
pot_with_very_long_line = pot_contents.replace('"bar"', long_message)
with open(self.cmd.input_file, 'wb') as f:
f.write(pot_with_very_long_line.encode('latin-1'))
self.cmd.no_wrap = True
self.cmd.finalize_options()
self.cmd.run()
po_file = _po_file('en_US')
assert os.path.isfile(po_file)
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en_US')
expected_content = fr"""# English (United States) translations for TestProject.
# Copyright (C) 2007 FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
#
msgid ""
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
"POT-Creation-Date: 2007-04-01 15:30+0200\n"
"PO-Revision-Date: {date}\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: en_US\n"
"Language-Team: en_US <LL@li.org>\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel {VERSION}\n"
#. This will be a translator coment,
#. that will include several lines
#: project/file1.py:8
msgid {long_message}
msgstr ""
#: project/file2.py:9
msgid "foobar"
msgid_plural "foobars"
msgstr[0] ""
msgstr[1] ""
"""
with open(po_file) as f:
actual_content = f.read()
assert expected_content == actual_content
@freeze_time("1994-11-11")
def test_supports_width(self):
self.cmd.input_file = 'project/i18n/long_messages.pot'
self.cmd.locale = 'en_US'
self.cmd.output_dir = 'project/i18n'
long_message = '"' + 'xxxxx ' * 15 + '"'
with open('project/i18n/messages.pot', 'rb') as f:
pot_contents = f.read().decode('latin-1')
pot_with_very_long_line = pot_contents.replace('"bar"', long_message)
with open(self.cmd.input_file, 'wb') as f:
f.write(pot_with_very_long_line.encode('latin-1'))
self.cmd.width = 120
self.cmd.finalize_options()
self.cmd.run()
po_file = _po_file('en_US')
assert os.path.isfile(po_file)
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en_US')
expected_content = fr"""# English (United States) translations for TestProject.
# Copyright (C) 2007 FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
#
msgid ""
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
"POT-Creation-Date: 2007-04-01 15:30+0200\n"
"PO-Revision-Date: {date}\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: en_US\n"
"Language-Team: en_US <LL@li.org>\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel {VERSION}\n"
#. This will be a translator coment,
#. that will include several lines
#: project/file1.py:8
msgid {long_message}
msgstr ""
#: project/file2.py:9
msgid "foobar"
msgid_plural "foobars"
msgstr[0] ""
msgstr[1] ""
"""
with open(po_file) as f:
actual_content = f.read()
assert expected_content == actual_content
class CommandLineInterfaceTestCase(unittest.TestCase):
def setUp(self):
data_dir = os.path.join(this_dir, 'data')
self.orig_working_dir = os.getcwd()
self.orig_argv = sys.argv
self.orig_stdout = sys.stdout
self.orig_stderr = sys.stderr
sys.argv = ['pybabel']
sys.stdout = StringIO()
sys.stderr = StringIO()
os.chdir(data_dir)
self._remove_log_handlers()
self.cli = frontend.CommandLineInterface()
def tearDown(self):
os.chdir(self.orig_working_dir)
sys.argv = self.orig_argv
sys.stdout = self.orig_stdout
sys.stderr = self.orig_stderr
for dirname in ['lv_LV', 'ja_JP']:
locale_dir = os.path.join(i18n_dir, dirname)
if os.path.isdir(locale_dir):
shutil.rmtree(locale_dir)
self._remove_log_handlers()
def _remove_log_handlers(self):
# Logging handlers will be reused if possible (#227). This breaks the
# implicit assumption that our newly created StringIO for sys.stderr
# contains the console output. Removing the old handler ensures that a
# new handler with our new StringIO instance will be used.
log = logging.getLogger('babel')
for handler in log.handlers:
log.removeHandler(handler)
def test_usage(self):
try:
self.cli.run(sys.argv)
self.fail('Expected SystemExit')
except SystemExit as e:
assert e.code == 2
assert sys.stderr.getvalue().lower() == """\
usage: pybabel command [options] [args]
pybabel: error: no valid command or option passed. try the -h/--help option for more information.
"""
def test_list_locales(self):
"""
Test the command with the --list-locales arg.
"""
result = self.cli.run(sys.argv + ['--list-locales'])
assert not result
output = sys.stdout.getvalue()
assert 'fr_CH' in output
assert 'French (Switzerland)' in output
assert "\nb'" not in output # No bytes repr markers in output
def _run_init_catalog(self):
i18n_dir = os.path.join(data_dir, 'project', 'i18n')
pot_path = os.path.join(data_dir, 'project', 'i18n', 'messages.pot')
init_argv = sys.argv + ['init', '--locale', 'en_US', '-d', i18n_dir,
'-i', pot_path]
self.cli.run(init_argv)
def test_no_duplicated_output_for_multiple_runs(self):
self._run_init_catalog()
first_output = sys.stderr.getvalue()
self._run_init_catalog()
second_output = sys.stderr.getvalue()[len(first_output):]
# in case the log message is not duplicated we should get the same
# output as before
assert first_output == second_output
def test_frontend_can_log_to_predefined_handler(self):
custom_stream = StringIO()
log = logging.getLogger('babel')
log.addHandler(logging.StreamHandler(custom_stream))
self._run_init_catalog()
assert id(sys.stderr) != id(custom_stream)
assert not sys.stderr.getvalue()
assert custom_stream.getvalue()
def test_help(self):
try:
self.cli.run(sys.argv + ['--help'])
self.fail('Expected SystemExit')
except SystemExit as e:
assert not e.code
content = sys.stdout.getvalue().lower()
assert 'options:' in content
assert all(command in content for command in ('init', 'update', 'compile', 'extract'))
def assert_pot_file_exists(self):
assert os.path.isfile(pot_file)
@freeze_time("1994-11-11")
def test_extract_with_default_mapping(self):
self.cli.run(sys.argv + ['extract',
'--copyright-holder', 'FooBar, Inc.',
'--project', 'TestProject', '--version', '0.1',
'--msgid-bugs-address', 'bugs.address@email.tld',
'-c', 'TRANSLATOR', '-c', 'TRANSLATORS:',
'-o', pot_file, 'project'])
self.assert_pot_file_exists()
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
expected_content = fr"""# Translations template for TestProject.
# Copyright (C) {time.strftime('%Y')} FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, {time.strftime('%Y')}.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
"POT-Creation-Date: {date}\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel {VERSION}\n"
#. TRANSLATOR: This will be a translator coment,
#. that will include several lines
#: project/file1.py:8
msgid "bar"
msgstr ""
#: project/file2.py:9
msgid "foobar"
msgid_plural "foobars"
msgstr[0] ""
msgstr[1] ""
#: project/ignored/this_wont_normally_be_here.py:11
msgid "FooBar"
msgid_plural "FooBars"
msgstr[0] ""
msgstr[1] ""
"""
with open(pot_file) as f:
actual_content = f.read()
assert expected_content == actual_content
@freeze_time("1994-11-11")
def test_extract_with_mapping_file(self):
self.cli.run(sys.argv + ['extract',
'--copyright-holder', 'FooBar, Inc.',
'--project', 'TestProject', '--version', '0.1',
'--msgid-bugs-address', 'bugs.address@email.tld',
'--mapping', os.path.join(data_dir, 'mapping.cfg'),
'-c', 'TRANSLATOR', '-c', 'TRANSLATORS:',
'-o', pot_file, 'project'])
self.assert_pot_file_exists()
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
expected_content = fr"""# Translations template for TestProject.
# Copyright (C) {time.strftime('%Y')} FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, {time.strftime('%Y')}.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
"POT-Creation-Date: {date}\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel {VERSION}\n"
#. TRANSLATOR: This will be a translator coment,
#. that will include several lines
#: project/file1.py:8
msgid "bar"
msgstr ""
#: project/file2.py:9
msgid "foobar"
msgid_plural "foobars"
msgstr[0] ""
msgstr[1] ""
"""
with open(pot_file) as f:
actual_content = f.read()
assert expected_content == actual_content
@freeze_time("1994-11-11")
def test_extract_with_exact_file(self):
"""Tests that we can call extract with a particular file and only
strings from that file get extracted. (Note the absence of strings from file1.py)
"""
file_to_extract = os.path.join(data_dir, 'project', 'file2.py')
self.cli.run(sys.argv + ['extract',
'--copyright-holder', 'FooBar, Inc.',
'--project', 'TestProject', '--version', '0.1',
'--msgid-bugs-address', 'bugs.address@email.tld',
'--mapping', os.path.join(data_dir, 'mapping.cfg'),
'-c', 'TRANSLATOR', '-c', 'TRANSLATORS:',
'-o', pot_file, file_to_extract])
self.assert_pot_file_exists()
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
expected_content = fr"""# Translations template for TestProject.
# Copyright (C) {time.strftime('%Y')} FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, {time.strftime('%Y')}.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
"POT-Creation-Date: {date}\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel {VERSION}\n"
#: project/file2.py:9
msgid "foobar"
msgid_plural "foobars"
msgstr[0] ""
msgstr[1] ""
"""
with open(pot_file) as f:
actual_content = f.read()
assert expected_content == actual_content
@freeze_time("1994-11-11")
def test_init_with_output_dir(self):
po_file = _po_file('en_US')
self.cli.run(sys.argv + ['init',
'--locale', 'en_US',
'-d', os.path.join(i18n_dir),
'-i', os.path.join(i18n_dir, 'messages.pot')])
assert os.path.isfile(po_file)
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
expected_content = fr"""# English (United States) translations for TestProject.
# Copyright (C) 2007 FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
#
msgid ""
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
"POT-Creation-Date: 2007-04-01 15:30+0200\n"
"PO-Revision-Date: {date}\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: en_US\n"
"Language-Team: en_US <LL@li.org>\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel {VERSION}\n"
#. This will be a translator coment,
#. that will include several lines
#: project/file1.py:8
msgid "bar"
msgstr ""
#: project/file2.py:9
msgid "foobar"
msgid_plural "foobars"
msgstr[0] ""
msgstr[1] ""
"""
with open(po_file) as f:
actual_content = f.read()
assert expected_content == actual_content
@freeze_time("1994-11-11")
def test_init_singular_plural_forms(self):
po_file = _po_file('ja_JP')
self.cli.run(sys.argv + ['init',