From 266f05c4c4ce981bfa7a1d2266380380aaf4bb72 Mon Sep 17 00:00:00 2001 From: turturica Date: Thu, 9 Aug 2018 18:28:22 -0700 Subject: [PATCH 1/3] Fix #3751 --- src/_pytest/main.py | 5 +++-- testing/test_collection.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 105891e4686..eae0bb25548 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -505,8 +505,9 @@ def _collect(self, arg): root = self._node_cache[pkginit] else: col = root._collectfile(pkginit) - if col and isinstance(col, Package): - root = col[0] + if col: + if isinstance(col[0], Package): + root = col[0] self._node_cache[root.fspath] = root # If it's a directory argument, recurse and look for any Subpackages. diff --git a/testing/test_collection.py b/testing/test_collection.py index 23d82cb141b..6480cc85d42 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -647,7 +647,7 @@ def test_pkgfile(self, testdir): col = testdir.getnode(config, x) assert isinstance(col, pytest.Module) assert col.name == "x.py" - assert col.parent.parent is None + assert col.parent.parent.parent is None for col in col.listchain(): assert col.config is config From 50db718a6a0ea09be107f6bf9e9aa1a7002a91de Mon Sep 17 00:00:00 2001 From: turturica Date: Fri, 10 Aug 2018 13:57:29 -0700 Subject: [PATCH 2/3] Add a test description. --- testing/test_collection.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/testing/test_collection.py b/testing/test_collection.py index 6480cc85d42..3b9c5df6cc4 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -638,6 +638,10 @@ def test_global_file(self, testdir, tmpdir): assert col.config is config def test_pkgfile(self, testdir): + """Verify nesting when a module is within a package. + The parent chain should match: Module -> Package -> Session. + Session's parent should always be None. + """ tmpdir = testdir.tmpdir subdir = tmpdir.join("subdir") x = subdir.ensure("x.py") From 27b5435a40384a7e5b2ba85a9916774d171c4d66 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 10 Aug 2018 18:18:07 -0300 Subject: [PATCH 3/3] Fix docs formatting and improve test a bit --- testing/test_collection.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/testing/test_collection.py b/testing/test_collection.py index 3b9c5df6cc4..5b494ba31af 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -639,9 +639,9 @@ def test_global_file(self, testdir, tmpdir): def test_pkgfile(self, testdir): """Verify nesting when a module is within a package. - The parent chain should match: Module -> Package -> Session. - Session's parent should always be None. - """ + The parent chain should match: Module -> Package -> Session. + Session's parent should always be None. + """ tmpdir = testdir.tmpdir subdir = tmpdir.join("subdir") x = subdir.ensure("x.py") @@ -649,8 +649,11 @@ def test_pkgfile(self, testdir): with subdir.as_cwd(): config = testdir.parseconfigure(x) col = testdir.getnode(config, x) - assert isinstance(col, pytest.Module) assert col.name == "x.py" + assert isinstance(col, pytest.Module) + assert isinstance(col.parent, pytest.Package) + assert isinstance(col.parent.parent, pytest.Session) + # session is batman (has no parents) assert col.parent.parent.parent is None for col in col.listchain(): assert col.config is config