Skip to content

Elide pytest-internal paths for --fixtures #9281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 10, 2021
Merged
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
1 change: 1 addition & 0 deletions changelog/8822.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When showing fixture paths in `--fixtures` or `--fixtures-by-test`, fixtures coming from pytest itself now display an elided path, rather than the full path to the file in the `site-packages` directory.
26 changes: 19 additions & 7 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
from _pytest.scope import _ScopeName


_PYTEST_DIR = Path(_pytest.__file__).parent


def pytest_addoption(parser: Parser) -> None:
group = parser.getgroup("general")
group.addoption(
Expand Down Expand Up @@ -1443,6 +1446,16 @@ def idmaker(
return resolved_ids


def _pretty_fixture_path(func) -> str:
cwd = Path.cwd()
loc = Path(getlocation(func, str(cwd)))
prefix = Path("...", "_pytest")
try:
return str(prefix / loc.relative_to(_PYTEST_DIR))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we opt for relative to site packages if in site packages and/or relative to start dir if sensible

I believe the real bug is that bestrelpath fails to determine when the absolute path is actually shorter than the best relative one

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have something already to check if a path is in site packages? From what I remember from things like hunter, getting a proper site packages path is quite a pain with differences between Python versions, distributions (Debian/Ubuntu and dist-packages), etc. etc.

I suppose bestrelpath could show an absolute path if that is shorter indeed. But given that it's used in various places I'm not sure if I want to introduce such a change...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh: The path from my example above is relative and "correct", but I still believe the elided path is nicer for the output.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proposed solution is already a enhancement, let's take my comment as a follow-up

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #9292 for the follow-up.

except ValueError:
return bestrelpath(cwd, loc)


def show_fixtures_per_test(config):
from _pytest.main import wrap_session

Expand All @@ -1465,9 +1478,9 @@ def write_fixture(fixture_def: fixtures.FixtureDef[object]) -> None:
argname = fixture_def.argname
if verbose <= 0 and argname.startswith("_"):
return
bestrel = get_best_relpath(fixture_def.func)
prettypath = _pretty_fixture_path(fixture_def.func)
tw.write(f"{argname}", green=True)
tw.write(f" -- {bestrel}", yellow=True)
tw.write(f" -- {prettypath}", yellow=True)
tw.write("\n")
fixture_doc = inspect.getdoc(fixture_def.func)
if fixture_doc:
Expand Down Expand Up @@ -1531,15 +1544,15 @@ def _showfixtures_main(config: Config, session: Session) -> None:
(
len(fixturedef.baseid),
fixturedef.func.__module__,
bestrelpath(curdir, Path(loc)),
_pretty_fixture_path(fixturedef.func),
fixturedef.argname,
fixturedef,
)
)

available.sort()
currentmodule = None
for baseid, module, bestrel, argname, fixturedef in available:
for baseid, module, prettypath, argname, fixturedef in available:
if currentmodule != module:
if not module.startswith("_pytest."):
tw.line()
Expand All @@ -1550,14 +1563,13 @@ def _showfixtures_main(config: Config, session: Session) -> None:
tw.write(f"{argname}", green=True)
if fixturedef.scope != "function":
tw.write(" [%s scope]" % fixturedef.scope, cyan=True)
tw.write(f" -- {bestrel}", yellow=True)
tw.write(f" -- {prettypath}", yellow=True)
tw.write("\n")
loc = getlocation(fixturedef.func, str(curdir))
doc = inspect.getdoc(fixturedef.func)
if doc:
write_docstring(tw, doc.split("\n\n")[0] if verbose <= 0 else doc)
else:
tw.line(f" {loc}: no docstring available", red=True)
tw.line(" no docstring available", red=True)
tw.line()


Expand Down
8 changes: 4 additions & 4 deletions testing/python/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -3346,9 +3346,9 @@ def test_show_fixtures(self, pytester: Pytester) -> None:
result = pytester.runpytest("--fixtures")
result.stdout.fnmatch_lines(
[
"tmp_path_factory [[]session scope[]] -- *tmpdir.py*",
"tmp_path_factory [[]session scope[]] -- .../_pytest/tmpdir.py:*",
"*for the test session*",
"tmp_path -- *",
"tmp_path -- .../_pytest/tmpdir.py:*",
"*temporary directory*",
]
)
Expand All @@ -3357,9 +3357,9 @@ def test_show_fixtures_verbose(self, pytester: Pytester) -> None:
result = pytester.runpytest("--fixtures", "-v")
result.stdout.fnmatch_lines(
[
"tmp_path_factory [[]session scope[]] -- *tmpdir.py*",
"tmp_path_factory [[]session scope[]] -- .../_pytest/tmpdir.py:*",
"*for the test session*",
"tmp_path -- *tmpdir.py*",
"tmp_path -- .../_pytest/tmpdir.py:*",
"*temporary directory*",
]
)
Expand Down