diff --git a/django/test/runner.py b/django/test/runner.py index c11681fde8..b63ca6adb8 100644 --- a/django/test/runner.py +++ b/django/test/runner.py @@ -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.