45
45
project_dir ,
46
46
this_dir ,
47
47
)
48
+ from tests .messages .utils import CUSTOM_EXTRACTOR_COOKIE
48
49
49
50
50
51
def _po_file (locale ):
@@ -1392,7 +1393,11 @@ def test_update_init_missing(self):
1392
1393
1393
1394
mapping_cfg = """
1394
1395
[extractors]
1395
- custom = mypackage.module:myfunc
1396
+ custom = tests.messages.utils:custom_extractor
1397
+
1398
+ # Special extractor for a given Python file
1399
+ [custom: special.py]
1400
+ treat = delicious
1396
1401
1397
1402
# Python source files
1398
1403
[python: **.py]
@@ -1411,7 +1416,13 @@ def test_update_init_missing(self):
1411
1416
1412
1417
mapping_toml = """
1413
1418
[extractors]
1414
- custom = "mypackage.module:myfunc"
1419
+ custom = "tests.messages.utils:custom_extractor"
1420
+
1421
+ # Special extractor for a given Python file
1422
+ [[mappings]]
1423
+ method = "custom"
1424
+ pattern = "special.py"
1425
+ treat = "delightful"
1415
1426
1416
1427
# Python source files
1417
1428
[[mappings]]
@@ -1470,18 +1481,17 @@ def test_parse_mapping(data: str, parser, preprocess, is_toml):
1470
1481
buf = StringIO (data )
1471
1482
1472
1483
method_map , options_map = parser (buf )
1473
- assert len (method_map ) == 4
1484
+ assert len (method_map ) == 5
1474
1485
1475
- assert method_map [0 ] == ('**.py' , 'python' )
1486
+ assert method_map [1 ] == ('**.py' , 'python' )
1476
1487
assert options_map ['**.py' ] == {}
1477
- assert method_map [1 ] == ('**/templates/**.html' , 'genshi' )
1488
+ assert method_map [2 ] == ('**/templates/**.html' , 'genshi' )
1478
1489
assert options_map ['**/templates/**.html' ]['include_attrs' ] == ''
1479
- assert method_map [2 ] == ('**/templates/**.txt' , 'genshi' )
1490
+ assert method_map [3 ] == ('**/templates/**.txt' , 'genshi' )
1480
1491
assert (options_map ['**/templates/**.txt' ]['template_class' ]
1481
1492
== 'genshi.template:TextTemplate' )
1482
1493
assert options_map ['**/templates/**.txt' ]['encoding' ] == 'latin-1'
1483
-
1484
- assert method_map [3 ] == ('**/custom/*.*' , 'mypackage.module:myfunc' )
1494
+ assert method_map [4 ] == ('**/custom/*.*' , 'tests.messages.utils:custom_extractor' )
1485
1495
assert options_map ['**/custom/*.*' ] == {}
1486
1496
1487
1497
@@ -1663,3 +1673,29 @@ def test_extract_header_comment(monkeypatch, tmp_path):
1663
1673
cmdinst .run ()
1664
1674
pot_content = pot_file .read_text ()
1665
1675
assert 'Boing' in pot_content
1676
+
1677
+
1678
+ @pytest .mark .parametrize ("mapping_format" , ("toml" , "cfg" ))
1679
+ def test_pr_1121 (tmp_path , monkeypatch , caplog , mapping_format ):
1680
+ """
1681
+ Test that extraction uses the first matching method and options,
1682
+ instead of the first matching method and last matching options.
1683
+
1684
+ Without the fix in PR #1121, this test would fail,
1685
+ since the `custom_extractor` isn't passed a delicious treat via
1686
+ the configuration.
1687
+ """
1688
+ if mapping_format == "cfg" :
1689
+ mapping_file = (tmp_path / "mapping.cfg" )
1690
+ mapping_file .write_text (mapping_cfg )
1691
+ else :
1692
+ mapping_file = (tmp_path / "mapping.toml" )
1693
+ mapping_file .write_text (mapping_toml )
1694
+ (tmp_path / "special.py" ).write_text ("# this file is special" )
1695
+ pot_path = (tmp_path / "output.pot" )
1696
+ monkeypatch .chdir (tmp_path )
1697
+ cmdinst = configure_cli_command (f"extract . -o { shlex .quote (str (pot_path ))} --mapping { shlex .quote (mapping_file .name )} " )
1698
+ assert isinstance (cmdinst , ExtractMessages )
1699
+ cmdinst .run ()
1700
+ # If the custom extractor didn't run, we wouldn't see the cookie in there.
1701
+ assert CUSTOM_EXTRACTOR_COOKIE in pot_path .read_text ()
0 commit comments