Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

0.2.0: Add optional usage report file.

  • Loading branch information...
commit fc4e81d8e73f7332b3a331b5a689d76b23a6a1b7 1 parent 9f37d90
@tkaemming tkaemming authored
Showing with 40 additions and 2 deletions.
  1. +1 −1  VERSION
  2. +12 −0 templateusage/plugin.py
  3. +27 −1 tests/tests.py
View
2  VERSION
@@ -1 +1 @@
-0.1.4
+0.2.0
View
12 templateusage/plugin.py
@@ -1,3 +1,4 @@
+import json
import os
import string
import sys
@@ -47,6 +48,9 @@ def options(self, parser, env):
action='append', help='Add a template directory to the ignore list.',
default=[])
+ parser.add_option('--template-usage-report-file', dest='outfile',
+ help='Write JSON template usage report to file.')
+
def configure(self, options, conf):
self.enabled = options.enabled
if not self.enabled:
@@ -58,6 +62,7 @@ def configure(self, options, conf):
ignore_prefixes = map(string.strip, ignore_prefixes[0].split('\n'))
self.ignore_prefixes = ignore_prefixes
+ self.outfile = options.outfile
def begin(self):
self.used_templates = set()
@@ -103,3 +108,10 @@ def filter_ignored(paths):
self.unused_templates = available_templates - self.used_templates
heading(stream, 'Unused Templates (%s)' % len(self.unused_templates))
bulleted(stream, sorted(self.unused_templates))
+
+ if self.outfile:
+ with open(self.outfile, 'w') as out:
+ json.dump({
+ 'used': list(self.used_templates),
+ 'unused': list(self.unused_templates),
+ }, out)
View
28 tests/tests.py
@@ -21,8 +21,10 @@
# Tests
-from cStringIO import StringIO
+import json
+import tempfile
import unittest
+from cStringIO import StringIO
from nose.plugins import PluginTester
from django.template.loader import get_template
@@ -60,3 +62,27 @@ class IgnoredDirectoryUsageReportPluginTestCase(TemplateUsageReportTestMixin, un
def test_ignored(self):
self.assertNotIn('ignored/ignored.html', self.plugins[0].used_templates)
self.assertNotIn('ignored/ignored.html', self.plugins[0].unused_templates)
+
+
+class ReportFileTestCase(TemplateUsageReportTestMixin, unittest.TestCase):
+ def setUp(self, *args, **kwargs):
+ f = tempfile.NamedTemporaryFile(delete=False)
+ f.close()
+ self.filename = f.name
+ self.args = ('--template-usage-report-file=%s' % self.filename,)
+ super(ReportFileTestCase, self).setUp(*args, **kwargs)
+
+ def tearDown(self):
+ os.unlink(self.filename)
+
+ def test_report_file(self):
+ self.assertIsNotNone(self.plugins[0].outfile)
+
+ with open(self.filename) as raw:
+ report = json.load(raw)
+
+ used = set(('example.html', 'included.html'))
+ self.assertEqual(used, set(report['used']))
+
+ unused = set(('unused.html', 'ignored/ignored.html'))
+ self.assertEqual(unused, set(report['unused']))
Please sign in to comment.
Something went wrong with that request. Please try again.