Skip to content

Commit a6550ac

Browse files
committed
Fix html5lib#237: fail if --update-xfail is used in wrong environment
1 parent 2b73979 commit a6550ac

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

html5lib/tests/conftest.py

+41-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import os.path
22

3+
import pkg_resources
34
import pytest
45

56
from .tree_construction import TreeConstructionFile
67
from .tokenizer import TokenizerFile
78
from .sanitizer import SanitizerFile
89

910
_dir = os.path.abspath(os.path.dirname(__file__))
11+
_root = os.path.join(_dir, "..", "..")
1012
_testdata = os.path.join(_dir, "testdata")
1113
_tree_construction = os.path.join(_testdata, "tree-construction")
1214
_tokenizer = os.path.join(_testdata, "tokenizer")
@@ -15,15 +17,53 @@
1517

1618
def pytest_configure(config):
1719
msgs = []
20+
1821
if not os.path.exists(_testdata):
1922
msg = "testdata not available! "
20-
if os.path.exists(os.path.join(_dir, "..", "..", ".git")):
23+
if os.path.exists(os.path.join(_root, ".git")):
2124
msg += ("Please run git submodule update --init --recursive " +
2225
"and then run tests again.")
2326
else:
2427
msg += ("The testdata doesn't appear to be included with this package, " +
2528
"so finding the right version will be hard. :(")
2629
msgs.append(msg)
30+
31+
if config.option.update_xfail:
32+
# Check for optional requirements
33+
req_file = os.path.join(_root, "requirements-optional.txt")
34+
if os.path.exists(req_file):
35+
with open(req_file, "r") as fp:
36+
for line in fp:
37+
if (line.strip() and
38+
not (line.startswith("-r") or
39+
line.startswith("#"))):
40+
if ";" in line:
41+
spec, marker = line.strip().split(";", 1)
42+
else:
43+
spec, marker = line.strip(), None
44+
req = pkg_resources.Requirement.parse(spec)
45+
if marker and not pkg_resources.evaluate_marker(marker):
46+
msgs.append("%s not available in this environment" % spec)
47+
else:
48+
try:
49+
installed = pkg_resources.working_set.find(req)
50+
except pkg_resources.VersionConflict:
51+
msgs.append("Outdated version of %s installed, need %s" % (req.name, spec))
52+
else:
53+
if not installed:
54+
msgs.append("Need %s" % spec)
55+
56+
# Check cElementTree
57+
import xml.etree.ElementTree as ElementTree
58+
59+
try:
60+
import xml.etree.cElementTree as cElementTree
61+
except ImportError:
62+
msgs.append("cElementTree unable to be imported")
63+
else:
64+
if cElementTree.Element is ElementTree.Element:
65+
msgs.append("cElementTree is just an alias for ElementTree")
66+
2767
if msgs:
2868
pytest.exit("\n".join(msgs))
2969

0 commit comments

Comments
 (0)