You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
7.4 KiB

  1. '''
  2. Created on Feb 2, 2014
  3. @author: Chris
  4. TODO:
  5. - test no argparse module
  6. - test argparse in main
  7. - test argparse in try/catch
  8. -
  9. '''
  10. import os
  11. import ast
  12. import unittest
  13. import source_parser
  14. basic_pyfile = '''
  15. import os
  16. def say_jello():
  17. print "Jello!"
  18. def main():
  19. print "hello!"
  20. parser = ArgumentParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter)
  21. parser.add_argument("filename", help="filename")
  22. parser.add_argument("-r", "--recursive", dest="recurse", action="store_true",
  23. help="recurse into subfolders [default: %(default)s]")
  24. parser.add_argument("-v", "--verbose", dest="verbose", action="count",
  25. help="set verbosity level [default: %(default)s]")
  26. parser.add_argument("-i", "--include", action="append",
  27. help="only include paths matching this regex pattern. Note: exclude is given preference over include. [default: %(default)s]",
  28. metavar="RE")
  29. parser.add_argument("-m", "--mycoolargument", help="mycoolargument")
  30. parser.add_argument("-e", "--exclude", dest="exclude",
  31. help="exclude paths matching this regex pattern. [default: %(default)s]", metavar="RE")
  32. parser.add_argument('-V', '--version', action='version')
  33. parser.add_argument('-T', '--tester', choices=['yes', 'no'])
  34. parser.add_argument(dest="paths", help="paths to folder(s) with source file(s) [default: %(default)s]",
  35. metavar="path", nargs='+')
  36. if __name__ == '__main__':
  37. main()
  38. '''
  39. class TestSourceParser(unittest.TestCase):
  40. PATH = os.path.join(os.path.dirname(__file__), 'mockapplications')
  41. def module_path(self, name):
  42. return os.path.join(self.PATH, name)
  43. def setUp(self):
  44. self._mockapp = self.module_path('mockapplications.py')
  45. self._module_with_noargparse = self.module_path('module_with_no_argparse.py')
  46. self._module_with_arparse_in_try = self.module_path('example_argparse_souce_in_try.py')
  47. self._module_with_argparse_in_main = self.module_path('example_argparse_souce_in_main.py')
  48. def test_should_throw_parser_exception_if_no_argparse_found_in_module(self):
  49. with self.assertRaises(source_parser.ParserError):
  50. source_parser.parse_source_file(self._module_with_noargparse)
  51. def test_find_main(self):
  52. example_source = '''
  53. def main(): pass
  54. '''
  55. nodes = ast.parse(example_source)
  56. main_node = source_parser.find_main(nodes)
  57. self.assertEqual('main', main_node.name)
  58. def test_find_main_throws_exception_if_not_found(self):
  59. example_source = '''
  60. def some_cool_function_that_is_not_main(): pass
  61. '''
  62. with self.assertRaises(source_parser.ParserError):
  63. nodes = ast.parse(example_source)
  64. main_node = source_parser.find_main(nodes)
  65. self.assertEqual('main', main_node.name)
  66. def test_find_try_blocks_finds_all_tryblock_styles(self):
  67. example_source = '''
  68. try: a = 1
  69. except: pass
  70. try: pass
  71. finally: pass
  72. try: pass
  73. except: pass
  74. else: pass
  75. '''
  76. nodes = ast.parse(example_source)
  77. try_blocks = source_parser.find_try_blocks(nodes)
  78. self.assertEqual(3, len(try_blocks))
  79. def test_find_try_blocks_returns_empty_if_no_blocks_present(self):
  80. example_source = 'def main(): pass'
  81. nodes = ast.parse(example_source)
  82. result = source_parser.find_try_blocks(nodes)
  83. self.assertEqual(list(), result)
  84. def test_find_argparse(selfs):
  85. example_source = '''
  86. parser = ArgumentParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter)
  87. parser.add_argument("filename", help="filename")
  88. parser.add_argument("-r", "--recursive", dest="recurse", action="store_true", help="recurse into subfolders [default: %(default)s]")
  89. parser.add_argument("-v", "--verbose", dest="verbose", action="count", help="set verbosity level [default: %(default)s]")
  90. '''
  91. nodes = ast.parse(example_source)
  92. def test_find_argparse_locates_assignment_stmnt_in_main(self):
  93. nodes = ast.parse(source_parser._openfile(self._module_with_argparse_in_main))
  94. main_node = source_parser.find_main(nodes)
  95. self.assertEqual('main', main_node.name)
  96. containing_block = source_parser.find_block_containing_argparse([main_node])
  97. self.assertTrue(containing_block is not None)
  98. self.assertEqual('main', containing_block.name)
  99. def test_find_argparse_locates_assignment_stmnt_in_try_block(self):
  100. nodes = ast.parse(source_parser._openfile(self._module_with_arparse_in_try))
  101. main_node = source_parser.find_main(nodes)
  102. self.assertEqual('main', main_node.name)
  103. try_nodes = source_parser.find_try_blocks(main_node)
  104. self.assertTrue(len(try_nodes) > 0)
  105. containing_block = source_parser.find_block_containing_argparse([main_node] + try_nodes)
  106. self.assertEqual(ast.TryExcept, type(containing_block))
  107. def test_find_argparse_throws_exception_if_not_found(self):
  108. with self.assertRaises(source_parser.ParserError):
  109. nodes = ast.parse(source_parser._openfile(self._module_with_noargparse))
  110. main_node = source_parser.find_main(nodes)
  111. self.assertEqual('main', main_node.name)
  112. try_nodes = source_parser.find_try_blocks(main_node)
  113. containing_block = source_parser.find_block_containing_argparse([main_node] + try_nodes)
  114. def test_has_instantiator_returns_true_if_object_found(self):
  115. source = '''
  116. parser = ArgumentParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter)
  117. parser.add_argument("filename", help="filename")
  118. '''
  119. nodes = ast.parse(source)
  120. self.assertTrue(source_parser.has_instantiator(nodes.body[0], 'ArgumentParser'))
  121. def test_has_instantiator_returns_false_if_object_not_found(self):
  122. source = '''
  123. parser = NopeParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter)
  124. parser.add_argument("filename", help="filename")
  125. '''
  126. nodes = ast.parse(source)
  127. self.assertFalse(source_parser.has_instantiator(nodes.body[0], 'ArgumentParser'))
  128. def test_has_assignment_returns_true_if_object_found(self):
  129. source = '''
  130. parser = ArgumentParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter)
  131. parser.add_argument("filename", help="filename")
  132. '''
  133. nodes = ast.parse(source)
  134. self.assertTrue(source_parser.has_assignment(nodes.body[1], 'add_argument'))
  135. def test_has_assignment_returns_false_if_object_not_found(self):
  136. source = '''
  137. parser = ArgumentParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter)
  138. parser.add_argument("filename", help="filename")
  139. '''
  140. nodes = ast.parse(source)
  141. self.assertFalse(source_parser.has_instantiator(nodes.body[1], 'add_argument'))
  142. # def test_parse_source_file__file_with_argparse_in_main__succesfully_finds_and_returns_ast_obejcts(self):
  143. # ast_objects = source_parser.parse_source_file(self._module_with_argparse_in_main)
  144. # for obj in ast_objects:
  145. # self.assertTrue(type(obj) in (ast.Assign, ast.Expr))
  146. #
  147. # def test_parse_source_file__file_with_argparse_in_try_block__succesfully_finds_and_returns_ast_obejcts(self):
  148. # ast_objects = source_parser.parse_source_file(self._module_with_arparse_in_try)
  149. # for obj in ast_objects:
  150. # self.assertTrue(type(obj) in (ast.Assign, ast.Expr))
  151. if __name__ == "__main__":
  152. #import sys;sys.argv = ['', 'Test.testName']
  153. unittest.main()