This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_moves.py
More file actions
315 lines (287 loc) · 14.4 KB
/
Copy pathtest_moves.py
File metadata and controls
315 lines (287 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
from __future__ import absolute_import
import os
import stat
from slicker import slicker
import base
class FileMoveSuggestorTest(base.TestBase):
def test_move_module_within_directory(self):
self.write_file('foo.py', 'def myfunc(): return 4\n')
self.write_file('bar.py', 'import foo\n\nfoo.myfunc()\n')
slicker.make_fixes(['foo'], 'baz',
project_root=self.tmpdir)
self.assertFileIs('baz.py', 'def myfunc(): return 4\n')
self.assertFileIs('bar.py', 'import baz\n\nbaz.myfunc()\n')
self.assertFileIsNot('foo.py')
self.assertFalse(self.error_output)
def test_move_module_to_a_new_directory(self):
self.write_file('foo.py', 'def myfunc(): return 4\n')
self.write_file('bar.py', 'import foo\n\nfoo.myfunc()\n')
slicker.make_fixes(['foo'], 'baz.bang',
project_root=self.tmpdir)
self.assertFileIs('baz/bang.py', 'def myfunc(): return 4\n')
self.assertFileIs('baz/__init__.py', '')
self.assertFileIs('bar.py', 'import baz.bang\n\nbaz.bang.myfunc()\n')
self.assertFileIsNot('foo.py')
self.assertFalse(self.error_output)
def test_move_module_to_directory_with_a_nonempty_init_py(self):
self.write_file('foo.py', 'def myfunc(): return 4\n')
self.write_file('bar.py', 'import foo\n\nfoo.myfunc()\n')
self.write_file('baz/__init__.py', '# Non-empty init.py\n')
slicker.make_fixes(['foo'], 'baz.bang',
project_root=self.tmpdir)
self.assertFileIs('baz/bang.py', 'def myfunc(): return 4\n')
self.assertFileIs('baz/__init__.py', '# Non-empty init.py\n')
self.assertFileIs('bar.py', 'import baz.bang\n\nbaz.bang.myfunc()\n')
self.assertFileIsNot('foo.py')
self.assertFalse(self.error_output)
def test_move_module_to_an_existing_directory(self):
self.write_file('foo.py', 'def myfunc(): return 4\n')
self.write_file('bar.py', 'import foo\n\nfoo.myfunc()\n')
self.write_file('baz/__init__.py', '')
slicker.make_fixes(['foo'], 'baz',
project_root=self.tmpdir)
self.assertFileIs('baz/foo.py', 'def myfunc(): return 4\n')
self.assertFileIs('bar.py', 'import baz.foo\n\nbaz.foo.myfunc()\n')
self.assertFileIsNot('foo.py')
self.assertFalse(self.error_output)
def test_move_module_out_of_a_directory(self):
self.write_file('foo/__init__.py', '')
self.write_file('foo/bar.py', 'def myfunc(): return 4\n')
self.write_file('baz.py', 'import foo.bar\n\nfoo.bar.myfunc()\n')
slicker.make_fixes(['foo.bar'], 'bang',
project_root=self.tmpdir)
self.assertFileIs('bang.py', 'def myfunc(): return 4\n')
self.assertFileIs('baz.py', 'import bang\n\nbang.myfunc()\n')
self.assertFileIsNot('foo/bar.py')
self.assertFalse(self.error_output)
# TODO(csilvers): assert that the whole dir `foo` has gone away.
def test_move_module_to_existing_name(self):
self.write_file('foo.py', 'def myfunc(): return 4\n')
self.write_file('bar.py', 'import foo\n\nfoo.myfunc()\n')
with self.assertRaises(ValueError):
slicker.make_fixes(['foo'], 'bar',
project_root=self.tmpdir)
def test_move_package(self):
self.write_file('foo/__init__.py', '')
self.write_file('foo/bar.py', 'def myfunc(): return 4\n')
self.write_file('foo/baz.py', 'def myfunc(): return 5\n')
self.write_file('foo/bang/__init__.py', '')
self.write_file('foo/bang/qux.py', 'qux = True\n')
self.write_file('toplevel.py',
('import foo.bar\nimport foo.baz\n'
'import foo.bang.qux\n\n'
'return foo.bar.val + foo.baz.val +'
' foo.bang.qux.qux\n'))
slicker.make_fixes(['foo'], 'newfoo',
project_root=self.tmpdir)
self.assertFileIs('newfoo/__init__.py', '')
self.assertFileIs('newfoo/bar.py', 'def myfunc(): return 4\n')
self.assertFileIs('newfoo/baz.py', 'def myfunc(): return 5\n')
self.assertFileIs('newfoo/bang/__init__.py', '')
self.assertFileIs('newfoo/bang/qux.py', 'qux = True\n')
self.assertFileIs('toplevel.py',
('import newfoo.bang.qux\nimport newfoo.bar\n'
'import newfoo.baz\n\n'
'return newfoo.bar.val + newfoo.baz.val +'
' newfoo.bang.qux.qux\n'))
self.assertFileIsNot('foo/bar.py')
self.assertFalse(self.error_output)
# TODO(csilvers): assert that the whole dir `foo` has gone away.
def test_move_package_to_existing_name(self):
self.write_file('foo/__init__.py', '')
self.write_file('foo/bar.py', 'def myfunc(): return 4\n')
self.write_file('foo/baz.py', 'def myfunc(): return 5\n')
self.write_file('qux/__init__.py', '')
slicker.make_fixes(['foo'], 'qux',
project_root=self.tmpdir)
self.assertFileIs('qux/__init__.py', '')
self.assertFileIs('qux/foo/__init__.py', '')
self.assertFileIs('qux/foo/bar.py', 'def myfunc(): return 4\n')
self.assertFileIs('qux/foo/baz.py', 'def myfunc(): return 5\n')
self.assertFalse(self.error_output)
def test_dash_f_flag(self):
self.write_file('foo.py', 'def myfunc(): return 4\n')
self.write_file('faa.py', 'def myfaa(): return 4\n')
self.write_file('bar.py', ('import foo\nimport faa\n\n'
'foo.myfunc()\nfaa.myfaa()\n'))
slicker.make_fixes(['foo', 'faa'], self.join('baz/'),
import_alias='FROM',
project_root=self.tmpdir)
self.assertFileIs('baz/foo.py', 'def myfunc(): return 4\n')
self.assertFileIs('baz/faa.py', 'def myfaa(): return 4\n')
self.assertFileIs('baz/__init__.py', '')
self.assertFileIs('bar.py',
('from baz import faa, foo\n\n'
'foo.myfunc()\nfaa.myfaa()\n'))
self.assertFalse(self.error_output)
def test_keep_permissions(self):
self.write_file('foo.py', 'def myfunc(): return 4\n')
os.chmod(self.join('foo.py'), 0o755)
slicker.make_fixes(['foo'], 'baz',
project_root=self.tmpdir)
self.assertEqual(
0o755, stat.S_IMODE(os.stat(self.join('baz.py')).st_mode))
class SymbolMoveSuggestorTest(base.TestBase):
def test_move_function(self):
self.write_file('foo.py', 'def myfunc(): return 17\n')
slicker.make_fixes(['foo.myfunc'], 'newfoo.myfunc',
project_root=self.tmpdir)
self.assertFileIs('newfoo.py', 'def myfunc(): return 17\n')
self.assertFileIsNot('foo.py')
self.assertFalse(self.error_output)
def test_move_class(self):
self.write_file('foo.py', 'class Classy(object): return 17\n')
slicker.make_fixes(['foo.Classy'], 'newfoo.Classy',
project_root=self.tmpdir)
self.assertFileIs('newfoo.py', 'class Classy(object): return 17\n')
self.assertFileIsNot('foo.py')
self.assertFalse(self.error_output)
def test_move_constant(self):
self.write_file('foo.py', 'CACHE = {}\n')
slicker.make_fixes(['foo.CACHE'], 'newfoo.CACHE',
project_root=self.tmpdir)
self.assertFileIs('newfoo.py', 'CACHE = {}\n')
self.assertFileIsNot('foo.py')
self.assertFalse(self.error_output)
def test_move_to_a_new_dir(self):
self.write_file('foo.py', 'CACHE = {}\n')
slicker.make_fixes(['foo.CACHE'], 'newfoo.bar.CACHE',
project_root=self.tmpdir)
self.assertFileIs('newfoo/bar.py', 'CACHE = {}\n')
self.assertFileIs('newfoo/__init__.py', '')
self.assertFileIsNot('foo.py')
self.assertFalse(self.error_output)
def test_appending_to_existing_file(self):
# Note since myfunc is at the top of foo.py, and there's only one
# newline at the bottom of newfoo.py, this tests the case where we add
# newlines.
self.write_file('foo.py', 'def myfunc(): return 17\n')
self.write_file('newfoo.py',
('"""A file with the new version of foo."""\n'
'import quux\n\n'
'def otherfunc():\n'
# Make sure that extra newline won't mess us up:
' return 71\n\n'))
slicker.make_fixes(['foo.myfunc'], 'newfoo.myfunc',
project_root=self.tmpdir)
self.assertFileIs('newfoo.py',
('"""A file with the new version of foo."""\n'
'import quux\n\n\n'
'def otherfunc():\n'
' return 71\n\n\n'
'def myfunc(): return 17\n'))
self.assertFileIsNot('foo.py')
self.assertFalse(self.error_output)
def test_moving_with_context(self):
self.write_file('foo.py',
('"""A file with the old version of foo."""\n'
'import quux\n\n'
'def _secretfunc():\n'
' return quux.secretmonkeys\n\n\n'
'# Does some stuff\n'
'# Be careful calling it!\n\n'
'def myfunc():\n'
' """Returns a number."""\n'
' return 289\n\n\n'
'# Here is another function.\n'
'def otherfunc():\n'
' return 1 + 1\n'))
self.write_file('newfoo.py',
('"""A file with the new version of foo."""\n'
'import quux\n\n'
'def otherfunc():\n'
' return quux.value\n'))
slicker.make_fixes(['foo.myfunc'], 'newfoo.myfunc',
project_root=self.tmpdir)
self.assertFileIs('newfoo.py',
('"""A file with the new version of foo."""\n'
'import quux\n\n\n'
'def otherfunc():\n'
' return quux.value\n\n\n'
'# Does some stuff\n'
'# Be careful calling it!\n\n'
'def myfunc():\n'
' """Returns a number."""\n'
' return 289\n'))
self.assertFileIs('foo.py',
('"""A file with the old version of foo."""\n'
'import quux\n\n\n'
'def _secretfunc():\n'
' return quux.secretmonkeys\n\n\n'
'# Here is another function.\n'
'def otherfunc():\n'
' return 1 + 1\n'))
self.assertFalse(self.error_output)
def test_renaming_function(self):
self.write_file('foo.py',
('def myfunc():\n'
' return 17\n'))
slicker.make_fixes(['foo.myfunc'], 'foo.mybetterfunc',
project_root=self.tmpdir)
self.assertFileIs('foo.py',
('def mybetterfunc():\n'
' return 17\n'))
self.assertFalse(self.error_output)
def test_renaming_decorated_function(self):
self.write_file('foo.py',
('@decorator\n'
'def myfunc():\n'
' return 17\n'))
slicker.make_fixes(['foo.myfunc'], 'foo.mybetterfunc',
project_root=self.tmpdir)
self.assertFileIs('foo.py',
('@decorator\n'
'def mybetterfunc():\n'
' return 17\n'))
self.assertFalse(self.error_output)
def test_renaming_new_style_class(self):
self.write_file('foo.py',
('class Classy(object):\n'
' return 17\n'))
slicker.make_fixes(['foo.Classy'], 'foo.Classier',
project_root=self.tmpdir)
self.assertFileIs('foo.py',
('class Classier(object):\n'
' return 17\n'))
self.assertFalse(self.error_output)
def test_renaming_old_style_class(self):
self.write_file('foo.py',
('class Classy:\n'
' return 17\n'))
slicker.make_fixes(['foo.Classy'], 'foo.Classier',
project_root=self.tmpdir)
self.assertFileIs('foo.py',
('class Classier:\n'
' return 17\n'))
self.assertFalse(self.error_output)
def test_renaming_decorated_class(self):
self.write_file('foo.py',
('@decorator\n'
'class Classy(object):\n'
' return 17\n'))
slicker.make_fixes(['foo.Classy'], 'foo.Classier',
project_root=self.tmpdir)
self.assertFileIs('foo.py',
('@decorator\n'
'class Classier(object):\n'
' return 17\n'))
self.assertFalse(self.error_output)
def test_renaming_constant(self):
self.write_file('foo.py', 'CACHE = {}\n')
slicker.make_fixes(['foo.CACHE'], 'foo._SECRET_CACHE',
project_root=self.tmpdir)
self.assertFileIs('foo.py', '_SECRET_CACHE = {}\n')
self.assertFalse(self.error_output)
def test_rename_and_move(self):
self.write_file('foo.py',
('# a class.\n'
'class Classy(object):\n'
' return 17\n'))
slicker.make_fixes(['foo.Classy'], 'newfoo.Classier',
project_root=self.tmpdir)
self.assertFileIs('newfoo.py',
('# a class.\n'
'class Classier(object):\n'
' return 17\n'))
self.assertFileIsNot('foo.py')
self.assertFalse(self.error_output)