• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Support code for test_*.py files"""
2# Author: Collin Winter
3
4# Python imports
5import unittest
6import sys
7import os
8import os.path
9import re
10from textwrap import dedent
11
12# Local imports
13from lib2to3 import pytree, refactor
14from lib2to3.pgen2 import driver as pgen2_driver
15
16test_dir = os.path.dirname(__file__)
17proj_dir = os.path.normpath(os.path.join(test_dir, ".."))
18grammar_path = os.path.join(test_dir, "..", "Grammar.txt")
19grammar = pgen2_driver.load_grammar(grammar_path)
20driver = pgen2_driver.Driver(grammar, convert=pytree.convert)
21
22def parse_string(string):
23    return driver.parse_string(reformat(string), debug=True)
24
25def 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
30def reformat(string):
31    return dedent(string) + u"\n\n"
32
33def 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
48def 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
54TestCase = unittest.TestCase
55