1 """Support code for test_*.py files""" 2 # Author: Collin Winter 3 4 # Python imports 5 import unittest 6 import sys 7 import os 8 import os.path 9 import re 10 from textwrap import dedent 11 12 # Local imports 13 from lib2to3 import pytree, refactor 14 from lib2to3.pgen2 import driver 15 16 test_dir = os.path.dirname(__file__) 17 proj_dir = os.path.normpath(os.path.join(test_dir, "..")) 18 grammar_path = os.path.join(test_dir, "..", "Grammar.txt") 19 grammar = driver.load_grammar(grammar_path) 20 driver = driver.Driver(grammar, convert=pytree.convert) 21 22 def parse_string(string): 23 return driver.parse_string(reformat(string), debug=True) 24 25 def run_all_tests(test_mod=None, tests=None): 26 if tests is None: 27 tests = unittest.TestLoader().loadTestsFromModule(test_mod) 28 unittest.TextTestRunner(verbosity=2).run(tests) 29 30 def reformat(string): 31 return dedent(string) + u"\n\n" 32 33 def get_refactorer(fixer_pkg="lib2to3", fixers=None, options=None): 34 """ 35 A convenience function for creating a RefactoringTool for tests. 36 37 fixers is a list of fixers for the RefactoringTool to use. By default 38 "lib2to3.fixes.*" is used. options is an optional dictionary of options to 39 be passed to the RefactoringTool. 40 """ 41 if fixers is not None: 42 fixers = [fixer_pkg + ".fixes.fix_" + fix for fix in fixers] 43 else: 44 fixers = refactor.get_fixers_from_package(fixer_pkg + ".fixes") 45 options = options or {} 46 return refactor.RefactoringTool(fixers, options, explicit=True) 47 48 def all_project_files(): 49 for dirpath, dirnames, filenames in os.walk(proj_dir): 50 for filename in filenames: 51 if filename.endswith(".py"): 52 yield os.path.join(dirpath, filename) 53 54 TestCase = unittest.TestCase 55