Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions django_coverage_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,20 @@ def check_debug():
# change over versions.
def filename_for_frame(frame):
try:
return frame.f_locals["self"].origin.name
except (KeyError, AttributeError):
self = frame.f_locals["self"]
except KeyError:
return None

try:
return self.origin.name
except AttributeError:
pass

try:
return self[0].origin.name
except (IndexError, AttributeError):
pass


def position_for_node(node):
try:
Expand Down
7 changes: 5 additions & 2 deletions tests/plugin_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,14 @@ 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, using=None
self, name=None, text=None, context=None, options=None, using=None, widget=None
):
"""Run a template under coverage.

The data context is `context` if provided, else {}.
If `text` is provided, make a string template to run. Otherwise,
if `name` is provided, run that template, otherwise use the last
if `name` is provided, run that template, otherwise,
if `widget` is provided, render the widget, otherwise use the last
template made by `make_template`.

If `options` is provided, they are kwargs for the Coverage
Expand All @@ -118,6 +119,8 @@ def run_django_coverage(
elif text is not None:
tem = Template(text)
use_real_context = True
elif widget is not None:
tem = widget
else:
tem = get_template(name or self.template_file)

Expand Down
25 changes: 25 additions & 0 deletions tests/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,28 @@ def test_custom_html_report_isnt_measured(self):
# Run coverage again with an HTML report on disk.
text = self.run_django_coverage(name="main.html")
self.assert_measured_files("main.html")

def test_widget_template_coverage_captured(self):
from django import forms

class Widget(forms.Widget):
template_name = 'widget.html'

def render(self, context):
"""
We need to replace the normal Widget.render (which accepts a name, value and attrs)
with something that just accepts a context, like a normal Template.render would.
We also need to replace the `renderer`, as the hooks for the testing only work
based on the TemplatesSetting.
"""
return self._render(
self.template_name,
context,
renderer=forms.renderers.TemplatesSetting(),
)

self.make_template(name='widget.html', text="Hello")
text = self.run_django_coverage(widget=Widget())
self.assertEqual(text, 'Hello')

self.assert_measured_files('widget.html')