From ae141b8d90c11a2b1ec4f4e7d54d554ef2440db3 Mon Sep 17 00:00:00 2001 From: Geoffrey Sneddon Date: Wed, 8 Jun 2016 02:53:36 +0100 Subject: [PATCH] Fix #237: fail if --update-xfail is used in wrong environment --- html5lib/tests/conftest.py | 42 +++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/html5lib/tests/conftest.py b/html5lib/tests/conftest.py index aa56dd17..ce93eff6 100644 --- a/html5lib/tests/conftest.py +++ b/html5lib/tests/conftest.py @@ -1,5 +1,6 @@ import os.path +import pkg_resources import pytest from .tree_construction import TreeConstructionFile @@ -7,6 +8,7 @@ from .sanitizer import SanitizerFile _dir = os.path.abspath(os.path.dirname(__file__)) +_root = os.path.join(_dir, "..", "..") _testdata = os.path.join(_dir, "testdata") _tree_construction = os.path.join(_testdata, "tree-construction") _tokenizer = os.path.join(_testdata, "tokenizer") @@ -15,15 +17,53 @@ def pytest_configure(config): msgs = [] + if not os.path.exists(_testdata): msg = "testdata not available! " - if os.path.exists(os.path.join(_dir, "..", "..", ".git")): + if os.path.exists(os.path.join(_root, ".git")): msg += ("Please run git submodule update --init --recursive " + "and then run tests again.") else: msg += ("The testdata doesn't appear to be included with this package, " + "so finding the right version will be hard. :(") msgs.append(msg) + + if config.option.update_xfail: + # Check for optional requirements + req_file = os.path.join(_root, "requirements-optional.txt") + if os.path.exists(req_file): + with open(req_file, "r") as fp: + for line in fp: + if (line.strip() and + not (line.startswith("-r") or + line.startswith("#"))): + if ";" in line: + spec, marker = line.strip().split(";", 1) + else: + spec, marker = line.strip(), None + req = pkg_resources.Requirement.parse(spec) + if marker and not pkg_resources.evaluate_marker(marker): + msgs.append("%s not available in this environment" % spec) + else: + try: + installed = pkg_resources.working_set.find(req) + except pkg_resources.VersionConflict: + msgs.append("Outdated version of %s installed, need %s" % (req.name, spec)) + else: + if not installed: + msgs.append("Need %s" % spec) + + # Check cElementTree + import xml.etree.ElementTree as ElementTree + + try: + import xml.etree.cElementTree as cElementTree + except ImportError: + msgs.append("cElementTree unable to be imported") + else: + if cElementTree.Element is ElementTree.Element: + msgs.append("cElementTree is just an alias for ElementTree") + if msgs: pytest.exit("\n".join(msgs))