Fix #2775 - running pytest with "--pyargs" will result in Items with empty "parent.nodeid" if run from a different root directory

This commit is contained in:
Christian Boelsen 2017-09-13 17:14:24 +01:00
parent 70cdfaf661
commit 14b6380e5f
3 changed files with 14 additions and 1 deletions

View File

@ -520,8 +520,18 @@ class FSCollector(Collector):
super(FSCollector, self).__init__(name, parent, config, session)
self.fspath = fspath
def _check_initialpaths_for_relpath(self):
for initialpath in self.session._initialpaths:
parent_path = self.fspath
for _ in parent_path.parts():
if parent_path.samefile(initialpath):
return self.fspath.relto(initialpath.dirname)
parent_path = parent_path.__class__(parent_path.dirname)
def _makeid(self):
relpath = self.fspath.relto(self.config.rootdir)
if not relpath:
relpath = self._check_initialpaths_for_relpath()
if os.sep != "/":
relpath = relpath.replace(os.sep, "/")
return relpath

1
changelog/2775.bugfix Normal file
View File

@ -0,0 +1 @@
Fix the bug where running pytest with "--pyargs" will result in Items with empty "parent.nodeid" if run from a different root directory.

View File

@ -624,8 +624,10 @@ class TestInvocationVariants(object):
for p in search_path:
monkeypatch.syspath_prepend(p)
os.chdir('world')
# mixed module and filenames:
result = testdir.runpytest("--pyargs", "-v", "ns_pkg.hello", "world/ns_pkg")
result = testdir.runpytest("--pyargs", "-v", "ns_pkg.hello", "ns_pkg/world")
testdir.chdir()
assert result.ret == 0
result.stdout.fnmatch_lines([
"*test_hello.py::test_hello*PASSED",