ยปCore Development>Code coverage>Lib/lib2to3/tests/support.py

Python code coverage for Lib/lib2to3/tests/support.py

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