Skip to content

Commit

Permalink
Added methods to access real files
Browse files Browse the repository at this point in the history
- allow to add really existing files and directory trees to the fake
file system, with the contents read on demand
- see pytest-dev#170
  • Loading branch information
mrbean-bremen committed Apr 1, 2017
1 parent b7fc0b4 commit d78cb99
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ The release versions are PyPi releases.
## Version 3.2 (as yet unreleased)

#### New Features
* Added possibility to add lazily read real files to fake filesystem
* Added the CHANGES.md release notes to the release manifest

#### Fixes
Expand Down
116 changes: 115 additions & 1 deletion fake_filesystem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4186,6 +4186,7 @@ def testDiskUsageOnFileCreation(self):

total_size = 100
self.filesystem.AddMountPoint('mount', total_size)

def create_too_large_file():
with fake_open('!mount!file', 'w') as dest:
dest.write('a' * (total_size + 1))
Expand All @@ -4199,7 +4200,6 @@ def create_too_large_file():

self.assertEqual(total_size, self.filesystem.GetDiskUsage('!mount').used)


def testFileSystemSizeAfterLargeFileCreation(self):
filesystem = fake_filesystem.FakeFilesystem(path_separator='!',
total_size=1024 * 1024 * 1024 * 100)
Expand Down Expand Up @@ -4444,5 +4444,119 @@ def testThatUncPathsAreAutoMounted(self):
self.assertEqual(5, self.filesystem.GetObject('!!foo!bar!bip!bop').st_dev)


class RealFileSystemAccessTest(TestCase):
def setUp(self):
# use the real path separator to work with the real file system
self.filesystem = fake_filesystem.FakeFilesystem()
self.fake_open = fake_filesystem.FakeFileOpen(self.filesystem)

def testAddNonExistingRealFileRaises(self):
nonexisting_path = os.path.join('nonexisting', 'test.txt')
self.assertRaises(OSError, self.filesystem.AddRealFile, nonexisting_path)
self.assertFalse(self.filesystem.Exists(nonexisting_path))

def testAddNonExistingRealDirectoryRaises(self):
nonexisting_path = '/nonexisting'
self.assertRaisesIOError(errno.ENOENT, self.filesystem.AddRealDirectory, nonexisting_path)
self.assertFalse(self.filesystem.Exists(nonexisting_path))

def checkFakeFileStat(self, fake_file, real_file_path):
self.assertTrue(self.filesystem.Exists(real_file_path))
real_stat = os.stat(real_file_path)
self.assertIsNone(fake_file._byte_contents)
self.assertEqual(fake_file.st_size, real_stat.st_size)
self.assertEqual(fake_file.st_ctime, real_stat.st_ctime)
self.assertEqual(fake_file.st_atime, real_stat.st_atime)
self.assertEqual(fake_file.st_mtime, real_stat.st_mtime)
self.assertEqual(fake_file.st_uid, real_stat.st_uid)
self.assertEqual(fake_file.st_gid, real_stat.st_gid)

def checkReadOnlyFile(self, fake_file, real_file_path):
with open(real_file_path, 'rb') as f:
real_contents = f.read()
self.assertEqual(fake_file.byte_contents, real_contents)
self.assertRaisesIOError(errno.EACCES, self.fake_open, real_file_path, 'w')

def checkWritableFile(self, fake_file, real_file_path):
with open(real_file_path, 'rb') as f:
real_contents = f.read()
self.assertEqual(fake_file.byte_contents, real_contents)
with self.fake_open(real_file_path, 'wb') as f:
f.write(b'test')
with open(real_file_path, 'rb') as f:
real_contents1 = f.read()
self.assertEqual(real_contents1, real_contents)
with self.fake_open(real_file_path, 'rb') as f:
fake_contents = f.read()
self.assertEqual(fake_contents, b'test')

def testAddExistingRealFileReadOnly(self):
real_file_path = __file__
fake_file = self.filesystem.AddRealFile(real_file_path)
self.checkFakeFileStat(fake_file, real_file_path)
self.assertEqual(fake_file.st_mode & 0o333, 0)
self.checkReadOnlyFile(fake_file, real_file_path)

def testAddExistingRealFileReadWrite(self):
real_file_path = os.path.realpath(__file__)
fake_file = self.filesystem.AddRealFile(real_file_path, read_only=False)

self.checkFakeFileStat(fake_file, real_file_path)
self.assertEqual(fake_file.st_mode, os.stat(real_file_path).st_mode)
self.checkWritableFile(fake_file, real_file_path)

def testAddExistingRealDirectoryReadOnly(self):
real_dir_path = os.path.join(os.path.dirname(__file__), 'pyfakefs')
fake_dir = self.filesystem.AddRealDirectory(real_dir_path)
self.assertTrue(self.filesystem.Exists(real_dir_path))
self.assertTrue(self.filesystem.Exists(os.path.join(real_dir_path, 'fake_filesystem.py')))
self.assertTrue(self.filesystem.Exists(os.path.join(real_dir_path, 'fake_pathlib.py')))

file_path = os.path.join(real_dir_path, 'fake_tempfile.py')
fake_file = self.filesystem.ResolveObject(file_path)
self.checkFakeFileStat(fake_file, file_path)
self.checkReadOnlyFile(fake_file, file_path)

def testAddExistingRealDirectoryReadWrite(self):
real_dir_path = os.path.join(os.path.dirname(__file__), 'pyfakefs')
self.filesystem.AddRealDirectory(real_dir_path, read_only=False)
self.assertTrue(self.filesystem.Exists(real_dir_path))
self.assertTrue(self.filesystem.Exists(os.path.join(real_dir_path, 'fake_filesystem.py')))
self.assertTrue(self.filesystem.Exists(os.path.join(real_dir_path, 'fake_pathlib.py')))

file_path = os.path.join(real_dir_path, 'pytest_plugin.py')
fake_file = self.filesystem.ResolveObject(file_path)
self.checkFakeFileStat(fake_file, file_path)
self.checkWritableFile(fake_file, file_path)

def testAddExistingRealPathsReadOnly(self):
real_file_path = os.path.realpath(__file__)
real_dir_path = os.path.join(os.path.dirname(__file__), 'pyfakefs')
self.filesystem.AddRealPaths([real_file_path, real_dir_path])

fake_file = self.filesystem.ResolveObject(real_file_path)
self.checkFakeFileStat(fake_file, real_file_path)
self.checkReadOnlyFile(fake_file, real_file_path)

real_file_path = os.path.join(real_dir_path, 'fake_filesystem_shutil.py')
fake_file = self.filesystem.ResolveObject(real_file_path)
self.checkFakeFileStat(fake_file, real_file_path)
self.checkReadOnlyFile(fake_file, real_file_path)

def testAddExistingRealPathsReadWrite(self):
real_file_path = os.path.realpath(__file__)
real_dir_path = os.path.join(os.path.dirname(__file__), 'pyfakefs')
self.filesystem.AddRealPaths([real_file_path, real_dir_path], read_only=False)

fake_file = self.filesystem.ResolveObject(real_file_path)
self.checkFakeFileStat(fake_file, real_file_path)
self.checkWritableFile(fake_file, real_file_path)

real_file_path = os.path.join(real_dir_path, 'fake_filesystem_glob.py')
fake_file = self.filesystem.ResolveObject(real_file_path)
self.checkFakeFileStat(fake_file, real_file_path)
self.checkWritableFile(fake_file, real_file_path)


if __name__ == '__main__':
unittest.main()
Loading

0 comments on commit d78cb99

Please sign in to comment.