-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-68451: Fix unittest discovery to support Unicode module names #144853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
3ac7310
bf3dd7a
2d951b0
f6c809c
b21f80e
d0a6428
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -919,6 +919,107 @@ def _import(packagename, *args, **kwargs): | |||||
| 'don\'t know how to discover from {!r}' | ||||||
| .format(package)) | ||||||
|
|
||||||
| def test_valid_module_name(self): | ||||||
| # gh-68451: _valid_module_name should accept Unicode module names | ||||||
| from unittest.loader import _valid_module_name | ||||||
|
|
||||||
| # Valid ASCII module names | ||||||
| self.assertTrue(_valid_module_name('test_foo.py')) | ||||||
| self.assertTrue(_valid_module_name('Test_foo.py')) | ||||||
| self.assertTrue(_valid_module_name('_test.py')) | ||||||
| self.assertTrue(_valid_module_name('a.py')) | ||||||
|
|
||||||
| # Valid Unicode module names (gh-68451) | ||||||
| self.assertTrue(_valid_module_name('café.py')) | ||||||
| self.assertTrue(_valid_module_name('tëst_foo.py')) | ||||||
| self.assertTrue(_valid_module_name('測試.py')) | ||||||
| self.assertTrue(_valid_module_name('テスト.py')) | ||||||
|
|
||||||
| # Invalid module names | ||||||
| self.assertFalse(_valid_module_name('123.py')) | ||||||
| self.assertFalse(_valid_module_name('test-foo.py')) | ||||||
| self.assertFalse(_valid_module_name('test.foo')) | ||||||
| self.assertFalse(_valid_module_name('test')) | ||||||
| self.assertFalse(_valid_module_name('.py')) | ||||||
|
|
||||||
| def test_find_tests_with_unicode_modules(self): | ||||||
| # gh-68451: test discovery should find modules with Unicode names | ||||||
| loader = unittest.TestLoader() | ||||||
|
|
||||||
| original_listdir = os.listdir | ||||||
| original_isfile = os.path.isfile | ||||||
| original_isdir = os.path.isdir | ||||||
|
|
||||||
| path_lists = [['test2.py', 'test1.py', '測試1.py', 'tëst_três.py', | ||||||
| 'not_a_test.py', 'test_dir', 'test.foo', | ||||||
| 'test-not-a-module.py', '123bad.py', 'another_dir'], | ||||||
| ['test3.py']] | ||||||
| os.listdir = lambda path: path_lists.pop(0) | ||||||
| self.addCleanup(setattr, os, 'listdir', original_listdir) | ||||||
|
|
||||||
| def isdir(path): | ||||||
| return path.endswith('dir') | ||||||
| os.path.isdir = isdir | ||||||
| self.addCleanup(setattr, os.path, 'isdir', original_isdir) | ||||||
|
|
||||||
| def isfile(path): | ||||||
| return not path.endswith('dir') and 'another_dir' not in path | ||||||
| os.path.isfile = isfile | ||||||
| self.addCleanup(setattr, os.path, 'isfile', original_isfile) | ||||||
|
|
||||||
| loader._get_module_from_name = lambda path: path + ' module' | ||||||
| orig_load_tests = loader.loadTestsFromModule | ||||||
| def loadTestsFromModule(module, pattern=None): | ||||||
| base = orig_load_tests(module, pattern=pattern) | ||||||
| return base + [module + ' tests'] | ||||||
| loader.loadTestsFromModule = loadTestsFromModule | ||||||
| loader.suiteClass = lambda thing: thing | ||||||
|
|
||||||
| top_level = os.path.abspath('/foo') | ||||||
| loader._top_level_dir = top_level | ||||||
| suite = list(loader._find_tests(top_level, '*.py')) | ||||||
|
|
||||||
| # Unicode modules should be discovered alongside ASCII ones. | ||||||
| # test-not-a-module.py and 123bad.py should be excluded; | ||||||
| # test.foo should be excluded (wrong extension). | ||||||
| # Sorted by Unicode code points: test_dir (and its children) come | ||||||
| # before tëst_três since '_' (U+005F) < 'ë' (U+00EB). | ||||||
|
||||||
| # before tëst_três since '_' (U+005F) < 'ë' (U+00EB). | |
| # before tëst_três since 'e' (U+0065) < 'ë' (U+00EB). |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| :mod:`unittest` test discovery now correctly finds test modules whose names | ||
| start with non-ASCII Unicode letters (e.g., ``café.py``, ``測試.py``). | ||
| Previously, discovery only accepted module names starting with ASCII letters | ||
| or underscores. |
Uh oh!
There was an error while loading. Please reload this page.