test_main.py 5.6 KB

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