[Python-checkins] cpython (3.5): #25320: Handle sockets in directories unittest discovery is scanning.

robert.collins python-checkins at python.org
Mon Mar 14 20:32:28 EDT 2016


https://hg.python.org/cpython/rev/efc9836e0c83
changeset:   100539:efc9836e0c83
branch:      3.5
parent:      100534:496e419860de
user:        Robert Collins <rbtcollins at hp.com>
date:        Tue Mar 15 13:29:17 2016 +1300
summary:
  #25320: Handle sockets in directories unittest discovery is scanning.

Patch from Victor van den Elzen.

files:
  Lib/unittest/loader.py              |   2 +
  Lib/unittest/test/test_discovery.py |  40 +++++++++++++++++
  Misc/ACKS                           |   1 +
  Misc/NEWS                           |   3 +
  4 files changed, 46 insertions(+), 0 deletions(-)


diff --git a/Lib/unittest/loader.py b/Lib/unittest/loader.py
--- a/Lib/unittest/loader.py
+++ b/Lib/unittest/loader.py
@@ -479,6 +479,8 @@
                     return tests, True
                 finally:
                     self._loading_packages.discard(name)
+        else:
+            return None, False
 
 
 defaultTestLoader = TestLoader()
diff --git a/Lib/unittest/test/test_discovery.py b/Lib/unittest/test/test_discovery.py
--- a/Lib/unittest/test/test_discovery.py
+++ b/Lib/unittest/test/test_discovery.py
@@ -90,6 +90,46 @@
                     ('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()
 
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -393,6 +393,7 @@
 Daniel Ellis
 Phil Elson
 David Ely
+Victor van den Elzen
 Jeff Epler
 Tom Epperly
 Gökcen Eraslan
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -91,6 +91,9 @@
 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.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list