[Python-checkins] cpython (merge 3.3 -> default): Issue #19352: Fix unittest discovery when a module can be reached through

antoine.pitrou python-checkins at python.org
Wed Oct 23 19:16:43 CEST 2013


http://hg.python.org/cpython/rev/ebbe87204114
changeset:   86589:ebbe87204114
parent:      86586:f797a14cbcfd
parent:      86588:a830cc1c0565
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Wed Oct 23 19:15:05 2013 +0200
summary:
  Issue #19352: Fix unittest discovery when a module can be reached through several paths (e.g. under Debian/Ubuntu with virtualenv).

files:
  Lib/unittest/loader.py              |   4 +-
  Lib/unittest/test/test_discovery.py |  24 ++++++++++++++++-
  Misc/NEWS                           |   3 ++
  3 files changed, 28 insertions(+), 3 deletions(-)


diff --git a/Lib/unittest/loader.py b/Lib/unittest/loader.py
--- a/Lib/unittest/loader.py
+++ b/Lib/unittest/loader.py
@@ -276,8 +276,8 @@
                     yield _make_failed_import_test(name, self.suiteClass)
                 else:
                     mod_file = os.path.abspath(getattr(module, '__file__', full_path))
-                    realpath = _jython_aware_splitext(mod_file)
-                    fullpath_noext = _jython_aware_splitext(full_path)
+                    realpath = _jython_aware_splitext(os.path.realpath(mod_file))
+                    fullpath_noext = _jython_aware_splitext(os.path.realpath(full_path))
                     if realpath.lower() != fullpath_noext.lower():
                         module_dir = os.path.dirname(realpath)
                         mod_name = _jython_aware_splitext(os.path.basename(full_path))
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
@@ -366,7 +366,7 @@
         self.assertTrue(program.failfast)
         self.assertTrue(program.catchbreak)
 
-    def test_detect_module_clash(self):
+    def setup_module_clash(self):
         class Module(object):
             __file__ = 'bar/foo.py'
         sys.modules['foo'] = Module
@@ -393,7 +393,10 @@
         os.listdir = listdir
         os.path.isfile = isfile
         os.path.isdir = isdir
+        return full_path
 
+    def test_detect_module_clash(self):
+        full_path = self.setup_module_clash()
         loader = unittest.TestLoader()
 
         mod_dir = os.path.abspath('bar')
@@ -406,6 +409,25 @@
         )
         self.assertEqual(sys.path[0], full_path)
 
+    def test_module_symlink_ok(self):
+        full_path = self.setup_module_clash()
+
+        original_realpath = os.path.realpath
+
+        mod_dir = os.path.abspath('bar')
+        expected_dir = os.path.abspath('foo')
+
+        def cleanup():
+            os.path.realpath = original_realpath
+        self.addCleanup(cleanup)
+
+        def realpath(path):
+            if path == os.path.join(mod_dir, 'foo.py'):
+                return os.path.join(expected_dir, 'foo.py')
+            return path
+        os.path.realpath = realpath
+        loader = unittest.TestLoader()
+        loader.discover(start_dir='foo', pattern='foo.py')
 
     def test_discovery_from_dotted_path(self):
         loader = unittest.TestLoader()
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -19,6 +19,9 @@
 Library
 -------
 
+- Issue #19352: Fix unittest discovery when a module can be reached
+  through several paths (e.g. under Debian/Ubuntu with virtualenv).
+
 - Issue #15207: Fix mimetypes to read from correct part of Windows registry
   Original patch by Dave Chambers
 

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


More information about the Python-checkins mailing list