Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
8ddce3b
Add ParentInfo.location.
ericsnowcurrently Aug 5, 2019
57e3028
Add "relpath" to reported parents (when available).
ericsnowcurrently Aug 6, 2019
d7edee9
Add util.fix_path() and friends.
ericsnowcurrently Aug 6, 2019
0d0017f
Fix should_never_reach_here().
ericsnowcurrently Aug 6, 2019
89790da
Use fix_nodeid() instead of os.path.normcase().
ericsnowcurrently Aug 6, 2019
267ca68
For parents, use the unnormalized name.
ericsnowcurrently Aug 6, 2019
beee582
Fix Python 2 support.
ericsnowcurrently Aug 6, 2019
11fbde1
Use "relpath" to generate "fullpath" for test files.
ericsnowcurrently Aug 6, 2019
020a5dc
Preserve the original test ID.
ericsnowcurrently Aug 6, 2019
9a76ec6
Fix tests on Windows.
ericsnowcurrently Aug 7, 2019
f80bc90
Add a functional test to make sure os.path is carefully controlled.
ericsnowcurrently Aug 7, 2019
94ede9c
Add tests for the file path utils.
ericsnowcurrently Aug 7, 2019
d993a19
Use NORMCASE in util.fix_fileid().
ericsnowcurrently Aug 7, 2019
b28acd8
Normalize path separator in file ID to "/" (but preserve case).
ericsnowcurrently Aug 8, 2019
41b38c9
Fix handling of an error path.
ericsnowcurrently Aug 8, 2019
3131528
Fix expected IDs in the functional tests.
ericsnowcurrently Aug 8, 2019
56fd620
Use normcase in the sort order of parents.
ericsnowcurrently Aug 8, 2019
eac3c66
Fix expected IDs in some of the unit tests.
ericsnowcurrently Aug 8, 2019
8f33b77
Fix node IDs properly in DiscoveredTests.add_test().
ericsnowcurrently Aug 8, 2019
1f68ca3
Fix the resulting "source" in the functional tests.
ericsnowcurrently Aug 8, 2019
462e58b
Do not normcase the ID when sorting parents.
ericsnowcurrently Aug 8, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 51 additions & 11 deletions pythonFiles/testing_tools/adapter/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,42 @@

from __future__ import absolute_import, print_function

import os.path
import re

from .util import fix_fileid, DIRNAME, NORMCASE
from .info import ParentInfo


FILE_ID_RE = re.compile(r"""
^
(?:
( .* [.] (?: py | txt ) \b ) # .txt for doctest files
( [^.] .* )?
)
$
""", re.VERBOSE)


def fix_nodeid(nodeid, kind, rootdir=None, #*,
_fix_fileid=fix_fileid,
):
if not nodeid:
raise ValueError('missing nodeid')
if nodeid == '.':
return nodeid

fileid = nodeid
remainder = ''
if kind not in ('folder', 'file'):
m = FILE_ID_RE.match(nodeid)
if m:
fileid, remainder = m.groups()
elif len(nodeid) > 1:
fileid = nodeid[:2]
remainder = nodeid[2:]
fileid = _fix_fileid(fileid, rootdir)
return fileid + (remainder or '')


class DiscoveredTests(object):
"""A container for the discovered tests and their parents."""
Expand All @@ -23,7 +54,10 @@ def __getitem__(self, index):

@property
def parents(self):
return sorted(self._parents.values(), key=lambda v: (v.root or v.name, v.id))
return sorted(self._parents.values(),
key=lambda p: (NORMCASE(p.root or p.name),
p.id),
)

def reset(self):
"""Clear out any previously discovered tests."""
Expand All @@ -36,25 +70,31 @@ def add_test(self, test, parents):
# Updating the parent ID and the test ID aren't necessary if the
# provided test and parents (from the test collector) are
# properly generated. However, we play it safe here.
test = test._replace(parentid=parentid)
if not test.id.startswith('.' + os.path.sep):
test = test._replace(id=os.path.join('.', test.id))
test = test._replace(
id=fix_nodeid(test.id, 'test', test.path.root),
parentid=parentid,
)
self._tests.append(test)

def _ensure_parent(self, path, parents):
def _ensure_parent(self, path, parents, #*,
_dirname=DIRNAME,
):
rootdir = path.root
relpath = path.relfile

_parents = iter(parents)
nodeid, name, kind = next(_parents)
# As in add_test(), the node ID *should* already be correct.
if nodeid != '.' and not nodeid.startswith('.' + os.path.sep):
nodeid = os.path.join('.', nodeid)
nodeid = fix_nodeid(nodeid, kind, rootdir)
_parentid = nodeid
for parentid, parentname, parentkind in _parents:
# As in add_test(), the parent ID *should* already be correct.
if parentid != '.' and not parentid.startswith('.' + os.path.sep):
parentid = os.path.join('.', parentid)
info = ParentInfo(nodeid, kind, name, rootdir, parentid)
parentid = fix_nodeid(parentid, kind, rootdir)
if kind in ('folder', 'file'):
info = ParentInfo(nodeid, kind, name, rootdir, relpath, parentid)
relpath = _dirname(relpath)
else:
info = ParentInfo(nodeid, kind, name, rootdir, None, parentid)
self._parents[(rootdir, nodeid)] = info
nodeid, name, kind = parentid, parentname, parentkind
assert nodeid == '.'
Expand Down
19 changes: 12 additions & 7 deletions pythonFiles/testing_tools/adapter/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@ def __init__(self, *args, **kwargs):
# self.sub may be None.


class ParentInfo(namedtuple('ParentInfo', 'id kind name root parentid')):
class ParentInfo(namedtuple('ParentInfo', 'id kind name root relpath parentid')):

KINDS = ('folder', 'file', 'suite', 'function', 'subtest')

def __new__(cls, id, kind, name, root=None, parentid=None):
def __new__(cls, id, kind, name, root=None, relpath=None, parentid=None):
self = super(ParentInfo, cls).__new__(
cls,
str(id) if id else None,
str(kind) if kind else None,
str(name) if name else None,
str(root) if root else None,
str(parentid) if parentid else None,
id=str(id) if id else None,
kind=str(kind) if kind else None,
name=str(name) if name else None,
root=str(root) if root else None,
relpath=str(relpath) if relpath else None,
parentid=str(parentid) if parentid else None,
)
return self

Expand All @@ -53,8 +54,12 @@ def __init__(self, *args, **kwargs):
if self.root is None:
if self.parentid is not None or self.kind != 'folder':
raise TypeError('missing root')
if self.relpath is not None:
raise TypeError('unexpected relpath {}'.format(self.relpath))
elif self.parentid is None:
raise TypeError('missing parentid')
elif self.relpath is None and self.kind in ('folder', 'file'):
raise TypeError('missing relpath')


class TestInfo(namedtuple('TestInfo', 'id name path source markers parentid kind')):
Expand Down
10 changes: 5 additions & 5 deletions pythonFiles/testing_tools/adapter/pytest/_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from __future__ import absolute_import, print_function

import os.path
import sys

import pytest
Expand Down Expand Up @@ -61,8 +60,9 @@ def _adjust_pytest_args(pytestargs):
class TestCollector(object):
"""This is a pytest plugin that collects the discovered tests."""

NORMCASE = staticmethod(os.path.normcase)
PATHSEP = os.path.sep
@classmethod
def parse_item(cls, item):
return parse_item(item)

def __init__(self, tests=None):
if tests is None:
Expand All @@ -77,7 +77,7 @@ def pytest_collection_modifyitems(self, session, config, items):
self._started = True
self._tests.reset()
for item in items:
test, parents = parse_item(item, self.NORMCASE, self.PATHSEP)
test, parents = self.parse_item(item)
self._tests.add_test(test, parents)

# This hook is not specified in the docs, so we also provide
Expand All @@ -91,5 +91,5 @@ def pytest_collection_finish(self, session):
return
self._tests.reset()
for item in items:
test, parents = parse_item(item, self.NORMCASE, self.PATHSEP)
test, parents = self.parse_item(item)
self._tests.add_test(test, parents)
Loading