Skip to content

Commit

Permalink
Fix Module.name from full path without drive letter
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Aug 14, 2020
1 parent 10f98e1 commit eb8048a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/7628.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +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``).
1 change: 1 addition & 0 deletions src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ def __init__(
session: Optional["Session"] = None,
nodeid: Optional[str] = None,
) -> None:
fspath = py.path.local(os.path.abspath(str(fspath)))
name = fspath.basename
if parent is not None:
rel = fspath.relto(parent.fspath)
Expand Down
56 changes: 56 additions & 0 deletions testing/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1426,3 +1426,59 @@ def test_modules_not_importable_as_side_effect(self, testdir):
"* 1 failed in *",
]
)


class TestCollectionFullPathWithoutDriveLetter:
def setup_test_file(self, testdir):
testdir.makepyfile(
**{
"project/tests/dummy_test.py": """
def test(fix):
assert fix == 1
"""
}
)
fn = testdir.tmpdir.join("project/tests/dummy_test.py")
assert fn.isfile()

drive, path = os.path.splitdrive(str(fn))
return path

def setup_conftest(self, testdir):
testdir.makepyfile(
**{
"project/conftest.py": """
import pytest
@pytest.fixture
def fix(): return 1
""",
}
)

def test_module_name(self, testdir):
"""Module name resolves correctly from full path without drive letter (#7628)
Full path without drive letter makes py.path.local.relto, during Module construction,
to incorrectly report that the module path is not relative to the Session root.
"""
path = self.setup_test_file(testdir)

[item], _ = testdir.inline_genitems(path)
assert item.name == "test"
assert item.parent.name == "project/tests/dummy_test.py"

def test_module_full_path_without_drive_integration(self, testdir):
"""Collect and run test using full path except for the drive letter (#7628)
Integration test as reported to make sure we don't regress in other areas covered by
the test above.
"""
self.setup_conftest(testdir)
path = self.setup_test_file(testdir)
result = testdir.runpytest(path, "-v")
result.stdout.fnmatch_lines(
[
os.path.join("project", "tests", "dummy_test.py") + "::test PASSED *",
"* 1 passed in *",
]
)

0 comments on commit eb8048a

Please sign in to comment.