mirror of
https://github.com/django/django.git
synced 2025-03-31 19:46:42 +00:00
Refs #32540 -- Added django.test.runner.find_top_level().
This commit is contained in:
parent
50ef9dfac2
commit
d5a214c7c4
@ -574,28 +574,7 @@ class DiscoverRunner:
|
|||||||
if not os.path.exists(label_as_path):
|
if not os.path.exists(label_as_path):
|
||||||
tests = self.test_loader.loadTestsFromName(label)
|
tests = self.test_loader.loadTestsFromName(label)
|
||||||
elif os.path.isdir(label_as_path) and not self.top_level:
|
elif os.path.isdir(label_as_path) and not self.top_level:
|
||||||
# Try to be a bit smarter than unittest about finding the
|
top_level = find_top_level(label_as_path)
|
||||||
# default top-level for a given directory path, to avoid
|
|
||||||
# breaking relative imports. (Unittest's default is to set
|
|
||||||
# top-level equal to the path, which means relative imports
|
|
||||||
# will result in "Attempted relative import in non-package.").
|
|
||||||
|
|
||||||
# We'd be happy to skip this and require dotted module paths
|
|
||||||
# (which don't cause this problem) instead of file paths (which
|
|
||||||
# do), but in the case of a directory in the cwd, which would
|
|
||||||
# be equally valid if considered as a top-level module or as a
|
|
||||||
# directory path, unittest unfortunately prefers the latter.
|
|
||||||
|
|
||||||
top_level = label_as_path
|
|
||||||
while True:
|
|
||||||
init_py = os.path.join(top_level, '__init__.py')
|
|
||||||
if not os.path.exists(init_py):
|
|
||||||
break
|
|
||||||
try_next = os.path.dirname(top_level)
|
|
||||||
if try_next == top_level:
|
|
||||||
# __init__.py all the way down? give up.
|
|
||||||
break
|
|
||||||
top_level = try_next
|
|
||||||
kwargs['top_level_dir'] = top_level
|
kwargs['top_level_dir'] = top_level
|
||||||
|
|
||||||
if not (tests and tests.countTestCases()) and is_discoverable(label):
|
if not (tests and tests.countTestCases()) and is_discoverable(label):
|
||||||
@ -764,6 +743,30 @@ def is_discoverable(label):
|
|||||||
return os.path.isdir(os.path.abspath(label))
|
return os.path.isdir(os.path.abspath(label))
|
||||||
|
|
||||||
|
|
||||||
|
def find_top_level(top_level):
|
||||||
|
# Try to be a bit smarter than unittest about finding the default top-level
|
||||||
|
# for a given directory path, to avoid breaking relative imports.
|
||||||
|
# (Unittest's default is to set top-level equal to the path, which means
|
||||||
|
# relative imports will result in "Attempted relative import in
|
||||||
|
# non-package.").
|
||||||
|
|
||||||
|
# We'd be happy to skip this and require dotted module paths (which don't
|
||||||
|
# cause this problem) instead of file paths (which do), but in the case of
|
||||||
|
# a directory in the cwd, which would be equally valid if considered as a
|
||||||
|
# top-level module or as a directory path, unittest unfortunately prefers
|
||||||
|
# the latter.
|
||||||
|
while True:
|
||||||
|
init_py = os.path.join(top_level, '__init__.py')
|
||||||
|
if not os.path.exists(init_py):
|
||||||
|
break
|
||||||
|
try_next = os.path.dirname(top_level)
|
||||||
|
if try_next == top_level:
|
||||||
|
# __init__.py all the way down? give up.
|
||||||
|
break
|
||||||
|
top_level = try_next
|
||||||
|
return top_level
|
||||||
|
|
||||||
|
|
||||||
def reorder_tests(tests, classes, reverse=False):
|
def reorder_tests(tests, classes, reverse=False):
|
||||||
"""
|
"""
|
||||||
Reorder an iterable of tests by test type, removing any duplicates.
|
Reorder an iterable of tests by test type, removing any duplicates.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user