diff options
author | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-05-07 23:42:40 (GMT) |
---|---|---|
committer | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-05-07 23:42:40 (GMT) |
commit | 3b2494f38c17ad9d673a1b8cac8d4143ba06e3df (patch) | |
tree | 332153c9f4cb64630cde3b29c61ca66c750b3a75 /Lib/unittest/test | |
parent | 135d87c23cd7e486d8b85adc0488f4fef1dbdc60 (diff) | |
download | cpython-3b2494f38c17ad9d673a1b8cac8d4143ba06e3df.zip cpython-3b2494f38c17ad9d673a1b8cac8d4143ba06e3df.tar.gz cpython-3b2494f38c17ad9d673a1b8cac8d4143ba06e3df.tar.bz2 |
Merged revisions 80946 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r80946 | michael.foord | 2010-05-08 01:39:38 +0200 (Sat, 08 May 2010) | 1 line
Issue 8547 - detecting and reporting that modules have been imported from the wrong location under test discovery.
........
Diffstat (limited to 'Lib/unittest/test')
-rw-r--r-- | Lib/unittest/test/test_discovery.py | 46 |
1 files changed, 44 insertions, 2 deletions
diff --git a/Lib/unittest/test/test_discovery.py b/Lib/unittest/test/test_discovery.py index 1b0f08b..ad86a42 100644 --- a/Lib/unittest/test/test_discovery.py +++ b/Lib/unittest/test/test_discovery.py @@ -1,4 +1,5 @@ import os +import re import sys import unittest @@ -53,8 +54,9 @@ class TestDiscovery(unittest.TestCase): loader._get_module_from_name = lambda path: path + ' module' loader.loadTestsFromModule = lambda module: module + ' tests' - loader._top_level_dir = '/foo' - suite = list(loader._find_tests('/foo', 'test*.py')) + top_level = os.path.abspath('/foo') + loader._top_level_dir = top_level + suite = list(loader._find_tests(top_level, 'test*.py')) expected = [name + ' module tests' for name in ('test1', 'test2')] @@ -294,6 +296,46 @@ class TestDiscovery(unittest.TestCase): self.assertTrue(program.failfast) self.assertTrue(program.catchbreak) + def test_detect_module_clash(self): + class Module(object): + __file__ = 'bar/foo.py' + sys.modules['foo'] = Module + full_path = os.path.abspath('foo') + original_listdir = os.listdir + original_isfile = os.path.isfile + original_isdir = os.path.isdir + + def cleanup(): + os.listdir = original_listdir + os.path.isfile = original_isfile + os.path.isdir = original_isdir + del sys.modules['foo'] + if full_path in sys.path: + sys.path.remove(full_path) + self.addCleanup(cleanup) + + def listdir(_): + return ['foo.py'] + def isfile(_): + return True + def isdir(_): + return True + os.listdir = listdir + os.path.isfile = isfile + os.path.isdir = isdir + + loader = unittest.TestLoader() + + mod_dir = os.path.abspath('bar') + expected_dir = os.path.abspath('foo') + msg = re.escape(r"'foo' module incorrectly imported from %r. Expected %r. " + "Is this module globally installed?" % (mod_dir, expected_dir)) + self.assertRaisesRegexp( + ImportError, '^%s$' % msg, loader.discover, + start_dir='foo', pattern='foo.py' + ) + self.assertEqual(sys.path[0], full_path) + if __name__ == '__main__': unittest.main() |