diff options
author | Gregory P. Smith <greg@krypto.org> | 2012-02-12 23:50:21 (GMT) |
---|---|---|
committer | Gregory P. Smith <greg@krypto.org> | 2012-02-12 23:50:21 (GMT) |
commit | 58f23ffb21b0fadb8df91c7c9735401f50b3e844 (patch) | |
tree | ef26a40799350fb1d788a1e05212810a1f5a4c5f /Lib/lib2to3/tests | |
parent | 51d04d1ba8643d8a0c89d8307bf43697223f31e2 (diff) | |
download | cpython-58f23ffb21b0fadb8df91c7c9735401f50b3e844.zip cpython-58f23ffb21b0fadb8df91c7c9735401f50b3e844.tar.gz cpython-58f23ffb21b0fadb8df91c7c9735401f50b3e844.tar.bz2 |
Issue #13930: Adds ability for 2to3 to write its output to a different
directory tree instead of overwriting the input files. Adds three command
line options: -o/--output-dir, -W/--write-unchanged-files and --add-suffix.
Feature backports into stable release branches for 2to3 are allowed by
a special exemption:
http://mail.python.org/pipermail/python-dev/2011-December/115089.html
Diffstat (limited to 'Lib/lib2to3/tests')
-rw-r--r-- | Lib/lib2to3/tests/test_main.py | 98 | ||||
-rw-r--r-- | Lib/lib2to3/tests/test_refactor.py | 36 |
2 files changed, 130 insertions, 4 deletions
diff --git a/Lib/lib2to3/tests/test_main.py b/Lib/lib2to3/tests/test_main.py index 754d5dd..407afa1 100644 --- a/Lib/lib2to3/tests/test_main.py +++ b/Lib/lib2to3/tests/test_main.py @@ -1,18 +1,30 @@ # -*- coding: utf-8 -*- -import sys import codecs -import logging import io +import logging +import os +import shutil +import sys +import tempfile import unittest from lib2to3 import main +TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data") +PY2_TEST_MODULE = os.path.join(TEST_DATA_DIR, "py2_test_grammar.py") + + class TestMain(unittest.TestCase): + def setUp(self): + self.temp_dir = None # tearDown() will rmtree this directory if set. + def tearDown(self): # Clean up logging configuration down by main. del logging.root.handlers[:] + if self.temp_dir: + shutil.rmtree(self.temp_dir) def run_2to3_capture(self, args, in_capture, out_capture, err_capture): save_stdin = sys.stdin @@ -39,3 +51,85 @@ class TestMain(unittest.TestCase): self.assertTrue("-print 'nothing'" in output) self.assertTrue("WARNING: couldn't encode <stdin>'s diff for " "your terminal" in err.getvalue()) + + 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 = io.StringIO() + err = io.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], + io.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) + self.assertRegexpMatches(stderr, r"No changes to .*/__init__\.py") + self.assertNotRegex(stderr, r"No changes to .*/trivial\.py") + + 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 = io.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, + io.StringIO(""), io.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 = io.StringIO() + ret = self.run_2to3_capture( + ["-n", "-w", "--no-diffs", "--output-dir", self.py3_dest_dir, + self.trivial_py2_file], + io.StringIO(""), io.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() diff --git a/Lib/lib2to3/tests/test_refactor.py b/Lib/lib2to3/tests/test_refactor.py index 54edeb4..4b87ed6 100644 --- a/Lib/lib2to3/tests/test_refactor.py +++ b/Lib/lib2to3/tests/test_refactor.py @@ -53,6 +53,12 @@ class TestRefactoringTool(unittest.TestCase): self.assertTrue(rt.driver.grammar is pygram.python_grammar_no_print_statement) + def test_write_unchanged_files_option(self): + rt = self.rt() + self.assertFalse(rt.write_unchanged_files) + rt = self.rt({"write_unchanged_files" : True}) + self.assertTrue(rt.write_unchanged_files) + def test_fixer_loading_helpers(self): contents = ["explicit", "first", "last", "parrot", "preorder"] non_prefixed = refactor.get_all_fix_names("myfixes") @@ -176,7 +182,9 @@ from __future__ import print_function""" "<stdin>", False] self.assertEqual(results, expected) - def check_file_refactoring(self, test_file, fixers=_2TO3_FIXERS): + def check_file_refactoring(self, test_file, fixers=_2TO3_FIXERS, + options=None, mock_log_debug=None, + actually_write=True): tmpdir = tempfile.mkdtemp(prefix="2to3-test_refactor") self.addCleanup(shutil.rmtree, tmpdir) # make a copy of the tested file that we can write to @@ -189,11 +197,15 @@ from __future__ import print_function""" return fp.read() old_contents = read_file() - rt = self.rt(fixers=fixers) + rt = self.rt(fixers=fixers, options=options) + if mock_log_debug: + rt.log_debug = mock_log_debug rt.refactor_file(test_file) self.assertEqual(old_contents, read_file()) + if not actually_write: + return rt.refactor_file(test_file, True) new_contents = read_file() self.assertNotEqual(old_contents, new_contents) @@ -203,6 +215,26 @@ from __future__ import print_function""" test_file = os.path.join(FIXER_DIR, "parrot_example.py") self.check_file_refactoring(test_file, _DEFAULT_FIXERS) + def test_refactor_file_write_unchanged_file(self): + test_file = os.path.join(FIXER_DIR, "parrot_example.py") + debug_messages = [] + def recording_log_debug(msg, *args): + debug_messages.append(msg % args) + self.check_file_refactoring(test_file, fixers=(), + options={"write_unchanged_files": True}, + mock_log_debug=recording_log_debug, + actually_write=False) + # Testing that it logged this message when write=False was passed is + # sufficient to see that it did not bail early after "No changes". + message_regex = r"Not writing changes to .*%s%s" % ( + os.sep, os.path.basename(test_file)) + for message in debug_messages: + if "Not writing changes" in message: + self.assertRegexpMatches(message, message_regex) + break + else: + self.fail("%r not matched in %r" % (message_regex, debug_messages)) + def test_refactor_dir(self): def check(structure, expected): def mock_refactor_file(self, f, *args): |