From 5b2b9697b893dd952ce30a8fa489db681c8941af Mon Sep 17 00:00:00 2001 From: veldakarimi Date: Tue, 23 Jan 2024 14:01:18 +0300 Subject: [PATCH 01/25] changing the default position of the toolbar to the top half instead of the top --- debug_toolbar/static/debug_toolbar/css/toolbar.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debug_toolbar/static/debug_toolbar/css/toolbar.css b/debug_toolbar/static/debug_toolbar/css/toolbar.css index a35286a1f..6d6b7b6f7 100644 --- a/debug_toolbar/static/debug_toolbar/css/toolbar.css +++ b/debug_toolbar/static/debug_toolbar/css/toolbar.css @@ -220,7 +220,7 @@ background-color: #fff; border: 1px solid #111; border-bottom: 0; - top: 0; + top: 268px; right: 0; z-index: 100000000; opacity: 0.75; From 2edf71e5fd725fd72470dd03037213ccea73106c Mon Sep 17 00:00:00 2001 From: veldakarimi Date: Tue, 23 Jan 2024 14:19:22 +0300 Subject: [PATCH 02/25] updated the change to the changes.rst file --- docs/changes.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/changes.rst b/docs/changes.rst index 15c380ddd..82185d756 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -17,6 +17,8 @@ Pending ``django.contrib.admindocs.utils.get_view_name``. * Switched from black to the `ruff formatter `__. +* Changed the default position of the toolbar from top to the upper top + position. 4.2.0 (2023-08-10) ------------------ From 3312069f2ee0d70520c6aac2a462c8fefa6828e9 Mon Sep 17 00:00:00 2001 From: veldakarimi Date: Wed, 24 Jan 2024 09:58:24 +0300 Subject: [PATCH 03/25] fixing the position of the toolbar button to the upper top --- debug_toolbar/static/debug_toolbar/css/toolbar.css | 2 +- debug_toolbar/static/debug_toolbar/js/toolbar.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/debug_toolbar/static/debug_toolbar/css/toolbar.css b/debug_toolbar/static/debug_toolbar/css/toolbar.css index 6d6b7b6f7..a35286a1f 100644 --- a/debug_toolbar/static/debug_toolbar/css/toolbar.css +++ b/debug_toolbar/static/debug_toolbar/css/toolbar.css @@ -220,7 +220,7 @@ background-color: #fff; border: 1px solid #111; border-bottom: 0; - top: 268px; + top: 0; right: 0; z-index: 100000000; opacity: 0.75; diff --git a/debug_toolbar/static/debug_toolbar/js/toolbar.js b/debug_toolbar/static/debug_toolbar/js/toolbar.js index 9546ef27e..6648fb52b 100644 --- a/debug_toolbar/static/debug_toolbar/js/toolbar.js +++ b/debug_toolbar/static/debug_toolbar/js/toolbar.js @@ -226,7 +226,7 @@ const djdt = { const handle = document.getElementById("djDebugToolbarHandle"); // set handle position const handleTop = Math.min( - localStorage.getItem("djdt.top") || 0, + localStorage.getItem("djdt.top") || 265, window.innerHeight - handle.offsetWidth ); handle.style.top = handleTop + "px"; From 83e9739cbcef2c8c7bcee5b528d223a130efb36c Mon Sep 17 00:00:00 2001 From: veldakarimi Date: Wed, 28 Feb 2024 02:39:02 +0300 Subject: [PATCH 04/25] solution to djdt registered namespace,update docs, system checks and tests --- debug_toolbar/apps.py | 23 ++++++++++- debug_toolbar/settings.py | 2 + docs/Makefile | 1 + docs/changes.rst | 2 + docs/installation.rst | 31 ++++++++++++++- example/settings.py | 12 +++++- tests/test_checks.py | 82 ++++++++++++++++++++++++++++++++++++++- 7 files changed, 148 insertions(+), 5 deletions(-) diff --git a/debug_toolbar/apps.py b/debug_toolbar/apps.py index 0a10a4b08..41cbf3d2e 100644 --- a/debug_toolbar/apps.py +++ b/debug_toolbar/apps.py @@ -1,9 +1,10 @@ import inspect import mimetypes +import sys from django.apps import AppConfig from django.conf import settings -from django.core.checks import Warning, register +from django.core.checks import Error, Warning, register from django.middleware.gzip import GZipMiddleware from django.utils.module_loading import import_string from django.utils.translation import gettext_lazy as _ @@ -206,3 +207,23 @@ def js_mimetype_check(app_configs, **kwargs): ) ] return [] + + +@register() +def debug_toolbar_namespace_check(app_configs, **kwargs): + """ + Check that the Django Debug Toolbar is registered as a namespace. + """ + if ( + settings.DEBUG is False + and dt_settings.get_config()["DEBUG_TOOLBAR_CONFIG"] == "test" in sys.argv + ): + return [ + Error( + "django debug toolbar is not a registered namespace ", + hint="The Django Debug Toolbar is misconfigured, check the documentation for proper configuration and run the tests.", + id="debug_toolbar.W008", + ) + ] + else: + return [] diff --git a/debug_toolbar/settings.py b/debug_toolbar/settings.py index eb6b59209..109abaa3e 100644 --- a/debug_toolbar/settings.py +++ b/debug_toolbar/settings.py @@ -1,3 +1,4 @@ +import sys import warnings from functools import lru_cache @@ -42,6 +43,7 @@ "SQL_WARNING_THRESHOLD": 500, # milliseconds "OBSERVE_REQUEST_CALLBACK": "debug_toolbar.toolbar.observe_request", "TOOLBAR_LANGUAGE": None, + "DEBUG_TOOLBAR_CONFIG": "test" in sys.argv, } diff --git a/docs/Makefile b/docs/Makefile index ee27505aa..bebc1f1b7 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -8,6 +8,7 @@ SPHINXBUILD ?= sphinx-build SOURCEDIR = . BUILDDIR = _build + # Put it first so that "make" without argument is like "make help". help: @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/docs/changes.rst b/docs/changes.rst index 82185d756..9bffe77f5 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -19,6 +19,8 @@ Pending `__. * Changed the default position of the toolbar from top to the upper top position. +* Fixed the bug causing ``'djdt' is not a registered namespace`` and updated + docs to help in initial configuration. 4.2.0 (2023-08-10) ------------------ diff --git a/docs/installation.rst b/docs/installation.rst index a350d9c3a..9548b86ed 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -99,7 +99,34 @@ Add django-debug-toolbar's URLs to your project's URLconf: This example uses the ``__debug__`` prefix, but you can use any prefix that doesn't clash with your application's URLs. -5. Add the Middleware +5. Configure django-debug-toolbar +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Add the following configuration for django-debug-toolbar in your setting: + +.. code-block:: python + + import sys + + DEBUG = bool({"runserver"}.intersection(sys.argv)) + + DEBUG_TOOLBAR_CONFIG = "test" in sys.argv + + DEBUG_TOOLBAR = DEBUG and not DEBUG_TOOLBAR_CONFIG + + +To explicitly check the ``settings.DEBUG_TOOLBAR_CONFIG``variable before +including the URLs, add the following code in your ``urls`` file. + +.. code-block:: python + + if settings.DEBUG_TOOLBAR_CONFIG: + + import debug_toolbar + + urlpatterns += [path('__debug__/', include(debug_toolbar.urls))] + +6. Add the Middleware ^^^^^^^^^^^^^^^^^^^^^ The Debug Toolbar is mostly implemented in a middleware. Add it to your @@ -122,7 +149,7 @@ The Debug Toolbar is mostly implemented in a middleware. Add it to your .. _internal-ips: -6. Configure Internal IPs +7. Configure Internal IPs ^^^^^^^^^^^^^^^^^^^^^^^^^ The Debug Toolbar is shown only if your IP address is listed in Django’s diff --git a/example/settings.py b/example/settings.py index d2bd57387..8b5b1da8d 100644 --- a/example/settings.py +++ b/example/settings.py @@ -1,15 +1,25 @@ """Django settings for example project.""" import os +import sys + +import environ BASE_DIR = os.path.dirname(os.path.dirname(__file__)) # Quick-start development settings - unsuitable for production +env = environ.Env() +environ.Env.read_env() + SECRET_KEY = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" -DEBUG = True +# DEBUG = True + +DEBUG = env("DEBUG", default=bool({"runserver"}.intersection(sys.argv))) +DEBUG_TOOLBAR_CONFIG = env("DEBUG_TOOLBAR_CONFIG", default="test" in sys.argv) +DEBUG_TOOLBAR = DEBUG and not DEBUG_TOOLBAR_CONFIG INTERNAL_IPS = ["127.0.0.1", "::1"] diff --git a/tests/test_checks.py b/tests/test_checks.py index 8e4f8e62f..4c50f5535 100644 --- a/tests/test_checks.py +++ b/tests/test_checks.py @@ -1,12 +1,16 @@ import os +import sys import unittest from unittest.mock import patch import django from django.conf import settings -from django.core.checks import Warning, run_checks +from django.core.checks import Error, Warning, run_checks from django.test import SimpleTestCase, override_settings +from debug_toolbar import settings as dt_settings +from debug_toolbar.apps import debug_toolbar_namespace_check + PATH_DOES_NOT_EXIST = os.path.join(settings.BASE_DIR, "tests", "invalid_static") @@ -258,3 +262,79 @@ def test_check_w007_invalid(self, mocked_guess_type): ) ], ) + + # @patch("debug_toolbar.apps.debug_toolbar_namespace_check", []) + # def test_check_w008_valid(self): + # self.assertEqual( + # run_checks(), + # [ + # Error( + # "debug toolbar is not a registered namespace", + # hint="The Django Debug Toolbar is misconfigured, check the documentation for proper configuration", + # id="debug_toolbar.W008", + # ) + # ], + # ) + + +def test_check_w008_valid(self): + settings.DEBUG = True + dt_settings.get_config()["DEBUG_TOOLBAR_CONFIG"] = "test" + errors = debug_toolbar_namespace_check(None) + self.assertEqual( + errors, + [ + Error( + "debug toolbar is not a registered namespace", + hint="The Django Debug Toolbar is misconfigured, check the documentation for proper configuration", + id="debug_toolbar.W008", + ) + ], + ) + + @patch("debug_toolbar.apps.debug_toolbar_namespace_check", []) + def test_debug_toolbar_namespace_check_with_debug_false_and_config_not_test( + self, mocked_get_config + ): + mocked_get_config.return_value = {"DEBUG_TOOLBAR_CONFIG": "not_test"} + settings.DEBUG = False + errors = debug_toolbar_namespace_check(None) + self.assertEqual(len(errors), 0) + + @patch("debug_toolbar.apps.debug_toolbar_namespace_check", []) + def test_debug_toolbar_namespace_check_with_debug_true_and_config_test( + self, mocked_get_config + ): + mocked_get_config.return_value = {"DEBUG_TOOLBAR_CONFIG": "test"} + settings.DEBUG = True + sys.argv = ["manage.py", "test"] + errors = debug_toolbar_namespace_check(None) + self.assertEqual(len(errors), 0) + + @patch("debug_toolbar.apps.debug_toolbar_namespace_check", []) + def test_debug_toolbar_namespace_check_with_debug_true_and_config_not_test( + self, mock_get_config + ): + mock_get_config.return_value = {"DEBUG_TOOLBAR_CONFIG": "not_test"} + settings.DEBUG = True + sys.argv = ["manage.py", "test"] + errors = debug_toolbar_namespace_check(None) + self.assertEqual(len(errors), 0) + + @patch("debug_toolbar.apps.debug_toolbar_namespace_check", []) + def test_debug_toolbar_namespace_check_with_debug_false_and_config_test( + self, mock_get_config + ): + mock_get_config.return_value = {"DEBUG_TOOLBAR_CONFIG": "test"} + settings.DEBUG = False + errors = debug_toolbar_namespace_check(None) + self.assertEqual( + errors, + [ + Error( + "django debug toolbar is not a registered namespace ", + hint="The Django Debug Toolbar is misconfigured, check the documentation for proper configuration and run the tests.", + id="debug_toolbar.W008", + ) + ], + ) From 3affaea3e0baa51efd8c285f3db34a202d0f4b4a Mon Sep 17 00:00:00 2001 From: veldakarimi Date: Wed, 28 Feb 2024 15:30:53 +0300 Subject: [PATCH 05/25] renaming the check to RUNNING_TESTS --- debug_toolbar/apps.py | 6 ++--- debug_toolbar/settings.py | 2 +- docs/changes.rst | 2 +- docs/installation.rst | 10 ++++---- example/settings.py | 10 +++----- tests/test_checks.py | 53 +++++++++++++++------------------------ 6 files changed, 33 insertions(+), 50 deletions(-) diff --git a/debug_toolbar/apps.py b/debug_toolbar/apps.py index 41cbf3d2e..10fbdbc78 100644 --- a/debug_toolbar/apps.py +++ b/debug_toolbar/apps.py @@ -210,13 +210,13 @@ def js_mimetype_check(app_configs, **kwargs): @register() -def debug_toolbar_namespace_check(app_configs, **kwargs): +def debug_toolbar_installed_when_running_tests_check(app_configs, **kwargs): """ - Check that the Django Debug Toolbar is registered as a namespace. + Check that the Django Debug Toolbar is installed and works well with the current tests """ if ( settings.DEBUG is False - and dt_settings.get_config()["DEBUG_TOOLBAR_CONFIG"] == "test" in sys.argv + and dt_settings.get_config()["RUNNING_TESTS"] == "test" in sys.argv ): return [ Error( diff --git a/debug_toolbar/settings.py b/debug_toolbar/settings.py index 109abaa3e..4b88cf679 100644 --- a/debug_toolbar/settings.py +++ b/debug_toolbar/settings.py @@ -43,7 +43,7 @@ "SQL_WARNING_THRESHOLD": 500, # milliseconds "OBSERVE_REQUEST_CALLBACK": "debug_toolbar.toolbar.observe_request", "TOOLBAR_LANGUAGE": None, - "DEBUG_TOOLBAR_CONFIG": "test" in sys.argv, + "RUNNING_TESTS": "test" in sys.argv, } diff --git a/docs/changes.rst b/docs/changes.rst index 9bffe77f5..42b9e444a 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -20,7 +20,7 @@ Pending * Changed the default position of the toolbar from top to the upper top position. * Fixed the bug causing ``'djdt' is not a registered namespace`` and updated - docs to help in initial configuration. + docs to help in initial configuration while testing. 4.2.0 (2023-08-10) ------------------ diff --git a/docs/installation.rst b/docs/installation.rst index 9548b86ed..de014a279 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -102,7 +102,7 @@ doesn't clash with your application's URLs. 5. Configure django-debug-toolbar ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -Add the following configuration for django-debug-toolbar in your setting: +Add the configuration for django-debug-toolbar in your setting to run tests: .. code-block:: python @@ -110,17 +110,17 @@ Add the following configuration for django-debug-toolbar in your setting: DEBUG = bool({"runserver"}.intersection(sys.argv)) - DEBUG_TOOLBAR_CONFIG = "test" in sys.argv + RUNNING_TESTS = "test" in sys.argv - DEBUG_TOOLBAR = DEBUG and not DEBUG_TOOLBAR_CONFIG + DEBUG_TOOLBAR = DEBUG and not RUNNING_TESTS -To explicitly check the ``settings.DEBUG_TOOLBAR_CONFIG``variable before +To explicitly check the ``settings.RUNNING_TESTS``variable before including the URLs, add the following code in your ``urls`` file. .. code-block:: python - if settings.DEBUG_TOOLBAR_CONFIG: + if settings.RUNNING_TESTS: import debug_toolbar diff --git a/example/settings.py b/example/settings.py index 8b5b1da8d..1580d1815 100644 --- a/example/settings.py +++ b/example/settings.py @@ -3,23 +3,19 @@ import os import sys -import environ - BASE_DIR = os.path.dirname(os.path.dirname(__file__)) # Quick-start development settings - unsuitable for production -env = environ.Env() -environ.Env.read_env() SECRET_KEY = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" # DEBUG = True -DEBUG = env("DEBUG", default=bool({"runserver"}.intersection(sys.argv))) -DEBUG_TOOLBAR_CONFIG = env("DEBUG_TOOLBAR_CONFIG", default="test" in sys.argv) -DEBUG_TOOLBAR = DEBUG and not DEBUG_TOOLBAR_CONFIG +DEBUG = default = bool({"runserver"}.intersection(sys.argv)) +RUNNING_TESTS = "test" in sys.argv +DEBUG_TOOLBAR = DEBUG and not RUNNING_TESTS INTERNAL_IPS = ["127.0.0.1", "::1"] diff --git a/tests/test_checks.py b/tests/test_checks.py index 4c50f5535..cb2cc2da2 100644 --- a/tests/test_checks.py +++ b/tests/test_checks.py @@ -9,7 +9,7 @@ from django.test import SimpleTestCase, override_settings from debug_toolbar import settings as dt_settings -from debug_toolbar.apps import debug_toolbar_namespace_check +from debug_toolbar.apps import debug_toolbar_installed_when_running_tests_check PATH_DOES_NOT_EXIST = os.path.join(settings.BASE_DIR, "tests", "invalid_static") @@ -263,71 +263,58 @@ def test_check_w007_invalid(self, mocked_guess_type): ], ) - # @patch("debug_toolbar.apps.debug_toolbar_namespace_check", []) - # def test_check_w008_valid(self): - # self.assertEqual( - # run_checks(), - # [ - # Error( - # "debug toolbar is not a registered namespace", - # hint="The Django Debug Toolbar is misconfigured, check the documentation for proper configuration", - # id="debug_toolbar.W008", - # ) - # ], - # ) - def test_check_w008_valid(self): settings.DEBUG = True - dt_settings.get_config()["DEBUG_TOOLBAR_CONFIG"] = "test" - errors = debug_toolbar_namespace_check(None) + dt_settings.get_config()["RUNNING_TESTS"] = "test" + errors = debug_toolbar_installed_when_running_tests_check(None) self.assertEqual( errors, [ Error( "debug toolbar is not a registered namespace", - hint="The Django Debug Toolbar is misconfigured, check the documentation for proper configuration", + hint="The Django Debug Toolbar is misconfigured, check the documentation for proper configuration when running tests", id="debug_toolbar.W008", ) ], ) - @patch("debug_toolbar.apps.debug_toolbar_namespace_check", []) - def test_debug_toolbar_namespace_check_with_debug_false_and_config_not_test( + @patch("debug_toolbar.apps.debug_toolbar_installed_when_running_tests_check", []) + def test_debug_toolbar_installed_when_running_tests_check_with_debug_false( self, mocked_get_config ): - mocked_get_config.return_value = {"DEBUG_TOOLBAR_CONFIG": "not_test"} + mocked_get_config.return_value = {"RUNNING_TESTS": "not_test"} settings.DEBUG = False - errors = debug_toolbar_namespace_check(None) + errors = debug_toolbar_installed_when_running_tests_check(None) self.assertEqual(len(errors), 0) - @patch("debug_toolbar.apps.debug_toolbar_namespace_check", []) - def test_debug_toolbar_namespace_check_with_debug_true_and_config_test( + @patch("debug_toolbar.apps.debug_toolbar_installed_when_running_tests_check", []) + def test_debug_toolbar_installed_when_running_tests_check_with_debug_true( self, mocked_get_config ): - mocked_get_config.return_value = {"DEBUG_TOOLBAR_CONFIG": "test"} + mocked_get_config.return_value = {"RUNNING_TESTS": "test"} settings.DEBUG = True sys.argv = ["manage.py", "test"] - errors = debug_toolbar_namespace_check(None) + errors = debug_toolbar_installed_when_running_tests_check(None) self.assertEqual(len(errors), 0) - @patch("debug_toolbar.apps.debug_toolbar_namespace_check", []) - def test_debug_toolbar_namespace_check_with_debug_true_and_config_not_test( + @patch("debug_toolbar.apps.debug_toolbar_installed_when_running_tests_check", []) + def test_debug_toolbar_installed_when_running_tests_check_with_running_tests_not_test( self, mock_get_config ): - mock_get_config.return_value = {"DEBUG_TOOLBAR_CONFIG": "not_test"} + mock_get_config.return_value = {"RUNNING_TESTS": "not_test"} settings.DEBUG = True sys.argv = ["manage.py", "test"] - errors = debug_toolbar_namespace_check(None) + errors = debug_toolbar_installed_when_running_tests_check(None) self.assertEqual(len(errors), 0) - @patch("debug_toolbar.apps.debug_toolbar_namespace_check", []) - def test_debug_toolbar_namespace_check_with_debug_false_and_config_test( + @patch("debug_toolbar.apps.debug_toolbar_installed_when_running_tests_check", []) + def test_debug_toolbar_installed_when_running_tests_check_with_debug_false_and_running_tests( self, mock_get_config ): - mock_get_config.return_value = {"DEBUG_TOOLBAR_CONFIG": "test"} + mock_get_config.return_value = {"RUNNING_TESTS": "test"} settings.DEBUG = False - errors = debug_toolbar_namespace_check(None) + errors = debug_toolbar_installed_when_running_tests_check(None) self.assertEqual( errors, [ From 8bf25e95d7733e865c9ff46dce140e117311e853 Mon Sep 17 00:00:00 2001 From: veldakarimi Date: Thu, 29 Feb 2024 15:04:14 +0300 Subject: [PATCH 06/25] removing test in the if statements --- debug_toolbar/apps.py | 6 +----- example/settings.py | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/debug_toolbar/apps.py b/debug_toolbar/apps.py index 10fbdbc78..5cfa01354 100644 --- a/debug_toolbar/apps.py +++ b/debug_toolbar/apps.py @@ -1,6 +1,5 @@ import inspect import mimetypes -import sys from django.apps import AppConfig from django.conf import settings @@ -214,10 +213,7 @@ def debug_toolbar_installed_when_running_tests_check(app_configs, **kwargs): """ Check that the Django Debug Toolbar is installed and works well with the current tests """ - if ( - settings.DEBUG is False - and dt_settings.get_config()["RUNNING_TESTS"] == "test" in sys.argv - ): + if settings.DEBUG is not False and dt_settings.get_config()["RUNNING_TESTS"]: return [ Error( "django debug toolbar is not a registered namespace ", diff --git a/example/settings.py b/example/settings.py index 1580d1815..a54bb399d 100644 --- a/example/settings.py +++ b/example/settings.py @@ -13,7 +13,7 @@ # DEBUG = True -DEBUG = default = bool({"runserver"}.intersection(sys.argv)) +DEBUG = bool({"runserver"}.intersection(sys.argv)) RUNNING_TESTS = "test" in sys.argv DEBUG_TOOLBAR = DEBUG and not RUNNING_TESTS From 46f64c4d9ac4d000b77ee3242610ec99b00b6ee3 Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Thu, 29 Feb 2024 13:57:53 -0600 Subject: [PATCH 07/25] Add basic test to example app and example_test to make commands. --- Makefile | 3 +++ example/settings.py | 24 ++++++++++++++++-------- example/test_views.py | 12 ++++++++++++ example/urls.py | 7 ++++++- 4 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 example/test_views.py diff --git a/Makefile b/Makefile index 1600496e5..24b59ab95 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,9 @@ example: --noinput --username="$(USER)" --email="$(USER)@mailinator.com" python example/manage.py runserver +example_test: + python example/manage.py test example + test: DJANGO_SETTINGS_MODULE=tests.settings \ python -m django test $${TEST_ARGS:-tests} diff --git a/example/settings.py b/example/settings.py index a54bb399d..1508b5a29 100644 --- a/example/settings.py +++ b/example/settings.py @@ -11,11 +11,7 @@ SECRET_KEY = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" -# DEBUG = True - -DEBUG = bool({"runserver"}.intersection(sys.argv)) -RUNNING_TESTS = "test" in sys.argv -DEBUG_TOOLBAR = DEBUG and not RUNNING_TESTS +DEBUG = True INTERNAL_IPS = ["127.0.0.1", "::1"] @@ -28,11 +24,9 @@ "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", - "debug_toolbar", ] MIDDLEWARE = [ - "debug_toolbar.middleware.DebugToolbarMiddleware", "django.middleware.security.SecurityMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", "django.middleware.common.CommonMiddleware", @@ -67,7 +61,6 @@ WSGI_APPLICATION = "example.wsgi.application" -DEBUG_TOOLBAR_CONFIG = {"ROOT_TAG_EXTRA_ATTRS": "data-turbo-permanent hx-preserve"} # Cache and database @@ -103,3 +96,18 @@ } STATICFILES_DIRS = [os.path.join(BASE_DIR, "example", "static")] + + +# Only enable the toolbar when we're in debug mode and we're +# not running tests. Django will change DEBUG to be False for +# tests, so we can't rely on DEBUG alone. +ENABLE_DEBUG_TOOLBAR = DEBUG and "test" not in sys.argv +if ENABLE_DEBUG_TOOLBAR: + INSTALLED_APPS += [ + "debug_toolbar", + ] + MIDDLEWARE += [ + "debug_toolbar.middleware.DebugToolbarMiddleware", + ] + # Customize the config to support turbo and htmx boosting. + DEBUG_TOOLBAR_CONFIG = {"ROOT_TAG_EXTRA_ATTRS": "data-turbo-permanent hx-preserve"} diff --git a/example/test_views.py b/example/test_views.py new file mode 100644 index 000000000..c3a8b96b0 --- /dev/null +++ b/example/test_views.py @@ -0,0 +1,12 @@ +# Add tests to example app to check how the toolbar is used +# when running tests for a project. +# See https://github.com/jazzband/django-debug-toolbar/issues/1405 + +from django.test import TestCase +from django.urls import reverse + + +class ViewTestCase(TestCase): + def test_index(self): + response = self.client.get(reverse("home")) + assert response.status_code == 200 diff --git a/example/urls.py b/example/urls.py index da52601f8..7569a57f9 100644 --- a/example/urls.py +++ b/example/urls.py @@ -1,3 +1,4 @@ +from django.conf import settings from django.contrib import admin from django.urls import include, path from django.views.generic import TemplateView @@ -33,5 +34,9 @@ ), path("admin/", admin.site.urls), path("ajax/increment", increment, name="ajax_increment"), - path("__debug__/", include("debug_toolbar.urls")), ] + +if settings.ENABLE_DEBUG_TOOLBAR: + urlpatterns += [ + path("__debug__/", include("debug_toolbar.urls")), + ] From 5941710f6dbff60cd82993ce72b0a97752fd6e3b Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Thu, 29 Feb 2024 13:59:21 -0600 Subject: [PATCH 08/25] Update check for toolbar and tests. --- debug_toolbar/apps.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/debug_toolbar/apps.py b/debug_toolbar/apps.py index 5cfa01354..7c69b3fa9 100644 --- a/debug_toolbar/apps.py +++ b/debug_toolbar/apps.py @@ -211,13 +211,18 @@ def js_mimetype_check(app_configs, **kwargs): @register() def debug_toolbar_installed_when_running_tests_check(app_configs, **kwargs): """ - Check that the Django Debug Toolbar is installed and works well with the current tests + Check that the toolbar is not being used when tests are running """ - if settings.DEBUG is not False and dt_settings.get_config()["RUNNING_TESTS"]: + if not settings.DEBUG and dt_settings.get_config()["RUNNING_TESTS"]: return [ Error( - "django debug toolbar is not a registered namespace ", - hint="The Django Debug Toolbar is misconfigured, check the documentation for proper configuration and run the tests.", + "The Django Debug Toolbar can't be used with tests", + hint="Django changes the DEBUG setting to False when running " + "tests. By default the Django Debug Toolbar is installed because " + "DEBUG is set to True. For most cases, you need to avoid installing " + "the toolbar when running tests. If you feel this check is in error, " + "you can set `DEBUG_TOOLBAR_CONFIG['IS_RUNNING_TESTS'] = False` to " + "bypass this check.", id="debug_toolbar.W008", ) ] From df02d335eaf5ac1bff3861ecff61143e8f0b7ed7 Mon Sep 17 00:00:00 2001 From: veldakarimi Date: Sat, 9 Mar 2024 02:38:30 +0300 Subject: [PATCH 09/25] update check using with, combine the four tests to one, update default config --- debug_toolbar/apps.py | 2 +- debug_toolbar/settings.py | 2 +- docs/installation.rst | 35 +++----------- tests/settings.py | 3 +- tests/test_checks.py | 96 ++++++++++++++++++--------------------- 5 files changed, 54 insertions(+), 84 deletions(-) diff --git a/debug_toolbar/apps.py b/debug_toolbar/apps.py index 7c69b3fa9..ffa76f6c8 100644 --- a/debug_toolbar/apps.py +++ b/debug_toolbar/apps.py @@ -213,7 +213,7 @@ def debug_toolbar_installed_when_running_tests_check(app_configs, **kwargs): """ Check that the toolbar is not being used when tests are running """ - if not settings.DEBUG and dt_settings.get_config()["RUNNING_TESTS"]: + if not settings.DEBUG and dt_settings.get_config()["IS_RUNNING_TESTS"]: return [ Error( "The Django Debug Toolbar can't be used with tests", diff --git a/debug_toolbar/settings.py b/debug_toolbar/settings.py index 9d709d497..868d50a90 100644 --- a/debug_toolbar/settings.py +++ b/debug_toolbar/settings.py @@ -43,7 +43,7 @@ "SQL_WARNING_THRESHOLD": 500, # milliseconds "OBSERVE_REQUEST_CALLBACK": "debug_toolbar.toolbar.observe_request", "TOOLBAR_LANGUAGE": None, - "RUNNING_TESTS": "test" in sys.argv, + "IS_RUNNING_TESTS": "test" in sys.argv, "UPDATE_ON_FETCH": False, } diff --git a/docs/installation.rst b/docs/installation.rst index af0182387..cd1618feb 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -99,34 +99,13 @@ Add django-debug-toolbar's URLs to your project's URLconf: This example uses the ``__debug__`` prefix, but you can use any prefix that doesn't clash with your application's URLs. -5. Configure django-debug-toolbar -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +> [!NOTE] +> Check out the configuration example in the +[example app](https://github.com/jazzband/django-debug-toolbar/tree/main/example) +to learn how to set up the toolbar to function smoothly while running your +tests. -Add the configuration for django-debug-toolbar in your setting to run tests: - -.. code-block:: python - - import sys - - DEBUG = bool({"runserver"}.intersection(sys.argv)) - - RUNNING_TESTS = "test" in sys.argv - - DEBUG_TOOLBAR = DEBUG and not RUNNING_TESTS - - -To explicitly check the ``settings.RUNNING_TESTS``variable before -including the URLs, add the following code in your ``urls`` file. - -.. code-block:: python - - if settings.RUNNING_TESTS: - - import debug_toolbar - - urlpatterns += [path('__debug__/', include(debug_toolbar.urls))] - -6. Add the Middleware +5. Add the Middleware ^^^^^^^^^^^^^^^^^^^^^ The Debug Toolbar is mostly implemented in a middleware. Add it to your @@ -149,7 +128,7 @@ The Debug Toolbar is mostly implemented in a middleware. Add it to your .. _internal-ips: -7. Configure Internal IPs +6. Configure Internal IPs ^^^^^^^^^^^^^^^^^^^^^^^^^ The Debug Toolbar is shown only if your IP address is listed in Django’s diff --git a/tests/settings.py b/tests/settings.py index b3c281242..f1ef5a7f2 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -126,5 +126,6 @@ DEBUG_TOOLBAR_CONFIG = { # Django's test client sets wsgi.multiprocess to True inappropriately - "RENDER_PANELS": False + "RENDER_PANELS": False, + "IS_RUNNING_TESTS": False, } diff --git a/tests/test_checks.py b/tests/test_checks.py index cb2cc2da2..8ac6e3cf7 100644 --- a/tests/test_checks.py +++ b/tests/test_checks.py @@ -1,5 +1,4 @@ import os -import sys import unittest from unittest.mock import patch @@ -263,65 +262,56 @@ def test_check_w007_invalid(self, mocked_guess_type): ], ) - -def test_check_w008_valid(self): - settings.DEBUG = True - dt_settings.get_config()["RUNNING_TESTS"] = "test" - errors = debug_toolbar_installed_when_running_tests_check(None) - self.assertEqual( - errors, - [ - Error( - "debug toolbar is not a registered namespace", - hint="The Django Debug Toolbar is misconfigured, check the documentation for proper configuration when running tests", - id="debug_toolbar.W008", - ) - ], - ) - - @patch("debug_toolbar.apps.debug_toolbar_installed_when_running_tests_check", []) - def test_debug_toolbar_installed_when_running_tests_check_with_debug_false( - self, mocked_get_config - ): - mocked_get_config.return_value = {"RUNNING_TESTS": "not_test"} - settings.DEBUG = False - errors = debug_toolbar_installed_when_running_tests_check(None) - self.assertEqual(len(errors), 0) - - @patch("debug_toolbar.apps.debug_toolbar_installed_when_running_tests_check", []) - def test_debug_toolbar_installed_when_running_tests_check_with_debug_true( - self, mocked_get_config - ): - mocked_get_config.return_value = {"RUNNING_TESTS": "test"} + def test_check_w008_valid(self): settings.DEBUG = True - sys.argv = ["manage.py", "test"] - errors = debug_toolbar_installed_when_running_tests_check(None) - self.assertEqual(len(errors), 0) - - @patch("debug_toolbar.apps.debug_toolbar_installed_when_running_tests_check", []) - def test_debug_toolbar_installed_when_running_tests_check_with_running_tests_not_test( - self, mock_get_config - ): - mock_get_config.return_value = {"RUNNING_TESTS": "not_test"} - settings.DEBUG = True - sys.argv = ["manage.py", "test"] - errors = debug_toolbar_installed_when_running_tests_check(None) - self.assertEqual(len(errors), 0) - - @patch("debug_toolbar.apps.debug_toolbar_installed_when_running_tests_check", []) - def test_debug_toolbar_installed_when_running_tests_check_with_debug_false_and_running_tests( - self, mock_get_config - ): - mock_get_config.return_value = {"RUNNING_TESTS": "test"} - settings.DEBUG = False + dt_settings.get_config()["IS_RUNNING_TESTS"] = "test" errors = debug_toolbar_installed_when_running_tests_check(None) self.assertEqual( + run_checks(), errors, [ Error( - "django debug toolbar is not a registered namespace ", - hint="The Django Debug Toolbar is misconfigured, check the documentation for proper configuration and run the tests.", + "The Django Debug Toolbar can't be used with tests", + hint="Django changes the DEBUG setting to False when running " + "tests. By default the Django Debug Toolbar is installed because " + "DEBUG is set to True. For most cases, you need to avoid installing " + "the toolbar when running tests. If you feel this check is in error, " + "you can set `DEBUG_TOOLBAR_CONFIG['IS_RUNNING_TESTS'] = False` to " + "bypass this check.", id="debug_toolbar.W008", ) ], ) + + @patch("debug_toolbar.apps.debug_toolbar_installed_when_running_tests_check", []) + def test_debug_toolbar_installed_when_running_tests(self): + with self.settings(DEBUG=False): + dt_settings.get_config()["IS_RUNNING_TESTS"] = False + errors = debug_toolbar_installed_when_running_tests_check(None) + self.assertEqual(len(errors), 0) + with self.settings(DEBUG=True): + dt_settings.get_config()["IS_RUNNING_TESTS"] = True + errors = debug_toolbar_installed_when_running_tests_check(None) + self.assertEqual(len(errors), 0) + with self.settings(DEBUG=True): + dt_settings.get_config()["IS_RUNNING_TESTS"] = False + errors = debug_toolbar_installed_when_running_tests_check(None) + self.assertEqual(len(errors), 0) + with self.settings(DEBUG=False): + dt_settings.get_config()["IS_RUNNING_TESTS"] = True + errors = debug_toolbar_installed_when_running_tests_check(None) + self.assertEqual( + errors, + [ + Error( + "The Django Debug Toolbar can't be used with tests", + hint="Django changes the DEBUG setting to False when running " + "tests. By default the Django Debug Toolbar is installed because " + "DEBUG is set to True. For most cases, you need to avoid installing " + "the toolbar when running tests. If you feel this check is in error, " + "you can set `DEBUG_TOOLBAR_CONFIG['IS_RUNNING_TESTS'] = False` to " + "bypass this check.", + id="debug_toolbar.W008", + ) + ], + ) From 18eaab557339cb10f6e8ee2a70237216aaace5d5 Mon Sep 17 00:00:00 2001 From: veldakarimi Date: Mon, 11 Mar 2024 03:02:45 +0300 Subject: [PATCH 10/25] update installation files and remove patch from namespace check --- .vscode/settings.json | 3 ++ docs/changes.rst | 7 +++-- docs/installation.rst | 10 +++---- tests/test_checks.py | 65 ++++++++++++++++++++++++++++--------------- 4 files changed, 56 insertions(+), 29 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..070cfdad1 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "esbonio.sphinx.confDir": "" +} diff --git a/docs/changes.rst b/docs/changes.rst index e28e444e7..26f76e77b 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -29,8 +29,11 @@ Pending `__. * Changed the default position of the toolbar from top to the upper top position. -* Fixed the bug causing ``'djdt' is not a registered namespace`` and updated - docs to help in initial configuration while running tests. +* Added a link in the installation docs to a more complete installation + example in the example app. +* Added check to prevent the toolbar from being installed when tests + are running. +* Added test to example app and command to run the example app's tests. * Added the setting, ``UPDATE_ON_FETCH`` to control whether the toolbar automatically updates to the latest AJAX request or not. It defaults to ``False``. diff --git a/docs/installation.rst b/docs/installation.rst index cd1618feb..60f383a07 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -82,6 +82,11 @@ Add ``"debug_toolbar"`` to your ``INSTALLED_APPS`` setting: # ... ] +.. note:: Check out the configuration example in the + [example app](https://github.com/jazzband/django-debug-toolbar/tree/main/example) + to learn how to set up the toolbar to function smoothly while running + your tests. + 4. Add the URLs ^^^^^^^^^^^^^^^ @@ -99,11 +104,6 @@ Add django-debug-toolbar's URLs to your project's URLconf: This example uses the ``__debug__`` prefix, but you can use any prefix that doesn't clash with your application's URLs. -> [!NOTE] -> Check out the configuration example in the -[example app](https://github.com/jazzband/django-debug-toolbar/tree/main/example) -to learn how to set up the toolbar to function smoothly while running your -tests. 5. Add the Middleware ^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/test_checks.py b/tests/test_checks.py index 8ac6e3cf7..ab8ab199c 100644 --- a/tests/test_checks.py +++ b/tests/test_checks.py @@ -123,7 +123,17 @@ def test_panels_is_empty(self): hint="Set DEBUG_TOOLBAR_PANELS to a non-empty list in your " "settings.py.", id="debug_toolbar.W005", - ) + ), + Error( + "The Django Debug Toolbar can't be used with tests", + hint="Django changes the DEBUG setting to False when running " + "tests. By default the Django Debug Toolbar is installed because " + "DEBUG is set to True. For most cases, you need to avoid installing " + "the toolbar when running tests. If you feel this check is in error, " + "you can set `DEBUG_TOOLBAR_CONFIG['IS_RUNNING_TESTS'] = False` to " + "bypass this check.", + id="debug_toolbar.W008", + ), ], ) @@ -262,28 +272,39 @@ def test_check_w007_invalid(self, mocked_guess_type): ], ) - def test_check_w008_valid(self): - settings.DEBUG = True - dt_settings.get_config()["IS_RUNNING_TESTS"] = "test" - errors = debug_toolbar_installed_when_running_tests_check(None) - self.assertEqual( - run_checks(), - errors, - [ - Error( - "The Django Debug Toolbar can't be used with tests", - hint="Django changes the DEBUG setting to False when running " - "tests. By default the Django Debug Toolbar is installed because " - "DEBUG is set to True. For most cases, you need to avoid installing " - "the toolbar when running tests. If you feel this check is in error, " - "you can set `DEBUG_TOOLBAR_CONFIG['IS_RUNNING_TESTS'] = False` to " - "bypass this check.", - id="debug_toolbar.W008", - ) - ], - ) + # def test_debug_toolbar_installed_when_running_tests(self, mocked_get_config): + # with self.settings(DEBUG=False): + # dt_settings.get_config()["IS_RUNNING_TESTS"] = False + # errors = debug_toolbar_installed_when_running_tests_check(None) + # self.assertEqual(len(errors), 0) + # with self.settings(DEBUG=True): + # dt_settings.get_config()["IS_RUNNING_TESTS"] = True + # errors = debug_toolbar_installed_when_running_tests_check(None) + # self.assertEqual(len(errors), 0) + # with self.settings(DEBUG=True): + # dt_settings.get_config()["IS_RUNNING_TESTS"] = False + # errors = debug_toolbar_installed_when_running_tests_check(None) + # self.assertEqual(len(errors), 0) + # with self.settings(DEBUG=False): + # dt_settings.get_config()["IS_RUNNING_TESTS"] = True + # errors = debug_toolbar_installed_when_running_tests_check(None) + # self.assertEqual( + # errors, + # [ + # Error( + # "The Django Debug Toolbar can't be used with tests", + # hint="Django changes the DEBUG setting to False when running " + # "tests. By default the Django Debug Toolbar is installed because " + # "DEBUG is set to True. For most cases, you need to avoid installing " + # "the toolbar when running tests. If you feel this check is in error, " + # "you can set `DEBUG_TOOLBAR_CONFIG['IS_RUNNING_TESTS'] = False` to " + # "bypass this check.", + # id="debug_toolbar.W008", + # ) + # ], + + # ) - @patch("debug_toolbar.apps.debug_toolbar_installed_when_running_tests_check", []) def test_debug_toolbar_installed_when_running_tests(self): with self.settings(DEBUG=False): dt_settings.get_config()["IS_RUNNING_TESTS"] = False From 2c21976b8c09064cb8896e736e0e7569216aa649 Mon Sep 17 00:00:00 2001 From: veldakarimi Date: Mon, 11 Mar 2024 10:37:04 +0300 Subject: [PATCH 11/25] fix additional text on step 3 on example app --- docs/installation.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/installation.rst b/docs/installation.rst index 60f383a07..3644bdd5c 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -81,11 +81,11 @@ Add ``"debug_toolbar"`` to your ``INSTALLED_APPS`` setting: "debug_toolbar", # ... ] - .. note:: Check out the configuration example in the - [example app](https://github.com/jazzband/django-debug-toolbar/tree/main/example) - to learn how to set up the toolbar to function smoothly while running - your tests. + `example app + `_ + to learn how to set up the toolbar to function smoothly while running + your tests. 4. Add the URLs ^^^^^^^^^^^^^^^ From 7f5da549fb93d86ccfb993000234cc370bcace75 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 11 Mar 2024 15:17:15 +0000 Subject: [PATCH 12/25] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_checks.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_checks.py b/tests/test_checks.py index 3d2ba21ad..e1ab8eda5 100644 --- a/tests/test_checks.py +++ b/tests/test_checks.py @@ -1,5 +1,5 @@ from unittest.mock import patch -import django + from django.conf import settings from django.core.checks import Error, Warning, run_checks from django.test import SimpleTestCase, override_settings @@ -10,7 +10,6 @@ PATH_DOES_NOT_EXIST = os.path.join(settings.BASE_DIR, "tests", "invalid_static") - class ChecksTestCase(SimpleTestCase): @override_settings( MIDDLEWARE=[ @@ -253,7 +252,6 @@ def test_check_w007_invalid(self, mocked_guess_type): ], ) - def test_debug_toolbar_installed_when_running_tests(self): with self.settings(DEBUG=False): dt_settings.get_config()["IS_RUNNING_TESTS"] = False From 713c4cf5183e89371cebcc6af70c704f7c7895ba Mon Sep 17 00:00:00 2001 From: veldakarimi Date: Mon, 11 Mar 2024 18:21:37 +0300 Subject: [PATCH 13/25] Remove .vscode/settings.json from tracking --- .vscode/settings.json | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 070cfdad1..000000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "esbonio.sphinx.confDir": "" -} From c7ad6989a0ccd40fd6063f841c0292eeff91dcc2 Mon Sep 17 00:00:00 2001 From: veldakarimi Date: Mon, 11 Mar 2024 18:46:13 +0300 Subject: [PATCH 14/25] Remove vscode.settings.json --- .vscode/settings.json | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 070cfdad1..000000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "esbonio.sphinx.confDir": "" -} From 18ead4e5bf4af4b8a8a1f0834a0861348bd98fe5 Mon Sep 17 00:00:00 2001 From: veldakarimi Date: Mon, 11 Mar 2024 19:14:35 +0300 Subject: [PATCH 15/25] simple fix --- .vscode/settings.json | 3 +++ tests/test_checks.py | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..070cfdad1 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "esbonio.sphinx.confDir": "" +} diff --git a/tests/test_checks.py b/tests/test_checks.py index e1ab8eda5..68b96eaf7 100644 --- a/tests/test_checks.py +++ b/tests/test_checks.py @@ -1,14 +1,11 @@ from unittest.mock import patch -from django.conf import settings from django.core.checks import Error, Warning, run_checks from django.test import SimpleTestCase, override_settings from debug_toolbar import settings as dt_settings from debug_toolbar.apps import debug_toolbar_installed_when_running_tests_check -PATH_DOES_NOT_EXIST = os.path.join(settings.BASE_DIR, "tests", "invalid_static") - class ChecksTestCase(SimpleTestCase): @override_settings( From a515cc143705edad1259de2e49ee6f928dbd2c59 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 25 Mar 2024 12:18:57 +0000 Subject: [PATCH 16/25] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- debug_toolbar/apps.py | 1 + tests/test_checks.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/debug_toolbar/apps.py b/debug_toolbar/apps.py index a4c1accf7..ffae34209 100644 --- a/debug_toolbar/apps.py +++ b/debug_toolbar/apps.py @@ -229,6 +229,7 @@ def debug_toolbar_installed_when_running_tests_check(app_configs, **kwargs): else: return [] + def check_settings(app_configs, **kwargs): errors = [] USER_CONFIG = getattr(settings, "DEBUG_TOOLBAR_CONFIG", {}) diff --git a/tests/test_checks.py b/tests/test_checks.py index b3f00c1e5..6362d47b0 100644 --- a/tests/test_checks.py +++ b/tests/test_checks.py @@ -280,6 +280,7 @@ def test_debug_toolbar_installed_when_running_tests(self): ) ], ) + @override_settings( DEBUG_TOOLBAR_CONFIG={"OBSERVE_REQUEST_CALLBACK": lambda request: False} ) @@ -287,4 +288,3 @@ def test_observe_request_callback_specified(self): errors = run_checks() self.assertEqual(len(errors), 1) self.assertEqual(errors[0].id, "debug_toolbar.W008") - From 4820a9a91930a5aef5cefc7d551b0645fb742a52 Mon Sep 17 00:00:00 2001 From: veldakarimi Date: Fri, 26 Apr 2024 18:24:59 +0300 Subject: [PATCH 17/25] fix the assertion error --- debug_toolbar/apps.py | 7 ++++--- docs/changes.rst | 2 ++ tests/test_checks.py | 17 +++++------------ 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/debug_toolbar/apps.py b/debug_toolbar/apps.py index ffae34209..614ce98f7 100644 --- a/debug_toolbar/apps.py +++ b/debug_toolbar/apps.py @@ -177,7 +177,7 @@ def check_panels(app_configs, **kwargs): return errors -@register() +@register def js_mimetype_check(app_configs, **kwargs): """ Check that JavaScript files are resolving to the correct content type. @@ -208,7 +208,7 @@ def js_mimetype_check(app_configs, **kwargs): return [] -@register() +@register def debug_toolbar_installed_when_running_tests_check(app_configs, **kwargs): """ Check that the toolbar is not being used when tests are running @@ -223,13 +223,14 @@ def debug_toolbar_installed_when_running_tests_check(app_configs, **kwargs): "the toolbar when running tests. If you feel this check is in error, " "you can set `DEBUG_TOOLBAR_CONFIG['IS_RUNNING_TESTS'] = False` to " "bypass this check.", - id="debug_toolbar.W008", + id="debug_toolbar.W009", ) ] else: return [] +@register def check_settings(app_configs, **kwargs): errors = [] USER_CONFIG = getattr(settings, "DEBUG_TOOLBAR_CONFIG", {}) diff --git a/docs/changes.rst b/docs/changes.rst index 959d42dde..7a737231f 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -22,6 +22,8 @@ Pending ``DEBUG_TOOLBAR_SETTINGS``. * Add a note on the profiling panel about using Python 3.12 and later about needing ``--nothreading`` +* Fixed the bug causing ``'djdt' is not a registered namespace`` and updated + docs to help in initial configuration while running tests. 4.3.0 (2024-02-01) ------------------ diff --git a/tests/test_checks.py b/tests/test_checks.py index 6362d47b0..1718fa4c6 100644 --- a/tests/test_checks.py +++ b/tests/test_checks.py @@ -101,16 +101,6 @@ def test_panels_is_empty(self): "settings.py.", id="debug_toolbar.W005", ), - Error( - "The Django Debug Toolbar can't be used with tests", - hint="Django changes the DEBUG setting to False when running " - "tests. By default the Django Debug Toolbar is installed because " - "DEBUG is set to True. For most cases, you need to avoid installing " - "the toolbar when running tests. If you feel this check is in error, " - "you can set `DEBUG_TOOLBAR_CONFIG['IS_RUNNING_TESTS'] = False` to " - "bypass this check.", - id="debug_toolbar.W008", - ), ], ) @@ -276,13 +266,16 @@ def test_debug_toolbar_installed_when_running_tests(self): "the toolbar when running tests. If you feel this check is in error, " "you can set `DEBUG_TOOLBAR_CONFIG['IS_RUNNING_TESTS'] = False` to " "bypass this check.", - id="debug_toolbar.W008", + id="debug_toolbar.W009", ) ], ) @override_settings( - DEBUG_TOOLBAR_CONFIG={"OBSERVE_REQUEST_CALLBACK": lambda request: False} + DEBUG_TOOLBAR_CONFIG={ + "OBSERVE_REQUEST_CALLBACK": lambda request: False, + "IS_RUNNING_TESTS": False, + } ) def test_observe_request_callback_specified(self): errors = run_checks() From 424bf0235881c253b17916b4b4e39254ae167043 Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Fri, 26 Apr 2024 11:04:56 -0500 Subject: [PATCH 18/25] Delete .vscode/settings.json --- .vscode/settings.json | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 070cfdad1..000000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "esbonio.sphinx.confDir": "" -} From c620b7c115ec00ab11b8851e07e53e67dd27558e Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Fri, 26 Apr 2024 11:05:54 -0500 Subject: [PATCH 19/25] Remove space from docs make file --- docs/Makefile | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/Makefile b/docs/Makefile index bebc1f1b7..ee27505aa 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -8,7 +8,6 @@ SPHINXBUILD ?= sphinx-build SOURCEDIR = . BUILDDIR = _build - # Put it first so that "make" without argument is like "make help". help: @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) From effccffa17dfdb0500d3e5225658dd93eddb8075 Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Fri, 26 Apr 2024 11:08:42 -0500 Subject: [PATCH 20/25] Clean-up the changelog --- docs/changes.rst | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/docs/changes.rst b/docs/changes.rst index 7a737231f..ea6105671 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -22,8 +22,16 @@ Pending ``DEBUG_TOOLBAR_SETTINGS``. * Add a note on the profiling panel about using Python 3.12 and later about needing ``--nothreading`` +* Added ``RUNNING_TESTS`` setting to allow overriding the + ``debug_toolbar.W009`` check to avoid including the toolbar when running + tests. * Fixed the bug causing ``'djdt' is not a registered namespace`` and updated docs to help in initial configuration while running tests. +* Added a link in the installation docs to a more complete installation + example in the example app. +* Added check to prevent the toolbar from being installed when tests + are running. +* Added test to example app and command to run the example app's tests. 4.3.0 (2024-02-01) ------------------ @@ -43,11 +51,6 @@ Pending `__. * Changed the default position of the toolbar from top to the upper top position. -* Added a link in the installation docs to a more complete installation - example in the example app. -* Added check to prevent the toolbar from being installed when tests - are running. -* Added test to example app and command to run the example app's tests. * Added the setting, ``UPDATE_ON_FETCH`` to control whether the toolbar automatically updates to the latest AJAX request or not. It defaults to ``False``. From 902665c1c37489e3642869bd7faf2650b0b42cf3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 26 Apr 2024 16:10:45 +0000 Subject: [PATCH 21/25] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/changes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/changes.rst b/docs/changes.rst index ea6105671..70fa57186 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -22,7 +22,7 @@ Pending ``DEBUG_TOOLBAR_SETTINGS``. * Add a note on the profiling panel about using Python 3.12 and later about needing ``--nothreading`` -* Added ``RUNNING_TESTS`` setting to allow overriding the +* Added ``RUNNING_TESTS`` setting to allow overriding the ``debug_toolbar.W009`` check to avoid including the toolbar when running tests. * Fixed the bug causing ``'djdt' is not a registered namespace`` and updated From 3cb2f7cb877d119f2e9081a97972990e8b8a8a4c Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Fri, 26 Apr 2024 11:13:50 -0500 Subject: [PATCH 22/25] Add docs for the IS_RUNNING_TESTS setting --- docs/configuration.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/configuration.rst b/docs/configuration.rst index b7db25900..2af0b7fa4 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -72,6 +72,17 @@ Toolbar options The toolbar searches for this string in the HTML and inserts itself just before. +* ``IS_RUNNING_TESTS`` + + Default: ``"test" in sys.argv`` + + This setting whether the application is running tests. If this resolves to + ``True``, the toolbar will prevent you from running tests. This should only + be changed if your test command doesn't include ``test`` or if you wish to + test your application with the toolbar configured. If you do wish to test + your application with the toolbar configured, set this setting to + ``False``. + .. _RENDER_PANELS: * ``RENDER_PANELS`` From a18240160487c8fc4b98cb7920a496e04d66cf2e Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Fri, 26 Apr 2024 16:41:01 -0500 Subject: [PATCH 23/25] Apply suggestions from code review Co-authored-by: Matthias Kestenholz --- docs/changes.rst | 2 +- tests/settings.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/changes.rst b/docs/changes.rst index 70fa57186..fa937fd14 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -22,7 +22,7 @@ Pending ``DEBUG_TOOLBAR_SETTINGS``. * Add a note on the profiling panel about using Python 3.12 and later about needing ``--nothreading`` -* Added ``RUNNING_TESTS`` setting to allow overriding the +* Added ``IS_RUNNING_TESTS`` setting to allow overriding the ``debug_toolbar.W009`` check to avoid including the toolbar when running tests. * Fixed the bug causing ``'djdt' is not a registered namespace`` and updated diff --git a/tests/settings.py b/tests/settings.py index f1ef5a7f2..269900c18 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -127,5 +127,6 @@ DEBUG_TOOLBAR_CONFIG = { # Django's test client sets wsgi.multiprocess to True inappropriately "RENDER_PANELS": False, + # IS_RUNNING_TESTS must be False even though we're running tests because we're running the toolbar's own tests. "IS_RUNNING_TESTS": False, } From 9d906fbd5d9c41f78bd22c03642815fa29902476 Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Fri, 26 Apr 2024 16:48:13 -0500 Subject: [PATCH 24/25] Change the code for the toolbar testing error to E001 --- debug_toolbar/apps.py | 2 +- docs/changes.rst | 2 +- tests/test_checks.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/debug_toolbar/apps.py b/debug_toolbar/apps.py index 614ce98f7..a2e977d84 100644 --- a/debug_toolbar/apps.py +++ b/debug_toolbar/apps.py @@ -223,7 +223,7 @@ def debug_toolbar_installed_when_running_tests_check(app_configs, **kwargs): "the toolbar when running tests. If you feel this check is in error, " "you can set `DEBUG_TOOLBAR_CONFIG['IS_RUNNING_TESTS'] = False` to " "bypass this check.", - id="debug_toolbar.W009", + id="debug_toolbar.E001", ) ] else: diff --git a/docs/changes.rst b/docs/changes.rst index fa937fd14..997a997f2 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -23,7 +23,7 @@ Pending * Add a note on the profiling panel about using Python 3.12 and later about needing ``--nothreading`` * Added ``IS_RUNNING_TESTS`` setting to allow overriding the - ``debug_toolbar.W009`` check to avoid including the toolbar when running + ``debug_toolbar.E001`` check to avoid including the toolbar when running tests. * Fixed the bug causing ``'djdt' is not a registered namespace`` and updated docs to help in initial configuration while running tests. diff --git a/tests/test_checks.py b/tests/test_checks.py index 1718fa4c6..45b7e9be2 100644 --- a/tests/test_checks.py +++ b/tests/test_checks.py @@ -266,7 +266,7 @@ def test_debug_toolbar_installed_when_running_tests(self): "the toolbar when running tests. If you feel this check is in error, " "you can set `DEBUG_TOOLBAR_CONFIG['IS_RUNNING_TESTS'] = False` to " "bypass this check.", - id="debug_toolbar.W009", + id="debug_toolbar.E001", ) ], ) From 0e908842b3ade4f05eabb54d545731825a2fd6f4 Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Fri, 26 Apr 2024 16:50:47 -0500 Subject: [PATCH 25/25] Reduce number of .settings calls and document config update. --- tests/test_checks.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/test_checks.py b/tests/test_checks.py index 45b7e9be2..827886db1 100644 --- a/tests/test_checks.py +++ b/tests/test_checks.py @@ -240,19 +240,21 @@ def test_check_w007_invalid(self, mocked_guess_type): ) def test_debug_toolbar_installed_when_running_tests(self): - with self.settings(DEBUG=False): - dt_settings.get_config()["IS_RUNNING_TESTS"] = False - errors = debug_toolbar_installed_when_running_tests_check(None) - self.assertEqual(len(errors), 0) with self.settings(DEBUG=True): + # Update the config options because self.settings() + # would require redefining DEBUG_TOOLBAR_CONFIG entirely. dt_settings.get_config()["IS_RUNNING_TESTS"] = True errors = debug_toolbar_installed_when_running_tests_check(None) self.assertEqual(len(errors), 0) - with self.settings(DEBUG=True): + dt_settings.get_config()["IS_RUNNING_TESTS"] = False errors = debug_toolbar_installed_when_running_tests_check(None) self.assertEqual(len(errors), 0) with self.settings(DEBUG=False): + dt_settings.get_config()["IS_RUNNING_TESTS"] = False + errors = debug_toolbar_installed_when_running_tests_check(None) + self.assertEqual(len(errors), 0) + dt_settings.get_config()["IS_RUNNING_TESTS"] = True errors = debug_toolbar_installed_when_running_tests_check(None) self.assertEqual(