Debian Codegraph @

tests/messages/test_setuptools_frontend.py

raw
1 import os
2 import shlex
3 import shutil
4 import subprocess
5 import sys
6 from pathlib import Path
7
8 import pytest
9
10 from tests.messages.consts import data_dir
11
12 Distribution = pytest.importorskip("setuptools").Distribution
13
14
15 @pytest.mark.parametrize("kwarg,expected", [
16 ("LW_", ("LW_",)),
17 ("LW_ QQ Q", ("LW_", "QQ", "Q")),
18 ("yiy aia", ("yiy", "aia")),
19 ])
20 def test_extract_distutils_keyword_arg_388(kwarg, expected):
21 from babel.messages import frontend, setuptools_frontend
22
23 # This is a regression test for https://github.com/python-babel/babel/issues/388
24
25 # Note that distutils-based commands only support a single repetition of the same argument;
26 # hence `--keyword ignored` will actually never end up in the output.
27
28 cmdline = (
29 f"extract_messages --no-default-keywords --keyword ignored --keyword '{kwarg}' "
30 "--input-dirs . --output-file django233.pot --add-comments Bar,Foo"
31 )
32 d = Distribution(attrs={
33 "cmdclass": setuptools_frontend.COMMANDS,
34 "script_args": shlex.split(cmdline),
35 })
36 d.parse_command_line()
37 assert len(d.commands) == 1
38 cmdinst = d.get_command_obj(d.commands[0])
39 cmdinst.ensure_finalized()
40 assert isinstance(cmdinst, frontend.ExtractMessages)
41 assert isinstance(cmdinst, setuptools_frontend.extract_messages)
42 assert set(cmdinst.keywords.keys()) == set(expected)
43
44 # Test the comma-separated comment argument while we're at it:
45 assert set(cmdinst.add_comments) == {"Bar", "Foo"}
46
47
48 @pytest.mark.xfail(
49 # Python 3.10.16[pypy-7.3.19-final] in GHA fails with "unsupported locale setting"
50 # in the subprocesses this test spawns. Hard to say why because it doesn't do that
51 # locally.
52 condition=(sys.implementation.name == "pypy" and "BABEL_TOX_INI_DIR" in os.environ),
53 reason="Test will likely fail with 'unsupported locale setting' in subprocesses; see comment",
54 )
55 def test_setuptools_commands(tmp_path, monkeypatch):
56 """
57 Smoke-tests all of the setuptools versions of the commands in turn.
58
59 Their full functionality is tested better in `test_frontend.py`.
60 """
61 # Copy the test project to a temporary directory and work there
62 dest = tmp_path / "dest"
63 shutil.copytree(data_dir, dest)
64 monkeypatch.chdir(dest)
65
66 # When in Tox, we need to hack things a bit so as not to have the
67 # sub-interpreter `sys.executable` use the tox virtualenv's Babel
68 # installation, so the locale data is where we expect it to be.
69 if "BABEL_TOX_INI_DIR" in os.environ:
70 monkeypatch.setenv("PYTHONPATH", os.environ["BABEL_TOX_INI_DIR"])
71
72 # Initialize an empty catalog
73 subprocess.check_call([
74 sys.executable,
75 "setup.py",
76 "init_catalog",
77 "-i", os.devnull,
78 "-l", "fi",
79 "-d", "inited",
80 ])
81 po_file = Path("inited/fi/LC_MESSAGES/messages.po")
82 orig_po_data = po_file.read_text()
83 subprocess.check_call([
84 sys.executable,
85 "setup.py",
86 "extract_messages",
87 "-o", "extracted.pot",
88 ])
89 pot_file = Path("extracted.pot")
90 pot_data = pot_file.read_text()
91 assert "FooBar, TM" in pot_data # should be read from setup.cfg
92 assert "[email protected]" in pot_data # should be read from setup.cfg
93 subprocess.check_call([
94 sys.executable,
95 "setup.py",
96 "update_catalog",
97 "-i", "extracted.pot",
98 "-d", "inited",
99 ])
100 new_po_data = po_file.read_text()
101 assert new_po_data != orig_po_data # check we updated the file
102 subprocess.check_call([
103 sys.executable,
104 "setup.py",
105 "compile_catalog",
106 "-d", "inited",
107 ])
108 assert po_file.with_suffix(".mo").exists()