mirror of https://github.com/django/django.git
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):
|
||||
tests = self.test_loader.loadTestsFromName(label)
|
||||
elif os.path.isdir(label_as_path) and not self.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.
|
||||
|
||||
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
|
||||
top_level = find_top_level(label_as_path)
|
||||
kwargs['top_level_dir'] = top_level
|
||||
|
||||
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))
|
||||
|
||||
|
||||
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):
|
||||
"""
|
||||
Reorder an iterable of tests by test type, removing any duplicates.
|
||||
|
|
Loading…
Reference in New Issue