Merged revisions 76871-76872,77093-77095,77097-77101 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r76871 | benjamin.peterson | 2009-12-17 20:49:21 -0600 (Thu, 17 Dec 2009) | 1 line
handle unencodable diffs gracefully #5093
........
r76872 | benjamin.peterson | 2009-12-17 20:51:37 -0600 (Thu, 17 Dec 2009) | 1 line
fix emacs header
........
r77093 | benjamin.peterson | 2009-12-28 14:43:32 -0600 (Mon, 28 Dec 2009) | 7 lines
replace callable(x) with isinstance(x, collections.Callable) #7006
This is a more accurate translation than hasattr(x, '__call__') which failed in
the case that somebody had put __call__ in the instance dictionary.
Patch mostly by Joe Amenta.
........
r77094 | benjamin.peterson | 2009-12-28 14:45:13 -0600 (Mon, 28 Dec 2009) | 2 lines
deuglify imports
........
r77095 | benjamin.peterson | 2009-12-28 14:49:23 -0600 (Mon, 28 Dec 2009) | 1 line
remove unused flag
........
r77097 | benjamin.peterson | 2009-12-28 16:12:13 -0600 (Mon, 28 Dec 2009) | 2 lines
clean up imports and whitespace
........
r77098 | benjamin.peterson | 2009-12-28 16:43:35 -0600 (Mon, 28 Dec 2009) | 1 line
*** empty log message ***
........
r77099 | benjamin.peterson | 2009-12-28 16:45:10 -0600 (Mon, 28 Dec 2009) | 1 line
revert unintended change
........
r77100 | benjamin.peterson | 2009-12-28 16:53:21 -0600 (Mon, 28 Dec 2009) | 1 line
revert unintended changes
........
r77101 | benjamin.peterson | 2009-12-28 17:46:02 -0600 (Mon, 28 Dec 2009) | 1 line
normalize whitespace
........
2009-12-28 19:50:41 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import sys
|
|
|
|
import codecs
|
2009-12-30 15:44:23 -04:00
|
|
|
import logging
|
2012-02-12 19:51:21 -04:00
|
|
|
import os
|
2012-02-13 20:45:47 -04:00
|
|
|
import re
|
2012-02-12 19:51:21 -04:00
|
|
|
import shutil
|
Merged revisions 76871-76872,77093-77095,77097-77101 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r76871 | benjamin.peterson | 2009-12-17 20:49:21 -0600 (Thu, 17 Dec 2009) | 1 line
handle unencodable diffs gracefully #5093
........
r76872 | benjamin.peterson | 2009-12-17 20:51:37 -0600 (Thu, 17 Dec 2009) | 1 line
fix emacs header
........
r77093 | benjamin.peterson | 2009-12-28 14:43:32 -0600 (Mon, 28 Dec 2009) | 7 lines
replace callable(x) with isinstance(x, collections.Callable) #7006
This is a more accurate translation than hasattr(x, '__call__') which failed in
the case that somebody had put __call__ in the instance dictionary.
Patch mostly by Joe Amenta.
........
r77094 | benjamin.peterson | 2009-12-28 14:45:13 -0600 (Mon, 28 Dec 2009) | 2 lines
deuglify imports
........
r77095 | benjamin.peterson | 2009-12-28 14:49:23 -0600 (Mon, 28 Dec 2009) | 1 line
remove unused flag
........
r77097 | benjamin.peterson | 2009-12-28 16:12:13 -0600 (Mon, 28 Dec 2009) | 2 lines
clean up imports and whitespace
........
r77098 | benjamin.peterson | 2009-12-28 16:43:35 -0600 (Mon, 28 Dec 2009) | 1 line
*** empty log message ***
........
r77099 | benjamin.peterson | 2009-12-28 16:45:10 -0600 (Mon, 28 Dec 2009) | 1 line
revert unintended change
........
r77100 | benjamin.peterson | 2009-12-28 16:53:21 -0600 (Mon, 28 Dec 2009) | 1 line
revert unintended changes
........
r77101 | benjamin.peterson | 2009-12-28 17:46:02 -0600 (Mon, 28 Dec 2009) | 1 line
normalize whitespace
........
2009-12-28 19:50:41 -04:00
|
|
|
import StringIO
|
2012-02-12 19:51:21 -04:00
|
|
|
import sys
|
|
|
|
import tempfile
|
Merged revisions 76871-76872,77093-77095,77097-77101 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r76871 | benjamin.peterson | 2009-12-17 20:49:21 -0600 (Thu, 17 Dec 2009) | 1 line
handle unencodable diffs gracefully #5093
........
r76872 | benjamin.peterson | 2009-12-17 20:51:37 -0600 (Thu, 17 Dec 2009) | 1 line
fix emacs header
........
r77093 | benjamin.peterson | 2009-12-28 14:43:32 -0600 (Mon, 28 Dec 2009) | 7 lines
replace callable(x) with isinstance(x, collections.Callable) #7006
This is a more accurate translation than hasattr(x, '__call__') which failed in
the case that somebody had put __call__ in the instance dictionary.
Patch mostly by Joe Amenta.
........
r77094 | benjamin.peterson | 2009-12-28 14:45:13 -0600 (Mon, 28 Dec 2009) | 2 lines
deuglify imports
........
r77095 | benjamin.peterson | 2009-12-28 14:49:23 -0600 (Mon, 28 Dec 2009) | 1 line
remove unused flag
........
r77097 | benjamin.peterson | 2009-12-28 16:12:13 -0600 (Mon, 28 Dec 2009) | 2 lines
clean up imports and whitespace
........
r77098 | benjamin.peterson | 2009-12-28 16:43:35 -0600 (Mon, 28 Dec 2009) | 1 line
*** empty log message ***
........
r77099 | benjamin.peterson | 2009-12-28 16:45:10 -0600 (Mon, 28 Dec 2009) | 1 line
revert unintended change
........
r77100 | benjamin.peterson | 2009-12-28 16:53:21 -0600 (Mon, 28 Dec 2009) | 1 line
revert unintended changes
........
r77101 | benjamin.peterson | 2009-12-28 17:46:02 -0600 (Mon, 28 Dec 2009) | 1 line
normalize whitespace
........
2009-12-28 19:50:41 -04:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from lib2to3 import main
|
|
|
|
|
|
|
|
|
2012-02-12 19:51:21 -04:00
|
|
|
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
|
|
|
|
PY2_TEST_MODULE = os.path.join(TEST_DATA_DIR, "py2_test_grammar.py")
|
|
|
|
|
|
|
|
|
Merged revisions 76871-76872,77093-77095,77097-77101 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r76871 | benjamin.peterson | 2009-12-17 20:49:21 -0600 (Thu, 17 Dec 2009) | 1 line
handle unencodable diffs gracefully #5093
........
r76872 | benjamin.peterson | 2009-12-17 20:51:37 -0600 (Thu, 17 Dec 2009) | 1 line
fix emacs header
........
r77093 | benjamin.peterson | 2009-12-28 14:43:32 -0600 (Mon, 28 Dec 2009) | 7 lines
replace callable(x) with isinstance(x, collections.Callable) #7006
This is a more accurate translation than hasattr(x, '__call__') which failed in
the case that somebody had put __call__ in the instance dictionary.
Patch mostly by Joe Amenta.
........
r77094 | benjamin.peterson | 2009-12-28 14:45:13 -0600 (Mon, 28 Dec 2009) | 2 lines
deuglify imports
........
r77095 | benjamin.peterson | 2009-12-28 14:49:23 -0600 (Mon, 28 Dec 2009) | 1 line
remove unused flag
........
r77097 | benjamin.peterson | 2009-12-28 16:12:13 -0600 (Mon, 28 Dec 2009) | 2 lines
clean up imports and whitespace
........
r77098 | benjamin.peterson | 2009-12-28 16:43:35 -0600 (Mon, 28 Dec 2009) | 1 line
*** empty log message ***
........
r77099 | benjamin.peterson | 2009-12-28 16:45:10 -0600 (Mon, 28 Dec 2009) | 1 line
revert unintended change
........
r77100 | benjamin.peterson | 2009-12-28 16:53:21 -0600 (Mon, 28 Dec 2009) | 1 line
revert unintended changes
........
r77101 | benjamin.peterson | 2009-12-28 17:46:02 -0600 (Mon, 28 Dec 2009) | 1 line
normalize whitespace
........
2009-12-28 19:50:41 -04:00
|
|
|
class TestMain(unittest.TestCase):
|
|
|
|
|
2012-02-12 19:51:21 -04:00
|
|
|
if not hasattr(unittest.TestCase, 'assertNotRegex'):
|
|
|
|
# This method was only introduced in 3.2.
|
|
|
|
def assertNotRegex(self, text, regexp, msg=None):
|
|
|
|
import re
|
|
|
|
if not hasattr(regexp, 'search'):
|
|
|
|
regexp = re.compile(regexp)
|
|
|
|
if regexp.search(text):
|
|
|
|
self.fail("regexp %s MATCHED text %r" % (regexp.pattern, text))
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.temp_dir = None # tearDown() will rmtree this directory if set.
|
|
|
|
|
2009-12-30 15:44:23 -04:00
|
|
|
def tearDown(self):
|
|
|
|
# Clean up logging configuration down by main.
|
|
|
|
del logging.root.handlers[:]
|
2012-02-12 19:51:21 -04:00
|
|
|
if self.temp_dir:
|
|
|
|
shutil.rmtree(self.temp_dir)
|
2009-12-30 15:44:23 -04:00
|
|
|
|
Merged revisions 76871-76872,77093-77095,77097-77101 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r76871 | benjamin.peterson | 2009-12-17 20:49:21 -0600 (Thu, 17 Dec 2009) | 1 line
handle unencodable diffs gracefully #5093
........
r76872 | benjamin.peterson | 2009-12-17 20:51:37 -0600 (Thu, 17 Dec 2009) | 1 line
fix emacs header
........
r77093 | benjamin.peterson | 2009-12-28 14:43:32 -0600 (Mon, 28 Dec 2009) | 7 lines
replace callable(x) with isinstance(x, collections.Callable) #7006
This is a more accurate translation than hasattr(x, '__call__') which failed in
the case that somebody had put __call__ in the instance dictionary.
Patch mostly by Joe Amenta.
........
r77094 | benjamin.peterson | 2009-12-28 14:45:13 -0600 (Mon, 28 Dec 2009) | 2 lines
deuglify imports
........
r77095 | benjamin.peterson | 2009-12-28 14:49:23 -0600 (Mon, 28 Dec 2009) | 1 line
remove unused flag
........
r77097 | benjamin.peterson | 2009-12-28 16:12:13 -0600 (Mon, 28 Dec 2009) | 2 lines
clean up imports and whitespace
........
r77098 | benjamin.peterson | 2009-12-28 16:43:35 -0600 (Mon, 28 Dec 2009) | 1 line
*** empty log message ***
........
r77099 | benjamin.peterson | 2009-12-28 16:45:10 -0600 (Mon, 28 Dec 2009) | 1 line
revert unintended change
........
r77100 | benjamin.peterson | 2009-12-28 16:53:21 -0600 (Mon, 28 Dec 2009) | 1 line
revert unintended changes
........
r77101 | benjamin.peterson | 2009-12-28 17:46:02 -0600 (Mon, 28 Dec 2009) | 1 line
normalize whitespace
........
2009-12-28 19:50:41 -04:00
|
|
|
def run_2to3_capture(self, args, in_capture, out_capture, err_capture):
|
|
|
|
save_stdin = sys.stdin
|
|
|
|
save_stdout = sys.stdout
|
|
|
|
save_stderr = sys.stderr
|
|
|
|
sys.stdin = in_capture
|
|
|
|
sys.stdout = out_capture
|
|
|
|
sys.stderr = err_capture
|
|
|
|
try:
|
|
|
|
return main.main("lib2to3.fixes", args)
|
|
|
|
finally:
|
|
|
|
sys.stdin = save_stdin
|
|
|
|
sys.stdout = save_stdout
|
|
|
|
sys.stderr = save_stderr
|
|
|
|
|
|
|
|
def test_unencodable_diff(self):
|
|
|
|
input_stream = StringIO.StringIO(u"print 'nothing'\nprint u'über'\n")
|
|
|
|
out = StringIO.StringIO()
|
|
|
|
out_enc = codecs.getwriter("ascii")(out)
|
|
|
|
err = StringIO.StringIO()
|
|
|
|
ret = self.run_2to3_capture(["-"], input_stream, out_enc, err)
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
output = out.getvalue()
|
|
|
|
self.assertTrue("-print 'nothing'" in output)
|
|
|
|
self.assertTrue("WARNING: couldn't encode <stdin>'s diff for "
|
|
|
|
"your terminal" in err.getvalue())
|
2012-02-12 19:51:21 -04:00
|
|
|
|
|
|
|
def setup_test_source_trees(self):
|
|
|
|
"""Setup a test source tree and output destination tree."""
|
|
|
|
self.temp_dir = tempfile.mkdtemp() # tearDown() cleans this up.
|
|
|
|
self.py2_src_dir = os.path.join(self.temp_dir, "python2_project")
|
|
|
|
self.py3_dest_dir = os.path.join(self.temp_dir, "python3_project")
|
|
|
|
os.mkdir(self.py2_src_dir)
|
|
|
|
os.mkdir(self.py3_dest_dir)
|
|
|
|
# Turn it into a package with a few files.
|
|
|
|
self.setup_files = []
|
|
|
|
open(os.path.join(self.py2_src_dir, "__init__.py"), "w").close()
|
|
|
|
self.setup_files.append("__init__.py")
|
|
|
|
shutil.copy(PY2_TEST_MODULE, self.py2_src_dir)
|
|
|
|
self.setup_files.append(os.path.basename(PY2_TEST_MODULE))
|
|
|
|
self.trivial_py2_file = os.path.join(self.py2_src_dir, "trivial.py")
|
|
|
|
self.init_py2_file = os.path.join(self.py2_src_dir, "__init__.py")
|
|
|
|
with open(self.trivial_py2_file, "w") as trivial:
|
|
|
|
trivial.write("print 'I need a simple conversion.'")
|
|
|
|
self.setup_files.append("trivial.py")
|
|
|
|
|
|
|
|
def test_filename_changing_on_output_single_dir(self):
|
|
|
|
"""2to3 a single directory with a new output dir and suffix."""
|
|
|
|
self.setup_test_source_trees()
|
|
|
|
out = StringIO.StringIO()
|
|
|
|
err = StringIO.StringIO()
|
|
|
|
suffix = "TEST"
|
|
|
|
ret = self.run_2to3_capture(
|
|
|
|
["-n", "--add-suffix", suffix, "--write-unchanged-files",
|
|
|
|
"--no-diffs", "--output-dir",
|
|
|
|
self.py3_dest_dir, self.py2_src_dir],
|
|
|
|
StringIO.StringIO(""), out, err)
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
stderr = err.getvalue()
|
|
|
|
self.assertIn(" implies -w.", stderr)
|
|
|
|
self.assertIn(
|
|
|
|
"Output in %r will mirror the input directory %r layout" % (
|
|
|
|
self.py3_dest_dir, self.py2_src_dir), stderr)
|
|
|
|
self.assertEqual(set(name+suffix for name in self.setup_files),
|
|
|
|
set(os.listdir(self.py3_dest_dir)))
|
|
|
|
for name in self.setup_files:
|
|
|
|
self.assertIn("Writing converted %s to %s" % (
|
|
|
|
os.path.join(self.py2_src_dir, name),
|
|
|
|
os.path.join(self.py3_dest_dir, name+suffix)), stderr)
|
2012-02-13 20:45:47 -04:00
|
|
|
sep = re.escape(os.sep)
|
2012-02-13 20:28:54 -04:00
|
|
|
self.assertRegexpMatches(
|
2012-02-13 20:45:47 -04:00
|
|
|
stderr, r"No changes to .*/__init__\.py".replace("/", sep))
|
2012-02-13 20:28:54 -04:00
|
|
|
self.assertNotRegex(
|
2012-02-13 20:45:47 -04:00
|
|
|
stderr, r"No changes to .*/trivial\.py".replace("/", sep))
|
2012-02-12 19:51:21 -04:00
|
|
|
|
|
|
|
def test_filename_changing_on_output_two_files(self):
|
|
|
|
"""2to3 two files in one directory with a new output dir."""
|
|
|
|
self.setup_test_source_trees()
|
|
|
|
err = StringIO.StringIO()
|
|
|
|
py2_files = [self.trivial_py2_file, self.init_py2_file]
|
|
|
|
expected_files = set(os.path.basename(name) for name in py2_files)
|
|
|
|
ret = self.run_2to3_capture(
|
|
|
|
["-n", "-w", "--write-unchanged-files",
|
|
|
|
"--no-diffs", "--output-dir", self.py3_dest_dir] + py2_files,
|
|
|
|
StringIO.StringIO(""), StringIO.StringIO(), err)
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
stderr = err.getvalue()
|
|
|
|
self.assertIn(
|
|
|
|
"Output in %r will mirror the input directory %r layout" % (
|
|
|
|
self.py3_dest_dir, self.py2_src_dir), stderr)
|
|
|
|
self.assertEqual(expected_files, set(os.listdir(self.py3_dest_dir)))
|
|
|
|
|
|
|
|
def test_filename_changing_on_output_single_file(self):
|
|
|
|
"""2to3 a single file with a new output dir."""
|
|
|
|
self.setup_test_source_trees()
|
|
|
|
err = StringIO.StringIO()
|
|
|
|
ret = self.run_2to3_capture(
|
|
|
|
["-n", "-w", "--no-diffs", "--output-dir", self.py3_dest_dir,
|
|
|
|
self.trivial_py2_file],
|
|
|
|
StringIO.StringIO(""), StringIO.StringIO(), err)
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
stderr = err.getvalue()
|
|
|
|
self.assertIn(
|
|
|
|
"Output in %r will mirror the input directory %r layout" % (
|
|
|
|
self.py3_dest_dir, self.py2_src_dir), stderr)
|
|
|
|
self.assertEqual(set([os.path.basename(self.trivial_py2_file)]),
|
|
|
|
set(os.listdir(self.py3_dest_dir)))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|