]> granicus.if.org Git - python/commitdiff
Issue #19352: Fix unittest discovery when a module can be reached through several...
authorAntoine Pitrou <solipsis@pitrou.net>
Wed, 23 Oct 2013 17:11:29 +0000 (19:11 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Wed, 23 Oct 2013 17:11:29 +0000 (19:11 +0200)
Lib/unittest/loader.py
Lib/unittest/test/test_discovery.py
Misc/NEWS

index 2b925254231cdaa43c850568722edf25d8f6c86e..9ab26c1efb59f292ff13d4772a1f911b8090b5f8 100644 (file)
@@ -263,8 +263,8 @@ class TestLoader(object):
                     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))
index 1fdf9913c970436f089bcf6d83b335de5455970e..ccc7db249e420aedbdbc2ab0f9e4cf0eed170d56 100644 (file)
@@ -347,7 +347,7 @@ class TestDiscovery(unittest.TestCase):
         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
@@ -374,7 +374,10 @@ class TestDiscovery(unittest.TestCase):
         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')
@@ -387,6 +390,25 @@ class TestDiscovery(unittest.TestCase):
         )
         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()
index 233affa808121f8ab5d8cdacb9432b6356740704..0787328d9c238edbbdc16adeba8160721162068f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -81,6 +81,9 @@ Core and Builtins
 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