Make FileInfo available to both read/write of files
This commit is contained in:
parent
cdb8ac8c1f
commit
eee0b0fc51
|
@ -530,7 +530,7 @@ class FileInfo(object):
|
|||
return FileInfo.DEFAULT_LINESEP
|
||||
|
||||
|
||||
def _ReadFile(filename):
|
||||
def _ReadFile(filename, fileinfo):
|
||||
"""Read from filename and return a list of file lines."""
|
||||
try:
|
||||
return open(filename).read().splitlines()
|
||||
|
@ -539,12 +539,9 @@ def _ReadFile(filename):
|
|||
return None
|
||||
|
||||
|
||||
def _WriteFile(filename, file_lines):
|
||||
def _WriteFile(filename, fileinfo, file_lines):
|
||||
"""Write the given file-lines to the file."""
|
||||
try:
|
||||
# Detect encoding details
|
||||
fileinfo = FileInfo.parse(filename)
|
||||
|
||||
# Open file in binary mode to preserve line endings
|
||||
with open(filename, 'wb') as f:
|
||||
f.write(fileinfo.linesep.join(file_lines))
|
||||
|
@ -2006,7 +2003,9 @@ def FixManyFiles(iwyu_records, flags):
|
|||
files_fixed = 0
|
||||
for iwyu_record in iwyu_records:
|
||||
try:
|
||||
file_contents = _ReadFile(iwyu_record.filename)
|
||||
fileinfo = FileInfo.parse(iwyu_record.filename)
|
||||
|
||||
file_contents = _ReadFile(iwyu_record.filename, fileinfo)
|
||||
if not file_contents:
|
||||
continue
|
||||
|
||||
|
@ -2019,7 +2018,7 @@ def FixManyFiles(iwyu_records, flags):
|
|||
if flags.dry_run:
|
||||
PrintFileDiff(old_lines, fixed_lines)
|
||||
else:
|
||||
_WriteFile(iwyu_record.filename, fixed_lines)
|
||||
_WriteFile(iwyu_record.filename, fileinfo, fixed_lines)
|
||||
|
||||
files_fixed += 1
|
||||
except FixIncludesError as why:
|
||||
|
|
|
@ -46,12 +46,15 @@ class FakeFlags(object):
|
|||
class FixIncludesBase(unittest.TestCase):
|
||||
"""Does setup that every test will want."""
|
||||
|
||||
def _ReadFile(self, filename):
|
||||
def _ReadFile(self, filename, fileinfo):
|
||||
assert filename in self.before_map, filename
|
||||
return self.before_map[filename]
|
||||
|
||||
def _WriteFile(self, filename, file_lines):
|
||||
self.actual_after_contents.extend(file_lines)
|
||||
def _ParseFileInfo(self, filename):
|
||||
return fix_includes.FileInfo('\n')
|
||||
|
||||
def _WriteFile(self, filename, fileinfo, contents):
|
||||
return self.actual_after_contents.extend(contents)
|
||||
|
||||
def setUp(self):
|
||||
self.flags = FakeFlags()
|
||||
|
@ -63,7 +66,11 @@ class FixIncludesBase(unittest.TestCase):
|
|||
|
||||
# INPUT: fix_includes._ReadFile takes a filename
|
||||
# and returns the contents of filename (as a list).
|
||||
# FileInfo controls encoding details of the file,
|
||||
# wire it to return something that agrees with the
|
||||
# tests.
|
||||
fix_includes._ReadFile = self._ReadFile
|
||||
fix_includes.FileInfo.parse = self._ParseFileInfo
|
||||
|
||||
# OUTPUT: Instead of writing to file, save full output.
|
||||
self.actual_after_contents = []
|
||||
|
|
Loading…
Reference in New Issue