Skip to content

Commit 0f10b6b

Browse files
Fix issue with slashes being turned into backslashes on Windows (#12760) (#12787)
Fix #12745 (cherry picked from commit d35b802) Co-authored-by: Nauman Ahmed <[email protected]>
1 parent 300d13d commit 0f10b6b

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ mrbean-bremen
300300
Nathan Goldbaum
301301
Nathaniel Compton
302302
Nathaniel Waisbrot
303+
Nauman Ahmed
303304
Ned Batchelder
304305
Neil Martin
305306
Neven Mundar

changelog/12745.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed an issue with backslashes being incorrectly converted in nodeid paths on Windows, ensuring consistent path handling across environments.

src/_pytest/config/__init__.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1179,8 +1179,12 @@ def notify_exception(
11791179
def cwd_relative_nodeid(self, nodeid: str) -> str:
11801180
# nodeid's are relative to the rootpath, compute relative to cwd.
11811181
if self.invocation_params.dir != self.rootpath:
1182-
fullpath = self.rootpath / nodeid
1183-
nodeid = bestrelpath(self.invocation_params.dir, fullpath)
1182+
base_path_part, *nodeid_part = nodeid.split("::")
1183+
# Only process path part
1184+
fullpath = self.rootpath / base_path_part
1185+
relative_path = bestrelpath(self.invocation_params.dir, fullpath)
1186+
1187+
nodeid = "::".join([relative_path, *nodeid_part])
11841188
return nodeid
11851189

11861190
@classmethod

testing/test_terminal.py

+35
Original file line numberDiff line numberDiff line change
@@ -3067,3 +3067,38 @@ def test_pass():
30673067
"*= 1 xpassed in * =*",
30683068
]
30693069
)
3070+
3071+
3072+
class TestNodeIDHandling:
3073+
def test_nodeid_handling_windows_paths(self, pytester: Pytester, tmp_path) -> None:
3074+
"""Test the correct handling of Windows-style paths with backslashes."""
3075+
pytester.makeini("[pytest]") # Change `config.rootpath`
3076+
3077+
test_path = pytester.path / "tests" / "test_foo.py"
3078+
test_path.parent.mkdir()
3079+
os.chdir(test_path.parent) # Change `config.invocation_params.dir`
3080+
3081+
test_path.write_text(
3082+
textwrap.dedent(
3083+
"""
3084+
import pytest
3085+
3086+
@pytest.mark.parametrize("a", ["x/y", "C:/path", "\\\\", "C:\\\\path", "a::b/"])
3087+
def test_x(a):
3088+
assert False
3089+
"""
3090+
),
3091+
encoding="utf-8",
3092+
)
3093+
3094+
result = pytester.runpytest("-v")
3095+
3096+
result.stdout.re_match_lines(
3097+
[
3098+
r".*test_foo.py::test_x\[x/y\] .*FAILED.*",
3099+
r".*test_foo.py::test_x\[C:/path\] .*FAILED.*",
3100+
r".*test_foo.py::test_x\[\\\\\] .*FAILED.*",
3101+
r".*test_foo.py::test_x\[C:\\\\path\] .*FAILED.*",
3102+
r".*test_foo.py::test_x\[a::b/\] .*FAILED.*",
3103+
]
3104+
)

0 commit comments

Comments
 (0)