diff options
Diffstat (limited to 'Lib/lib2to3/tests/test_refactor.py')
-rw-r--r-- | Lib/lib2to3/tests/test_refactor.py | 36 |
1 files changed, 34 insertions, 2 deletions
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): |