summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2021-03-12 03:22:57 -0800
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-03-15 10:15:21 +0100
commitd5a214c7c4575773e0f1b8b99f6018bcb7adb8cd (patch)
tree8121e85d32c1f9de4cca50c5e52b1e47dd8d6fa2
parent50ef9dfac21499ff6c46c2f6a33103e40b60cd4e (diff)
Refs #32540 -- Added django.test.runner.find_top_level().
-rw-r--r--django/test/runner.py47
1 files changed, 25 insertions, 22 deletions
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.