File: conftest.py

package info (click to toggle)
python-html5rdf 1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,448 kB
  • sloc: python: 12,794; makefile: 3
file content (112 lines) | stat: -rw-r--r-- 4,072 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import os.path
import sys

import pkg_resources
import pytest

from .tree_construction import TreeConstructionFile
from .tokenizer import TokenizerFile

_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")
_sanitizer_testdata = os.path.join(_dir, "sanitizer-testdata")


def fail_if_missing_pytest_expect():
    """Throws an exception halting pytest if pytest-expect isn't working"""
    try:
        from pytest_expect import expect  # noqa
    except ImportError:
        header = '*' * 78
        print(
            '\n' +
            header + '\n' +
            'ERROR: Either pytest-expect or its dependency u-msgpack-python is not\n' +
            'installed. Please install them both before running pytest.\n' +
            header + '\n',
            file=sys.stderr
        )
        raise


fail_if_missing_pytest_expect()


def pytest_configure(config):
    msgs = []

    if not os.path.exists(_testdata):
        msg = "testdata not available! "
        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) 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.ElementTree 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))


def pytest_collect_file(path, parent):
    dir = os.path.abspath(path.dirname)
    dir_and_parents = set()
    while dir not in dir_and_parents:
        dir_and_parents.add(dir)
        dir = os.path.dirname(dir)

    if _tree_construction in dir_and_parents:
        if path.ext == ".dat":
            return TreeConstructionFile.from_parent(parent, fspath=path)
    elif _tokenizer in dir_and_parents:
        if path.ext == ".test":
            return TokenizerFile.from_parent(parent, fspath=path)


# Tiny wrapper to allow .from_parent constructors on older pytest for PY27
if not hasattr(pytest.Item.__base__, "from_parent"):
    @classmethod
    def from_parent(cls, parent, **kwargs):
        return cls(parent=parent, **kwargs)

    pytest.Item.__base__.from_parent = from_parent