From 0af81b22b5289de878c6ebf93d0579dc31599539 Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Mon, 15 Mar 2021 04:55:18 -0700 Subject: [PATCH] Refs #32532 -- Replaced is_discoverable() with try_importing(). --- django/test/runner.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/django/test/runner.py b/django/test/runner.py index 65a399a709f..f1389551a65 100644 --- a/django/test/runner.py +++ b/django/test/runner.py @@ -595,7 +595,11 @@ class DiscoverRunner: if tests.countTestCases(): return tests # Try discovery if "label" is a package or directory. - if not is_discoverable(label): + is_importable, is_package = try_importing(label) + if is_importable: + if not is_package: + return tests + elif not os.path.isdir(label_as_path): return tests kwargs = discover_kwargs.copy() @@ -774,20 +778,18 @@ class DiscoverRunner: return self.suite_result(suite, result) -def is_discoverable(label): +def try_importing(label): """ - Check if a test label points to a Python package or file directory. + Try importing a test label, and return (is_importable, is_package). Relative labels like "." and ".." are seen as directories. """ try: mod = import_module(label) except (ImportError, TypeError): - pass - else: - return hasattr(mod, '__path__') + return (False, False) - return os.path.isdir(os.path.abspath(label)) + return (True, hasattr(mod, '__path__')) def find_top_level(top_level):