Skip to content
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

Use correct input arg type for _bestrelpath #9238

Merged
merged 7 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ Grigorii Eremeev (budulianin)
Guido Wesdorp
Guoqiang Zhang
Harald Armin Massa
Harshna
Henk-Jaap Wagenaar
Holger Kohr
Hugo van Kemenade
Expand Down
1 change: 1 addition & 0 deletions changelog/8990.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix `pytest -vv` crashing with an internal exception `AttributeError: 'str' object has no attribute 'relative_to'` in some cases.
2 changes: 2 additions & 0 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,8 @@ def bestrelpath(directory: Path, dest: Path) -> str:

If no such path can be determined, returns dest.
"""
assert isinstance(directory, Path)
assert isinstance(dest, Path)
if dest == directory:
return os.curdir
# Find the longest common directory.
Expand Down
9 changes: 5 additions & 4 deletions src/_pytest/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,8 +862,10 @@ def _report_keyboardinterrupt(self) -> None:
yellow=True,
)

def _locationline(self, nodeid, fspath, lineno, domain):
def mkrel(nodeid):
def _locationline(
self, nodeid: str, fspath: str, lineno: Optional[int], domain: str
) -> str:
def mkrel(nodeid: str) -> str:
line = self.config.cwd_relative_nodeid(nodeid)
if domain and line.endswith(domain):
line = line[: -len(domain)]
Expand All @@ -873,13 +875,12 @@ def mkrel(nodeid):
return line

# collect_fspath comes from testid which has a "/"-normalized path.

if fspath:
res = mkrel(nodeid)
if self.verbosity >= 2 and nodeid.split("::")[0] != fspath.replace(
"\\", nodes.SEP
):
res += " <- " + bestrelpath(self.startpath, fspath)
res += " <- " + bestrelpath(self.startpath, Path(fspath))
else:
res = "[location]"
return res + " "
Expand Down
15 changes: 15 additions & 0 deletions testing/logging/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1165,3 +1165,18 @@ def test_log_file_cli_subdirectories_are_successfully_created(
result = pytester.runpytest("--log-file=foo/bar/logf.log")
assert "logf.log" in os.listdir(expected)
assert result.ret == ExitCode.OK


def test_locationline_returns_best_relative_location(pytester: Pytester) -> None:
item = pytester.getitem("def test_func(): pass")
item.config.option.verbose = 2

tr = TerminalReporter(item.config)
path_to_test = os.path.join("unique", "path")
fspath = os.path.join(pytester.path, path_to_test)
nodeid = "nodeid::test"

result = tr._locationline(nodeid, fspath, 1, "domain")
hp310780 marked this conversation as resolved.
Show resolved Hide resolved

expected_result = f"{nodeid} <- {path_to_test} "
assert result == expected_result