| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import pathlib |
| 4 |
from io import BytesIO |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from babel.messages import frontend |
| 9 |
|
| 10 |
toml_test_cases_path = pathlib.Path(__file__).parent / "toml-test-cases" |
| 11 |
assert toml_test_cases_path.is_dir(), "toml-test-cases directory not found" |
| 12 |
|
| 13 |
|
| 14 |
def parse_toml(cfg: bytes | str): |
| 15 |
if isinstance(cfg, str): |
| 16 |
cfg = cfg.encode("utf-8") |
| 17 |
return frontend._parse_mapping_toml(BytesIO(cfg)) |
| 18 |
|
| 19 |
|
| 20 |
def test_toml_mapping_multiple_patterns(): |
| 21 |
""" |
| 22 |
Test that patterns may be specified as a list in TOML, |
| 23 |
and are expanded to multiple entries in the method map. |
| 24 |
""" |
| 25 |
method_map, options_map = parse_toml(""" |
| 26 |
[[mappings]] |
| 27 |
method = "python" |
| 28 |
pattern = ["xyz/**.py", "foo/**.py"] |
| 29 |
""") |
| 30 |
assert method_map == [ |
| 31 |
('xyz/**.py', 'python'), |
| 32 |
('foo/**.py', 'python'), |
| 33 |
] |
| 34 |
|
| 35 |
|
| 36 |
@pytest.mark.parametrize( |
| 37 |
("keywords_val", "expected"), |
| 38 |
[ |
| 39 |
pytest.param('"foo bar quz"', {'bar': None, 'foo': None, 'quz': None}, id='string'), |
| 40 |
pytest.param('["foo", "bar", "quz"]', {'bar': None, 'foo': None, 'quz': None}, id='list'), |
| 41 |
pytest.param('"foo:1,2 bar quz"', {'bar': None, 'foo': (1, 2), 'quz': None}, id='s-args'), |
| 42 |
pytest.param('["bar", "foo:1,2", "quz"]', {'bar': None, 'foo': (1, 2), 'quz': None}, id='l-args'), |
| 43 |
pytest.param('[]', None, id='empty'), |
| 44 |
], |
| 45 |
) |
| 46 |
def test_toml_mapping_keywords_parsing(keywords_val, expected): |
| 47 |
method_map, options_map = parse_toml(f""" |
| 48 |
[[mappings]] |
| 49 |
method = "python" |
| 50 |
pattern = ["**.py"] |
| 51 |
keywords = {keywords_val} |
| 52 |
""") |
| 53 |
assert options_map['**.py'].get('keywords') == expected |
| 54 |
|
| 55 |
|
| 56 |
@pytest.mark.parametrize( |
| 57 |
("add_comments_val", "expected"), |
| 58 |
[ |
| 59 |
('"SPECIAL SAUCE"', ['SPECIAL SAUCE']), # TOML will allow this as a single string |
| 60 |
('["SPECIAL", "SAUCE"]', ['SPECIAL', 'SAUCE']), |
| 61 |
('[]', None), |
| 62 |
], |
| 63 |
) |
| 64 |
def test_toml_mapping_add_comments_parsing(add_comments_val, expected): |
| 65 |
method_map, options_map = parse_toml(f""" |
| 66 |
[[mappings]] |
| 67 |
method = "python" |
| 68 |
pattern = ["**.py"] |
| 69 |
add_comments = {add_comments_val} |
| 70 |
""") |
| 71 |
assert options_map['**.py'].get('add_comments') == expected |
| 72 |
|
| 73 |
|
| 74 |
@pytest.mark.parametrize("test_case", toml_test_cases_path.glob("bad.*.toml"), ids=lambda p: p.name) |
| 75 |
def test_bad_toml_test_case(test_case: pathlib.Path): |
| 76 |
""" |
| 77 |
Test that bad TOML files raise a ValueError. |
| 78 |
""" |
| 79 |
with pytest.raises(frontend.ConfigurationError): |
| 80 |
with test_case.open("rb") as f: |
| 81 |
frontend._parse_mapping_toml( |
| 82 |
f, |
| 83 |
filename=test_case.name, |
| 84 |
style="pyproject.toml" if "pyproject" in test_case.name else "standalone", |
| 85 |
) |