blob: 4756e4f6f77622e568c7a75a0c7be9265fac44fc [file] [log] [blame]
Éric Araujoe84e2632012-02-25 16:24:59 +01001"""Tests for scripts in the Tools directory.
2
3This file contains regression tests for some of the scripts found in the
4Tools directory of a Python checkout or tarball, such as reindent.py.
5"""
6
7import os
Serhiy Storchaka8cd7f822013-01-11 11:59:59 +02008import sys
Éric Araujoe84e2632012-02-25 16:24:59 +01009import unittest
Serhiy Storchaka8cd7f822013-01-11 11:59:59 +020010import shutil
11import subprocess
Éric Araujoe84e2632012-02-25 16:24:59 +010012import sysconfig
Serhiy Storchaka8cd7f822013-01-11 11:59:59 +020013import tempfile
14import textwrap
Éric Araujoe84e2632012-02-25 16:24:59 +010015from test import test_support
Serhiy Storchaka8cd7f822013-01-11 11:59:59 +020016from test.script_helper import assert_python_ok, temp_dir
Éric Araujoe84e2632012-02-25 16:24:59 +010017
18if not sysconfig.is_python_build():
19 # XXX some installers do contain the tools, should we detect that
20 # and run the tests in that case too?
21 raise unittest.SkipTest('test irrelevant for an installed Python')
22
doko@ubuntu.com7a8634d2012-09-10 14:34:42 +020023basepath = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
24 'Tools')
Serhiy Storchaka8cd7f822013-01-11 11:59:59 +020025scriptsdir = os.path.join(basepath, 'scripts')
Éric Araujoe84e2632012-02-25 16:24:59 +010026
27
28class ReindentTests(unittest.TestCase):
Serhiy Storchaka8cd7f822013-01-11 11:59:59 +020029 script = os.path.join(scriptsdir, 'reindent.py')
Éric Araujoe84e2632012-02-25 16:24:59 +010030
31 def test_noargs(self):
32 assert_python_ok(self.script)
33
34 def test_help(self):
35 rc, out, err = assert_python_ok(self.script, '-h')
36 self.assertEqual(out, b'')
37 self.assertGreater(err, b'')
38
39
Serhiy Storchaka8cd7f822013-01-11 11:59:59 +020040class PindentTests(unittest.TestCase):
41 script = os.path.join(scriptsdir, 'pindent.py')
42
43 def assertFileEqual(self, fn1, fn2):
44 with open(fn1) as f1, open(fn2) as f2:
45 self.assertEqual(f1.readlines(), f2.readlines())
46
47 def pindent(self, source, *args):
48 proc = subprocess.Popen(
49 (sys.executable, self.script) + args,
50 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
51 universal_newlines=True)
52 out, err = proc.communicate(source)
53 self.assertIsNone(err)
54 return out
55
56 def lstriplines(self, data):
57 return '\n'.join(line.lstrip() for line in data.splitlines()) + '\n'
58
59 def test_selftest(self):
60 with temp_dir() as directory:
61 data_path = os.path.join(directory, '_test.py')
62 with open(self.script) as f:
63 closed = f.read()
64 with open(data_path, 'w') as f:
65 f.write(closed)
66
67 rc, out, err = assert_python_ok(self.script, '-d', data_path)
68 self.assertEqual(out, b'')
69 self.assertEqual(err, b'')
70 backup = data_path + '~'
71 self.assertTrue(os.path.exists(backup))
72 with open(backup) as f:
73 self.assertEqual(f.read(), closed)
74 with open(data_path) as f:
75 clean = f.read()
76 compile(clean, '_test.py', 'exec')
77 self.assertEqual(self.pindent(clean, '-c'), closed)
78 self.assertEqual(self.pindent(closed, '-d'), clean)
79
80 rc, out, err = assert_python_ok(self.script, '-c', data_path)
81 self.assertEqual(out, b'')
82 self.assertEqual(err, b'')
83 with open(backup) as f:
84 self.assertEqual(f.read(), clean)
85 with open(data_path) as f:
86 self.assertEqual(f.read(), closed)
87
88 broken = self.lstriplines(closed)
89 with open(data_path, 'w') as f:
90 f.write(broken)
91 rc, out, err = assert_python_ok(self.script, '-r', data_path)
92 self.assertEqual(out, b'')
93 self.assertEqual(err, b'')
94 with open(backup) as f:
95 self.assertEqual(f.read(), broken)
96 with open(data_path) as f:
97 indented = f.read()
98 compile(indented, '_test.py', 'exec')
99 self.assertEqual(self.pindent(broken, '-r'), indented)
100
101 def pindent_test(self, clean, closed):
102 self.assertEqual(self.pindent(clean, '-c'), closed)
103 self.assertEqual(self.pindent(closed, '-d'), clean)
104 broken = self.lstriplines(closed)
105 self.assertEqual(self.pindent(broken, '-r', '-e', '-s', '4'), closed)
106
107 def test_statements(self):
108 clean = textwrap.dedent("""\
109 if a:
110 pass
111
112 if a:
113 pass
114 else:
115 pass
116
117 if a:
118 pass
119 elif:
120 pass
121 else:
122 pass
123
124 while a:
125 break
126
127 while a:
128 break
129 else:
130 pass
131
132 for i in a:
133 break
134
135 for i in a:
136 break
137 else:
138 pass
139
140 try:
141 pass
142 finally:
143 pass
144
145 try:
146 pass
147 except TypeError:
148 pass
149 except ValueError:
150 pass
151 else:
152 pass
153
154 try:
155 pass
156 except TypeError:
157 pass
158 except ValueError:
159 pass
160 finally:
161 pass
162
163 with a:
164 pass
165
166 class A:
167 pass
168
169 def f():
170 pass
171 """)
172
173 closed = textwrap.dedent("""\
174 if a:
175 pass
176 # end if
177
178 if a:
179 pass
180 else:
181 pass
182 # end if
183
184 if a:
185 pass
186 elif:
187 pass
188 else:
189 pass
190 # end if
191
192 while a:
193 break
194 # end while
195
196 while a:
197 break
198 else:
199 pass
200 # end while
201
202 for i in a:
203 break
204 # end for
205
206 for i in a:
207 break
208 else:
209 pass
210 # end for
211
212 try:
213 pass
214 finally:
215 pass
216 # end try
217
218 try:
219 pass
220 except TypeError:
221 pass
222 except ValueError:
223 pass
224 else:
225 pass
226 # end try
227
228 try:
229 pass
230 except TypeError:
231 pass
232 except ValueError:
233 pass
234 finally:
235 pass
236 # end try
237
238 with a:
239 pass
240 # end with
241
242 class A:
243 pass
244 # end class A
245
246 def f():
247 pass
248 # end def f
249 """)
250 self.pindent_test(clean, closed)
251
252 def test_multilevel(self):
253 clean = textwrap.dedent("""\
254 def foobar(a, b):
255 if a == b:
256 a = a+1
257 elif a < b:
258 b = b-1
259 if b > a: a = a-1
260 else:
261 print 'oops!'
262 """)
263 closed = textwrap.dedent("""\
264 def foobar(a, b):
265 if a == b:
266 a = a+1
267 elif a < b:
268 b = b-1
269 if b > a: a = a-1
270 # end if
271 else:
272 print 'oops!'
273 # end if
274 # end def foobar
275 """)
276 self.pindent_test(clean, closed)
277
278 def test_preserve_indents(self):
279 clean = textwrap.dedent("""\
280 if a:
281 if b:
282 pass
283 """)
284 closed = textwrap.dedent("""\
285 if a:
286 if b:
287 pass
288 # end if
289 # end if
290 """)
291 self.assertEqual(self.pindent(clean, '-c'), closed)
292 self.assertEqual(self.pindent(closed, '-d'), clean)
293 broken = self.lstriplines(closed)
294 self.assertEqual(self.pindent(broken, '-r', '-e', '-s', '9'), closed)
295 clean = textwrap.dedent("""\
296 if a:
297 \tif b:
298 \t\tpass
299 """)
300 closed = textwrap.dedent("""\
301 if a:
302 \tif b:
303 \t\tpass
304 \t# end if
305 # end if
306 """)
307 self.assertEqual(self.pindent(clean, '-c'), closed)
308 self.assertEqual(self.pindent(closed, '-d'), clean)
309 broken = self.lstriplines(closed)
310 self.assertEqual(self.pindent(broken, '-r'), closed)
311
312 def test_escaped_newline(self):
313 clean = textwrap.dedent("""\
314 class\\
315 \\
316 A:
317 def\
318 \\
319 f:
320 pass
321 """)
322 closed = textwrap.dedent("""\
323 class\\
324 \\
325 A:
326 def\
327 \\
328 f:
329 pass
330 # end def f
331 # end class A
332 """)
333 self.assertEqual(self.pindent(clean, '-c'), closed)
334 self.assertEqual(self.pindent(closed, '-d'), clean)
335
336 def test_empty_line(self):
337 clean = textwrap.dedent("""\
338 if a:
339
340 pass
341 """)
342 closed = textwrap.dedent("""\
343 if a:
344
345 pass
346 # end if
347 """)
348 self.pindent_test(clean, closed)
349
350 def test_oneline(self):
351 clean = textwrap.dedent("""\
352 if a: pass
353 """)
354 closed = textwrap.dedent("""\
355 if a: pass
356 # end if
357 """)
358 self.pindent_test(clean, closed)
359
360
Éric Araujoe84e2632012-02-25 16:24:59 +0100361def test_main():
Serhiy Storchaka8cd7f822013-01-11 11:59:59 +0200362 test_support.run_unittest(*[obj for obj in globals().values()
363 if isinstance(obj, type)])
Éric Araujoe84e2632012-02-25 16:24:59 +0100364
365
366if __name__ == '__main__':
367 unittest.main()