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