|
1 | 1 | import os.path
|
2 | 2 |
|
| 3 | +import pkg_resources |
| 4 | +import pytest |
| 5 | + |
3 | 6 | from .tree_construction import TreeConstructionFile
|
4 | 7 | from .tokenizer import TokenizerFile
|
5 | 8 | from .sanitizer import SanitizerFile
|
6 | 9 |
|
7 | 10 | _dir = os.path.abspath(os.path.dirname(__file__))
|
| 11 | +_root = os.path.join(_dir, "..", "..") |
8 | 12 | _testdata = os.path.join(_dir, "testdata")
|
9 | 13 | _tree_construction = os.path.join(_testdata, "tree-construction")
|
10 | 14 | _tokenizer = os.path.join(_testdata, "tokenizer")
|
11 | 15 | _sanitizer_testdata = os.path.join(_dir, "sanitizer-testdata")
|
12 | 16 |
|
13 | 17 |
|
14 |
| -def pytest_collectstart(): |
15 |
| - """check to see if the git submodule has been init'd""" |
16 |
| - pass |
| 18 | +def pytest_configure(config): |
| 19 | + msgs = [] |
| 20 | + |
| 21 | + if not os.path.exists(_testdata): |
| 22 | + msg = "testdata not available! " |
| 23 | + if os.path.exists(os.path.join(_root, ".git")): |
| 24 | + msg += ("Please run git submodule update --init --recursive " + |
| 25 | + "and then run tests again.") |
| 26 | + else: |
| 27 | + msg += ("The testdata doesn't appear to be included with this package, " + |
| 28 | + "so finding the right version will be hard. :(") |
| 29 | + 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 | + |
| 67 | + if msgs: |
| 68 | + pytest.exit("\n".join(msgs)) |
17 | 69 |
|
18 | 70 |
|
19 | 71 | def pytest_collect_file(path, parent):
|
20 | 72 | dir = os.path.abspath(path.dirname)
|
21 |
| - if dir == _tree_construction: |
22 |
| - if path.basename == "template.dat": |
23 |
| - return |
| 73 | + dir_and_parents = set() |
| 74 | + while dir not in dir_and_parents: |
| 75 | + dir_and_parents.add(dir) |
| 76 | + dir = os.path.dirname(dir) |
| 77 | + |
| 78 | + if _tree_construction in dir_and_parents: |
24 | 79 | if path.ext == ".dat":
|
25 | 80 | return TreeConstructionFile(path, parent)
|
26 |
| - elif dir == _tokenizer: |
| 81 | + elif _tokenizer in dir_and_parents: |
27 | 82 | if path.ext == ".test":
|
28 | 83 | return TokenizerFile(path, parent)
|
29 |
| - elif dir == _sanitizer_testdata: |
| 84 | + elif _sanitizer_testdata in dir_and_parents: |
30 | 85 | if path.ext == ".dat":
|
31 | 86 | return SanitizerFile(path, parent)
|
0 commit comments