Skip to content

Commit e2da328

Browse files
louisomabadger
authored andcommitted
bpo-24263: Fix unittest can not load unicode pattern test
1 parent 6751a4a commit e2da328

2 files changed

Lines changed: 60 additions & 3 deletions

File tree

Lib/test/test_unittest/test_discovery.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,57 @@ def loadTestsFromModule(module, pattern=None):
9292
('test3', 'test4')])
9393
self.assertEqual(suite, expected)
9494

95+
def test_find_tests_with_unicode(self):
96+
loader = unittest.TestLoader()
97+
98+
original_listdir = os.listdir
99+
def restore_listdir():
100+
os.listdir = original_listdir
101+
original_isfile = os.path.isfile
102+
def restore_isfile():
103+
os.path.isfile = original_isfile
104+
original_isdir = os.path.isdir
105+
def restore_isdir():
106+
os.path.isdir = original_isdir
107+
108+
path_lists = [['測試2.py', '測試1.py', '不是測試.py', '測試_資料夾',
109+
'測試.付歐歐', '測試-不是-一個-模組.py', '另外的_資料夾'],
110+
['測試4.py', '測試3.py', ]]
111+
os.listdir = lambda path: path_lists.pop(0)
112+
self.addCleanup(restore_listdir)
113+
114+
def isdir(path):
115+
return path.endswith('資料夾')
116+
os.path.isdir = isdir
117+
self.addCleanup(restore_isdir)
118+
119+
def isfile(path):
120+
# another_dir is not a package and so shouldn't be recursed into
121+
return not path.endswith('資料夾') and not '另外的_資料夾' in path
122+
os.path.isfile = isfile
123+
self.addCleanup(restore_isfile)
124+
125+
loader._get_module_from_name = lambda path: path + ' module'
126+
orig_load_tests = loader.loadTestsFromModule
127+
def loadTestsFromModule(module, pattern=None):
128+
# This is where load_tests is called.
129+
base = orig_load_tests(module, pattern=pattern)
130+
return base + [module + ' tests']
131+
loader.loadTestsFromModule = loadTestsFromModule
132+
loader.suiteClass = lambda thing: thing
133+
134+
top_level = os.path.abspath('/foo')
135+
loader._top_level_dir = top_level
136+
suite = list(loader._find_tests(top_level, '測試*.py'))
137+
138+
# The test suites found should be sorted alphabetically for reliable
139+
# execution order.
140+
expected = [[name + ' module tests'] for name in
141+
('測試1', '測試2', '測試_資料夾')]
142+
expected.extend([[('測試_資料夾.%s' % name) + ' module tests'] for name in
143+
('測試3', '測試4')])
144+
self.assertEqual(suite, expected)
145+
95146
def test_find_tests_socket(self):
96147
# A socket is neither a directory nor a regular file.
97148
# https://bugs.python.org/issue25320

Lib/unittest/loader.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,15 @@ def _find_test_path(self, full_path, pattern):
371371
"""
372372
basename = os.path.basename(full_path)
373373
if os.path.isfile(full_path):
374-
if not VALID_MODULE_NAME.match(basename):
375-
# valid Python identifiers only
376-
return None, False
374+
root, ext = os.path.splitext(basename)
375+
try:
376+
if not (ext == '.py' and root.isidentifier()):
377+
# valid Python identifiers only
378+
return None, False
379+
except AttributeError:
380+
if not VALID_MODULE_NAME.match(basename):
381+
# valid Python identifiers only
382+
return None, False
377383
if not self._match_path(basename, full_path, pattern):
378384
return None, False
379385
# if the test file matches, load it

0 commit comments

Comments
 (0)