Skip to content

Commit 3861837

Browse files
committed
Move pathlist support to legacypath plugin
1 parent ec01d81 commit 3861837

File tree

4 files changed

+90
-25
lines changed

4 files changed

+90
-25
lines changed

src/_pytest/config/__init__.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
from _pytest._io import TerminalWriter
4848
from _pytest.compat import final
4949
from _pytest.compat import importlib_metadata
50-
from _pytest.compat import legacy_path
5150
from _pytest.outcomes import fail
5251
from _pytest.outcomes import Skipped
5352
from _pytest.pathlib import absolutepath
@@ -1367,6 +1366,10 @@ def getini(self, name: str):
13671366
self._inicache[name] = val = self._getini(name)
13681367
return val
13691368

1369+
# Meant for easy monkeypatching by legacypath plugin.
1370+
def _getini_unknown_type(self, name: str, type: str, value: Union[str, List[str]]):
1371+
raise ValueError(f"unknown configuration type: {type}", value)
1372+
13701373
def _getini(self, name: str):
13711374
try:
13721375
description, type, default = self._parser._inidict[name]
@@ -1399,13 +1402,7 @@ def _getini(self, name: str):
13991402
# a_line_list = ["tests", "acceptance"]
14001403
# in this case, we already have a list ready to use.
14011404
#
1402-
if type == "pathlist":
1403-
# TODO: This assert is probably not valid in all cases.
1404-
assert self.inipath is not None
1405-
dp = self.inipath.parent
1406-
input_values = shlex.split(value) if isinstance(value, str) else value
1407-
return [legacy_path(str(dp / x)) for x in input_values]
1408-
elif type == "paths":
1405+
if type == "paths":
14091406
# TODO: This assert is probably not valid in all cases.
14101407
assert self.inipath is not None
14111408
dp = self.inipath.parent
@@ -1420,9 +1417,12 @@ def _getini(self, name: str):
14201417
return value
14211418
elif type == "bool":
14221419
return _strtobool(str(value).strip())
1423-
else:
1424-
assert type in [None, "string"]
1420+
elif type == "string":
14251421
return value
1422+
elif type is None:
1423+
return value
1424+
else:
1425+
return self._getini_unknown_type(name, type, value)
14261426

14271427
def _getconftest_pathlist(
14281428
self, name: str, path: Path, rootpath: Path

src/_pytest/legacypath.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Add backward compatibility support for the legacy py path type."""
2+
import shlex
23
import subprocess
34
from pathlib import Path
45
from typing import List
@@ -375,6 +376,19 @@ def Session_stardir(self: pytest.Session) -> LEGACY_PATH:
375376
return legacy_path(self.startpath)
376377

377378

379+
def Config__getini_unknown_type(
380+
self, name: str, type: str, value: Union[str, List[str]]
381+
):
382+
if type == "pathlist":
383+
# TODO: This assert is probably not valid in all cases.
384+
assert self.inipath is not None
385+
dp = self.inipath.parent
386+
input_values = shlex.split(value) if isinstance(value, str) else value
387+
return [legacy_path(str(dp / x)) for x in input_values]
388+
else:
389+
raise ValueError(f"unknown configuration type: {type}", value)
390+
391+
378392
def pytest_configure(config: pytest.Config) -> None:
379393
mp = pytest.MonkeyPatch()
380394
config._cleanup.append(mp.undo)
@@ -415,3 +429,6 @@ def pytest_configure(config: pytest.Config) -> None:
415429

416430
# Add Session.startdir property.
417431
mp.setattr(pytest.Session, "startdir", property(Session_stardir), raising=False)
432+
433+
# Add pathlist configuration type.
434+
mp.setattr(pytest.Config, "_getini_unknown_type", Config__getini_unknown_type)

testing/test_config.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -635,14 +635,11 @@ def pytest_addoption(parser):
635635
pytest.raises(ValueError, config.getini, "other")
636636

637637
@pytest.mark.parametrize("config_type", ["ini", "pyproject"])
638-
@pytest.mark.parametrize("ini_type", ["paths", "pathlist"])
639-
def test_addini_paths(
640-
self, pytester: Pytester, config_type: str, ini_type: str
641-
) -> None:
638+
def test_addini_paths(self, pytester: Pytester, config_type: str) -> None:
642639
pytester.makeconftest(
643-
f"""
640+
"""
644641
def pytest_addoption(parser):
645-
parser.addini("paths", "my new ini value", type="{ini_type}")
642+
parser.addini("paths", "my new ini value", type="paths")
646643
parser.addini("abc", "abc value")
647644
"""
648645
)
@@ -1521,28 +1518,24 @@ def test_pass(pytestconfig):
15211518
assert result.ret == 0
15221519
result.stdout.fnmatch_lines(["custom_option:3.0"])
15231520

1524-
@pytest.mark.parametrize("ini_type", ["paths", "pathlist"])
1525-
def test_override_ini_paths(self, pytester: Pytester, ini_type: str) -> None:
1521+
def test_override_ini_paths(self, pytester: Pytester) -> None:
15261522
pytester.makeconftest(
1527-
f"""
1523+
"""
15281524
def pytest_addoption(parser):
1529-
parser.addini("paths", "my new ini value", type="{ini_type}")"""
1525+
parser.addini("paths", "my new ini value", type="paths")"""
15301526
)
15311527
pytester.makeini(
15321528
"""
15331529
[pytest]
15341530
paths=blah.py"""
15351531
)
15361532
pytester.makepyfile(
1537-
rf"""
1533+
r"""
15381534
def test_overriden(pytestconfig):
15391535
config_paths = pytestconfig.getini("paths")
15401536
print(config_paths)
15411537
for cpf in config_paths:
1542-
if "{ini_type}" == "pathlist":
1543-
print('\nuser_path:%s' % cpf.basename)
1544-
else:
1545-
print('\nuser_path:%s' % cpf.name)
1538+
print('\nuser_path:%s' % cpf.name)
15461539
"""
15471540
)
15481541
result = pytester.runpytest(

testing/test_legacypath.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,58 @@ def test_session_scoped_unavailable_attributes(self, session_request):
7878
match="path not available in session-scoped context",
7979
):
8080
session_request.fspath
81+
82+
83+
@pytest.mark.parametrize("config_type", ["ini", "pyproject"])
84+
def test_addini_paths(self, pytester: Pytester, config_type: str) -> None:
85+
pytester.makeconftest(
86+
"""
87+
def pytest_addoption(parser):
88+
parser.addini("paths", "my new ini value", type="pathlist")
89+
parser.addini("abc", "abc value")
90+
"""
91+
)
92+
if config_type == "ini":
93+
inipath = pytester.makeini(
94+
"""
95+
[pytest]
96+
paths=hello world/sub.py
97+
"""
98+
)
99+
elif config_type == "pyproject":
100+
inipath = pytester.makepyprojecttoml(
101+
"""
102+
[tool.pytest.ini_options]
103+
paths=["hello", "world/sub.py"]
104+
"""
105+
)
106+
config = pytester.parseconfig()
107+
values = config.getini("paths")
108+
assert len(values) == 2
109+
assert values[0] == inipath.parent.joinpath("hello")
110+
assert values[1] == inipath.parent.joinpath("world/sub.py")
111+
pytest.raises(ValueError, config.getini, "other")
112+
113+
114+
def test_override_ini_paths(self, pytester: Pytester) -> None:
115+
pytester.makeconftest(
116+
"""
117+
def pytest_addoption(parser):
118+
parser.addini("paths", "my new ini value", type="pathlist")"""
119+
)
120+
pytester.makeini(
121+
"""
122+
[pytest]
123+
paths=blah.py"""
124+
)
125+
pytester.makepyfile(
126+
r"""
127+
def test_overriden(pytestconfig):
128+
config_paths = pytestconfig.getini("paths")
129+
print(config_paths)
130+
for cpf in config_paths:
131+
print('\nuser_path:%s' % cpf.basename)
132+
"""
133+
)
134+
result = pytester.runpytest("--override-ini", "paths=foo/bar1.py foo/bar2.py", "-s")
135+
result.stdout.fnmatch_lines(["user_path:bar1.py", "user_path:bar2.py"])

0 commit comments

Comments
 (0)