|
| 1 | +import os.path |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from .errors import UnsupportedCommandError |
| 6 | +from .info import TestInfo, TestPath |
| 7 | + |
| 8 | + |
| 9 | +def add_cli_subparser(cmd, name, parent): |
| 10 | + """Add a new subparser to the given parent and add args to it.""" |
| 11 | + parser = parent.add_parser(name) |
| 12 | + if cmd == 'discover': |
| 13 | + # For now we don't have any tool-specific CLI options to add. |
| 14 | + pass |
| 15 | + else: |
| 16 | + raise UnsupportedCommandError(cmd) |
| 17 | + return parser |
| 18 | + |
| 19 | + |
| 20 | +def discover(pytestargs=None, |
| 21 | + _pytest_main=pytest.main, _plugin=None): |
| 22 | + """Return the results of test discovery.""" |
| 23 | + if _plugin is None: |
| 24 | + _plugin = TestCollector() |
| 25 | + |
| 26 | + pytestargs = _adjust_pytest_args(pytestargs) |
| 27 | + ec = _pytest_main(pytestargs, [_plugin]) |
| 28 | + if ec != 0: |
| 29 | + raise Exception('pytest discovery failed (exit code {})'.format(ec)) |
| 30 | + if _plugin.discovered is None: |
| 31 | + raise Exception('pytest discovery did not start') |
| 32 | + return _plugin.discovered |
| 33 | + |
| 34 | + |
| 35 | +def _adjust_pytest_args(pytestargs): |
| 36 | + pytestargs = list(pytestargs) if pytestargs else [] |
| 37 | + # Duplicate entries should be okay. |
| 38 | + pytestargs.insert(0, '--collect-only') |
| 39 | + pytestargs.insert(0, '-pno:terminal') |
| 40 | + # TODO: pull in code from: |
| 41 | + # src/client/unittests/pytest/services/discoveryService.ts |
| 42 | + # src/client/unittests/pytest/services/argsService.ts |
| 43 | + return pytestargs |
| 44 | + |
| 45 | + |
| 46 | +class TestCollector(object): |
| 47 | + """This is a pytest plugin that collects the discovered tests.""" |
| 48 | + |
| 49 | + discovered = None |
| 50 | + |
| 51 | + # Relevant plugin hooks: |
| 52 | + # https://docs.pytest.org/en/latest/reference.html#collection-hooks |
| 53 | + |
| 54 | + def pytest_collection_modifyitems(self, session, config, items): |
| 55 | + self.discovered = [] |
| 56 | + for item in items: |
| 57 | + info = _parse_item(item) |
| 58 | + self.discovered.append(info) |
| 59 | + |
| 60 | + # This hook is not specified in the docs, so we also provide |
| 61 | + # the "modifyitems" hook just in case. |
| 62 | + def pytest_collection_finish(self, session): |
| 63 | + try: |
| 64 | + items = session.items |
| 65 | + except AttributeError: |
| 66 | + # TODO: Is there an alternative? |
| 67 | + return |
| 68 | +# print(', '.join(k for k in dir(items[0]) if k[0].islower())) |
| 69 | + self.discovered = [] |
| 70 | + for item in items: |
| 71 | +# print(' ', item.user_properties) |
| 72 | +# print(' ', item.own_markers) |
| 73 | +# print(' ', list(item.iter_markers())) |
| 74 | +# print() |
| 75 | + info = _parse_item(item) |
| 76 | + self.discovered.append(info) |
| 77 | + |
| 78 | + |
| 79 | +def _parse_item(item): |
| 80 | + """ |
| 81 | + (pytest.Collector) |
| 82 | + pytest.Session |
| 83 | + pytest.Package |
| 84 | + pytest.Module |
| 85 | + pytest.Class |
| 86 | + (pytest.File) |
| 87 | + (pytest.Item) |
| 88 | + pytest.Function |
| 89 | + """ |
| 90 | + # Figure out the file. |
| 91 | + filename, lineno, fullname = item.location |
| 92 | + if not str(item.fspath).endswith(os.path.sep + filename): |
| 93 | + raise NotImplementedError |
| 94 | + testroot = str(item.fspath)[:-len(filename)].rstrip(os.path.sep) |
| 95 | + if os.path.sep in filename: |
| 96 | + relfile = filename |
| 97 | + else: |
| 98 | + relfile = os.path.join('.', filename) |
| 99 | + |
| 100 | + # Figure out the func (and subs). |
| 101 | + funcname = item.function.__name__ |
| 102 | + parts = item.nodeid.split('::') |
| 103 | + if parts.pop(0) != filename: |
| 104 | + # TODO: What to do? |
| 105 | + raise NotImplementedError |
| 106 | + suites = [] |
| 107 | + while len(parts) > 1: |
| 108 | + suites.append(parts.pop(0)) |
| 109 | + parameterized = '' |
| 110 | + if '[' in parts[0]: |
| 111 | + _func, sep, parameterized = parts[0].partition('[') |
| 112 | + parameterized = sep + parameterized |
| 113 | + if _func != funcname: |
| 114 | + # TODO: What to do? |
| 115 | + raise NotImplementedError |
| 116 | + if suites: |
| 117 | + testfunc = '.'.join(suites) + '.' + funcname |
| 118 | + else: |
| 119 | + testfunc = funcname |
| 120 | + if fullname != testfunc + parameterized: |
| 121 | + # TODO: What to do? |
| 122 | + raise NotImplementedError |
| 123 | + |
| 124 | + # Sort out markers. |
| 125 | + # See: https://docs.pytest.org/en/latest/reference.html#marks |
| 126 | + markers = set() |
| 127 | + for marker in item.own_markers: |
| 128 | + if marker.name == 'parameterize': |
| 129 | + # We've already covered these. |
| 130 | + continue |
| 131 | + elif marker.name == 'skip': |
| 132 | + markers.add('skip') |
| 133 | + elif marker.name == 'skipif': |
| 134 | + markers.add('skip-if') |
| 135 | + elif marker.name == 'xfail': |
| 136 | + markers.add('expected-failure') |
| 137 | + # TODO: Support other markers? |
| 138 | + |
| 139 | + return TestInfo( |
| 140 | + id=item.nodeid, |
| 141 | + name=item.name, |
| 142 | + path=TestPath( |
| 143 | + root=testroot, |
| 144 | + relfile=relfile, |
| 145 | + func=testfunc, |
| 146 | + sub=[parameterized] if parameterized else None, |
| 147 | + ), |
| 148 | + lineno=lineno, |
| 149 | + markers=sorted(markers) if markers else None, |
| 150 | + ) |
0 commit comments