未验证 提交 817c25c2 编写于 作者: C Cleber Rosa

Merge remote-tracking branch 'apahim/improve_docstring_tags_v2'

Signed-off-by: NCleber Rosa <crosa@redhat.com>
......@@ -611,14 +611,14 @@ class FileLoader(TestLoader):
docstring = ast.get_docstring(statement)
# Looking for a class that has in the docstring either
# ":avocado: enable" or ":avocado: disable
if safeloader.is_docstring_directive_disable(docstring):
if safeloader.check_docstring_directive(docstring, 'disable'):
continue
elif safeloader.is_docstring_directive_enable(docstring):
elif safeloader.check_docstring_directive(docstring, 'enable'):
methods = [st.name for st in statement.body if
isinstance(st, ast.FunctionDef) and
st.name.startswith('test')]
methods = data_structures.ordered_list_unique(methods)
tags = safeloader.get_docstring_directive_tags(docstring)
tags = safeloader.get_docstring_directives_tags(docstring)
result[statement.name] = {'methods': methods,
'tags': tags}
continue
......@@ -632,7 +632,7 @@ class FileLoader(TestLoader):
isinstance(st, ast.FunctionDef) and
st.name.startswith('test')]
methods = data_structures.ordered_list_unique(methods)
tags = safeloader.get_docstring_directive_tags(docstring)
tags = safeloader.get_docstring_directives_tags(docstring)
result[statement.name] = {'methods': methods,
'tags': tags}
continue
......@@ -647,7 +647,7 @@ class FileLoader(TestLoader):
isinstance(st, ast.FunctionDef) and
st.name.startswith('test')]
methods = data_structures.ordered_list_unique(methods)
tags = safeloader.get_docstring_directive_tags(docstring)
tags = safeloader.get_docstring_directives_tags(docstring)
result[statement.name] = {'methods': methods,
'tags': tags}
......
......@@ -68,65 +68,44 @@ def modules_imported_as(module):
AVOCADO_DOCSTRING_DIRECTIVE_RE = re.compile(r'\s*:avocado:\s*(\S+)\s*')
def get_docstring_directive(docstring):
def get_docstring_directives(docstring):
"""
Returns the value of the avocado docstring directive
Returns the values of the avocado docstring directives
:param docstring: the complete text used as documentation
:type docstring: str
"""
if docstring is None:
return None
result = AVOCADO_DOCSTRING_DIRECTIVE_RE.search(docstring)
if result is not None:
return result.groups()[0]
def is_docstring_directive_enable(docstring):
:rtype: builtin.list
"""
Checks if there's a docstring directive that enables a Test class
:rtype: bool
"""
result = get_docstring_directive(docstring)
return result == 'enable'
def is_docstring_directive_disable(docstring):
"""
Checks if there's a docstring directive that disables a Test class
:rtype: bool
"""
result = get_docstring_directive(docstring)
return result == 'disable'
try:
return AVOCADO_DOCSTRING_DIRECTIVE_RE.findall(docstring)
except TypeError:
return []
def is_docstring_directive_tags(docstring):
def check_docstring_directive(docstring, directive):
"""
Checks if there's a docstring directive that tags a test
Checks if there's a given directive in a given docstring
:rtype: bool
"""
result = get_docstring_directive(docstring)
if result is not None:
return result.startswith('tags=')
return False
return directive in get_docstring_directives(docstring)
def get_docstring_directive_tags(docstring):
def get_docstring_directives_tags(docstring):
"""
Returns the test categories based on a `:avocado: tags=category` docstring
Returns the test categories based on a `:avocado: tags=category`
docstring
:rtype: set
"""
if not is_docstring_directive_tags(docstring):
return []
tags = []
for item in get_docstring_directives(docstring):
if item.startswith('tags='):
_, comma_tags = item.split('tags=', 1)
tags.extend([tag for tag in comma_tags.split(',') if tag])
raw_tag = get_docstring_directive(docstring)
if raw_tag is not None:
_, comma_tags = raw_tag.split('tags=', 1)
return set([tag for tag in comma_tags.split(',') if tag])
return set(tags)
def find_class_and_methods(path, method_pattern=None, base_class=None):
......
......@@ -1238,6 +1238,10 @@ You can also use the ``:avocado: disable`` docstring directive, that
works the opposite way: something that would be considered an Avocado
test, but we force it to not be listed as one.
The docstring ``:avocado: disable`` is evaluated first by Avocado,
meaning that if both ``:avocado: disable`` and ``:avocado: enable`` are
present in the same docstring, the test will not be listed.
.. _categorizing-tests:
Categorizing tests
......
......@@ -61,7 +61,9 @@ class DocstringDirectives(unittest.TestCase):
":avocado: tags=SLOW,disk, invalid": set(["SLOW", "disk"]),
":avocado: tags=SLOW,disk , invalid": set(["SLOW", "disk"]),
":avocado:\ttags=FAST": set(["FAST"]),
":avocado: tags=": set([])}
":avocado: tags=": set([]),
":avocado: enable\n:avocado: tags=fast": set(["fast"]),
":avocado: tags=fast,slow\n:avocado: enable": set(["fast", "slow"])}
def test_longline(self):
docstring = ("This is a very long docstring in a single line. "
......@@ -69,39 +71,34 @@ class DocstringDirectives(unittest.TestCase):
"mention avocado: it's awesome, but that was not a "
"directive. a tag would be something line this: "
":avocado: enable")
self.assertIsNotNone(safeloader.get_docstring_directive(docstring))
self.assertIsNotNone(safeloader.get_docstring_directives(docstring))
def test_newlines(self):
docstring = ("\n\n\nThis is a docstring with many new\n\nlines "
"followed by an avocado tag\n"
"\n\n:avocado: enable\n\n")
self.assertIsNotNone(safeloader.get_docstring_directive(docstring))
self.assertIsNotNone(safeloader.get_docstring_directives(docstring))
def test_enabled(self):
self.assertTrue(safeloader.is_docstring_directive_enable(":avocado: enable"))
self.assertTrue(safeloader.is_docstring_directive_enable(":avocado:\tenable"))
self.assertFalse(safeloader.is_docstring_directive_enable(":AVOCADO: ENABLE"))
self.assertFalse(safeloader.is_docstring_directive_enable(":avocado: enabled"))
self.assertTrue(safeloader.check_docstring_directive(":avocado: enable", 'enable'))
self.assertTrue(safeloader.check_docstring_directive(":avocado:\tenable", 'enable'))
self.assertTrue(safeloader.check_docstring_directive(":avocado: enable\n:avocado: tags=fast", 'enable'))
self.assertFalse(safeloader.check_docstring_directive(":AVOCADO: ENABLE", 'enable'))
self.assertFalse(safeloader.check_docstring_directive(":avocado: enabled", 'enable'))
def test_disabled(self):
self.assertTrue(safeloader.is_docstring_directive_disable(":avocado: disable"))
self.assertTrue(safeloader.is_docstring_directive_disable(":avocado:\tdisable"))
self.assertFalse(safeloader.is_docstring_directive_disable(":AVOCADO: DISABLE"))
self.assertFalse(safeloader.is_docstring_directive_disable(":avocado: disabled"))
def test_is_tags(self):
for tag in self.VALID_TAGS:
self.assertTrue(safeloader.is_docstring_directive_tags(tag))
for tag in self.NO_TAGS:
self.assertFalse(safeloader.is_docstring_directive_tags(tag))
self.assertTrue(safeloader.check_docstring_directive(":avocado: disable", 'disable'))
self.assertTrue(safeloader.check_docstring_directive(":avocado:\tdisable", 'disable'))
self.assertFalse(safeloader.check_docstring_directive(":AVOCADO: DISABLE", 'disable'))
self.assertFalse(safeloader.check_docstring_directive(":avocado: disabled", 'disable'))
def test_get_tags_empty(self):
for tag in self.NO_TAGS:
self.assertEqual([], safeloader.get_docstring_directive_tags(tag))
self.assertEqual(set([]), safeloader.get_docstring_directives_tags(tag))
def test_get_tags(self):
for raw, tags in self.VALID_TAGS.items():
self.assertEqual(safeloader.get_docstring_directive_tags(raw), tags)
self.assertEqual(safeloader.get_docstring_directives_tags(raw), tags)
class UnlimitedDiff(unittest.TestCase):
......@@ -128,7 +125,6 @@ class FindClassAndMethods(UnlimitedDiff):
'test_newlines',
'test_enabled',
'test_disabled',
'test_is_tags',
'test_get_tags_empty',
'test_get_tags'],
'FindClassAndMethods': ['test_self',
......@@ -150,7 +146,6 @@ class FindClassAndMethods(UnlimitedDiff):
'test_newlines',
'test_enabled',
'test_disabled',
'test_is_tags',
'test_get_tags_empty',
'test_get_tags'],
'FindClassAndMethods': ['test_self',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册