diff --git a/os_loganalyze/tests/samples/empty.html b/os_loganalyze/tests/samples/empty.html new file mode 100644 index 0000000..e69de29 diff --git a/os_loganalyze/tests/samples/sample_doctype.html b/os_loganalyze/tests/samples/sample_doctype.html index 77e7f90..213975c 100644 --- a/os_loganalyze/tests/samples/sample_doctype.html +++ b/os_loganalyze/tests/samples/sample_doctype.html @@ -1,3 +1,9 @@ + + + + +Should detect HTML even though there is leading whitespace. + diff --git a/os_loganalyze/tests/test_views.py b/os_loganalyze/tests/test_views.py index 05d25e6..3174b69 100644 --- a/os_loganalyze/tests/test_views.py +++ b/os_loganalyze/tests/test_views.py @@ -50,3 +50,12 @@ class TestViews(base.TestCase): # Move the generator so that the is_html flag is set i.next() self.assertTrue(html_view.is_html) + + def test_empty_file_html_view(self): + gen = self.get_generator('empty.html') + html_view = osview.HTMLView(gen) + self.assertFalse(html_view.is_html) + full_text = "" + for i in html_view: + full_text += i + self.assertEqual("", full_text) diff --git a/os_loganalyze/view.py b/os_loganalyze/view.py index 4fee84b..4d43f8e 100644 --- a/os_loganalyze/view.py +++ b/os_loganalyze/view.py @@ -165,7 +165,15 @@ class HTMLView(collections.Iterable): def __iter__(self): igen = (x for x in self.filter_generator) - first_line = next(igen) + # Grab the first non-empty line + first_line = None + for i in igen: + if i.line: + first_line = i + break + if not first_line: + # The file must be empty + return self._discover_html(first_line.line) if not self.is_html: