PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib-python/2/lib2to3/tests/test_main.py

https://bitbucket.org/kcr/pypy
Python | 149 lines | 131 code | 12 blank | 6 comment | 7 complexity | 066cafec39ed69eb8659745b6c5d0e86 MD5 | raw file
Possible License(s): Apache-2.0
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import codecs
  4. import logging
  5. import os
  6. import re
  7. import shutil
  8. import StringIO
  9. import sys
  10. import tempfile
  11. import unittest
  12. from lib2to3 import main
  13. TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
  14. PY2_TEST_MODULE = os.path.join(TEST_DATA_DIR, "py2_test_grammar.py")
  15. class TestMain(unittest.TestCase):
  16. if not hasattr(unittest.TestCase, 'assertNotRegex'):
  17. # This method was only introduced in 3.2.
  18. def assertNotRegex(self, text, regexp, msg=None):
  19. import re
  20. if not hasattr(regexp, 'search'):
  21. regexp = re.compile(regexp)
  22. if regexp.search(text):
  23. self.fail("regexp %s MATCHED text %r" % (regexp.pattern, text))
  24. def setUp(self):
  25. self.temp_dir = None # tearDown() will rmtree this directory if set.
  26. def tearDown(self):
  27. # Clean up logging configuration down by main.
  28. del logging.root.handlers[:]
  29. if self.temp_dir:
  30. shutil.rmtree(self.temp_dir)
  31. def run_2to3_capture(self, args, in_capture, out_capture, err_capture):
  32. save_stdin = sys.stdin
  33. save_stdout = sys.stdout
  34. save_stderr = sys.stderr
  35. sys.stdin = in_capture
  36. sys.stdout = out_capture
  37. sys.stderr = err_capture
  38. try:
  39. return main.main("lib2to3.fixes", args)
  40. finally:
  41. sys.stdin = save_stdin
  42. sys.stdout = save_stdout
  43. sys.stderr = save_stderr
  44. def test_unencodable_diff(self):
  45. input_stream = StringIO.StringIO(u"print 'nothing'\nprint u'Ăźber'\n")
  46. out = StringIO.StringIO()
  47. out_enc = codecs.getwriter("ascii")(out)
  48. err = StringIO.StringIO()
  49. ret = self.run_2to3_capture(["-"], input_stream, out_enc, err)
  50. self.assertEqual(ret, 0)
  51. output = out.getvalue()
  52. self.assertTrue("-print 'nothing'" in output)
  53. self.assertTrue("WARNING: couldn't encode <stdin>'s diff for "
  54. "your terminal" in err.getvalue())
  55. def setup_test_source_trees(self):
  56. """Setup a test source tree and output destination tree."""
  57. self.temp_dir = tempfile.mkdtemp() # tearDown() cleans this up.
  58. self.py2_src_dir = os.path.join(self.temp_dir, "python2_project")
  59. self.py3_dest_dir = os.path.join(self.temp_dir, "python3_project")
  60. os.mkdir(self.py2_src_dir)
  61. os.mkdir(self.py3_dest_dir)
  62. # Turn it into a package with a few files.
  63. self.setup_files = []
  64. open(os.path.join(self.py2_src_dir, "__init__.py"), "w").close()
  65. self.setup_files.append("__init__.py")
  66. shutil.copy(PY2_TEST_MODULE, self.py2_src_dir)
  67. self.setup_files.append(os.path.basename(PY2_TEST_MODULE))
  68. self.trivial_py2_file = os.path.join(self.py2_src_dir, "trivial.py")
  69. self.init_py2_file = os.path.join(self.py2_src_dir, "__init__.py")
  70. with open(self.trivial_py2_file, "w") as trivial:
  71. trivial.write("print 'I need a simple conversion.'")
  72. self.setup_files.append("trivial.py")
  73. def test_filename_changing_on_output_single_dir(self):
  74. """2to3 a single directory with a new output dir and suffix."""
  75. self.setup_test_source_trees()
  76. out = StringIO.StringIO()
  77. err = StringIO.StringIO()
  78. suffix = "TEST"
  79. ret = self.run_2to3_capture(
  80. ["-n", "--add-suffix", suffix, "--write-unchanged-files",
  81. "--no-diffs", "--output-dir",
  82. self.py3_dest_dir, self.py2_src_dir],
  83. StringIO.StringIO(""), out, err)
  84. self.assertEqual(ret, 0)
  85. stderr = err.getvalue()
  86. self.assertIn(" implies -w.", stderr)
  87. self.assertIn(
  88. "Output in %r will mirror the input directory %r layout" % (
  89. self.py3_dest_dir, self.py2_src_dir), stderr)
  90. self.assertEqual(set(name+suffix for name in self.setup_files),
  91. set(os.listdir(self.py3_dest_dir)))
  92. for name in self.setup_files:
  93. self.assertIn("Writing converted %s to %s" % (
  94. os.path.join(self.py2_src_dir, name),
  95. os.path.join(self.py3_dest_dir, name+suffix)), stderr)
  96. sep = re.escape(os.sep)
  97. self.assertRegexpMatches(
  98. stderr, r"No changes to .*/__init__\.py".replace("/", sep))
  99. self.assertNotRegex(
  100. stderr, r"No changes to .*/trivial\.py".replace("/", sep))
  101. def test_filename_changing_on_output_two_files(self):
  102. """2to3 two files in one directory with a new output dir."""
  103. self.setup_test_source_trees()
  104. err = StringIO.StringIO()
  105. py2_files = [self.trivial_py2_file, self.init_py2_file]
  106. expected_files = set(os.path.basename(name) for name in py2_files)
  107. ret = self.run_2to3_capture(
  108. ["-n", "-w", "--write-unchanged-files",
  109. "--no-diffs", "--output-dir", self.py3_dest_dir] + py2_files,
  110. StringIO.StringIO(""), StringIO.StringIO(), err)
  111. self.assertEqual(ret, 0)
  112. stderr = err.getvalue()
  113. self.assertIn(
  114. "Output in %r will mirror the input directory %r layout" % (
  115. self.py3_dest_dir, self.py2_src_dir), stderr)
  116. self.assertEqual(expected_files, set(os.listdir(self.py3_dest_dir)))
  117. def test_filename_changing_on_output_single_file(self):
  118. """2to3 a single file with a new output dir."""
  119. self.setup_test_source_trees()
  120. err = StringIO.StringIO()
  121. ret = self.run_2to3_capture(
  122. ["-n", "-w", "--no-diffs", "--output-dir", self.py3_dest_dir,
  123. self.trivial_py2_file],
  124. StringIO.StringIO(""), StringIO.StringIO(), err)
  125. self.assertEqual(ret, 0)
  126. stderr = err.getvalue()
  127. self.assertIn(
  128. "Output in %r will mirror the input directory %r layout" % (
  129. self.py3_dest_dir, self.py2_src_dir), stderr)
  130. self.assertEqual(set([os.path.basename(self.trivial_py2_file)]),
  131. set(os.listdir(self.py3_dest_dir)))
  132. if __name__ == '__main__':
  133. unittest.main()