summaryrefslogtreecommitdiff
path: root/Lib/lib2to3/tests/test_refactor.py
diff options
context:
space:
mode:
authorGregory P. Smith <greg@krypto.org>2012-02-12 15:50:21 -0800
committerGregory P. Smith <greg@krypto.org>2012-02-12 15:50:21 -0800
commit5e110d247f132e8c05a0d93dc53df65eff0e60d8 (patch)
tree130f164e65ada283f2970ed08b0b1352d81f2687 /Lib/lib2to3/tests/test_refactor.py
parentb48852c53e73d5c8dee20600933a74e9fb60cf7a (diff)
downloadcpython-5e110d247f132e8c05a0d93dc53df65eff0e60d8.tar.gz
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/test_refactor.py')
-rw-r--r--Lib/lib2to3/tests/test_refactor.py36
1 files changed, 34 insertions, 2 deletions
diff --git a/Lib/lib2to3/tests/test_refactor.py b/Lib/lib2to3/tests/test_refactor.py
index 54edeb413e..4b87ed6544 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):