Skip to content

Commit a3bf03a

Browse files
committed
Added tests to check behavior inside a unittest
- regression test for exception happening if 'genericpath' is not added to SKIPNAMES - do not add 'genericpath' to SKIPNAMES if path is not patched
1 parent 409f86d commit a3bf03a

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

fake_filesystem_unittest_test.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def test_own_path_module(self):
190190

191191

192192
@unittest.skipIf(sys.version_info < (2, 7), "No byte strings in Python 2.6")
193-
class TestCopyRealFile(TestPyfakefsUnittestBase):
193+
class TestCopyOrAddRealFile(TestPyfakefsUnittestBase):
194194
"""Tests the `fake_filesystem_unittest.TestCase.copyRealFile()` method."""
195195
with open(__file__) as f:
196196
real_string_contents = f.read()
@@ -235,6 +235,28 @@ def testCopyRealFileNoDestination(self):
235235
self.copyRealFile(real_file_path)
236236
self.assertTrue(self.fs.Exists(real_file_path))
237237

238+
def testAddRealFile(self):
239+
'''Add a real file to the fake file system to be read on demand'''
240+
241+
# this tests only the basic functionality inside a unit test, more thorough tests
242+
# are done in fake_filesystem_test.RealFileSystemAccessTest
243+
real_file_path = __file__
244+
fake_file = self.fs.add_real_file(real_file_path)
245+
self.assertTrue(self.fs.Exists(real_file_path))
246+
self.assertIsNone(fake_file._byte_contents)
247+
self.assertEqual(self.real_byte_contents, fake_file.byte_contents)
248+
249+
def testAddRealDirectory(self):
250+
'''Add a real directory and the contained files to the fake file system to be read on demand'''
251+
252+
# this tests only the basic functionality inside a unit test, more thorough tests
253+
# are done in fake_filesystem_test.RealFileSystemAccessTest
254+
# Note: this test fails (add_real_directory raises) if 'genericpath' is not added to SKIPNAMES
255+
real_dir_path = os.path.join(os.path.dirname(__file__), 'pyfakefs')
256+
self.fs.add_real_directory(real_dir_path)
257+
self.assertTrue(self.fs.Exists(real_dir_path))
258+
self.assertTrue(self.fs.Exists(os.path.join(real_dir_path, 'fake_filesystem.py')))
259+
238260

239261
if __name__ == "__main__":
240262
unittest.main()

pyfakefs/fake_filesystem_unittest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ def __init__(self, additional_skip_names=None, patch_path=True):
239239
self._patchPath = patch_path
240240
if not patch_path:
241241
self._skipNames.discard('path')
242+
self._skipNames.discard('genericpath')
242243

243244
# Attributes set by _findModules()
244245
self._os_modules = None

0 commit comments

Comments
 (0)