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