mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Fixed #25791 -- Implement autoreload behaviour for cached template loader.
This commit is contained in:
parent
29845ecf69
commit
658bcc16f1
7 changed files with 191 additions and 7 deletions
92
tests/template_tests/test_autoreloader.py
Normal file
92
tests/template_tests/test_autoreloader.py
Normal file
|
@ -0,0 +1,92 @@
|
|||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from django.template import autoreload
|
||||
from django.test import SimpleTestCase, override_settings
|
||||
from django.test.utils import require_jinja2
|
||||
|
||||
ROOT = Path(__file__).parent.absolute()
|
||||
EXTRA_TEMPLATES_DIR = ROOT / "templates_extra"
|
||||
|
||||
|
||||
@override_settings(
|
||||
INSTALLED_APPS=['template_tests'],
|
||||
TEMPLATES=[{
|
||||
'BACKEND': 'django.template.backends.dummy.TemplateStrings',
|
||||
'APP_DIRS': True,
|
||||
}, {
|
||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||
'DIRS': [EXTRA_TEMPLATES_DIR],
|
||||
'OPTIONS': {
|
||||
'context_processors': [
|
||||
'django.template.context_processors.request',
|
||||
],
|
||||
'loaders': [
|
||||
'django.template.loaders.filesystem.Loader',
|
||||
'django.template.loaders.app_directories.Loader',
|
||||
]
|
||||
},
|
||||
}])
|
||||
class TemplateReloadTests(SimpleTestCase):
|
||||
@mock.patch('django.template.autoreload.reset_loaders')
|
||||
def test_template_changed(self, mock_reset):
|
||||
template_path = Path(__file__).parent / 'templates' / 'index.html'
|
||||
self.assertTrue(autoreload.template_changed(None, template_path))
|
||||
mock_reset.assert_called_once()
|
||||
|
||||
@mock.patch('django.template.autoreload.reset_loaders')
|
||||
def test_non_template_changed(self, mock_reset):
|
||||
self.assertIsNone(autoreload.template_changed(None, Path(__file__)))
|
||||
mock_reset.assert_not_called()
|
||||
|
||||
def test_watch_for_template_changes(self):
|
||||
mock_reloader = mock.MagicMock()
|
||||
autoreload.watch_for_template_changes(mock_reloader)
|
||||
self.assertSequenceEqual(
|
||||
sorted(mock_reloader.watch_dir.call_args_list),
|
||||
[
|
||||
mock.call(ROOT / 'templates', '**/*'),
|
||||
mock.call(ROOT / 'templates_extra', '**/*')
|
||||
]
|
||||
)
|
||||
|
||||
def test_get_template_directories(self):
|
||||
self.assertSetEqual(
|
||||
autoreload.get_template_directories(),
|
||||
{
|
||||
ROOT / 'templates_extra',
|
||||
ROOT / 'templates',
|
||||
}
|
||||
)
|
||||
|
||||
@mock.patch('django.template.loaders.base.Loader.reset')
|
||||
def test_reset_all_loaders(self, mock_reset):
|
||||
autoreload.reset_loaders()
|
||||
self.assertEqual(mock_reset.call_count, 2)
|
||||
|
||||
|
||||
@require_jinja2
|
||||
@override_settings(INSTALLED_APPS=['template_tests'])
|
||||
class Jinja2TemplateReloadTests(SimpleTestCase):
|
||||
def test_watch_for_template_changes(self):
|
||||
mock_reloader = mock.MagicMock()
|
||||
autoreload.watch_for_template_changes(mock_reloader)
|
||||
self.assertSequenceEqual(
|
||||
sorted(mock_reloader.watch_dir.call_args_list),
|
||||
[
|
||||
mock.call(ROOT / 'templates', '**/*'),
|
||||
]
|
||||
)
|
||||
|
||||
def test_get_template_directories(self):
|
||||
self.assertSetEqual(
|
||||
autoreload.get_template_directories(),
|
||||
{
|
||||
ROOT / 'templates',
|
||||
}
|
||||
)
|
||||
|
||||
@mock.patch('django.template.loaders.base.Loader.reset')
|
||||
def test_reset_all_loaders(self, mock_reset):
|
||||
autoreload.reset_loaders()
|
||||
self.assertEqual(mock_reset.call_count, 0)
|
|
@ -14,6 +14,8 @@ from pathlib import Path
|
|||
from subprocess import CompletedProcess
|
||||
from unittest import mock, skip, skipIf
|
||||
|
||||
import pytz
|
||||
|
||||
import django.__main__
|
||||
from django.apps.registry import Apps
|
||||
from django.test import SimpleTestCase
|
||||
|
@ -201,6 +203,26 @@ class TestChildArguments(SimpleTestCase):
|
|||
autoreload.get_child_arguments()
|
||||
|
||||
|
||||
class TestUtilities(SimpleTestCase):
|
||||
def test_is_django_module(self):
|
||||
for module, expected in (
|
||||
(pytz, False),
|
||||
(sys, False),
|
||||
(autoreload, True)
|
||||
):
|
||||
with self.subTest(module=module):
|
||||
self.assertIs(autoreload.is_django_module(module), expected)
|
||||
|
||||
def test_is_django_path(self):
|
||||
for module, expected in (
|
||||
(pytz.__file__, False),
|
||||
(contextlib.__file__, False),
|
||||
(autoreload.__file__, True)
|
||||
):
|
||||
with self.subTest(module=module):
|
||||
self.assertIs(autoreload.is_django_path(module), expected)
|
||||
|
||||
|
||||
class TestCommonRoots(SimpleTestCase):
|
||||
def test_common_roots(self):
|
||||
paths = (
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue