From 14b6380e5f8c2e26aa518de8a499978eb9601848 Mon Sep 17 00:00:00 2001 From: Christian Boelsen Date: Wed, 13 Sep 2017 17:14:24 +0100 Subject: [PATCH 1/2] Fix #2775 - running pytest with "--pyargs" will result in Items with empty "parent.nodeid" if run from a different root directory --- _pytest/main.py | 10 ++++++++++ changelog/2775.bugfix | 1 + testing/acceptance_test.py | 4 +++- 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 changelog/2775.bugfix diff --git a/_pytest/main.py b/_pytest/main.py index 4bddf1e2d..6203f15c5 100644 --- a/_pytest/main.py +++ b/_pytest/main.py @@ -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 diff --git a/changelog/2775.bugfix b/changelog/2775.bugfix new file mode 100644 index 000000000..8123522ac --- /dev/null +++ b/changelog/2775.bugfix @@ -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. diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 712776906..8a8c32762 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -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", From 794d4585d3b0e3c892ee97142e5c1d392928d372 Mon Sep 17 00:00:00 2001 From: Christian Boelsen Date: Thu, 28 Sep 2017 20:53:50 +0100 Subject: [PATCH 2/2] Remove unnecessary complexity in _check_initialpaths_for_relpath(). --- _pytest/main.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/_pytest/main.py b/_pytest/main.py index 6203f15c5..472cf77b1 100644 --- a/_pytest/main.py +++ b/_pytest/main.py @@ -522,11 +522,8 @@ class FSCollector(Collector): 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) + if self.fspath.common(initialpath) == initialpath: + return self.fspath.relto(initialpath.dirname) def _makeid(self): relpath = self.fspath.relto(self.config.rootdir)