Skip to content

Commit 0085fb8

Browse files
committed
Test that TEMPLATE.DEBUG is set in the settings.
1 parent 6ae1b5f commit 0085fb8

File tree

6 files changed

+100
-18
lines changed

6 files changed

+100
-18
lines changed

django_coverage_plugin/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Django Template Coverage Plugin"""
22

3-
from .plugin import DjangoTemplatePlugin
3+
from .plugin import DjangoTemplatePlugin, DjangoTemplatePluginException
44

55

66
def coverage_init(reg, options):

django_coverage_plugin/plugin.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import coverage.plugin
1010

11-
from django import VERSION as DJANGO_VERSION
11+
import django
1212
import django.template
1313
from django.template.base import (
1414
Lexer, TextNode, NodeList, Template,
@@ -21,14 +21,42 @@
2121
# Django 1.4 didn't have VerbatimNode
2222
VerbatimNode = None
2323

24+
class DjangoTemplatePluginException(Exception):
25+
"""Used for any errors from the plugin itself."""
26+
pass
2427

28+
29+
# For debugging the plugin itself.
2530
SHOW_PARSING = False
2631
SHOW_TRACING = False
2732

28-
# TODO: Add a check for TEMPLATE_DEBUG, and make noise if it is false.
29-
3033

31-
if DJANGO_VERSION >= (1, 9):
34+
if django.VERSION >= (1, 8):
35+
def check_debug():
36+
from django.conf import settings
37+
templates = settings.TEMPLATES
38+
if len(templates) > 1:
39+
raise DjangoTemplatePluginException(
40+
"Can't use multiple template engines, help me add support!"
41+
)
42+
template_settings = templates[0]
43+
if template_settings['BACKEND'] != 'django.template.backends.django.DjangoTemplates':
44+
raise DjangoTemplatePluginException(
45+
"Can't use non-Django templates!"
46+
)
47+
if not template_settings['OPTIONS']['debug']:
48+
raise DjangoTemplatePluginException(
49+
"Template debugging must be enabled in settings."
50+
)
51+
else:
52+
def check_debug():
53+
from django.conf import settings
54+
if not settings.TEMPLATE_DEBUG:
55+
raise DjangoTemplatePluginException(
56+
"Template debugging must be enabled in settings."
57+
)
58+
59+
if django.VERSION >= (1, 9):
3260
# The filename used to be self.source[0].name. In more modern Djangos,
3361
# it's context.template.origin.
3462
def filename_for_frame(frame):
@@ -77,6 +105,8 @@ class DjangoTemplatePlugin(
77105
):
78106

79107
def __init__(self):
108+
check_debug()
109+
80110
self.django_template_dir = os.path.realpath(
81111
os.path.dirname(django.template.__file__)
82112
)
@@ -208,7 +238,7 @@ def lines(self):
208238
if SHOW_PARSING:
209239
print("-------------- {}".format(self.filename))
210240

211-
if DJANGO_VERSION >= (1, 9):
241+
if django.VERSION >= (1, 9):
212242
lexer = Lexer(self.source())
213243
else:
214244
lexer = Lexer(self.source(), self.filename)

tests/plugin_test.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,46 @@
1313
# Make Django templates outside of Django.
1414
# Originally taken from: http://stackoverflow.com/a/98178/14343
1515
from django.conf import settings
16-
settings.configure(
17-
CACHES={
16+
test_settings = {
17+
'CACHES': {
1818
'default': {
1919
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
2020
},
2121
},
22-
DATABASES={
22+
'DATABASES': {
2323
'default': {
2424
'ENGINE': 'django.db.backends.sqlite3',
25-
'NAME': ":memory:",
25+
'NAME': ':memory:',
2626
}
2727
},
28-
TEMPLATE_DEBUG=True,
29-
ALLOWED_INCLUDE_ROOTS=["/"], # for {% ssi %}
30-
ROOT_URLCONF="tests",
31-
)
28+
'ROOT_URLCONF': 'tests',
29+
}
30+
31+
if django.VERSION >= (1, 8):
32+
test_settings.update({
33+
'TEMPLATES': [
34+
{
35+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
36+
'DIRS': ['templates'], # where the tests put things.
37+
'OPTIONS': {
38+
'debug': True,
39+
},
40+
},
41+
],
42+
})
43+
44+
if django.VERSION < (1, 10):
45+
# for {% ssi %}
46+
test_settings['TEMPLATES'][0]['OPTIONS']['allowed_include_roots'] = ['/']
47+
48+
else:
49+
test_settings.update({
50+
'ALLOWED_INCLUDE_ROOTS': ['/'], # for {% ssi %}
51+
'TEMPLATE_DEBUG': True,
52+
'TEMPLATE_DIRS': ['templates'], # where the tests put things.
53+
})
54+
55+
settings.configure(**test_settings)
3256

3357
if hasattr(django, "setup"):
3458
django.setup()
@@ -79,7 +103,7 @@ def run_django_coverage(
79103
if options is None:
80104
options = {'source': ["."]}
81105

82-
with self.settings(TEMPLATE_DIRS=("templates",)):
106+
with self.settings():
83107
if text is not None:
84108
tem = Template(text)
85109
else:

tests/test_extends.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Tests of template inheritance for django_coverage_plugin."""
22

3-
from .plugin_test import DjangoPluginTestCase, django_start_at
3+
from .plugin_test import DjangoPluginTestCase, django_start_at, django_stop_at
44

55

66
class BlockTest(DjangoPluginTestCase):
@@ -147,6 +147,7 @@ def test_include(self):
147147

148148
# {% ssi %} is in earlier Djangos than 1.5, but doesn't trace properly.
149149
@django_start_at(1, 5)
150+
@django_stop_at(1, 10)
150151
class SsiTest(DjangoPluginTestCase):
151152
"""Test {% ssi %}, which does not trace the included file."""
152153

tests/test_settings.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""Settings tests for django_coverage_plugin."""
2+
3+
import copy
4+
5+
import django
6+
from django.test import override_settings
7+
8+
from django_coverage_plugin import DjangoTemplatePluginException
9+
10+
from .plugin_test import DjangoPluginTestCase, test_settings
11+
12+
13+
if django.VERSION >= (1, 8):
14+
DEBUG_FALSE_OVERRIDES = {
15+
'TEMPLATES': [copy.deepcopy(test_settings['TEMPLATES'][0])]
16+
}
17+
DEBUG_FALSE_OVERRIDES['TEMPLATES'][0]['OPTIONS']['debug'] = False
18+
else:
19+
DEBUG_FALSE_OVERRIDES = {'TEMPLATE_DEBUG': False}
20+
21+
22+
class SettingsTest(DjangoPluginTestCase):
23+
24+
@override_settings(**DEBUG_FALSE_OVERRIDES)
25+
def test_debug_false(self):
26+
self.make_template('Hello')
27+
msg = "Template debugging must be enabled in settings."
28+
with self.assertRaisesRegexp(DjangoTemplatePluginException, msg):
29+
self.run_django_coverage()

tests/test_simple.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
from .plugin_test import DjangoPluginTestCase, django_start_at
55

6-
# TODO: test what happens if TEMPLATE_DEBUG is not set.
7-
86

97
# 200 Unicode chars: snowman + poo.
108
UNIUNI = u"\u26C4\U0001F4A9"*100

0 commit comments

Comments
 (0)