2021-03-03 18:10:03 +08:00
|
|
|
from pathlib import Path
|
2017-01-03 06:35:43 +08:00
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.staticfiles.checks import check_finders
|
2021-02-26 20:23:26 +08:00
|
|
|
from django.contrib.staticfiles.finders import BaseFinder, get_finder
|
|
|
|
from django.core.checks import Error, Warning
|
2021-03-03 18:10:03 +08:00
|
|
|
from django.test import override_settings
|
2017-01-03 06:35:43 +08:00
|
|
|
|
2021-03-03 18:10:03 +08:00
|
|
|
from .cases import CollectionTestCase
|
|
|
|
from .settings import TEST_ROOT
|
2017-01-03 06:35:43 +08:00
|
|
|
|
2021-03-03 18:10:03 +08:00
|
|
|
|
|
|
|
class FindersCheckTests(CollectionTestCase):
|
|
|
|
run_collectstatic_in_setUp = False
|
2017-01-03 06:35:43 +08:00
|
|
|
|
|
|
|
def test_base_finder_check_not_implemented(self):
|
|
|
|
finder = BaseFinder()
|
2022-02-04 03:24:19 +08:00
|
|
|
msg = "subclasses may provide a check() method to verify the finder is configured correctly."
|
2017-01-03 06:35:43 +08:00
|
|
|
with self.assertRaisesMessage(NotImplementedError, msg):
|
|
|
|
finder.check()
|
|
|
|
|
|
|
|
def test_check_finders(self):
|
|
|
|
"""check_finders() concatenates all errors."""
|
2022-02-04 03:24:19 +08:00
|
|
|
error1 = Error("1")
|
|
|
|
error2 = Error("2")
|
|
|
|
error3 = Error("3")
|
2017-01-03 06:35:43 +08:00
|
|
|
|
|
|
|
def get_finders():
|
|
|
|
class Finder1(BaseFinder):
|
|
|
|
def check(self, **kwargs):
|
|
|
|
return [error1]
|
|
|
|
|
|
|
|
class Finder2(BaseFinder):
|
|
|
|
def check(self, **kwargs):
|
|
|
|
return []
|
|
|
|
|
|
|
|
class Finder3(BaseFinder):
|
|
|
|
def check(self, **kwargs):
|
|
|
|
return [error2, error3]
|
|
|
|
|
|
|
|
class Finder4(BaseFinder):
|
|
|
|
pass
|
|
|
|
|
|
|
|
return [Finder1(), Finder2(), Finder3(), Finder4()]
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
with mock.patch("django.contrib.staticfiles.checks.get_finders", get_finders):
|
2017-01-03 06:35:43 +08:00
|
|
|
errors = check_finders(None)
|
|
|
|
self.assertEqual(errors, [error1, error2, error3])
|
|
|
|
|
|
|
|
def test_no_errors_with_test_settings(self):
|
|
|
|
self.assertEqual(check_finders(None), [])
|
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
@override_settings(STATICFILES_DIRS="a string")
|
2017-01-03 06:35:43 +08:00
|
|
|
def test_dirs_not_tuple_or_list(self):
|
2022-02-04 03:24:19 +08:00
|
|
|
self.assertEqual(
|
|
|
|
check_finders(None),
|
|
|
|
[
|
|
|
|
Error(
|
|
|
|
"The STATICFILES_DIRS setting is not a tuple or list.",
|
|
|
|
hint="Perhaps you forgot a trailing comma?",
|
|
|
|
id="staticfiles.E001",
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)
|
2017-01-03 06:35:43 +08:00
|
|
|
|
|
|
|
def test_dirs_contains_static_root(self):
|
2021-03-03 18:10:03 +08:00
|
|
|
with self.settings(STATICFILES_DIRS=[settings.STATIC_ROOT]):
|
2022-02-04 03:24:19 +08:00
|
|
|
self.assertEqual(
|
|
|
|
check_finders(None),
|
|
|
|
[
|
|
|
|
Error(
|
|
|
|
"The STATICFILES_DIRS setting should not contain the "
|
|
|
|
"STATIC_ROOT setting.",
|
|
|
|
id="staticfiles.E002",
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)
|
2017-01-03 06:35:43 +08:00
|
|
|
|
|
|
|
def test_dirs_contains_static_root_in_tuple(self):
|
2022-02-04 03:24:19 +08:00
|
|
|
with self.settings(STATICFILES_DIRS=[("prefix", settings.STATIC_ROOT)]):
|
|
|
|
self.assertEqual(
|
|
|
|
check_finders(None),
|
|
|
|
[
|
|
|
|
Error(
|
|
|
|
"The STATICFILES_DIRS setting should not contain the "
|
|
|
|
"STATIC_ROOT setting.",
|
|
|
|
id="staticfiles.E002",
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)
|
2018-09-28 07:49:37 +08:00
|
|
|
|
|
|
|
def test_prefix_contains_trailing_slash(self):
|
2022-02-04 03:24:19 +08:00
|
|
|
static_dir = Path(TEST_ROOT) / "project" / "documents"
|
|
|
|
with self.settings(STATICFILES_DIRS=[("prefix/", static_dir)]):
|
|
|
|
self.assertEqual(
|
|
|
|
check_finders(None),
|
|
|
|
[
|
|
|
|
Error(
|
|
|
|
"The prefix 'prefix/' in the STATICFILES_DIRS setting must "
|
|
|
|
"not end with a slash.",
|
|
|
|
id="staticfiles.E003",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2021-02-26 20:23:26 +08:00
|
|
|
|
|
|
|
def test_nonexistent_directories(self):
|
2022-02-04 03:24:19 +08:00
|
|
|
with self.settings(
|
|
|
|
STATICFILES_DIRS=[
|
|
|
|
"/fake/path",
|
|
|
|
("prefix", "/fake/prefixed/path"),
|
|
|
|
]
|
|
|
|
):
|
|
|
|
self.assertEqual(
|
|
|
|
check_finders(None),
|
|
|
|
[
|
|
|
|
Warning(
|
|
|
|
"The directory '/fake/path' in the STATICFILES_DIRS "
|
|
|
|
"setting does not exist.",
|
|
|
|
id="staticfiles.W004",
|
|
|
|
),
|
|
|
|
Warning(
|
|
|
|
"The directory '/fake/prefixed/path' in the "
|
|
|
|
"STATICFILES_DIRS setting does not exist.",
|
|
|
|
id="staticfiles.W004",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2021-02-26 20:23:26 +08:00
|
|
|
# Nonexistent directories are skipped.
|
2022-02-04 03:24:19 +08:00
|
|
|
finder = get_finder("django.contrib.staticfiles.finders.FileSystemFinder")
|
2021-02-26 20:23:26 +08:00
|
|
|
self.assertEqual(list(finder.list(None)), [])
|