From a89e975caf69ffdbec527f9fe84824e73a7c9cac Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Wed, 24 Mar 2021 05:54:21 -0700 Subject: [PATCH] Fixed #32532 -- Made DiscoverRunner raise RuntimeError when a test label is a file path. --- django/test/runner.py | 7 +++++++ tests/test_runner/test_discover_runner.py | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/django/test/runner.py b/django/test/runner.py index f1389551a65..3c4a6cf8063 100644 --- a/django/test/runner.py +++ b/django/test/runner.py @@ -600,6 +600,13 @@ class DiscoverRunner: if not is_package: return tests elif not os.path.isdir(label_as_path): + if os.path.exists(label_as_path): + assert tests is None + raise RuntimeError( + f'One of the test labels is a path to a file: {label!r}, ' + f'which is not supported. Use a dotted module name ' + f'instead.' + ) return tests kwargs = discover_kwargs.copy() diff --git a/tests/test_runner/test_discover_runner.py b/tests/test_runner/test_discover_runner.py index 6b19f2f274f..c9105989fa3 100644 --- a/tests/test_runner/test_discover_runner.py +++ b/tests/test_runner/test_discover_runner.py @@ -47,6 +47,16 @@ class DiscoverRunnerTests(SimpleTestCase): ns = parser.parse_args(["--debug-mode"]) self.assertTrue(ns.debug_mode) + def test_load_tests_for_label_file_path(self): + with change_cwd('.'): + msg = ( + "One of the test labels is a path to a file: " + "'test_discover_runner.py', which is not supported. Use a " + "dotted module name instead." + ) + with self.assertRaisesMessage(RuntimeError, msg): + DiscoverRunner().load_tests_for_label('test_discover_runner.py', {}) + def test_dotted_test_module(self): count = DiscoverRunner().build_suite( ['test_runner_apps.sample.tests_sample'],