From 0548a369e8fb875d6235d99a90e21afbb55f109e Mon Sep 17 00:00:00 2001 From: mrbean-bremen Date: Fri, 27 Apr 2018 18:38:05 +0200 Subject: [PATCH] Added "closed" property to file wrapper - see #380 --- CHANGES.md | 1 + pyfakefs/fake_filesystem.py | 5 +++++ pyfakefs/tests/fake_open_test.py | 12 ++++++++++++ 3 files changed, 18 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 8c68102b..f37d56c9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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: diff --git a/pyfakefs/fake_filesystem.py b/pyfakefs/fake_filesystem.py index 9fd9e39b..f95273ee 100644 --- a/pyfakefs/fake_filesystem.py +++ b/pyfakefs/fake_filesystem.py @@ -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() diff --git a/pyfakefs/tests/fake_open_test.py b/pyfakefs/tests/fake_open_test.py index 8c61f4f4..42e86820 100644 --- a/pyfakefs/tests/fake_open_test.py +++ b/pyfakefs/tests/fake_open_test.py @@ -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') @@ -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):