Skip to content

Fixes to check_debug and adds support for Django 1.10 (the issues were heavily intertwined) #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jan 17, 2017
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sudo: false
language: python
python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"
install: pip install tox-travis
script: tox
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ contributions by:

Jessamyn Smith
Simon Charette
Pamela McA'Nulty
30 changes: 26 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ A `coverage.py`_ plugin to measure the coverage of Django templates.
| |license| |versions| |djversions| |status|
| |kit| |downloads|

Supported Python versions are 2.7, 3.4, and 3.5. Supported Django versions are
1.4 through 1.9.
Supported Python versions are 2.7, 3.4, 3.5 and 3.6.

Supported Django versions are 1.4 through 1.10.

Supported coverage.py versions are 4.0 and higher.


The plugin is pip installable::

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

Then run your tests under coverage.py. It requires coverage.py 4.0 or later.
Then run your tests under coverage.py.

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

Support for Django versions 1.4 through 1.7 should be considered deprecated.

Files included by the ``{% ssi %}`` tag are not included in the coverage
measurements.

Expand All @@ -54,6 +60,22 @@ Changes
~~~~~~~


v1.4 --- 2017-01-15
---------------------

Django 1.10.5 is now supported.

Checking settings configuration is deferred so that settings.py is included
in coverage reporting. Fixes `issue 28`_.

Only the ``django.template.backends.django.DjangoTemplates`` template engine is
supported, and it must be configured with ``['OPTIONS']['debug'] = True``. Fixes
`issue 27`_.

.. _issue 28: https://github.com/nedbat/django_coverage_plugin/issues/28
.. _issue 27: https://github.com/nedbat/django_coverage_plugin/issues/27


v1.3.1 --- 2016-06-02
---------------------

Expand Down Expand Up @@ -140,7 +162,7 @@ To run the tests::
.. |versions| image:: https://img.shields.io/pypi/pyversions/django_coverage_plugin.svg
:target: https://pypi.python.org/pypi/django_coverage_plugin
:alt: Python versions supported
.. |djversions| image:: https://img.shields.io/badge/Django-1.4, 1.5, 1.6, 1.7, 1.8, 1.9-44b78b.svg
.. |djversions| image:: https://img.shields.io/badge/Django-1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 1.10
:target: https://pypi.python.org/pypi/django_coverage_plugin
:alt: Django versions supported
.. |status| image:: https://img.shields.io/pypi/status/django_coverage_plugin.svg
Expand Down
47 changes: 25 additions & 22 deletions django_coverage_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import django
import django.template
from django.core.exceptions import ImproperlyConfigured
from django.template.base import (
Lexer, TextNode, NodeList, Template,
TOKEN_BLOCK, TOKEN_MAPPING, TOKEN_TEXT, TOKEN_VAR,
Expand Down Expand Up @@ -43,28 +42,29 @@ def check_debug():
to do its work. Check that the setting is correct, and raise an exception
if it is not.

Returns True if the debug check was performed, False otherwise
"""
# The settings for templates changed in Django 1.8 from TEMPLATE_DEBUG to
# TEMPLATES[..]['debug']. Django 1.9 tolerated both forms, 1.10 insists on
# the new form. Don't try to be version-specific here. If the new
# settings exist, use them, otherwise use the old.

from django.conf import settings

try:
templates = getattr(settings, 'TEMPLATES', [])
except ImproperlyConfigured:
# Maybe there are no settings at all. We are fine with this. Our
# code will need settings, but if this program we're in runs without
# settings, then it must be that it never uses templates, and our code
# will never try to use the settings anyway.
return

if templates:
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):
if not settings.configured:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's how we defer check_debug until settings are configured. Note that we can't really encounter the case where we'd be tracing a template until after settings have been configured because the template can't be run without configured settings.

return False

if django.VERSION >= (1, 8):
# Django 1.8+ handles both old and new-style settings and converts them
# into template engines, so we don't need to depend on settings values
# directly and can look at the resulting configured objects
if not hasattr(django.template.backends, "django"):
raise DjangoTemplatePluginException("Can't use non-Django templates.")

if not hasattr(django.template.backends.django, "DjangoTemplates"):
raise DjangoTemplatePluginException("Can't use non-Django templates.")

for engine in django.template.engines.all():
if not isinstance(engine, django.template.backends.django.DjangoTemplates):
raise DjangoTemplatePluginException(
"Can't use non-Django templates."
)
if not engine.engine.debug:
raise DjangoTemplatePluginException(
"Template debugging must be enabled in settings."
)
Expand All @@ -75,6 +75,8 @@ def check_debug():
"Template debugging must be enabled in settings."
)

return True


if django.VERSION >= (1, 9):
# Since we are grabbing at internal details, we have to adapt as they
Expand Down Expand Up @@ -151,8 +153,9 @@ def sys_info(self):
def file_tracer(self, filename):
if filename.startswith(self.django_template_dir):
if not self.debug_checked:
check_debug()
self.debug_checked = True
# Keep calling check_debug until it returns True, which it
# will only do after settings have been configured
self.debug_checked = check_debug()

return self
return None
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Programming Language :: Python :: 2.7
Programming Language :: Python :: 3.4
Programming Language :: Python :: 3.5
Programming Language :: Python :: 3.6
Programming Language :: Python :: Implementation :: CPython
Programming Language :: Python :: Implementation :: PyPy
Topic :: Software Development :: Quality Assurance
Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def index(request):
"""A bogus view to use in the urls below."""
pass


urlpatterns = [
url(r'^home$', index, name='index'),
]
9 changes: 6 additions & 3 deletions tests/plugin_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,18 @@ def test_settings():

return the_settings


settings.configure(**test_settings())

if hasattr(django, "setup"):
django.setup()

from django.template import Context, Template # noqa
from django.template.loader import get_template # noqa
from django.test import TestCase # noqa

from django.template import Context, Template # noqa
from django.template.loader import get_template # noqa
from django.test import TestCase # noqa
if django.VERSION >= (1, 8):
from django.template.backends.django import DjangoTemplates # noqa


class DjangoPluginTestCase(StdStreamCapturingMixin, TempDirMixin, TestCase):
Expand Down
10 changes: 6 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

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

[testenv]
Expand All @@ -28,7 +29,8 @@ deps =
django17: Django >=1.7, <1.8
django18: Django >=1.8, <1.9
django19: Django >=1.9, <1.10
django19tip: https://github.com/django/django/archive/stable/1.9.x.tar.gz
django110: Django >=1.10, <1.11
django110tip: https://github.com/django/django/archive/stable/1.10.x.tar.gz
djangotip: https://github.com/django/django/archive/master.tar.gz

commands =
Expand Down