Skip to content
New issue

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

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

Already on GitHub? # to your account

Breaking running of coverage when enabled #29

Closed
clokep opened this issue Dec 29, 2016 · 7 comments
Closed

Breaking running of coverage when enabled #29

clokep opened this issue Dec 29, 2016 · 7 comments

Comments

@clokep
Copy link

clokep commented Dec 29, 2016

First, thanks for this great plug-in! I've found it quite useful for testing some of the logic in my templates. 👍

I know this used to work in my project, but I'm not sure when it broke. (I don't think this has updated in a while, it could have been a Django update that broke this?)

My project is using Django 1.10.4, coverage 4.3.1, django-coverage-plugin 1.3.1. (I also tried with coverage 4.2, that exhibited the same behavior.)

While running with this I'm getting a stack trace:

/vagrant$ coverage run manage.py test
Coverage.py warning: Disabling plugin 'django_coverage_plugin.DjangoTemplatePlugin' due to an exception:
Traceback (most recent call last):
  File "/usr/local/venv/local/lib/python2.7/site-packages/coverage/control.py", line 497, in _should_trace_internal
    file_tracer = plugin.file_tracer(canonical)
  File "/usr/local/venv/local/lib/python2.7/site-packages/django_coverage_plugin/plugin.py", line 154, in file_tracer
    check_debug()
  File "/usr/local/venv/local/lib/python2.7/site-packages/django_coverage_plugin/plugin.py", line 55, in check_debug
    templates = getattr(settings, 'TEMPLATES', [])
  File "/usr/local/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/usr/local/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 97, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/vagrant/my_project/settings.py", line 8, in <module>
    from django.contrib.messages import constants as message_constants
  File "/usr/local/venv/local/lib/python2.7/site-packages/django/contrib/messages/__init__.py", line 1, in <module>
    from django.contrib.messages.api import *  # NOQA
  File "/usr/local/venv/local/lib/python2.7/site-packages/django/contrib/messages/api.py", line 3, in <module>
    from django.http import HttpRequest
  File "/usr/local/venv/local/lib/python2.7/site-packages/django/http/__init__.py", line 5, in <module>
    from django.http.response import (
  File "/usr/local/venv/local/lib/python2.7/site-packages/django/http/response.py", line 13, in <module>
    from django.core.serializers.json import DjangoJSONEncoder
  File "/usr/local/venv/local/lib/python2.7/site-packages/django/core/serializers/__init__.py", line 23, in <module>
    from django.core.serializers.base import SerializerDoesNotExist
  File "/usr/local/venv/local/lib/python2.7/site-packages/django/core/serializers/base.py", line 4, in <module>
    from django.db import models
ImportError: cannot import name models
Traceback (most recent call last):
  File "manage.py", line 45, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/usr/local/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 316, in execute
    settings.INSTALLED_APPS
  File "/usr/local/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/usr/local/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 97, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/vagrant/my_project/settings.py", line 8, in <module>
    from django.contrib.messages import constants as message_constants
  File "/usr/local/venv/local/lib/python2.7/site-packages/django/contrib/messages/__init__.py", line 1, in <module>
    from django.contrib.messages.api import *  # NOQA
  File "/usr/local/venv/local/lib/python2.7/site-packages/django/contrib/messages/api.py", line 1, in <module>
    from django.contrib.messages import constants
ImportError: cannot import name constants

This seems to be because I'm importing django.contrib.messages.constants in my settings file, note that if I remove this line I get other errors about importing. This seems related to loading of Django settings before Django is configured (see #28 (comment)), but with much worse consequences.

Let me know if there is anything I can do to help confirm my suspicion or narrow this down! Thanks.

@nedbat
Copy link
Owner

nedbat commented Jan 2, 2017

@clokep: Sorry things are going badly. I haven't had time to work on the Django template plugin in quite a while, and it might be a while before I do. What I'd really like is to find someone else to take it over... :(

@PamelaM
Copy link
Collaborator

PamelaM commented Jan 17, 2017

@clokep: Could you try the latest version (https://pypi.python.org/pypi/django_coverage_plugin/1.4)? I'm not sure it will solve your problem, but if your problem is related to #28, then it might.

@clokep
Copy link
Author

clokep commented Jan 17, 2017

@PamelaM First, thanks for taking a look at this! 👍

This seems to have fixed the issue of coverage erroring immediately when running, but unfortunately I don't seem to have any coverage of the templates! (I can run coverage and get a coverage report, including the template files, but it doesn't seem to have actually processed these files.)

I'm getting the following warning when I run coverage:

coverage run manage.py test 
Coverage.py warning: Disabling plugin 'django_coverage_plugin.DjangoTemplatePlugin' due to an exception:
Traceback (most recent call last):
  File "/usr/local/venv/local/lib/python2.7/site-packages/coverage/control.py", line 476, in _should_trace_internal
    file_tracer = plugin.file_tracer(canonical)
  File "/usr/local/venv/local/lib/python2.7/site-packages/django_coverage_plugin/plugin.py", line 158, in file_tracer
    self.debug_checked = check_debug()
  File "/usr/local/venv/local/lib/python2.7/site-packages/django_coverage_plugin/plugin.py", line 56, in check_debug
    if not hasattr(django.template.backends, "django"):
AttributeError: 'module' object has no attribute 'backends'

I tried this quickly in a Django shell and django.template DOES have an attribute backends. My settings look like:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [normpath(join(DJANGO_ROOT, 'templates'))],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
                'django.template.context_processors.request',

                # + a few custom ones.
            ],
            'debug': DEBUG,
        },
    },
]

Any idea what's happening here? Thanks again!

@PamelaM
Copy link
Collaborator

PamelaM commented Jan 19, 2017 via email

@clokep
Copy link
Author

clokep commented Jan 19, 2017

Awesome! Let me know if you want help testing a branch or anything! 👍

@PamelaM
Copy link
Collaborator

PamelaM commented Jan 20, 2017

@clokep I've merged a fix for the most common likely problems. Could you try out the master branch and see if there are any surprises?

@clokep
Copy link
Author

clokep commented Jan 21, 2017

💥 That worked! Seems like this was fixed by #33. Thanks so much for the help!

@clokep clokep closed this as completed Jan 21, 2017
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants