Skip to content

Commit

Permalink
Fix issue with slashes being turned into backslashes on Windows (#12760
Browse files Browse the repository at this point in the history
…) (#12787)

Fix #12745

(cherry picked from commit d35b802)

Co-authored-by: Nauman Ahmed <90570675+nauman897@users.noreply.github.com>
  • Loading branch information
patchback[bot] and nauman897 committed Sep 8, 2024
1 parent 300d13d commit 0f10b6b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ mrbean-bremen
Nathan Goldbaum
Nathaniel Compton
Nathaniel Waisbrot
Nauman Ahmed
Ned Batchelder
Neil Martin
Neven Mundar
Expand Down
1 change: 1 addition & 0 deletions changelog/12745.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed an issue with backslashes being incorrectly converted in nodeid paths on Windows, ensuring consistent path handling across environments.
8 changes: 6 additions & 2 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,8 +1179,12 @@ def notify_exception(
def cwd_relative_nodeid(self, nodeid: str) -> str:
# nodeid's are relative to the rootpath, compute relative to cwd.
if self.invocation_params.dir != self.rootpath:
fullpath = self.rootpath / nodeid
nodeid = bestrelpath(self.invocation_params.dir, fullpath)
base_path_part, *nodeid_part = nodeid.split("::")
# Only process path part
fullpath = self.rootpath / base_path_part
relative_path = bestrelpath(self.invocation_params.dir, fullpath)

nodeid = "::".join([relative_path, *nodeid_part])
return nodeid

@classmethod
Expand Down
35 changes: 35 additions & 0 deletions testing/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3067,3 +3067,38 @@ def test_pass():
"*= 1 xpassed in * =*",
]
)


class TestNodeIDHandling:
def test_nodeid_handling_windows_paths(self, pytester: Pytester, tmp_path) -> None:
"""Test the correct handling of Windows-style paths with backslashes."""
pytester.makeini("[pytest]") # Change `config.rootpath`

test_path = pytester.path / "tests" / "test_foo.py"
test_path.parent.mkdir()
os.chdir(test_path.parent) # Change `config.invocation_params.dir`

test_path.write_text(
textwrap.dedent(
"""
import pytest
@pytest.mark.parametrize("a", ["x/y", "C:/path", "\\\\", "C:\\\\path", "a::b/"])
def test_x(a):
assert False
"""
),
encoding="utf-8",
)

result = pytester.runpytest("-v")

result.stdout.re_match_lines(
[
r".*test_foo.py::test_x\[x/y\] .*FAILED.*",
r".*test_foo.py::test_x\[C:/path\] .*FAILED.*",
r".*test_foo.py::test_x\[\\\\\] .*FAILED.*",
r".*test_foo.py::test_x\[C:\\\\path\] .*FAILED.*",
r".*test_foo.py::test_x\[a::b/\] .*FAILED.*",
]
)

0 comments on commit 0f10b6b

Please sign in to comment.