test_tools.py 9.3 KB

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