Skip to content

Commit

Permalink
Merge pull request #155 from jannero/free_space
Browse files Browse the repository at this point in the history
Fix disk usage calculations when too large file is created
  • Loading branch information
mrbean-bremen authored Feb 7, 2017
2 parents 315d635 + 1c486a8 commit 298a692
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
35 changes: 35 additions & 0 deletions fake_filesystem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4165,6 +4165,25 @@ def setUp(self):
self.filesystem = fake_filesystem.FakeFilesystem(path_separator='!', total_size=100)
self.os = fake_filesystem.FakeOsModule(self.filesystem)

def testDiskUsageOnFileCreation(self):
fake_open = fake_filesystem.FakeFileOpen(self.filesystem)

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))

self.assertRaises((OSError, IOError), create_too_large_file)

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

with fake_open('!mount!file', 'w') as dest:
dest.write('a' * total_size)

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 @@ -4203,29 +4222,45 @@ def testFileSystemSizeAfterDirectoryRemoval(self):
self.assertEqual((100, 40, 60), self.filesystem.GetDiskUsage())

def testCreatingFileWithFittingContent(self):
initial_usage = self.filesystem.GetDiskUsage()

try:
self.filesystem.CreateFile('!foo!bar', contents=b'a' * 100)
except IOError:
self.fail('File with contents fitting into disk space could not be written.')

self.assertEqual(initial_usage.used + 100, self.filesystem.GetDiskUsage().used)

def testCreatingFileWithContentTooLarge(self):
def create_large_file():
self.filesystem.CreateFile('!foo!bar', contents=b'a' * 101)

initial_usage = self.filesystem.GetDiskUsage()

self.assertRaises(IOError, create_large_file)

self.assertEqual(initial_usage, self.filesystem.GetDiskUsage())

def testCreatingFileWithFittingSize(self):
initial_usage = self.filesystem.GetDiskUsage()

try:
self.filesystem.CreateFile('!foo!bar', st_size=100)
except IOError:
self.fail('File with size fitting into disk space could not be written.')

self.assertEqual(initial_usage.used + 100, self.filesystem.GetDiskUsage().used)

def testCreatingFileWithSizeTooLarge(self):
initial_usage = self.filesystem.GetDiskUsage()

def create_large_file():
self.filesystem.CreateFile('!foo!bar', st_size=101)

self.assertRaises(IOError, create_large_file)

self.assertEqual(initial_usage, self.filesystem.GetDiskUsage())

def testResizeFileWithFittingSize(self):
file_object = self.filesystem.CreateFile('!foo!bar', st_size=50)
try:
Expand Down
4 changes: 2 additions & 2 deletions pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,10 @@ def _SetInitialContents(self, contents, encoding):
if self.byte_contents:
self.SetSize(0)
current_size = self.st_size or 0
if self.filesystem:
self.filesystem.ChangeDiskUsage(st_size - current_size, self.name, self.st_dev)
self.byte_contents = contents
self.st_size = st_size
if self.filesystem:
self.filesystem.ChangeDiskUsage(self.st_size - current_size, self.name, self.st_dev)
self.epoch += 1

def SetContents(self, contents, encoding=None):
Expand Down

0 comments on commit 298a692

Please sign in to comment.