Skip to content
New issue

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

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

Already on GitHub? # to your account

[PR #12760/d35b8028 backport][8.3.x] Fix issue with slashes being turned into backslashes on Windows #12787

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 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 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("::")

Check warning on line 1182 in src/_pytest/config/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/config/__init__.py#L1182

Added line #L1182 was not covered by tests
# Only process path part
fullpath = self.rootpath / base_path_part
relative_path = bestrelpath(self.invocation_params.dir, fullpath)

Check warning on line 1185 in src/_pytest/config/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/config/__init__.py#L1184-L1185

Added lines #L1184 - L1185 were not covered by tests

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

Check warning on line 1187 in src/_pytest/config/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/config/__init__.py#L1187

Added line #L1187 was not covered by tests
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.*",
]
)
Loading