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