Skip to content

Commit

Permalink
Merge pull request #21 from charettes/allow-multiple-dtl-engines
Browse files Browse the repository at this point in the history
Fix #20 -- Allow multiple DTL engines.
  • Loading branch information
nedbat committed Apr 3, 2016
2 parents 1eeb2c4 + cd4d4c2 commit e9a4695
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 24 deletions.
13 changes: 5 additions & 8 deletions django_coverage_plugin/plugin.py
Expand Up @@ -51,14 +51,11 @@ def check_debug():
from django.conf import settings
templates = getattr(settings, 'TEMPLATES', [])
if templates:
# New-style settings.
if len(templates) > 1:
raise DjangoTemplatePluginException("Can't use multiple template engines.")
template_settings = templates[0]
if template_settings['BACKEND'] != 'django.template.backends.django.DjangoTemplates':
raise DjangoTemplatePluginException("Can't use non-Django templates.")
if not template_settings.get('OPTIONS', {}).get('debug', False):
raise DjangoTemplatePluginException("Template debugging must be enabled in settings.")
for template_settings in templates:
if template_settings['BACKEND'] != 'django.template.backends.django.DjangoTemplates':
raise DjangoTemplatePluginException("Can't use non-Django templates.")
if not template_settings.get('OPTIONS', {}).get('debug', False):
raise DjangoTemplatePluginException("Template debugging must be enabled in settings.")
else:
# Old-style settings.
if not settings.TEMPLATE_DEBUG:
Expand Down
11 changes: 9 additions & 2 deletions tests/plugin_test.py
Expand Up @@ -90,7 +90,7 @@ def make_template(self, text, name=None):
return os.path.abspath(self.make_file(template_path, text))

def run_django_coverage(
self, name=None, text=None, context=None, options=None,
self, name=None, text=None, context=None, options=None, using=None
):
"""Run a template under coverage.
Expand All @@ -109,7 +109,14 @@ def run_django_coverage(
if options is None:
options = {'source': ["."]}

if text is not None:
if using:
from django.template import engines
engine = engines[using]
if text is not None:
tem = engine.from_string(text)
else:
tem = engine.get_template(name or self.template_file)
elif text is not None:
tem = Template(text)
else:
tem = get_template(name or self.template_file)
Expand Down
38 changes: 38 additions & 0 deletions tests/test_engines.py
@@ -0,0 +1,38 @@
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/django_coverage_plugin/blob/master/NOTICE.txt

"""Tests of multiple engines for django_coverage_plugin."""

from .plugin_test import DjangoPluginTestCase, django_start_at


@django_start_at(1, 8)
class MultipleEngineTests(DjangoPluginTestCase):
@classmethod
def setUpClass(cls):
# Move to module imports once we drop support for Django < 1.7
from django.test import modify_settings
engine = {
'NAME': 'other',
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'], # where the tests put things.
'OPTIONS': {
'debug': True,
},
}
cls._modified_settings = modify_settings(TEMPLATES={'append': [engine]})
cls._modified_settings.enable()

@classmethod
def tearDownClass(cls):
cls._modified_settings.disable()

def test_file_template(self):
self.make_template('Hello')
text = self.run_django_coverage(using='other')
self.assertEqual(text, 'Hello')
self.assert_analysis([1])

def test_string_template(self):
text = self.run_django_coverage(text='Hello', using='other')
self.assertEqual(text, 'Hello')
15 changes: 1 addition & 14 deletions tests/test_settings.py
Expand Up @@ -21,17 +21,12 @@
NO_OPTIONS_OVERRIDES = test_settings()
del NO_OPTIONS_OVERRIDES['TEMPLATES'][0]['OPTIONS']

MULTIPLE_ENGINE_OVERRIDES = test_settings()
MULTIPLE_ENGINE_OVERRIDES['TEMPLATES'].append({
'BACKEND': NON_DJANGO_BACKEND,
})

OTHER_ENGINE_OVERRIDES = test_settings()
OTHER_ENGINE_OVERRIDES['TEMPLATES'][0]['BACKEND'] = NON_DJANGO_BACKEND
OTHER_ENGINE_OVERRIDES['TEMPLATES'][0]['OPTIONS'] = {}
else:
DEBUG_FALSE_OVERRIDES = {'TEMPLATE_DEBUG': False}
NO_OPTIONS_OVERRIDES = MULTIPLE_ENGINE_OVERRIDES = OTHER_ENGINE_OVERRIDES = {}
NO_OPTIONS_OVERRIDES = OTHER_ENGINE_OVERRIDES = {}


class SettingsTest(DjangoPluginTestCase):
Expand All @@ -51,14 +46,6 @@ def test_no_options(self):
with self.assertRaisesRegexp(DjangoTemplatePluginException, msg):
self.run_django_coverage()

@django_start_at(1, 8)
@override_settings(**MULTIPLE_ENGINE_OVERRIDES)
def test_multiple_engines(self):
self.make_template('Hello')
msg = "Can't use multiple template engines."
with self.assertRaisesRegexp(DjangoTemplatePluginException, msg):
self.run_django_coverage()

@django_start_at(1, 8)
@override_settings(**OTHER_ENGINE_OVERRIDES)
def test_other_engine(self):
Expand Down

0 comments on commit e9a4695

Please sign in to comment.