Skip to content

Commit 757bded

Browse files
committed
Use Path() instead of str for path comparison
On Windows specifically is common to have drives diverging just by casing ("C:" vs "c:"), depending on the cwd provided by the user.
1 parent b98aa19 commit 757bded

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/_pytest/nodes.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from _pytest.mark.structures import MarkDecorator
3030
from _pytest.mark.structures import NodeKeywords
3131
from _pytest.outcomes import fail
32+
from _pytest.pathlib import Path
3233
from _pytest.store import Store
3334

3435
if TYPE_CHECKING:
@@ -361,8 +362,14 @@ def _repr_failure_py(
361362
else:
362363
truncate_locals = True
363364

365+
# excinfo.getrepr() formats paths relative to the CWD if `abspath` is False.
366+
# It is possible for a fixture/test to change the CWD while this code runs, which
367+
# would then result in the user seeing confusing paths in the failure message.
368+
# To fix this, if the CWD changed, always display the full absolute path.
369+
# It will be better to just always display paths relative to invocation_dir, but
370+
# this requires a lot of plumbing (#6428).
364371
try:
365-
abspath = os.getcwd() != str(self.config.invocation_dir)
372+
abspath = Path(os.getcwd()) != Path(self.config.invocation_dir)
366373
except OSError:
367374
abspath = True
368375

0 commit comments

Comments
 (0)