-
Notifications
You must be signed in to change notification settings - Fork 34
/
test_source.py
136 lines (115 loc) · 5.76 KB
/
test_source.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/django_coverage_plugin/blob/master/NOTICE.txt
"""Tests of template inheritance for django_coverage_plugin."""
import os
try:
from coverage.exceptions import NoSource
except ImportError:
# for coverage 5.x
from coverage.misc import NoSource
from .plugin_test import DjangoPluginTestCase
class FindSourceTest(DjangoPluginTestCase):
def test_finding_source(self):
# This is a template that is rendered.
self.make_template(name="main.html", text="Hello")
# These are templates that aren't rendered, but are considered renderable.
self.make_template(name="unused.html", text="Not used")
self.make_template(name="unused.htm", text="Not used")
self.make_template(name="unused.txt", text="Not used")
# These are things left behind by an editor.
self.make_template(name="~unused.html", text="junk")
self.make_template(name="unused=.html", text="junk")
self.make_template(name="unused.html,", text="junk")
# This is some other file format we don't recognize.
self.make_template(name="phd.tex", text="Too complicated to read")
text = self.run_django_coverage(name="main.html")
self.assertEqual(text, "Hello")
# The rendered file has data, and was measured.
self.assert_analysis([1], name="main.html")
# The unrendered files have data, and were not measured.
self.assert_analysis([1], name="unused.html", missing=[1])
self.assert_analysis([1], name="unused.htm", missing=[1])
self.assert_analysis([1], name="unused.txt", missing=[1])
# The editor leave-behinds are not in the measured files.
self.assert_measured_files("main.html", "unused.html", "unused.htm", "unused.txt")
def test_customized_extensions(self):
self.make_file(".coveragerc", """\
[run]
plugins = django_coverage_plugin
[django_coverage_plugin]
template_extensions = html, tex
""")
# This is a template that is rendered.
self.make_template(name="main.html", text="Hello")
# These are templates that aren't rendered, but are considered renderable.
self.make_template(name="unused.html", text="Not used")
self.make_template(name="phd.tex", text="Too complicated to read")
# These are things left behind by an editor.
self.make_template(name="~unused.html", text="junk")
self.make_template(name="unused=.html", text="junk")
self.make_template(name="unused.html,", text="junk")
# This is some other file format we don't recognize.
self.make_template(name="unused.htm", text="Not used")
self.make_template(name="unused.txt", text="Not used")
text = self.run_django_coverage(name="main.html")
self.assertEqual(text, "Hello")
# The rendered file has data, and was measured.
self.assert_analysis([1], name="main.html")
# The unrendered files have data, and were not measured.
self.assert_analysis([1], name="unused.html", missing=[1])
self.assert_analysis([1], name="phd.tex", missing=[1])
# The editor leave-behinds are not in the measured files.
self.assert_measured_files("main.html", "unused.html", "phd.tex")
def test_non_utf8_error(self):
# A non-UTF8 text file will raise an error.
self.make_file(".coveragerc", """\
[run]
plugins = django_coverage_plugin
source = .
""")
# This is a template that is rendered.
self.make_template(name="main.html", text="Hello")
# Extra file containing a word encoded in CP-1252
self.make_file(self._path("static/changelog.txt"), bytes=b"sh\xf6n")
text = self.run_django_coverage(name="main.html")
self.assertEqual(text, "Hello")
self.assert_measured_files("main.html", "static{}changelog.txt".format(os.sep))
self.assert_analysis([1], name="main.html")
with self.assertRaisesRegexp(NoSource, r"changelog.txt.*invalid start byte"):
self.cov.html_report()
def test_non_utf8_omitted(self):
# If we omit the directory with the non-UTF8 file, all is well.
self.make_file(".coveragerc", """\
[run]
plugins = django_coverage_plugin
source = .
[report]
omit = */static/*
""")
# This is a template that is rendered.
self.make_template(name="main.html", text="Hello")
# Extra file containing a word encoded in CP-1252
self.make_file(self._path("static/changelog.txt"), bytes=b"sh\xf6n")
text = self.run_django_coverage(name="main.html")
self.assertEqual(text, "Hello")
self.assert_measured_files("main.html", "static{}changelog.txt".format(os.sep))
self.assert_analysis([1], name="main.html")
self.cov.html_report()
def test_non_utf8_ignored(self):
# If we ignore reporting errors, a non-UTF8 text file is fine.
self.make_file(".coveragerc", """\
[run]
plugins = django_coverage_plugin
source = .
[report]
ignore_errors = True
""")
# This is a template that is rendered.
self.make_template(name="main.html", text="Hello")
# Extra file containing a word encoded in CP-1252
self.make_file(self._path("static/changelog.txt"), bytes=b"sh\xf6n")
text = self.run_django_coverage(name="main.html")
self.assertEqual(text, "Hello")
self.assert_measured_files("main.html", "static{}changelog.txt".format(os.sep))
self.assert_analysis([1], name="main.html")
self.cov.html_report()