Skip to content

Commit 6b4ba8f

Browse files
authored
Merge pull request #30 from PamelaM/pm/defer_reading_settings
Fixes to check_debug and adds support for Django 1.10 (the issues were heavily intertwined)
2 parents 6ca6525 + 41aec8e commit 6b4ba8f

File tree

8 files changed

+75
-33
lines changed

8 files changed

+75
-33
lines changed

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
sudo: false
2+
language: python
3+
python:
4+
- "2.7"
5+
- "3.4"
6+
- "3.5"
7+
- "3.6"
8+
install: pip install tox-travis
9+
script: tox

AUTHORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ contributions by:
33

44
Jessamyn Smith
55
Simon Charette
6+
Pamela McA'Nulty

README.rst

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ A `coverage.py`_ plugin to measure the coverage of Django templates.
1010
| |license| |versions| |djversions| |status|
1111
| |kit| |downloads|
1212
13-
Supported Python versions are 2.7, 3.4, and 3.5. Supported Django versions are
14-
1.4 through 1.9.
13+
Supported Python versions are 2.7, 3.4, 3.5 and 3.6.
14+
15+
Supported Django versions are 1.4 through 1.10.
16+
17+
Supported coverage.py versions are 4.0 and higher.
18+
1519

1620
The plugin is pip installable::
1721

@@ -23,7 +27,7 @@ To run it, add this setting to your .coveragerc file::
2327
plugins =
2428
django_coverage_plugin
2529

26-
Then run your tests under coverage.py. It requires coverage.py 4.0 or later.
30+
Then run your tests under coverage.py.
2731

2832
You will see your templates listed in your coverage report along with your
2933
Python modules.
@@ -43,6 +47,8 @@ template files are included in the report.
4347
Caveats
4448
~~~~~~~
4549

50+
Support for Django versions 1.4 through 1.7 should be considered deprecated.
51+
4652
Files included by the ``{% ssi %}`` tag are not included in the coverage
4753
measurements.
4854

@@ -54,6 +60,22 @@ Changes
5460
~~~~~~~
5561

5662

63+
v1.4 --- 2017-01-15
64+
---------------------
65+
66+
Django 1.10.5 is now supported.
67+
68+
Checking settings configuration is deferred so that settings.py is included
69+
in coverage reporting. Fixes `issue 28`_.
70+
71+
Only the ``django.template.backends.django.DjangoTemplates`` template engine is
72+
supported, and it must be configured with ``['OPTIONS']['debug'] = True``. Fixes
73+
`issue 27`_.
74+
75+
.. _issue 28: https://github.com/nedbat/django_coverage_plugin/issues/28
76+
.. _issue 27: https://github.com/nedbat/django_coverage_plugin/issues/27
77+
78+
5779
v1.3.1 --- 2016-06-02
5880
---------------------
5981

@@ -140,7 +162,7 @@ To run the tests::
140162
.. |versions| image:: https://img.shields.io/pypi/pyversions/django_coverage_plugin.svg
141163
:target: https://pypi.python.org/pypi/django_coverage_plugin
142164
:alt: Python versions supported
143-
.. |djversions| image:: https://img.shields.io/badge/Django-1.4, 1.5, 1.6, 1.7, 1.8, 1.9-44b78b.svg
165+
.. |djversions| image:: https://img.shields.io/badge/Django-1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 1.10
144166
:target: https://pypi.python.org/pypi/django_coverage_plugin
145167
:alt: Django versions supported
146168
.. |status| image:: https://img.shields.io/pypi/status/django_coverage_plugin.svg

django_coverage_plugin/plugin.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
import django
1515
import django.template
16-
from django.core.exceptions import ImproperlyConfigured
1716
from django.template.base import (
1817
Lexer, TextNode, NodeList, Template,
1918
TOKEN_BLOCK, TOKEN_MAPPING, TOKEN_TEXT, TOKEN_VAR,
@@ -43,28 +42,29 @@ def check_debug():
4342
to do its work. Check that the setting is correct, and raise an exception
4443
if it is not.
4544
45+
Returns True if the debug check was performed, False otherwise
4646
"""
47-
# The settings for templates changed in Django 1.8 from TEMPLATE_DEBUG to
48-
# TEMPLATES[..]['debug']. Django 1.9 tolerated both forms, 1.10 insists on
49-
# the new form. Don't try to be version-specific here. If the new
50-
# settings exist, use them, otherwise use the old.
51-
5247
from django.conf import settings
5348

54-
try:
55-
templates = getattr(settings, 'TEMPLATES', [])
56-
except ImproperlyConfigured:
57-
# Maybe there are no settings at all. We are fine with this. Our
58-
# code will need settings, but if this program we're in runs without
59-
# settings, then it must be that it never uses templates, and our code
60-
# will never try to use the settings anyway.
61-
return
62-
63-
if templates:
64-
for template_settings in templates:
65-
if template_settings['BACKEND'] != 'django.template.backends.django.DjangoTemplates':
66-
raise DjangoTemplatePluginException("Can't use non-Django templates.")
67-
if not template_settings.get('OPTIONS', {}).get('debug', False):
49+
if not settings.configured:
50+
return False
51+
52+
if django.VERSION >= (1, 8):
53+
# Django 1.8+ handles both old and new-style settings and converts them
54+
# into template engines, so we don't need to depend on settings values
55+
# directly and can look at the resulting configured objects
56+
if not hasattr(django.template.backends, "django"):
57+
raise DjangoTemplatePluginException("Can't use non-Django templates.")
58+
59+
if not hasattr(django.template.backends.django, "DjangoTemplates"):
60+
raise DjangoTemplatePluginException("Can't use non-Django templates.")
61+
62+
for engine in django.template.engines.all():
63+
if not isinstance(engine, django.template.backends.django.DjangoTemplates):
64+
raise DjangoTemplatePluginException(
65+
"Can't use non-Django templates."
66+
)
67+
if not engine.engine.debug:
6868
raise DjangoTemplatePluginException(
6969
"Template debugging must be enabled in settings."
7070
)
@@ -75,6 +75,8 @@ def check_debug():
7575
"Template debugging must be enabled in settings."
7676
)
7777

78+
return True
79+
7880

7981
if django.VERSION >= (1, 9):
8082
# Since we are grabbing at internal details, we have to adapt as they
@@ -151,8 +153,9 @@ def sys_info(self):
151153
def file_tracer(self, filename):
152154
if filename.startswith(self.django_template_dir):
153155
if not self.debug_checked:
154-
check_debug()
155-
self.debug_checked = True
156+
# Keep calling check_debug until it returns True, which it
157+
# will only do after settings have been configured
158+
self.debug_checked = check_debug()
156159

157160
return self
158161
return None

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Programming Language :: Python :: 2.7
1313
Programming Language :: Python :: 3.4
1414
Programming Language :: Python :: 3.5
15+
Programming Language :: Python :: 3.6
1516
Programming Language :: Python :: Implementation :: CPython
1617
Programming Language :: Python :: Implementation :: PyPy
1718
Topic :: Software Development :: Quality Assurance

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def index(request):
1212
"""A bogus view to use in the urls below."""
1313
pass
1414

15+
1516
urlpatterns = [
1617
url(r'^home$', index, name='index'),
1718
]

tests/plugin_test.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,18 @@ def test_settings():
6161

6262
return the_settings
6363

64+
6465
settings.configure(**test_settings())
6566

6667
if hasattr(django, "setup"):
6768
django.setup()
6869

70+
from django.template import Context, Template # noqa
71+
from django.template.loader import get_template # noqa
72+
from django.test import TestCase # noqa
6973

70-
from django.template import Context, Template # noqa
71-
from django.template.loader import get_template # noqa
72-
from django.test import TestCase # noqa
74+
if django.VERSION >= (1, 8):
75+
from django.template.backends.django import DjangoTemplates # noqa
7376

7477

7578
class DjangoPluginTestCase(StdStreamCapturingMixin, TempDirMixin, TestCase):

tox.ini

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414

1515
[tox]
1616
envlist =
17-
py27-django{14,15,16,17,18,19,19tip,tip},
18-
py34-django{15,16,17,18,19,19tip,tip},
19-
py35-django{18,19,19tip,tip},
17+
py27-django{14,15,16,17,18,19,110,110tip,tip},
18+
py34-django{15,16,17,18,19,110,110tip,tip},
19+
py35-django{18,19,110,110tip,tip},
20+
py36-django{18,19,110,110tip,tip},
2021
check,doc
2122

2223
[testenv]
@@ -28,7 +29,8 @@ deps =
2829
django17: Django >=1.7, <1.8
2930
django18: Django >=1.8, <1.9
3031
django19: Django >=1.9, <1.10
31-
django19tip: https://github.com/django/django/archive/stable/1.9.x.tar.gz
32+
django110: Django >=1.10, <1.11
33+
django110tip: https://github.com/django/django/archive/stable/1.10.x.tar.gz
3234
djangotip: https://github.com/django/django/archive/master.tar.gz
3335

3436
commands =

0 commit comments

Comments
 (0)