Skip to content

Commit 0c1091c

Browse files
akxtomasr8
andauthored
Small test cleanup (#1172)
* Configure Ruff formatter to preserve quotes for now * Split support functionality tests to a separate modules * Use standard `monkeypatch` fixture for `os.environ` patching * Use even more `monkeypatch` for patching --------- Co-authored-by: Tomas R. <[email protected]>
1 parent db48791 commit 0c1091c

File tree

8 files changed

+404
-422
lines changed

8 files changed

+404
-422
lines changed

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ extend-exclude = [
44
"tests/messages/data",
55
]
66

7+
[tool.ruff.format]
8+
quote-style = "preserve"
9+
710
[tool.ruff.lint]
811
select = [
912
"B",

tests/conftest.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import os
2-
31
import pytest
42

53
try:
@@ -16,13 +14,6 @@
1614
pytz = None
1715

1816

19-
@pytest.fixture
20-
def os_environ(monkeypatch):
21-
mock_environ = dict(os.environ)
22-
monkeypatch.setattr(os, 'environ', mock_environ)
23-
return mock_environ
24-
25-
2617
def pytest_generate_tests(metafunc):
2718
if hasattr(metafunc.function, "pytestmark"):
2819
for mark in metafunc.function.pytestmark:

tests/messages/test_setuptools_frontend.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,11 @@ def test_setuptools_commands(tmp_path, monkeypatch):
5656
shutil.copytree(data_dir, dest)
5757
monkeypatch.chdir(dest)
5858

59-
env = os.environ.copy()
6059
# When in Tox, we need to hack things a bit so as not to have the
6160
# sub-interpreter `sys.executable` use the tox virtualenv's Babel
6261
# installation, so the locale data is where we expect it to be.
63-
if "BABEL_TOX_INI_DIR" in env:
64-
env["PYTHONPATH"] = env["BABEL_TOX_INI_DIR"]
62+
if "BABEL_TOX_INI_DIR" in os.environ:
63+
monkeypatch.setenv("PYTHONPATH", os.environ["BABEL_TOX_INI_DIR"])
6564

6665
# Initialize an empty catalog
6766
subprocess.check_call([
@@ -71,15 +70,15 @@ def test_setuptools_commands(tmp_path, monkeypatch):
7170
"-i", os.devnull,
7271
"-l", "fi",
7372
"-d", "inited",
74-
], env=env)
73+
])
7574
po_file = Path("inited/fi/LC_MESSAGES/messages.po")
7675
orig_po_data = po_file.read_text()
7776
subprocess.check_call([
7877
sys.executable,
7978
"setup.py",
8079
"extract_messages",
8180
"-o", "extracted.pot",
82-
], env=env)
81+
])
8382
pot_file = Path("extracted.pot")
8483
pot_data = pot_file.read_text()
8584
assert "FooBar, TM" in pot_data # should be read from setup.cfg
@@ -90,13 +89,13 @@ def test_setuptools_commands(tmp_path, monkeypatch):
9089
"update_catalog",
9190
"-i", "extracted.pot",
9291
"-d", "inited",
93-
], env=env)
92+
])
9493
new_po_data = po_file.read_text()
9594
assert new_po_data != orig_po_data # check we updated the file
9695
subprocess.check_call([
9796
sys.executable,
9897
"setup.py",
9998
"compile_catalog",
10099
"-d", "inited",
101-
], env=env)
100+
])
102101
assert po_file.with_suffix(".mo").exists()

tests/test_core.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ def test_locale_comparison():
4242
assert fi_FI != bad_en_US
4343

4444

45-
def test_can_return_default_locale(os_environ):
46-
os_environ['LC_MESSAGES'] = 'fr_FR.UTF-8'
45+
def test_can_return_default_locale(monkeypatch):
46+
monkeypatch.setenv('LC_MESSAGES', 'fr_FR.UTF-8')
4747
assert Locale('fr', 'FR') == Locale.default('LC_MESSAGES')
4848

4949

50-
def test_ignore_invalid_locales_in_lc_ctype(os_environ):
50+
def test_ignore_invalid_locales_in_lc_ctype(monkeypatch):
5151
# This is a regression test specifically for a bad LC_CTYPE setting on
5252
# MacOS X 10.6 (#200)
53-
os_environ['LC_CTYPE'] = 'UTF-8'
53+
monkeypatch.setenv('LC_CTYPE', 'UTF-8')
5454
# must not throw an exception
5555
default_locale('LC_CTYPE')
5656

@@ -76,10 +76,10 @@ def test_attributes(self):
7676
assert locale.language == 'en'
7777
assert locale.territory == 'US'
7878

79-
def test_default(self, os_environ):
79+
def test_default(self, monkeypatch):
8080
for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LC_MESSAGES']:
81-
os_environ[name] = ''
82-
os_environ['LANG'] = 'fr_FR.UTF-8'
81+
monkeypatch.setenv(name, '')
82+
monkeypatch.setenv('LANG', 'fr_FR.UTF-8')
8383
default = Locale.default('LC_MESSAGES')
8484
assert (default.language, default.territory) == ('fr', 'FR')
8585

@@ -264,17 +264,16 @@ def test_plural_form_property(self):
264264
assert Locale('ru').plural_form(100) == 'many'
265265

266266

267-
def test_default_locale(os_environ):
267+
def test_default_locale(monkeypatch):
268268
for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LC_MESSAGES']:
269-
os_environ[name] = ''
270-
os_environ['LANG'] = 'fr_FR.UTF-8'
269+
monkeypatch.setenv(name, '')
270+
monkeypatch.setenv('LANG', 'fr_FR.UTF-8')
271271
assert default_locale('LC_MESSAGES') == 'fr_FR'
272-
273-
os_environ['LC_MESSAGES'] = 'POSIX'
272+
monkeypatch.setenv('LC_MESSAGES', 'POSIX')
274273
assert default_locale('LC_MESSAGES') == 'en_US_POSIX'
275274

276275
for value in ['C', 'C.UTF-8', 'POSIX']:
277-
os_environ['LANGUAGE'] = value
276+
monkeypatch.setenv('LANGUAGE', value)
278277
assert default_locale() == 'en_US_POSIX'
279278

280279

0 commit comments

Comments
 (0)