Skip to content

Commit

Permalink
fix: make template_loader consider tuples for STATICFILES_DIRS (#489)
Browse files Browse the repository at this point in the history
Co-authored-by: Juro Oravec <[email protected]>
  • Loading branch information
alexandreMartinEcl and JuroOravec authored May 7, 2024
1 parent 085c60a commit d18aefc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/django_components/template_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ def get_dirs(self) -> List[Path]:

directories: Set[Path] = set()
for component_dir in component_dirs:
if isinstance(component_dir, (tuple, list)) and len(component_dir) == 2:
component_dir = component_dir[1]
try:
Path(component_dir)
except TypeError:
logger.warning(
f"STATICFILES_DIRS expected str, bytes or os.PathLike object, or tuple/list of length 2. "
f"See Django documentation. Got {type(component_dir)} : {component_dir}"
)
continue
curr_directories: Set[Path] = set()

# For each dir in `settings.STATICFILES_DIRS`, we go over all Django apps
Expand Down
24 changes: 24 additions & 0 deletions tests/test_autodiscover.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pathlib import Path
from unittest import mock
from unittest.mock import MagicMock, patch

from django.template.engine import Engine
from django.urls import include, path
Expand Down Expand Up @@ -125,6 +126,29 @@ def test_base_dir(self):
self.assertEqual(sorted(dirs), sorted(expected))


class TestStaticFilesDirs(BaseTestCase):
def setUp(self):
settings.STATICFILES_DIRS = [
"components",
("with_alias", "components"),
("too_many", "items", "components"),
("with_not_str_alias", 3),
] # noqa

def tearDown(self) -> None:
del settings.STATICFILES_DIRS # noqa

@patch("django_components.template_loader.logger.warning")
def test_static_files_dirs(self, mock_warning: MagicMock):
mock_warning.reset_mock()
current_engine = Engine.get_default()
Loader(current_engine).get_dirs()

warn_inputs = [warn.args[0] for warn in mock_warning.call_args_list]
assert "Got <class 'tuple'> : ('too_many', 'items', 'components')" in warn_inputs[0]
assert "Got <class 'int'> : 3" in warn_inputs[1]


class TestFilepathToPythonModule(BaseTestCase):
def test_prepares_path(self):
self.assertEqual(
Expand Down

0 comments on commit d18aefc

Please sign in to comment.