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: 4 additions & 1 deletion babel/messages/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ def callback(filename: str, method: str, options: dict):

def run(self):
mappings = self._get_mappings()
with open(self.output_file, 'wb') as outfile:
with self._open_output_file() as outfile:
catalog = Catalog(
project=self.project,
version=self.version,
Expand Down Expand Up @@ -550,6 +550,9 @@ def run(self):
width=self.width,
)

def _open_output_file(self) -> BinaryIO:
return open(self.output_file, 'wb')

def _get_mappings(self):
mappings = []

Expand Down
7 changes: 7 additions & 0 deletions babel/messages/setuptools_frontend.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from io import BytesIO

from babel.messages import frontend

try:
Expand Down Expand Up @@ -65,6 +67,11 @@ class extract_messages(frontend.ExtractMessages, Command):
)
"""

def _open_output_file(self):
if self.dry_run:
return BytesIO()
return super()._open_output_file()


class init_catalog(frontend.InitCatalog, Command):
"""New catalog initialization command for use in ``setup.py`` scripts.
Expand Down
20 changes: 20 additions & 0 deletions tests/messages/test_setuptools_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ def test_extract_distutils_keyword_arg_388(kwarg, expected):
assert set(cmdinst.add_comments) == {"Bar", "Foo"}


def test_extract_messages_dry_run(tmp_path, caplog):
from babel.messages import setuptools_frontend

source_file = tmp_path / "source.py"
source_file.write_text("_('message')\n")
output_file = tmp_path / "messages.pot"
output_file.write_text("original contents\n")

command = setuptools_frontend.extract_messages(Distribution())
command.dry_run = True
command.input_paths = [str(source_file)]
command.output_file = str(output_file)
command.ensure_finalized()
with caplog.at_level("INFO"):
command.run()

assert output_file.read_text() == "original contents\n"
assert f"extracting messages from {source_file}" in caplog.text


@pytest.mark.xfail(
# Python 3.10.16[pypy-7.3.19-final] in GHA fails with "unsupported locale setting"
# in the subprocesses this test spawns. Hard to say why because it doesn't do that
Expand Down