|
3 | 3 |
|
4 | 4 | """Tests of template inheritance for django_coverage_plugin."""
|
5 | 5 |
|
| 6 | +try: |
| 7 | + from coverage.exceptions import NoSource |
| 8 | +except ImportError: # for coverage 5.x |
| 9 | + from coverage.misc import NoSource |
| 10 | + |
6 | 11 | from .plugin_test import DjangoPluginTestCase
|
7 | 12 |
|
8 | 13 |
|
@@ -64,3 +69,65 @@ def test_customized_extensions(self):
|
64 | 69 | self.assert_analysis([1], name="phd.tex", missing=[1])
|
65 | 70 | # The editor leave-behinds are not in the measured files.
|
66 | 71 | self.assert_measured_files("main.html", "unused.html", "phd.tex")
|
| 72 | + |
| 73 | + def test_non_utf8_error(self): |
| 74 | + # A non-UTF8 text file will raise an error. |
| 75 | + self.make_file(".coveragerc", """\ |
| 76 | + [run] |
| 77 | + plugins = django_coverage_plugin |
| 78 | + source = . |
| 79 | + """) |
| 80 | + # This is a template that is rendered. |
| 81 | + self.make_template(name="main.html", text="Hello") |
| 82 | + # Extra file containing a word encoded in CP-1252 |
| 83 | + self.make_file(self._path("static/changelog.txt"), bytes=b"sh\xf6n") |
| 84 | + |
| 85 | + text = self.run_django_coverage(name="main.html") |
| 86 | + self.assertEqual(text, "Hello") |
| 87 | + |
| 88 | + self.assert_measured_files("main.html", "static/changelog.txt") |
| 89 | + self.assert_analysis([1], name="main.html") |
| 90 | + with self.assertRaisesRegexp(NoSource, r"changelog.txt.*invalid start byte"): |
| 91 | + self.cov.html_report() |
| 92 | + |
| 93 | + def test_non_utf8_omitted(self): |
| 94 | + # If we omit the directory with the non-UTF8 file, all is well. |
| 95 | + self.make_file(".coveragerc", """\ |
| 96 | + [run] |
| 97 | + plugins = django_coverage_plugin |
| 98 | + source = . |
| 99 | + [report] |
| 100 | + omit = */static/* |
| 101 | + """) |
| 102 | + # This is a template that is rendered. |
| 103 | + self.make_template(name="main.html", text="Hello") |
| 104 | + # Extra file containing a word encoded in CP-1252 |
| 105 | + self.make_file(self._path("static/changelog.txt"), bytes=b"sh\xf6n") |
| 106 | + |
| 107 | + text = self.run_django_coverage(name="main.html") |
| 108 | + self.assertEqual(text, "Hello") |
| 109 | + |
| 110 | + self.assert_measured_files("main.html", "static/changelog.txt") |
| 111 | + self.assert_analysis([1], name="main.html") |
| 112 | + self.cov.html_report() |
| 113 | + |
| 114 | + def test_non_utf8_ignored(self): |
| 115 | + # If we ignore reporting errors, a non-UTF8 text file is fine. |
| 116 | + self.make_file(".coveragerc", """\ |
| 117 | + [run] |
| 118 | + plugins = django_coverage_plugin |
| 119 | + source = . |
| 120 | + [report] |
| 121 | + ignore_errors = True |
| 122 | + """) |
| 123 | + # This is a template that is rendered. |
| 124 | + self.make_template(name="main.html", text="Hello") |
| 125 | + # Extra file containing a word encoded in CP-1252 |
| 126 | + self.make_file(self._path("static/changelog.txt"), bytes=b"sh\xf6n") |
| 127 | + |
| 128 | + text = self.run_django_coverage(name="main.html") |
| 129 | + self.assertEqual(text, "Hello") |
| 130 | + |
| 131 | + self.assert_measured_files("main.html", "static/changelog.txt") |
| 132 | + self.assert_analysis([1], name="main.html") |
| 133 | + self.cov.html_report() |
0 commit comments