Skip to content

Commit 61f80a7

Browse files
committed
terminal: fix crash in header reporting when absolute testpaths is used
Regressed in 6.1.0 in 62e249a. The `x` is an `str` but is expected to be a `pathlib.Path`. Not caught by mypy because `config.getini()` returns `Any`. Fix by just removing the `bestrelpath` call: - testpaths are always relative to the rootdir, it thus would be very unusual to specify an absolute path there. - The code was wrong even before the regression: `py.path.local`'s `bestrelpath` function expects a `py.path.local`, not an `str`. But it had some weird `try ... except AttributeError` fallback which just returns the argument, i.e. it was a no-op. So there is no behavior change. - It seems reasonable to me to just print the full path if that's what the ini specifies.
1 parent db08c7f commit 61f80a7

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

changelog/7814.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed crash in header reporting when :confval:`testpaths` is used and contains absolute paths (regression in 6.1.0).

src/_pytest/terminal.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -718,10 +718,10 @@ def pytest_report_header(self, config: Config) -> List[str]:
718718
if config.inipath:
719719
line += ", configfile: " + bestrelpath(config.rootpath, config.inipath)
720720

721-
testpaths = config.getini("testpaths")
721+
testpaths = config.getini("testpaths") # type: List[str]
722722
if testpaths and config.args == testpaths:
723-
rel_paths = [bestrelpath(config.rootpath, x) for x in testpaths]
724-
line += ", testpaths: {}".format(", ".join(rel_paths))
723+
line += ", testpaths: {}".format(", ".join(testpaths))
724+
725725
result = [line]
726726

727727
plugininfo = config.pluginmanager.list_plugin_distinfo()

testing/test_terminal.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from _pytest._io.wcwidth import wcswidth
1919
from _pytest.config import Config
2020
from _pytest.config import ExitCode
21+
from _pytest.monkeypatch import MonkeyPatch
2122
from _pytest.pathlib import Path
2223
from _pytest.pytester import Testdir
2324
from _pytest.reports import BaseReport
@@ -749,6 +750,29 @@ def test_header(self, testdir):
749750
result = testdir.runpytest("tests")
750751
result.stdout.fnmatch_lines(["rootdir: *test_header0, configfile: tox.ini"])
751752

753+
def test_header_absolute_testpath(
754+
self, testdir: Testdir, monkeypatch: MonkeyPatch
755+
) -> None:
756+
"""Regresstion test for #7814."""
757+
tests = testdir.tmpdir.join("tests")
758+
tests.ensure_dir()
759+
testdir.makepyprojecttoml(
760+
"""
761+
[tool.pytest.ini_options]
762+
testpaths = ['{}']
763+
""".format(
764+
tests
765+
)
766+
)
767+
result = testdir.runpytest()
768+
result.stdout.fnmatch_lines(
769+
[
770+
"rootdir: *absolute_testpath0, configfile: pyproject.toml, testpaths: {}".format(
771+
tests
772+
)
773+
]
774+
)
775+
752776
def test_no_header(self, testdir):
753777
testdir.tmpdir.join("tests").ensure_dir()
754778
testdir.tmpdir.join("gui").ensure_dir()

0 commit comments

Comments
 (0)