Skip to content

Commit

Permalink
Disable all html5parser tests if html5lib is not installed, not just …
Browse files Browse the repository at this point in the history
…some of them.
  • Loading branch information
scoder committed Jan 8, 2024
1 parent f17f0e2 commit 12e4da6
Showing 1 changed file with 13 additions and 46 deletions.
59 changes: 13 additions & 46 deletions src/lxml/html/tests/test_html5parser.py
Expand Up @@ -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__ = "<dummy>", 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)
Expand All @@ -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)

Expand All @@ -124,14 +91,15 @@ 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'))


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)

Expand Down Expand Up @@ -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('a<b>c</b>')
self.assertEqual(len(fragments), 2)
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -288,19 +259,19 @@ 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('<p></p>')
self.assertEqual(elem.tag, xhtml_tag('p'))


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)

Expand All @@ -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')
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 12e4da6

Please sign in to comment.