Skip to content

Commit

Permalink
Added "closed" property to file wrapper
Browse files Browse the repository at this point in the history
- see #380
  • Loading branch information
mrbean-bremen committed Apr 27, 2018
1 parent 2730677 commit 0548a36
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The release versions are PyPi releases.
* use README.md in pypi ([#358](../../issues/358))

#### Fixes
* `closed` attribute was not implemented in fake file ([#380](../../issues/380))
* `add_real_directory` did not behave correctly for nested paths
* several fixes for the behavior if using file paths that end with a path
separator:
Expand Down
5 changes: 5 additions & 0 deletions pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -4251,6 +4251,11 @@ def close(self):
if self.delete_on_close:
self._filesystem.remove_object(self.get_object().path)

@property
def closed(self):
"""Simulate the `closed` attribute on file."""
return not self._is_open()

def flush(self):
"""Flush file contents to 'disk'."""
self._check_open_file()
Expand Down
12 changes: 12 additions & 0 deletions pyfakefs/tests/fake_open_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,17 @@ def test_seek_outside_and_truncate_sets_size_in_append_mode(self):
# Regression test for #295
self.check_seek_outside_and_truncate_sets_size('a')

def test_closed(self):
file_path = self.make_path('foo')
f = self.open(file_path, 'w')
self.assertFalse(f.closed)
f.close()
self.assertTrue(f.closed)
f = self.open(file_path)
self.assertFalse(f.closed)
f.close()
self.assertTrue(f.closed)

def test_closing_closed_file_does_nothing(self):
# Regression test for #299
file_path = self.make_path('baz')
Expand All @@ -902,6 +913,7 @@ def test_closing_closed_file_does_nothing(self):
f0.close()
self.assertEqual('', f1.read())


@unittest.skipIf(TestCase.is_python2,
'closefd argument not available in Python2')
def test_closing_file_with_different_close_mode(self):
Expand Down

0 comments on commit 0548a36

Please sign in to comment.