diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2013-10-23 17:11:29 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2013-10-23 17:11:29 (GMT) |
commit | 5d791802c925425e75370e88c7d1e68f23b7d724 (patch) | |
tree | 0bb1619573a1882f00c4b710c2ddd35edf5fd4a2 | |
parent | 17934856dc76b8cf1667a293445df6aeca225768 (diff) | |
download | cpython-5d791802c925425e75370e88c7d1e68f23b7d724.zip cpython-5d791802c925425e75370e88c7d1e68f23b7d724.tar.gz cpython-5d791802c925425e75370e88c7d1e68f23b7d724.tar.bz2 |
Issue #19352: Fix unittest discovery when a module can be reached through several paths (e.g. under Debian/Ubuntu with virtualenv).
-rw-r--r-- | Lib/unittest/loader.py | 4 | ||||
-rw-r--r-- | Lib/unittest/test/test_discovery.py | 24 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
3 files changed, 28 insertions, 3 deletions
diff --git a/Lib/unittest/loader.py b/Lib/unittest/loader.py index c6fc663..9163a1a 100644 --- a/Lib/unittest/loader.py +++ b/Lib/unittest/loader.py @@ -256,8 +256,8 @@ class TestLoader(object): yield _make_failed_import_test(name, self.suiteClass) else: mod_file = os.path.abspath(getattr(module, '__file__', full_path)) - realpath = os.path.splitext(mod_file)[0] - fullpath_noext = os.path.splitext(full_path)[0] + realpath = os.path.splitext(os.path.realpath(mod_file))[0] + fullpath_noext = os.path.splitext(os.path.realpath(full_path))[0] if realpath.lower() != fullpath_noext.lower(): module_dir = os.path.dirname(realpath) mod_name = os.path.splitext(os.path.basename(full_path))[0] diff --git a/Lib/unittest/test/test_discovery.py b/Lib/unittest/test/test_discovery.py index 8c031a0..e0277d5 100644 --- a/Lib/unittest/test/test_discovery.py +++ b/Lib/unittest/test/test_discovery.py @@ -314,7 +314,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 @@ -341,7 +341,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') @@ -354,6 +357,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() @@ -40,6 +40,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 |