From 12e4da6dfb86910d2bb8d35321dacf949f8320df Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Mon, 8 Jan 2024 21:12:04 +0100 Subject: [PATCH] Disable all html5parser tests if html5lib is not installed, not just some of them. --- src/lxml/html/tests/test_html5parser.py | 59 ++++++------------------- 1 file changed, 13 insertions(+), 46 deletions(-) diff --git a/src/lxml/html/tests/test_html5parser.py b/src/lxml/html/tests/test_html5parser.py index 57878a5e3..a5129d5ac 100644 --- a/src/lxml/html/tests/test_html5parser.py +++ b/src/lxml/html/tests/test_html5parser.py @@ -33,49 +33,14 @@ def path2url(path): except ImportError: html5lib = None - class BogusModules(object): - # See PEP 302 for details on how this works - def __init__(self, mocks): - self.mocks = mocks - - def find_module(self, fullname, path=None): - if fullname in self.mocks: - return self - return None - - def load_module(self, fullname): - class Cls: pass - fake_module = Cls() - fake_module.__qualname__ = fullname - fake_module.__name__ = fullname.rsplit('.', 1)[-1] - mod = sys.modules.setdefault(fullname, fake_module) - mod.__file__, mod.__loader__, mod.__path__ = "", self, [] - mod.__dict__.update(self.mocks[fullname]) - return mod - - # Fake just enough of html5lib so that html5parser.py is importable - # without errors. - sys.meta_path.append(BogusModules({ - 'html5lib': { - # A do-nothing HTMLParser class - 'HTMLParser': type('HTMLParser', (object,), { - '__init__': lambda self, **kw: None, - }), - }, - 'html5lib.treebuilders': { - }, - 'html5lib.treebuilders.etree_lxml': { - 'TreeBuilder': 'dummy treebuilder', - }, - })) - class Test_HTMLParser(unittest.TestCase): def make_one(self, **kwargs): + if html5lib is None: + raise unittest.SkipTest("html5lib is not installed") from lxml.html.html5parser import HTMLParser return HTMLParser(**kwargs) - @skipUnless(html5lib, 'html5lib is not installed') def test_integration(self): parser = self.make_one(strict=True) tree = parser.parse(XHTML_TEST_DOCUMENT) @@ -100,6 +65,8 @@ def test_integration(self): class Test_document_fromstring(unittest.TestCase): def call_it(self, *args, **kwargs): + if html5lib is None: + raise unittest.SkipTest("html5lib is not installed") from lxml.html.html5parser import document_fromstring return document_fromstring(*args, **kwargs) @@ -124,7 +91,6 @@ def test_raises_type_error_on_nonstring_input(self): not_a_string = None self.assertRaises(TypeError, self.call_it, not_a_string) - @skipUnless(html5lib, 'html5lib is not installed') def test_integration(self): elem = self.call_it(XHTML_TEST_DOCUMENT) self.assertEqual(elem.tag, xhtml_tag('html')) @@ -132,6 +98,8 @@ def test_integration(self): class Test_fragments_fromstring(unittest.TestCase): def call_it(self, *args, **kwargs): + if html5lib is None: + raise unittest.SkipTest("html5lib is not installed") from lxml.html.html5parser import fragments_fromstring return fragments_fromstring(*args, **kwargs) @@ -165,7 +133,6 @@ def test_no_leading_text_raises_error_if_leading_text(self): self.assertRaises(ParserError, self.call_it, '', parser=parser, no_leading_text=True) - @skipUnless(html5lib, 'html5lib is not installed') def test_integration(self): fragments = self.call_it('ac') self.assertEqual(len(fragments), 2) @@ -175,6 +142,8 @@ def test_integration(self): class Test_fragment_fromstring(unittest.TestCase): def call_it(self, *args, **kwargs): + if html5lib is None: + raise unittest.SkipTest("html5lib is not installed") from lxml.html.html5parser import fragment_fromstring return fragment_fromstring(*args, **kwargs) @@ -218,6 +187,8 @@ def test_raises_error_if_tail(self): class Test_fromstring(unittest.TestCase): def call_it(self, *args, **kwargs): + if html5lib is None: + raise unittest.SkipTest("html5lib is not installed") from lxml.html.html5parser import fromstring return fromstring(*args, **kwargs) @@ -288,12 +259,10 @@ def test_raises_type_error_on_nonstring_input(self): not_a_string = None self.assertRaises(TypeError, self.call_it, not_a_string) - @skipUnless(html5lib, 'html5lib is not installed') def test_integration_whole_doc(self): elem = self.call_it(XHTML_TEST_DOCUMENT) self.assertEqual(elem.tag, xhtml_tag('html')) - @skipUnless(html5lib, 'html5lib is not installed') def test_integration_single_fragment(self): elem = self.call_it('

') self.assertEqual(elem.tag, xhtml_tag('p')) @@ -301,6 +270,8 @@ def test_integration_single_fragment(self): class Test_parse(unittest.TestCase): def call_it(self, *args, **kwargs): + if html5lib is None: + raise unittest.SkipTest("html5lib is not installed") from lxml.html.html5parser import parse return parse(*args, **kwargs) @@ -320,12 +291,9 @@ def make_temp_file(self, contents=''): def test_with_file_object(self): parser = DummyParser(doc='the doc') - fp = open(__file__) - try: + with open(__file__) as fp: self.assertEqual(self.call_it(fp, parser=parser), 'the doc') self.assertEqual(parser.parse_args, (fp,)) - finally: - fp.close() def test_with_file_name(self): parser = DummyParser(doc='the doc') @@ -362,7 +330,6 @@ def test_with_url(self): finally: os.unlink(tmpfile.name) - @skipUnless(html5lib, 'html5lib is not installed') def test_integration(self): doc = self.call_it(StringIO(XHTML_TEST_DOCUMENT)) root = doc.getroot()