Skip to content

Commit e9a4695

Browse files
committed
Merge pull request #21 from charettes/allow-multiple-dtl-engines
Fix #20 -- Allow multiple DTL engines.
2 parents 1eeb2c4 + cd4d4c2 commit e9a4695

File tree

4 files changed

+53
-24
lines changed

4 files changed

+53
-24
lines changed

django_coverage_plugin/plugin.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,11 @@ def check_debug():
5151
from django.conf import settings
5252
templates = getattr(settings, 'TEMPLATES', [])
5353
if templates:
54-
# New-style settings.
55-
if len(templates) > 1:
56-
raise DjangoTemplatePluginException("Can't use multiple template engines.")
57-
template_settings = templates[0]
58-
if template_settings['BACKEND'] != 'django.template.backends.django.DjangoTemplates':
59-
raise DjangoTemplatePluginException("Can't use non-Django templates.")
60-
if not template_settings.get('OPTIONS', {}).get('debug', False):
61-
raise DjangoTemplatePluginException("Template debugging must be enabled in settings.")
54+
for template_settings in templates:
55+
if template_settings['BACKEND'] != 'django.template.backends.django.DjangoTemplates':
56+
raise DjangoTemplatePluginException("Can't use non-Django templates.")
57+
if not template_settings.get('OPTIONS', {}).get('debug', False):
58+
raise DjangoTemplatePluginException("Template debugging must be enabled in settings.")
6259
else:
6360
# Old-style settings.
6461
if not settings.TEMPLATE_DEBUG:

tests/plugin_test.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def make_template(self, text, name=None):
9090
return os.path.abspath(self.make_file(template_path, text))
9191

9292
def run_django_coverage(
93-
self, name=None, text=None, context=None, options=None,
93+
self, name=None, text=None, context=None, options=None, using=None
9494
):
9595
"""Run a template under coverage.
9696
@@ -109,7 +109,14 @@ def run_django_coverage(
109109
if options is None:
110110
options = {'source': ["."]}
111111

112-
if text is not None:
112+
if using:
113+
from django.template import engines
114+
engine = engines[using]
115+
if text is not None:
116+
tem = engine.from_string(text)
117+
else:
118+
tem = engine.get_template(name or self.template_file)
119+
elif text is not None:
113120
tem = Template(text)
114121
else:
115122
tem = get_template(name or self.template_file)

tests/test_engines.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
2+
# For details: https://github.com/nedbat/django_coverage_plugin/blob/master/NOTICE.txt
3+
4+
"""Tests of multiple engines for django_coverage_plugin."""
5+
6+
from .plugin_test import DjangoPluginTestCase, django_start_at
7+
8+
9+
@django_start_at(1, 8)
10+
class MultipleEngineTests(DjangoPluginTestCase):
11+
@classmethod
12+
def setUpClass(cls):
13+
# Move to module imports once we drop support for Django < 1.7
14+
from django.test import modify_settings
15+
engine = {
16+
'NAME': 'other',
17+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
18+
'DIRS': ['templates'], # where the tests put things.
19+
'OPTIONS': {
20+
'debug': True,
21+
},
22+
}
23+
cls._modified_settings = modify_settings(TEMPLATES={'append': [engine]})
24+
cls._modified_settings.enable()
25+
26+
@classmethod
27+
def tearDownClass(cls):
28+
cls._modified_settings.disable()
29+
30+
def test_file_template(self):
31+
self.make_template('Hello')
32+
text = self.run_django_coverage(using='other')
33+
self.assertEqual(text, 'Hello')
34+
self.assert_analysis([1])
35+
36+
def test_string_template(self):
37+
text = self.run_django_coverage(text='Hello', using='other')
38+
self.assertEqual(text, 'Hello')

tests/test_settings.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,12 @@
2121
NO_OPTIONS_OVERRIDES = test_settings()
2222
del NO_OPTIONS_OVERRIDES['TEMPLATES'][0]['OPTIONS']
2323

24-
MULTIPLE_ENGINE_OVERRIDES = test_settings()
25-
MULTIPLE_ENGINE_OVERRIDES['TEMPLATES'].append({
26-
'BACKEND': NON_DJANGO_BACKEND,
27-
})
28-
2924
OTHER_ENGINE_OVERRIDES = test_settings()
3025
OTHER_ENGINE_OVERRIDES['TEMPLATES'][0]['BACKEND'] = NON_DJANGO_BACKEND
3126
OTHER_ENGINE_OVERRIDES['TEMPLATES'][0]['OPTIONS'] = {}
3227
else:
3328
DEBUG_FALSE_OVERRIDES = {'TEMPLATE_DEBUG': False}
34-
NO_OPTIONS_OVERRIDES = MULTIPLE_ENGINE_OVERRIDES = OTHER_ENGINE_OVERRIDES = {}
29+
NO_OPTIONS_OVERRIDES = OTHER_ENGINE_OVERRIDES = {}
3530

3631

3732
class SettingsTest(DjangoPluginTestCase):
@@ -51,14 +46,6 @@ def test_no_options(self):
5146
with self.assertRaisesRegexp(DjangoTemplatePluginException, msg):
5247
self.run_django_coverage()
5348

54-
@django_start_at(1, 8)
55-
@override_settings(**MULTIPLE_ENGINE_OVERRIDES)
56-
def test_multiple_engines(self):
57-
self.make_template('Hello')
58-
msg = "Can't use multiple template engines."
59-
with self.assertRaisesRegexp(DjangoTemplatePluginException, msg):
60-
self.run_django_coverage()
61-
6249
@django_start_at(1, 8)
6350
@override_settings(**OTHER_ENGINE_OVERRIDES)
6451
def test_other_engine(self):

0 commit comments

Comments
 (0)