@@ -87,7 +87,8 @@ def assert_POT_equal(self, expected, actual):
8787 self .maxDiff = None
8888 self .assertEqual (normalize_POT_file (expected ), normalize_POT_file (actual ))
8989
90- def extract_from_str (self , module_content , * , args = (), strict = True , with_stderr = False ):
90+ def extract_from_str (self , module_content , * , args = (), strict = True ,
91+ with_stderr = False , raw = False ):
9192 """Return all msgids extracted from module_content."""
9293 filename = 'test.py'
9394 with temp_cwd (None ):
@@ -98,10 +99,11 @@ def extract_from_str(self, module_content, *, args=(), strict=True, with_stderr=
9899 self .assertEqual (res .err , b'' )
99100 with open ('messages.pot' , encoding = 'utf-8' ) as fp :
100101 data = fp .read ()
101- msgids = self .get_msgids (data )
102+ if not raw :
103+ data = self .get_msgids (data )
102104 if not with_stderr :
103- return msgids
104- return msgids , res .err
105+ return data
106+ return data , res .err
105107
106108 def extract_docstrings_from_str (self , module_content ):
107109 """Return all docstrings extracted from module_content."""
@@ -381,7 +383,8 @@ def test_pygettext_output(self):
381383 contents = input_file .read_text (encoding = 'utf-8' )
382384 with temp_cwd (None ):
383385 Path (input_file .name ).write_text (contents )
384- assert_python_ok ('-Xutf8' , self .script , '--docstrings' , input_file .name )
386+ assert_python_ok ('-Xutf8' , self .script , '--docstrings' ,
387+ '--add-comments=i18n:' , input_file .name )
385388 output = Path ('messages.pot' ).read_text (encoding = 'utf-8' )
386389
387390 expected = output_file .read_text (encoding = 'utf-8' )
@@ -437,14 +440,60 @@ def test_error_messages(self):
437440 "*** test.py:3: Variable positional arguments are not allowed in gettext calls\n "
438441 )
439442
443+ def test_extract_all_comments (self ):
444+ """
445+ Test that the --add-comments option without an
446+ explicit tag extracts all translator comments.
447+ """
448+ for arg in ('--add-comments' , '-c' ):
449+ with self .subTest (arg = arg ):
450+ data = self .extract_from_str (dedent ('''\
451+ # Translator comment
452+ _("foo")
453+ ''' ), args = (arg ,), raw = True )
454+ self .assertIn ('#. Translator comment' , data )
455+
456+ def test_comments_with_multiple_tags (self ):
457+ """
458+ Test that multiple --add-comments tags can be specified.
459+ """
460+ for arg in ('--add-comments={}' , '-c{}' ):
461+ with self .subTest (arg = arg ):
462+ args = (arg .format ('foo:' ), arg .format ('bar:' ))
463+ data = self .extract_from_str (dedent ('''\
464+ # foo: comment
465+ _("foo")
466+
467+ # bar: comment
468+ _("bar")
469+
470+ # baz: comment
471+ _("baz")
472+ ''' ), args = args , raw = True )
473+ self .assertIn ('#. foo: comment' , data )
474+ self .assertIn ('#. bar: comment' , data )
475+ self .assertNotIn ('#. baz: comment' , data )
476+
477+ def test_comments_not_extracted_without_tags (self ):
478+ """
479+ Test that translator comments are not extracted without
480+ specifying --add-comments.
481+ """
482+ data = self .extract_from_str (dedent ('''\
483+ # Translator comment
484+ _("foo")
485+ ''' ), raw = True )
486+ self .assertNotIn ('#.' , data )
487+
440488
441489def update_POT_snapshots ():
442490 for input_file in DATA_DIR .glob ('*.py' ):
443491 output_file = input_file .with_suffix ('.pot' )
444492 contents = input_file .read_bytes ()
445493 with temp_cwd (None ):
446494 Path (input_file .name ).write_bytes (contents )
447- assert_python_ok ('-Xutf8' , Test_pygettext .script , '--docstrings' , input_file .name )
495+ assert_python_ok ('-Xutf8' , Test_pygettext .script , '--docstrings' ,
496+ '--add-comments=i18n:' , input_file .name )
448497 output = Path ('messages.pot' ).read_text (encoding = 'utf-8' )
449498
450499 output = normalize_POT_file (output )
0 commit comments