bpo-11594: Ensure line-endings are respected when using 2to3 (GH-6483)
(cherry picked from commit c127a86e18
)
Co-authored-by: Aaron Ang <aaronang@users.noreply.github.com>
This commit is contained in:
parent
902bb62d5a
commit
3b3be1fe10
|
@ -314,7 +314,7 @@ class RefactoringTool(object):
|
||||||
encoding = tokenize.detect_encoding(f.readline)[0]
|
encoding = tokenize.detect_encoding(f.readline)[0]
|
||||||
finally:
|
finally:
|
||||||
f.close()
|
f.close()
|
||||||
with io.open(filename, "r", encoding=encoding) as f:
|
with io.open(filename, "r", encoding=encoding, newline='') as f:
|
||||||
return f.read(), encoding
|
return f.read(), encoding
|
||||||
|
|
||||||
def refactor_file(self, filename, write=False, doctests_only=False):
|
def refactor_file(self, filename, write=False, doctests_only=False):
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
print "hi"
|
print "hi"
|
||||||
|
|
||||||
print "Like bad Windows newlines?"
|
print "Like bad Windows newlines?"
|
||||||
|
|
|
@ -180,32 +180,42 @@ from __future__ import print_function"""
|
||||||
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,
|
options=None, mock_log_debug=None,
|
||||||
actually_write=True):
|
actually_write=True):
|
||||||
tmpdir = tempfile.mkdtemp(prefix="2to3-test_refactor")
|
test_file = self.init_test_file(test_file)
|
||||||
self.addCleanup(shutil.rmtree, tmpdir)
|
old_contents = self.read_file(test_file)
|
||||||
# make a copy of the tested file that we can write to
|
|
||||||
shutil.copy(test_file, tmpdir)
|
|
||||||
test_file = os.path.join(tmpdir, os.path.basename(test_file))
|
|
||||||
os.chmod(test_file, 0o644)
|
|
||||||
|
|
||||||
def read_file():
|
|
||||||
with open(test_file, "rb") as fp:
|
|
||||||
return fp.read()
|
|
||||||
|
|
||||||
old_contents = read_file()
|
|
||||||
rt = self.rt(fixers=fixers, options=options)
|
rt = self.rt(fixers=fixers, options=options)
|
||||||
if mock_log_debug:
|
if mock_log_debug:
|
||||||
rt.log_debug = mock_log_debug
|
rt.log_debug = mock_log_debug
|
||||||
|
|
||||||
rt.refactor_file(test_file)
|
rt.refactor_file(test_file)
|
||||||
self.assertEqual(old_contents, read_file())
|
self.assertEqual(old_contents, self.read_file(test_file))
|
||||||
|
|
||||||
if not actually_write:
|
if not actually_write:
|
||||||
return
|
return
|
||||||
rt.refactor_file(test_file, True)
|
rt.refactor_file(test_file, True)
|
||||||
new_contents = read_file()
|
new_contents = self.read_file(test_file)
|
||||||
self.assertNotEqual(old_contents, new_contents)
|
self.assertNotEqual(old_contents, new_contents)
|
||||||
return new_contents
|
return new_contents
|
||||||
|
|
||||||
|
def init_test_file(self, test_file):
|
||||||
|
tmpdir = tempfile.mkdtemp(prefix="2to3-test_refactor")
|
||||||
|
self.addCleanup(shutil.rmtree, tmpdir)
|
||||||
|
shutil.copy(test_file, tmpdir)
|
||||||
|
test_file = os.path.join(tmpdir, os.path.basename(test_file))
|
||||||
|
os.chmod(test_file, 0o644)
|
||||||
|
return test_file
|
||||||
|
|
||||||
|
def read_file(self, test_file):
|
||||||
|
with open(test_file, "rb") as fp:
|
||||||
|
return fp.read()
|
||||||
|
|
||||||
|
def refactor_file(self, test_file, fixers=_2TO3_FIXERS):
|
||||||
|
test_file = self.init_test_file(test_file)
|
||||||
|
old_contents = self.read_file(test_file)
|
||||||
|
rt = self.rt(fixers=fixers)
|
||||||
|
rt.refactor_file(test_file, True)
|
||||||
|
new_contents = self.read_file(test_file)
|
||||||
|
return old_contents, new_contents
|
||||||
|
|
||||||
def test_refactor_file(self):
|
def test_refactor_file(self):
|
||||||
test_file = os.path.join(FIXER_DIR, "parrot_example.py")
|
test_file = os.path.join(FIXER_DIR, "parrot_example.py")
|
||||||
self.check_file_refactoring(test_file, _DEFAULT_FIXERS)
|
self.check_file_refactoring(test_file, _DEFAULT_FIXERS)
|
||||||
|
@ -285,6 +295,12 @@ from __future__ import print_function"""
|
||||||
finally:
|
finally:
|
||||||
os.linesep = old_sep
|
os.linesep = old_sep
|
||||||
|
|
||||||
|
def test_crlf_unchanged(self):
|
||||||
|
fn = os.path.join(TEST_DATA_DIR, "crlf.py")
|
||||||
|
old, new = self.refactor_file(fn)
|
||||||
|
self.assertIn(b"\r\n", old)
|
||||||
|
self.assertIn(b"\r\n", new)
|
||||||
|
|
||||||
def test_refactor_docstring(self):
|
def test_refactor_docstring(self):
|
||||||
rt = self.rt()
|
rt = self.rt()
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Ensure line-endings are respected when using lib2to3.
|
Loading…
Reference in New Issue