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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import contextlib
import re
import sys
def filter_tests(filters, test_ids):
"""Filter test_ids by the test_filters.
:param list filters: A list of regex filters to apply to the test_ids. The
output will contain any test_ids which have a re.search() match for any
of the regexes in this list. If this is None all test_ids will be
returned
:param list test_ids: A list of test_ids that will be filtered
:return: A list of test ids.
"""
if filters is None:
return test_ids
_filters = []
for f in filters:
if isinstance(f, str):
try:
_filters.append(re.compile(f))
except re.error:
print("Invalid regex: %s provided in filters" % f, file=sys.stderr)
sys.exit(5)
else:
_filters.append(f)
def include(test_id):
for pred in _filters:
if pred.search(test_id):
return True
return list(filter(include, test_ids))
def exclusion_reader(exclude_list):
with contextlib.closing(open(exclude_list)) as exclude_file:
regex_comment_lst = [] # tuple of (regex_compiled, msg, skipped_lst)
for line in exclude_file:
raw_line = line.strip()
split_line = raw_line.split("#")
# Before the # is the regex
line_regex = split_line[0].strip()
if len(split_line) > 1:
# After the # is a comment
comment = "".join(split_line[1:]).strip()
else:
comment = "Skipped because of regex %s:" % line_regex
if not line_regex:
continue
try:
regex_comment_lst.append((re.compile(line_regex), comment, []))
except re.error:
print(
"Invalid regex: %s in provided exclusion list file" % line_regex,
file=sys.stderr,
)
sys.exit(5)
return regex_comment_lst
def _get_regex_from_include_list(file_path):
lines = []
for line in open(file_path).read().splitlines():
split_line = line.strip().split("#")
# Before the # is the regex
line_regex = split_line[0].strip()
if line_regex:
try:
lines.append(re.compile(line_regex))
except re.error:
print(
"Invalid regex: %s in provided inclusion_list file" % line_regex,
file=sys.stderr,
)
sys.exit(5)
return lines
def construct_list(
test_ids, regexes=None, exclude_list=None, include_list=None, exclude_regex=None
):
"""Filters the discovered test cases
:param list test_ids: The set of test_ids to be filtered
:param list regexes: A list of regex filters to apply to the test_ids. The
output will contain any test_ids which have a re.search() match for any
of the regexes in this list. If this is None all test_ids will be
returned
:param str exclude_list: The path to an exclusion_list file
:param str include_list: The path to an inclusion_list file
:param str exclude_regex: regex pattern to exclude tests
:return: iterable of strings. The strings are full
test_ids
:rtype: set
"""
if not regexes:
regexes = None # handle the other false things
safe_re = None
if include_list:
safe_re = _get_regex_from_include_list(include_list)
if not regexes and safe_re:
regexes = safe_re
elif regexes and safe_re:
regexes += safe_re
if exclude_list:
exclude_data = exclusion_reader(exclude_list)
else:
exclude_data = None
if exclude_regex:
msg = "Skipped because of regexp provided as a command line argument:"
try:
record = (re.compile(exclude_regex), msg, [])
except re.error:
print(
"Invalid regex: %s used for exclude_regex" % exclude_regex,
file=sys.stderr,
)
sys.exit(5)
if exclude_data:
exclude_data.append(record)
else:
exclude_data = [record]
list_of_test_cases = filter_tests(regexes, test_ids)
set_of_test_cases = set(list_of_test_cases)
if not exclude_data:
return set_of_test_cases
# NOTE(afazekas): We might use a faster logic when the
# print option is not requested
for rex, msg, s_list in exclude_data:
for test_case in list_of_test_cases:
if rex.search(test_case):
# NOTE(mtreinish): In the case of overlapping regex the test
# case might have already been removed from the set of tests
if test_case in set_of_test_cases:
set_of_test_cases.remove(test_case)
s_list.append(test_case)
return set_of_test_cases
|