Skip to content

Commit eb8048a

Browse files
committed
Fix Module.name from full path without drive letter
Fix #7628
1 parent 10f98e1 commit eb8048a

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

changelog/7628.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix test collection when a full path without a drive letter was passed to pytest on Windows (for example ``\projects\tests\test.py`` instead of ``c:\projects\tests\pytest.py``).

src/_pytest/nodes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ def __init__(
515515
session: Optional["Session"] = None,
516516
nodeid: Optional[str] = None,
517517
) -> None:
518+
fspath = py.path.local(os.path.abspath(str(fspath)))
518519
name = fspath.basename
519520
if parent is not None:
520521
rel = fspath.relto(parent.fspath)

testing/test_collection.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,3 +1426,59 @@ def test_modules_not_importable_as_side_effect(self, testdir):
14261426
"* 1 failed in *",
14271427
]
14281428
)
1429+
1430+
1431+
class TestCollectionFullPathWithoutDriveLetter:
1432+
def setup_test_file(self, testdir):
1433+
testdir.makepyfile(
1434+
**{
1435+
"project/tests/dummy_test.py": """
1436+
def test(fix):
1437+
assert fix == 1
1438+
"""
1439+
}
1440+
)
1441+
fn = testdir.tmpdir.join("project/tests/dummy_test.py")
1442+
assert fn.isfile()
1443+
1444+
drive, path = os.path.splitdrive(str(fn))
1445+
return path
1446+
1447+
def setup_conftest(self, testdir):
1448+
testdir.makepyfile(
1449+
**{
1450+
"project/conftest.py": """
1451+
import pytest
1452+
@pytest.fixture
1453+
def fix(): return 1
1454+
""",
1455+
}
1456+
)
1457+
1458+
def test_module_name(self, testdir):
1459+
"""Module name resolves correctly from full path without drive letter (#7628)
1460+
1461+
Full path without drive letter makes py.path.local.relto, during Module construction,
1462+
to incorrectly report that the module path is not relative to the Session root.
1463+
"""
1464+
path = self.setup_test_file(testdir)
1465+
1466+
[item], _ = testdir.inline_genitems(path)
1467+
assert item.name == "test"
1468+
assert item.parent.name == "project/tests/dummy_test.py"
1469+
1470+
def test_module_full_path_without_drive_integration(self, testdir):
1471+
"""Collect and run test using full path except for the drive letter (#7628)
1472+
1473+
Integration test as reported to make sure we don't regress in other areas covered by
1474+
the test above.
1475+
"""
1476+
self.setup_conftest(testdir)
1477+
path = self.setup_test_file(testdir)
1478+
result = testdir.runpytest(path, "-v")
1479+
result.stdout.fnmatch_lines(
1480+
[
1481+
os.path.join("project", "tests", "dummy_test.py") + "::test PASSED *",
1482+
"* 1 passed in *",
1483+
]
1484+
)

0 commit comments

Comments
 (0)