Commits

holger krekel  committed 2a855c3

fix issue 109 - sibling conftest.py files shall not be loaded.
also simplify / refine tests a bit.

  • Participants
  • Parent commits 866bbb5

Comments (0)

Files changed (5)

 Changes between 1.3.4 and 1.4.0.dev0
 ==================================================
 
+- fix issue109 - sibling conftest.py files will not be loaded.
+  (and Directory collectors cannot be customized anymore from a Directory's
+  conftest.py - this needs to happen at least one level up).
 - introduce (customizable) assertion failure representations (Floris Bruynooghe)
 - nose-plugin: pass through type-signature failures in setup/teardown
   functions instead of not calling them (Ed Singleton)

File py/_plugin/pytest_default.py

     return True
 
 def pytest_ignore_collect(path, config):
-    ignore_paths = config.getconftest_pathlist("collect_ignore", path=path)
+    p = path.dirpath()
+    ignore_paths = config.getconftest_pathlist("collect_ignore", path=p)
     ignore_paths = ignore_paths or []
     excludeopt = config.getvalue("ignore")
     if excludeopt:
     return path in ignore_paths
 
 def pytest_collect_directory(path, parent):
-    # XXX reconsider the following comment
-    # not use parent.Directory here as we generally
-    # want dir/conftest.py to be able to
-    # define Directory(dir) already
     if not parent.recfilter(path): # by default special ".cvs", ...
         # check if cmdline specified this dir or a subdir directly
         for arg in parent.collection._argfspaths:
                 break
         else:
             return
-    Directory = parent.config._getcollectclass('Directory', path)
-    return Directory(path, parent=parent)
+    return parent.Directory(path, parent=parent)
 
 def pytest_report_iteminfo(item):
     return item.reportinfo()

File py/_test/conftesthandle.py

                 self._path2confmods[None] = self.getconftestmodules(anchor)
                 # let's also consider test* dirs
                 if anchor.check(dir=1):
-                    for x in anchor.listdir(lambda x: x.check(dir=1, dotfile=0)):
-                        self.getconftestmodules(x)
+                    for x in anchor.listdir("test*"):
+                        if x.check(dir=1):
+                            self.getconftestmodules(x)
                 break
         else:
             assert 0, "no root of filesystem?"

File testing/acceptance_test.py

         result = testdir.runpython(p, prepend=False)
         assert not result.ret
 
-    @py.test.mark.xfail(reason="http://bitbucket.org/hpk42/py-trunk/issue/109")
-    def test_sibling_conftest_issue109(self, testdir):
-        """
-        This test is to make sure that the conftest.py of sibling directories is not loaded
-        if py.test is run for/in one of the siblings directory and those sibling directories
-        are not packaged together with an __init__.py. See bitbucket issue #109.
-        """
-        for dirname in ['a', 'b']:
-            testdir.tmpdir.ensure(dirname, dir=True)
-            testdir.tmpdir.ensure(dirname, '__init__.py')
+    def test_issue109_sibling_conftests_not_loaded(self, testdir):
+        sub1 = testdir.tmpdir.mkdir("sub1")
+        sub2 = testdir.tmpdir.mkdir("sub2")
+        sub1.join("conftest.py").write("assert 0")
+        result = testdir.runpytest(sub2)
+        assert result.ret == 0
+        sub2.ensure("__init__.py")
+        p = sub2.ensure("test_hello.py")
+        result = testdir.runpytest(p)
+        assert result.ret == 0
+        result = testdir.runpytest(sub1)
+        assert result.ret != 0
 
-            # To create the conftest.py I would like to use testdir.make*-methods
-            # but as far as I have seen they can only create files in testdir.tempdir
-            # Maybe there is a way to explicitly specifiy the directory on which those
-            # methods work or a completely better way to do that?
-            backupTmpDir = testdir.tmpdir
-            testdir.tmpdir = testdir.tmpdir.join(dirname)
-            testdir.makeconftest("""
-                _DIR_NAME = '%s'
-                def pytest_configure(config):
-                    if config.args and config.args[0] != _DIR_NAME:
-                        raise Exception("py.test run for '" + config.args[0] + "', but '" + _DIR_NAME + "/conftest.py' loaded.")
-            """ % dirname)
-            testdir.tmpdir = backupTmpDir
-
-        for dirname, other_dirname in [('a', 'b'), ('b', 'a')]:
-            result = testdir.runpytest(dirname)
-            assert result.ret == 0, "test_sibling_conftest: py.test run for '%s', but '%s/conftest.py' loaded." % (dirname, other_dirname)
+    def test_directory_skipped(self, testdir):
+        testdir.makeconftest("""
+            import py
+            def pytest_ignore_collect():
+                py.test.skip("intentional")
+        """)
+        testdir.makepyfile("def test_hello(): pass")
+        result = testdir.runpytest()
+        assert result.ret == 0
+        result.stdout.fnmatch_lines([
+            "*1 skipped*"
+        ])
 
     def test_multiple_items_per_collector_byid(self, testdir):
         c = testdir.makeconftest("""

File testing/test_conftesthandle.py

         conftest = Conftest(onimport=l.append)
         conftest.setinitial([basedir.join("adir"),
             '--confcutdir=%s' % basedir])
-        assert len(l) == 2
+        assert len(l) == 1
         assert conftest.rget("a") == 1
         assert conftest.rget("b", basedir.join("adir", "b")) == 2
         assert len(l) == 2
     subconftest = sub.ensure("conftest.py")
     conftest = Conftest()
     conftest.setinitial([sub.dirpath(), '--confcutdir=%s' % testdir.tmpdir])
-    if name != ".dotdir":
+    if name not in ('whatever', '.dotdir'):
         assert  subconftest in conftest._conftestpath2mod
         assert len(conftest._conftestpath2mod) == 1
     else: