Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6c6b3f7

Browse files
committed
Issue #19352: Fix unittest discovery when a module can be reached through several paths (e.g. under Debian/Ubuntu with virtualenv).
2 parents 3d3e1ba + d5d0bc3 commit 6c6b3f7

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

Lib/unittest/loader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ def _find_tests(self, start_dir, pattern):
276276
yield _make_failed_import_test(name, self.suiteClass)
277277
else:
278278
mod_file = os.path.abspath(getattr(module, '__file__', full_path))
279-
realpath = _jython_aware_splitext(mod_file)
280-
fullpath_noext = _jython_aware_splitext(full_path)
279+
realpath = _jython_aware_splitext(os.path.realpath(mod_file))
280+
fullpath_noext = _jython_aware_splitext(os.path.realpath(full_path))
281281
if realpath.lower() != fullpath_noext.lower():
282282
module_dir = os.path.dirname(realpath)
283283
mod_name = _jython_aware_splitext(os.path.basename(full_path))

Lib/unittest/test/test_discovery.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ def discover(self, start_dir, pattern, top_level_dir):
366366
self.assertTrue(program.failfast)
367367
self.assertTrue(program.catchbreak)
368368

369-
def test_detect_module_clash(self):
369+
def setup_module_clash(self):
370370
class Module(object):
371371
__file__ = 'bar/foo.py'
372372
sys.modules['foo'] = Module
@@ -393,7 +393,10 @@ def isdir(_):
393393
os.listdir = listdir
394394
os.path.isfile = isfile
395395
os.path.isdir = isdir
396+
return full_path
396397

398+
def test_detect_module_clash(self):
399+
full_path = self.setup_module_clash()
397400
loader = unittest.TestLoader()
398401

399402
mod_dir = os.path.abspath('bar')
@@ -406,6 +409,25 @@ def isdir(_):
406409
)
407410
self.assertEqual(sys.path[0], full_path)
408411

412+
def test_module_symlink_ok(self):
413+
full_path = self.setup_module_clash()
414+
415+
original_realpath = os.path.realpath
416+
417+
mod_dir = os.path.abspath('bar')
418+
expected_dir = os.path.abspath('foo')
419+
420+
def cleanup():
421+
os.path.realpath = original_realpath
422+
self.addCleanup(cleanup)
423+
424+
def realpath(path):
425+
if path == os.path.join(mod_dir, 'foo.py'):
426+
return os.path.join(expected_dir, 'foo.py')
427+
return path
428+
os.path.realpath = realpath
429+
loader = unittest.TestLoader()
430+
loader.discover(start_dir='foo', pattern='foo.py')
409431

410432
def test_discovery_from_dotted_path(self):
411433
loader = unittest.TestLoader()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Core and Builtins
1919
Library
2020
-------
2121

22+
- Issue #19352: Fix unittest discovery when a module can be reached
23+
through several paths (e.g. under Debian/Ubuntu with virtualenv).
24+
2225
- Issue #15207: Fix mimetypes to read from correct part of Windows registry
2326
Original patch by Dave Chambers
2427

0 commit comments

Comments
 (0)