Skip to content

Commit

Permalink
Fixed flush after truncate
Browse files Browse the repository at this point in the history
- fixes #412
  • Loading branch information
mrbean-bremen committed Jun 7, 2018
1 parent 3cdbc6b commit c4755b0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -4549,7 +4549,6 @@ def truncate_wrapper(*args, **kwargs):
if self._append:
self._io.seek(self._read_seek, self._read_whence)
size = io_attr(*args, **kwargs)
self.flush()
if not self.is_stream:
self.file_object.SetSize(size)
buffer_size = len(self._io.getvalue())
Expand All @@ -4559,6 +4558,7 @@ def truncate_wrapper(*args, **kwargs):
self.file_object.SetContents(
self._io.getvalue(), self._encoding)
self._flush_pos = size
self.flush()
if not IS_PY2:
return size

Expand Down
11 changes: 11 additions & 0 deletions pyfakefs/tests/fake_open_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,17 @@ def test_update_other_instances_of_same_file_on_flush(self):
f1.flush()
self.assertEqual(4, self.os.path.getsize(file_path))

def test_getsize_after_truncate(self):
# Regression test for #412
file_path = self.make_path('foo')
with self.open(file_path, 'a') as f:
f.write('a')
f.seek(0)
f.truncate()
f.write('b')
f.truncate()
self.assertEqual(1, self.os.path.getsize(file_path))

def test_that_read_over_end_does_not_reset_position(self):
# Regression test for #286
file_path = self.make_path('baz')
Expand Down

0 comments on commit c4755b0

Please sign in to comment.