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

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

#countcontent
1n/a"""
2n/aUnit tests for refactor.py.
3n/a"""
4n/a
5n/aimport sys
6n/aimport os
7n/aimport codecs
8n/aimport io
9n/aimport re
10n/aimport tempfile
11n/aimport shutil
12n/aimport unittest
13n/a
14n/afrom lib2to3 import refactor, pygram, fixer_base
15n/afrom lib2to3.pgen2 import token
16n/a
17n/a
18n/aTEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
19n/aFIXER_DIR = os.path.join(TEST_DATA_DIR, "fixers")
20n/a
21n/asys.path.append(FIXER_DIR)
22n/atry:
23n/a _DEFAULT_FIXERS = refactor.get_fixers_from_package("myfixes")
24n/afinally:
25n/a sys.path.pop()
26n/a
27n/a_2TO3_FIXERS = refactor.get_fixers_from_package("lib2to3.fixes")
28n/a
29n/aclass TestRefactoringTool(unittest.TestCase):
30n/a
31n/a def setUp(self):
32n/a sys.path.append(FIXER_DIR)
33n/a
34n/a def tearDown(self):
35n/a sys.path.pop()
36n/a
37n/a def check_instances(self, instances, classes):
38n/a for inst, cls in zip(instances, classes):
39n/a if not isinstance(inst, cls):
40n/a self.fail("%s are not instances of %s" % instances, classes)
41n/a
42n/a def rt(self, options=None, fixers=_DEFAULT_FIXERS, explicit=None):
43n/a return refactor.RefactoringTool(fixers, options, explicit)
44n/a
45n/a def test_print_function_option(self):
46n/a rt = self.rt({"print_function" : True})
47n/a self.assertIs(rt.grammar, pygram.python_grammar_no_print_statement)
48n/a self.assertIs(rt.driver.grammar,
49n/a pygram.python_grammar_no_print_statement)
50n/a
51n/a def test_write_unchanged_files_option(self):
52n/a rt = self.rt()
53n/a self.assertFalse(rt.write_unchanged_files)
54n/a rt = self.rt({"write_unchanged_files" : True})
55n/a self.assertTrue(rt.write_unchanged_files)
56n/a
57n/a def test_fixer_loading_helpers(self):
58n/a contents = ["explicit", "first", "last", "parrot", "preorder"]
59n/a non_prefixed = refactor.get_all_fix_names("myfixes")
60n/a prefixed = refactor.get_all_fix_names("myfixes", False)
61n/a full_names = refactor.get_fixers_from_package("myfixes")
62n/a self.assertEqual(prefixed, ["fix_" + name for name in contents])
63n/a self.assertEqual(non_prefixed, contents)
64n/a self.assertEqual(full_names,
65n/a ["myfixes.fix_" + name for name in contents])
66n/a
67n/a def test_detect_future_features(self):
68n/a run = refactor._detect_future_features
69n/a fs = frozenset
70n/a empty = fs()
71n/a self.assertEqual(run(""), empty)
72n/a self.assertEqual(run("from __future__ import print_function"),
73n/a fs(("print_function",)))
74n/a self.assertEqual(run("from __future__ import generators"),
75n/a fs(("generators",)))
76n/a self.assertEqual(run("from __future__ import generators, feature"),
77n/a fs(("generators", "feature")))
78n/a inp = "from __future__ import generators, print_function"
79n/a self.assertEqual(run(inp), fs(("generators", "print_function")))
80n/a inp ="from __future__ import print_function, generators"
81n/a self.assertEqual(run(inp), fs(("print_function", "generators")))
82n/a inp = "from __future__ import (print_function,)"
83n/a self.assertEqual(run(inp), fs(("print_function",)))
84n/a inp = "from __future__ import (generators, print_function)"
85n/a self.assertEqual(run(inp), fs(("generators", "print_function")))
86n/a inp = "from __future__ import (generators, nested_scopes)"
87n/a self.assertEqual(run(inp), fs(("generators", "nested_scopes")))
88n/a inp = """from __future__ import generators
89n/afrom __future__ import print_function"""
90n/a self.assertEqual(run(inp), fs(("generators", "print_function")))
91n/a invalid = ("from",
92n/a "from 4",
93n/a "from x",
94n/a "from x 5",
95n/a "from x im",
96n/a "from x import",
97n/a "from x import 4",
98n/a )
99n/a for inp in invalid:
100n/a self.assertEqual(run(inp), empty)
101n/a inp = "'docstring'\nfrom __future__ import print_function"
102n/a self.assertEqual(run(inp), fs(("print_function",)))
103n/a inp = "'docstring'\n'somng'\nfrom __future__ import print_function"
104n/a self.assertEqual(run(inp), empty)
105n/a inp = "# comment\nfrom __future__ import print_function"
106n/a self.assertEqual(run(inp), fs(("print_function",)))
107n/a inp = "# comment\n'doc'\nfrom __future__ import print_function"
108n/a self.assertEqual(run(inp), fs(("print_function",)))
109n/a inp = "class x: pass\nfrom __future__ import print_function"
110n/a self.assertEqual(run(inp), empty)
111n/a
112n/a def test_get_headnode_dict(self):
113n/a class NoneFix(fixer_base.BaseFix):
114n/a pass
115n/a
116n/a class FileInputFix(fixer_base.BaseFix):
117n/a PATTERN = "file_input< any * >"
118n/a
119n/a class SimpleFix(fixer_base.BaseFix):
120n/a PATTERN = "'name'"
121n/a
122n/a no_head = NoneFix({}, [])
123n/a with_head = FileInputFix({}, [])
124n/a simple = SimpleFix({}, [])
125n/a d = refactor._get_headnode_dict([no_head, with_head, simple])
126n/a top_fixes = d.pop(pygram.python_symbols.file_input)
127n/a self.assertEqual(top_fixes, [with_head, no_head])
128n/a name_fixes = d.pop(token.NAME)
129n/a self.assertEqual(name_fixes, [simple, no_head])
130n/a for fixes in d.values():
131n/a self.assertEqual(fixes, [no_head])
132n/a
133n/a def test_fixer_loading(self):
134n/a from myfixes.fix_first import FixFirst
135n/a from myfixes.fix_last import FixLast
136n/a from myfixes.fix_parrot import FixParrot
137n/a from myfixes.fix_preorder import FixPreorder
138n/a
139n/a rt = self.rt()
140n/a pre, post = rt.get_fixers()
141n/a
142n/a self.check_instances(pre, [FixPreorder])
143n/a self.check_instances(post, [FixFirst, FixParrot, FixLast])
144n/a
145n/a def test_naughty_fixers(self):
146n/a self.assertRaises(ImportError, self.rt, fixers=["not_here"])
147n/a self.assertRaises(refactor.FixerError, self.rt, fixers=["no_fixer_cls"])
148n/a self.assertRaises(refactor.FixerError, self.rt, fixers=["bad_order"])
149n/a
150n/a def test_refactor_string(self):
151n/a rt = self.rt()
152n/a input = "def parrot(): pass\n\n"
153n/a tree = rt.refactor_string(input, "<test>")
154n/a self.assertNotEqual(str(tree), input)
155n/a
156n/a input = "def f(): pass\n\n"
157n/a tree = rt.refactor_string(input, "<test>")
158n/a self.assertEqual(str(tree), input)
159n/a
160n/a def test_refactor_stdin(self):
161n/a
162n/a class MyRT(refactor.RefactoringTool):
163n/a
164n/a def print_output(self, old_text, new_text, filename, equal):
165n/a results.extend([old_text, new_text, filename, equal])
166n/a
167n/a results = []
168n/a rt = MyRT(_DEFAULT_FIXERS)
169n/a save = sys.stdin
170n/a sys.stdin = io.StringIO("def parrot(): pass\n\n")
171n/a try:
172n/a rt.refactor_stdin()
173n/a finally:
174n/a sys.stdin = save
175n/a expected = ["def parrot(): pass\n\n",
176n/a "def cheese(): pass\n\n",
177n/a "<stdin>", False]
178n/a self.assertEqual(results, expected)
179n/a
180n/a def check_file_refactoring(self, test_file, fixers=_2TO3_FIXERS,
181n/a options=None, mock_log_debug=None,
182n/a actually_write=True):
183n/a tmpdir = tempfile.mkdtemp(prefix="2to3-test_refactor")
184n/a self.addCleanup(shutil.rmtree, tmpdir)
185n/a # make a copy of the tested file that we can write to
186n/a shutil.copy(test_file, tmpdir)
187n/a test_file = os.path.join(tmpdir, os.path.basename(test_file))
188n/a os.chmod(test_file, 0o644)
189n/a
190n/a def read_file():
191n/a with open(test_file, "rb") as fp:
192n/a return fp.read()
193n/a
194n/a old_contents = read_file()
195n/a rt = self.rt(fixers=fixers, options=options)
196n/a if mock_log_debug:
197n/a rt.log_debug = mock_log_debug
198n/a
199n/a rt.refactor_file(test_file)
200n/a self.assertEqual(old_contents, read_file())
201n/a
202n/a if not actually_write:
203n/a return
204n/a rt.refactor_file(test_file, True)
205n/a new_contents = read_file()
206n/a self.assertNotEqual(old_contents, new_contents)
207n/a return new_contents
208n/a
209n/a def test_refactor_file(self):
210n/a test_file = os.path.join(FIXER_DIR, "parrot_example.py")
211n/a self.check_file_refactoring(test_file, _DEFAULT_FIXERS)
212n/a
213n/a def test_refactor_file_write_unchanged_file(self):
214n/a test_file = os.path.join(FIXER_DIR, "parrot_example.py")
215n/a debug_messages = []
216n/a def recording_log_debug(msg, *args):
217n/a debug_messages.append(msg % args)
218n/a self.check_file_refactoring(test_file, fixers=(),
219n/a options={"write_unchanged_files": True},
220n/a mock_log_debug=recording_log_debug,
221n/a actually_write=False)
222n/a # Testing that it logged this message when write=False was passed is
223n/a # sufficient to see that it did not bail early after "No changes".
224n/a message_regex = r"Not writing changes to .*%s" % \
225n/a re.escape(os.sep + os.path.basename(test_file))
226n/a for message in debug_messages:
227n/a if "Not writing changes" in message:
228n/a self.assertRegex(message, message_regex)
229n/a break
230n/a else:
231n/a self.fail("%r not matched in %r" % (message_regex, debug_messages))
232n/a
233n/a def test_refactor_dir(self):
234n/a def check(structure, expected):
235n/a def mock_refactor_file(self, f, *args):
236n/a got.append(f)
237n/a save_func = refactor.RefactoringTool.refactor_file
238n/a refactor.RefactoringTool.refactor_file = mock_refactor_file
239n/a rt = self.rt()
240n/a got = []
241n/a dir = tempfile.mkdtemp(prefix="2to3-test_refactor")
242n/a try:
243n/a os.mkdir(os.path.join(dir, "a_dir"))
244n/a for fn in structure:
245n/a open(os.path.join(dir, fn), "wb").close()
246n/a rt.refactor_dir(dir)
247n/a finally:
248n/a refactor.RefactoringTool.refactor_file = save_func
249n/a shutil.rmtree(dir)
250n/a self.assertEqual(got,
251n/a [os.path.join(dir, path) for path in expected])
252n/a check([], [])
253n/a tree = ["nothing",
254n/a "hi.py",
255n/a ".dumb",
256n/a ".after.py",
257n/a "notpy.npy",
258n/a "sappy"]
259n/a expected = ["hi.py"]
260n/a check(tree, expected)
261n/a tree = ["hi.py",
262n/a os.path.join("a_dir", "stuff.py")]
263n/a check(tree, tree)
264n/a
265n/a def test_file_encoding(self):
266n/a fn = os.path.join(TEST_DATA_DIR, "different_encoding.py")
267n/a self.check_file_refactoring(fn)
268n/a
269n/a def test_false_file_encoding(self):
270n/a fn = os.path.join(TEST_DATA_DIR, "false_encoding.py")
271n/a data = self.check_file_refactoring(fn)
272n/a
273n/a def test_bom(self):
274n/a fn = os.path.join(TEST_DATA_DIR, "bom.py")
275n/a data = self.check_file_refactoring(fn)
276n/a self.assertTrue(data.startswith(codecs.BOM_UTF8))
277n/a
278n/a def test_crlf_newlines(self):
279n/a old_sep = os.linesep
280n/a os.linesep = "\r\n"
281n/a try:
282n/a fn = os.path.join(TEST_DATA_DIR, "crlf.py")
283n/a fixes = refactor.get_fixers_from_package("lib2to3.fixes")
284n/a self.check_file_refactoring(fn, fixes)
285n/a finally:
286n/a os.linesep = old_sep
287n/a
288n/a def test_refactor_docstring(self):
289n/a rt = self.rt()
290n/a
291n/a doc = """
292n/a>>> example()
293n/a42
294n/a"""
295n/a out = rt.refactor_docstring(doc, "<test>")
296n/a self.assertEqual(out, doc)
297n/a
298n/a doc = """
299n/a>>> def parrot():
300n/a... return 43
301n/a"""
302n/a out = rt.refactor_docstring(doc, "<test>")
303n/a self.assertNotEqual(out, doc)
304n/a
305n/a def test_explicit(self):
306n/a from myfixes.fix_explicit import FixExplicit
307n/a
308n/a rt = self.rt(fixers=["myfixes.fix_explicit"])
309n/a self.assertEqual(len(rt.post_order), 0)
310n/a
311n/a rt = self.rt(explicit=["myfixes.fix_explicit"])
312n/a for fix in rt.post_order:
313n/a if isinstance(fix, FixExplicit):
314n/a break
315n/a else:
316n/a self.fail("explicit fixer not loaded")