Advertisement
Guest User

Untitled

a guest
Sep 1st, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.07 KB | None | 0 0
  1. diff --git a/testimony/__init__.py b/testimony/__init__.py
  2. index 8957a18..623f5c9 100755
  3. --- a/testimony/__init__.py
  4. +++ b/testimony/__init__.py
  5. @@ -191,101 +191,65 @@ def print_json_output(report, results):
  6. def get_docstrings(report, path, result):
  7. """Function to read docstrings from test_*** methods for a given file"""
  8. return_list = []
  9. - obj = ast.parse(''.join(open(path)))
  10. - # The body field inside obj.body[] contains the docstring
  11. - # So first find the body field of obj.body[] array
  12. - for i in range(0, len(obj.body)):
  13. - parameters = obj.body[i]._fields
  14. - for attr in parameters:
  15. - if attr == 'body':
  16. - # Now iterate the found body[] list from obj.body[] to find the
  17. - # docstrings. Remember that body[] list will have all different
  18. - # items like class docstrings and functions. So first find the
  19. - # items which are functions
  20. - for j in range(0, len(obj.body[i].body)):
  21. + with open(path) as handler:
  22. + tree = ast.parse(handler.read())
  23. + test_methods = [
  24. + node for node in ast.walk(tree)
  25. + if isinstance(node, ast.FunctionDef) and node.name.startswith('test_')
  26. + ]
  27. + for test_method in test_methods:
  28. + docstring = ast.get_docstring(test_method)
  29. + if docstring is None:
  30. + if (report == PRINT_REPORT or
  31. + report == VALIDATE_REPORT):
  32. + item_list.append("%s: %s" % (
  33. + func_name,
  34. + colored(PRINT_DOC_MISSING, CLR_ERR)))
  35. + return_list.append(item_list)
  36. + result.no_docstring += 1
  37. + continue
  38. + else:
  39. + featurefound = False
  40. + testfound = False
  41. + assertfound = False
  42. + for line in docstring.split('@'):
  43. + # Remove trailing spaces
  44. + line = line.rstrip()
  45. + # Sometimes there are double new line
  46. + # characters in the middle. We need
  47. + # only one of those to print
  48. + line = line.replace('\n\n', '\n')
  49. + if len(line) > 0:
  50. item_list = []
  51. - try:
  52. - obj_param = obj.body[i].body[j]._fields
  53. - for attr in obj_param:
  54. - # Retrieve the func name to check if this is a
  55. - # test_* function
  56. - if attr == 'name':
  57. - func_name = getattr(obj.body[i].body[j],
  58. - "name")
  59. - if func_name.startswith('test_'):
  60. - # Find the docstring value of this function
  61. - # Remove the trailing spaces
  62. - value = (obj.body[i].body[j].body[0].
  63. - value.s.lstrip())
  64. - # Split the docstring with @
  65. - doclines = value.split('@',)
  66. - featurefound = False
  67. - testfound = False
  68. - assertfound = False
  69. - for attr in doclines:
  70. - # Remove trailing spaces
  71. - attr = attr.rstrip()
  72. - # Remove any new line characters
  73. - attr = attr.rstrip('\n')
  74. - # Sometimes there are double new line
  75. - # characters in the middle. We need
  76. - # only one of those to print
  77. - attr = attr.replace('\n\n', '\n')
  78. - if attr != '':
  79. - if report == VALIDATE_REPORT:
  80. - tag = attr.split(" ", 1)
  81. - # Error out invalid docstring
  82. - if not any(
  83. - x in tag[0].lower() for
  84. - x in DOC_TAGS):
  85. - item_list.append(
  86. - "%s: Invalid Doc:%s"
  87. - % (func_name, colored(
  88. - attr, CLR_ERR,
  89. - attrs=['bold'])))
  90. - result.invalid_doc += 1
  91. - if (DOC_TAGS[0] in
  92. - tag[0].lower()):
  93. - featurefound = True
  94. - if (DOC_TAGS[1] in
  95. - tag[0].lower()):
  96. - testfound = True
  97. - if (DOC_TAGS[4] in
  98. - tag[0].lower()):
  99. - assertfound = True
  100. - elif report == BUGS_REPORT:
  101. - # Find the bug from docstring
  102. - bug = attr.split(" ", 1)
  103. - if (DOC_TAGS[5] in
  104. - bug[0].lower()):
  105. - item_list.append(attr)
  106. - result.bugs += 1
  107. - result.bugs_list.append(
  108. - bug[1])
  109. - else:
  110. - # For printing all test cases
  111. - item_list.append(attr)
  112. - if report == VALIDATE_REPORT:
  113. - if (not featurefound or
  114. - not testfound or
  115. - not assertfound):
  116. - item_list.append("%s: %s" % (
  117. - func_name,
  118. - PRINT_NO_MINIMUM_DOC))
  119. - result.no_minimal_docstring += 1
  120. - if len(item_list) != 0:
  121. - return_list.append(item_list)
  122. - except AttributeError:
  123. - if (report == PRINT_REPORT or
  124. - report == VALIDATE_REPORT):
  125. - item_list.append("%s: %s" % (
  126. - func_name,
  127. - colored(PRINT_DOC_MISSING, CLR_ERR)))
  128. - return_list.append(item_list)
  129. - result.no_docstring += 1
  130. - continue
  131. - except:
  132. - print colored(PRINT_PARSE_ERR, CLR_ERR, attrs=['bold'])
  133. + if report == VALIDATE_REPORT:
  134. + tag = line.split(' ', 1)
  135. + # Error out invalid docstring
  136. + if not any(x in tag[0].lower() for x in DOC_TAGS):
  137. + item_list.append('%s: Invalid Doc:%s' % (
  138. + func_name, colored(line, CLR_ERR, attrs=['bold'])))
  139. + result.invalid_doc += 1
  140. + if (DOC_TAGS[0] in tag[0].lower()):
  141. + featurefound = True
  142. + if (DOC_TAGS[1] in tag[0].lower()):
  143. + testfound = True
  144. + if (DOC_TAGS[4] in tag[0].lower()):
  145. + assertfound = True
  146. + elif report == BUGS_REPORT:
  147. + # Find the bug from docstring
  148. + bug = line.split(' ', 1)
  149. + if (DOC_TAGS[5] in bug[0].lower()):
  150. + item_list.append(line)
  151. + result.bugs += 1
  152. + result.bugs_list.append(bug[1])
  153. + else:
  154. + # For printing all test cases
  155. + item_list.append(line)
  156. + if report == VALIDATE_REPORT:
  157. + if (not featurefound or not testfound or not assertfound):
  158. + item_list.append('%s: %s' % (func_name, PRINT_NO_MINIMUM_DOC))
  159. + result.no_minimal_docstring += 1
  160. + if len(item_list) != 0:
  161. + return_list.append(item_list)
  162. return return_list, result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement