aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/testing/filter/filter.py
blob: 4075c67a5a9a2aeca2239bcff50404d2d3458cf0 (plain) (blame)
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
# coding: utf-8
# TODO move devtools/ya/test/filter.py to library/python/testing/filter/filter.py
import re
import fnmatch
import logging

logger = logging.getLogger(__name__)
TEST_SUBTEST_SEPARATOR = '::'

PARSE_TAG_RE = re.compile(r"([+-]?[\w:]*)")


class FilterException(Exception):
    mute = True


def fix_filter(flt):
    if TEST_SUBTEST_SEPARATOR not in flt and "*" not in flt:
        # user wants to filter by test module name
        flt = flt + TEST_SUBTEST_SEPARATOR + "*"
    return flt


def escape_for_fnmatch(s):
    return s.replace("[", "[").replace("]", "]")


def make_py_file_filter(filter_names):
    if filter_names is not None:
        with_star = []
        wo_star = set()
        for flt in filter_names:
            flt = flt.split(':')[0]
            if '*' in flt:
                with_star.append(flt.split('*')[0] + '*')
            else:
                wo_star.add(flt)

    def predicate(filename):
        if filter_names is None:
            return True
        return filename in wo_star or any([fnmatch.fnmatch(escape_for_fnmatch(filename), escape_for_fnmatch(filter_name)) for filter_name in with_star])

    return predicate


def make_name_filter(filter_names):
    filter_names = map(fix_filter, filter_names)
    filter_full_names = set()
    for name in filter_names:
        if '*' not in name:
            filter_full_names.add(name)

    def predicate(testname):
        return testname in filter_full_names or any([fnmatch.fnmatch(escape_for_fnmatch(testname), escape_for_fnmatch(filter_name)) for filter_name in filter_names])

    return predicate