]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#25320: Handle sockets in directories unittest discovery is scanning.
authorRobert Collins <rbtcollins@hp.com>
Tue, 15 Mar 2016 00:29:17 +0000 (13:29 +1300)
committerRobert Collins <rbtcollins@hp.com>
Tue, 15 Mar 2016 00:29:17 +0000 (13:29 +1300)
Patch from Victor van den Elzen.

Lib/unittest/loader.py
Lib/unittest/test/test_discovery.py
Misc/ACKS
Misc/NEWS

index c776f16c30613bd15c8d13c8bb300da49643c017..b254c8086b665b0ae81442d25b800fb1b8793b33 100644 (file)
@@ -479,6 +479,8 @@ class TestLoader(object):
                     return tests, True
                 finally:
                     self._loading_packages.discard(name)
+        else:
+            return None, False
 
 
 defaultTestLoader = TestLoader()
index 55921febf58a648876ad30c4b66d97ab8a4653fb..bb196e6997af3229213af5448f778f0cad00c4f4 100644 (file)
@@ -90,6 +90,46 @@ class TestDiscovery(unittest.TestCase):
                     ('test3', 'test4')])
         self.assertEqual(suite, expected)
 
+    def test_find_tests_socket(self):
+        # A socket is neither a directory nor a regular file.
+        # https://bugs.python.org/issue25320
+        loader = unittest.TestLoader()
+
+        original_listdir = os.listdir
+        def restore_listdir():
+            os.listdir = original_listdir
+        original_isfile = os.path.isfile
+        def restore_isfile():
+            os.path.isfile = original_isfile
+        original_isdir = os.path.isdir
+        def restore_isdir():
+            os.path.isdir = original_isdir
+
+        path_lists = [['socket']]
+        os.listdir = lambda path: path_lists.pop(0)
+        self.addCleanup(restore_listdir)
+
+        os.path.isdir = lambda path: False
+        self.addCleanup(restore_isdir)
+
+        os.path.isfile = lambda path: False
+        self.addCleanup(restore_isfile)
+
+        loader._get_module_from_name = lambda path: path + ' module'
+        orig_load_tests = loader.loadTestsFromModule
+        def loadTestsFromModule(module, pattern=None):
+            # This is where load_tests is called.
+            base = orig_load_tests(module, pattern=pattern)
+            return base + [module + ' tests']
+        loader.loadTestsFromModule = loadTestsFromModule
+        loader.suiteClass = lambda thing: thing
+
+        top_level = os.path.abspath('/foo')
+        loader._top_level_dir = top_level
+        suite = list(loader._find_tests(top_level, 'test*.py'))
+
+        self.assertEqual(suite, [])
+
     def test_find_tests_with_package(self):
         loader = unittest.TestLoader()
 
index a478d4fb16de7a7fb02378927961610153b9ba1c..85220d131941c0bc753f10c216a207dc639af29d 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -393,6 +393,7 @@ Lance Ellinghaus
 Daniel Ellis
 Phil Elson
 David Ely
+Victor van den Elzen
 Jeff Epler
 Tom Epperly
 Gökcen Eraslan
index 58ee5a478bbeaa2db9d93f4b746de913159c7ad3..803924a81c9d68b9df4d75028212f5168d971680 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -91,6 +91,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #25320: Handle sockets in directories unittest discovery is scanning.
+  Patch from Victor van den Elzen.
+
 - Issue #16181: cookiejar.http2time() now returns None if year is higher than
   datetime.MAXYEAR.