Skip to content

Commit

Permalink
Remove obsolete staticfiles check
Browse files Browse the repository at this point in the history
The toolbar had a check for misconfigured static files directories.
However, Django 4.0 added its own check for this situation.  Since the
toolbar's minimum supported Django version is now 4.2, the toolbar's
check is made redundant by Django's check and can thus be removed.
  • Loading branch information
living180 committed Mar 11, 2024
1 parent 0baef8c commit cc48a14
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 85 deletions.
25 changes: 0 additions & 25 deletions debug_toolbar/panels/staticfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from django.conf import settings
from django.contrib.staticfiles import finders, storage
from django.core.checks import Warning
from django.utils.functional import LazyObject
from django.utils.translation import gettext_lazy as _, ngettext

Expand Down Expand Up @@ -178,27 +177,3 @@ def get_staticfiles_apps(self):
if app not in apps:
apps.append(app)
return apps

@classmethod
def run_checks(cls):
"""
Check that the integration is configured correctly for the panel.
Specifically look for static files that haven't been collected yet.
Return a list of :class: `django.core.checks.CheckMessage` instances.
"""
errors = []
for finder in finders.get_finders():
try:
for path, finder_storage in finder.list([]):
finder_storage.path(path)
except OSError:
errors.append(
Warning(
"debug_toolbar requires the STATICFILES_DIRS directories to exist.",
hint="Running manage.py collectstatic may help uncover the issue.",
id="debug_toolbar.staticfiles.W001",
)
)
return errors
4 changes: 4 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Pending
* Stayed on top of pre-commit hook updates.
* Added :doc:`architecture documentation <architecture>` to help
on-board new contributors.
* Removed the static file path validation check in
:class:`StaticFilesPanel <debug_toolbar.panels.staticfiles.StaticFilesPanel>`
since that check is made redundant by a similar check in Django 4.0 and
later.

4.3.0 (2024-02-01)
------------------
Expand Down
37 changes: 0 additions & 37 deletions tests/panels/test_staticfiles.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import os
import unittest

import django
from django.conf import settings
from django.contrib.staticfiles import finders
from django.test.utils import override_settings

from ..base import BaseTestCase

PATH_DOES_NOT_EXIST = os.path.join(settings.BASE_DIR, "tests", "invalid_static")


class StaticFilesPanelTestCase(BaseTestCase):
panel_id = "StaticFilesPanel"
Expand Down Expand Up @@ -52,33 +45,3 @@ def test_insert_content(self):
"django.contrib.staticfiles.finders.AppDirectoriesFinder", content
)
self.assertValidHTML(content)

@unittest.skipIf(django.VERSION >= (4,), "Django>=4 handles missing dirs itself.")
@override_settings(
STATICFILES_DIRS=[PATH_DOES_NOT_EXIST] + settings.STATICFILES_DIRS,
STATIC_ROOT=PATH_DOES_NOT_EXIST,
)
def test_finder_directory_does_not_exist(self):
"""Misconfigure the static files settings and verify the toolbar runs.
The test case is that the STATIC_ROOT is in STATICFILES_DIRS and that
the directory of STATIC_ROOT does not exist.
"""
response = self.panel.process_request(self.request)
self.panel.generate_stats(self.request, response)
content = self.panel.content
self.assertIn(
"django.contrib.staticfiles.finders.AppDirectoriesFinder", content
)
self.assertNotIn(
"django.contrib.staticfiles.finders.FileSystemFinder (2 files)", content
)
self.assertEqual(self.panel.num_used, 0)
self.assertNotEqual(self.panel.num_found, 0)
expected_apps = ["django.contrib.admin", "debug_toolbar"]
if settings.USE_GIS:
expected_apps = ["django.contrib.gis"] + expected_apps
self.assertEqual(self.panel.get_staticfiles_apps(), expected_apps)
self.assertEqual(
self.panel.get_staticfiles_dirs(), finders.FileSystemFinder().locations
)
23 changes: 0 additions & 23 deletions tests/test_checks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import os
import unittest
from unittest.mock import patch

import django
from django.conf import settings
from django.core.checks import Warning, run_checks
from django.test import SimpleTestCase, override_settings

PATH_DOES_NOT_EXIST = os.path.join(settings.BASE_DIR, "tests", "invalid_static")


class ChecksTestCase(SimpleTestCase):
@override_settings(
Expand Down Expand Up @@ -92,23 +86,6 @@ def test_check_middleware_classes_error(self):
messages,
)

@unittest.skipIf(django.VERSION >= (4,), "Django>=4 handles missing dirs itself.")
@override_settings(
STATICFILES_DIRS=[PATH_DOES_NOT_EXIST],
)
def test_panel_check_errors(self):
messages = run_checks()
self.assertEqual(
messages,
[
Warning(
"debug_toolbar requires the STATICFILES_DIRS directories to exist.",
hint="Running manage.py collectstatic may help uncover the issue.",
id="debug_toolbar.staticfiles.W001",
)
],
)

@override_settings(DEBUG_TOOLBAR_PANELS=[])
def test_panels_is_empty(self):
errors = run_checks()
Expand Down

0 comments on commit cc48a14

Please sign in to comment.