Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions babel/messages/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,11 @@ def _configure_command(self, cmdname: str, argv: list[str]) -> CommandMixin:
if name == as_args:
parser.usage += f"<{name}>"
elif name in cmdclass.boolean_options:
if normalized_name in toml_config and not isinstance(default, bool):
parser.error(
f"[tool.babel.{cmdinst.toml_config_table}] {normalized_name} "
"must be a TOML boolean (true or false)",
)
parser.add_option(*strs, action="store_true", default=default, help=help)
elif name in cmdclass.multiple_value_options:
default_list = [default] if isinstance(default, str) else default
Expand Down
30 changes: 30 additions & 0 deletions tests/messages/test_toml_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,33 @@ def test_cli_pyproject_toml_list_values_not_split(in_tmp_path):
cmdinst = cli._configure_command("extract", [])
assert cmdinst.add_comments == ["NOTE TO TRANSLATORS:"]
assert cmdinst.input_paths == ["a b.py"]


@pytest.mark.parametrize("value", ['"false"', "0"])
def test_update_rejects_non_boolean_ignore_obsolete(in_tmp_path, capsys, value):
(in_tmp_path / "pyproject.toml").write_text(f"[tool.babel.update_catalog]\nignore_obsolete = {value}\n")
po_file = in_tmp_path / "messages.po"
po_file.write_bytes(b'msgid "old"\nmsgstr "Old translation"\n')
original = po_file.read_bytes()
(in_tmp_path / "messages.pot").write_bytes(b'msgid "new"\nmsgstr ""\n')

with pytest.raises(SystemExit) as exc_info:
frontend.CommandLineInterface().run(
["pybabel", "update", "-l", "en", "-i", "messages.pot", "-o", "messages.po"],
)

assert exc_info.value.code == 2
assert "ignore_obsolete must be a TOML boolean" in capsys.readouterr().err
assert po_file.read_bytes() == original


@pytest.mark.parametrize(
("value", "flags", "expected"),
[("true", [], True), ("false", [], False), ("false", ["--ignore-obsolete"], True)],
)
def test_update_boolean_ignore_obsolete(in_tmp_path, value, flags, expected):
(in_tmp_path / "pyproject.toml").write_text(f"[tool.babel.update_catalog]\nignore_obsolete = {value}\n")
cli = frontend.CommandLineInterface()
cli.log = frontend.log
command = cli._configure_command("update", ["-i", "messages.pot", "-d", "locale", *flags])
assert command.ignore_obsolete is expected