Skip to content

Fix issue with slashes being turned into backslashes on Windows #12760

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 11 commits into from
Sep 8, 2024
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 @@ -1178,8 +1178,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 1181 in src/_pytest/config/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/config/__init__.py#L1181

Added line #L1181 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 1184 in src/_pytest/config/__init__.py

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L1183 - L1184 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

src/_pytest/config/__init__.py#L1186

Added line #L1186 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