diff options
| author | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-05-07 18:16:19 (GMT) |
|---|---|---|
| committer | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-05-07 18:16:19 (GMT) |
| commit | ae4dde0858bc04dc587d8b5213e1f1531a8fad03 (patch) | |
| tree | ecd735bf6c788968ffd5d16b61f6b683c44e053d /Lib/unittest/test | |
| parent | 3843cd8e86673ce8969c6e948cd15735168882d3 (diff) | |
| download | cpython-ae4dde0858bc04dc587d8b5213e1f1531a8fad03.zip cpython-ae4dde0858bc04dc587d8b5213e1f1531a8fad03.tar.gz cpython-ae4dde0858bc04dc587d8b5213e1f1531a8fad03.tar.bz2 | |
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 | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/Lib/unittest/test/test_discovery.py b/Lib/unittest/test/test_discovery.py index de60968..e6f8910 100644 --- a/Lib/unittest/test/test_discovery.py +++ b/Lib/unittest/test/test_discovery.py @@ -298,6 +298,45 @@ 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 = (r"^'foo' module incorrectly imported from %r\. Expected %r\. " + "Is this module globally installed\?$") % (mod_dir, expected_dir) + self.assertRaisesRegexp( + ImportError, msg, loader.discover, + start_dir='foo', pattern='foo.py' + ) + self.assertEqual(sys.path[0], full_path) if __name__ == '__main__': unittest.main() |
