Skip to content

Commit

Permalink
Removed most of FakeShutilModule functionality
Browse files Browse the repository at this point in the history
- adapted tests to work with fake_filesystem_unittest.TestCase
- fixed some tests
- fake_filesystem.ResolvePath: consider alternative path separator for resolving links
- fixes pytest-dev#194
  • Loading branch information
mrbean-bremen committed Jun 17, 2017
1 parent 360c998 commit bf5ce85
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 302 deletions.
43 changes: 18 additions & 25 deletions fake_filesystem_shutil_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,26 @@

"""Unittest for fake_filesystem_shutil."""

import shutil
import stat
import time
import sys
import time

if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest

from pyfakefs import fake_filesystem
from pyfakefs import fake_filesystem_shutil
from pyfakefs import fake_filesystem_unittest


class FakeShutilModuleTest(unittest.TestCase):
class FakeShutilModuleTest(fake_filesystem_unittest.TestCase):
def setUp(self):
self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/', total_size=1000)
self.shutil = fake_filesystem_shutil.FakeShutilModule(self.filesystem)
self.setUpPyfakefs()
self.filesystem = self.fs
self.filesystem.SetDiskUsage(1000)
self.shutil = shutil

def testRmtree(self):
directory = 'xyzzy'
Expand Down Expand Up @@ -81,10 +84,10 @@ def testRmtreeWithOpenFileFailsUnderWindows(self):

def testRmtreeNonExistingDir(self):
directory = 'nonexisting'
self.assertRaises(IOError, self.shutil.rmtree, directory)
self.assertRaises(OSError, self.shutil.rmtree, directory)
try:
self.shutil.rmtree(directory, ignore_errors=True)
except IOError:
except OSError:
self.fail('rmtree raised despite ignore_errors True')

def testRmtreeNonExistingDirWithHandler(self):
Expand Down Expand Up @@ -148,16 +151,12 @@ def testCopystat(self):
src_obj = self.filesystem.CreateFile(src_file)
dst_obj = self.filesystem.CreateFile(dst_file)
src_obj.st_mode = ((src_obj.st_mode & ~0o7777) | 0o750)
src_obj.st_uid = 123
src_obj.st_gid = 123
src_obj.st_atime = time.time()
src_obj.st_mtime = time.time()
self.assertTrue(self.filesystem.Exists(src_file))
self.assertTrue(self.filesystem.Exists(dst_file))
self.shutil.copystat(src_file, dst_file)
self.assertEqual(src_obj.st_mode, dst_obj.st_mode)
self.assertEqual(src_obj.st_uid, dst_obj.st_uid)
self.assertEqual(src_obj.st_gid, dst_obj.st_gid)
self.assertEqual(src_obj.st_atime, dst_obj.st_atime)
self.assertEqual(src_obj.st_mtime, dst_obj.st_mtime)

Expand All @@ -166,8 +165,6 @@ def testCopy2(self):
dst_file = 'xyzzy_copy'
src_obj = self.filesystem.CreateFile(src_file)
src_obj.st_mode = ((src_obj.st_mode & ~0o7777) | 0o750)
src_obj.st_uid = 123
src_obj.st_gid = 123
src_obj.st_atime = time.time()
src_obj.st_mtime = time.time()
self.assertTrue(self.filesystem.Exists(src_file))
Expand All @@ -176,8 +173,6 @@ def testCopy2(self):
self.assertTrue(self.filesystem.Exists(dst_file))
dst_obj = self.filesystem.GetObject(dst_file)
self.assertEqual(src_obj.st_mode, dst_obj.st_mode)
self.assertEqual(src_obj.st_uid, dst_obj.st_uid)
self.assertEqual(src_obj.st_gid, dst_obj.st_gid)
self.assertEqual(src_obj.st_atime, dst_obj.st_atime)
self.assertEqual(src_obj.st_mtime, dst_obj.st_mtime)

Expand All @@ -188,8 +183,6 @@ def testCopy2Directory(self):
src_obj = self.filesystem.CreateFile(src_file)
self.filesystem.CreateDirectory(parent_directory)
src_obj.st_mode = ((src_obj.st_mode & ~0o7777) | 0o750)
src_obj.st_uid = 123
src_obj.st_gid = 123
src_obj.st_atime = time.time()
src_obj.st_mtime = time.time()
self.assertTrue(self.filesystem.Exists(src_file))
Expand All @@ -199,8 +192,6 @@ def testCopy2Directory(self):
self.assertTrue(self.filesystem.Exists(dst_file))
dst_obj = self.filesystem.GetObject(dst_file)
self.assertEqual(src_obj.st_mode, dst_obj.st_mode)
self.assertEqual(src_obj.st_uid, dst_obj.st_uid)
self.assertEqual(src_obj.st_gid, dst_obj.st_gid)
self.assertEqual(src_obj.st_atime, dst_obj.st_atime)
self.assertEqual(src_obj.st_mtime, dst_obj.st_mtime)

Expand Down Expand Up @@ -283,8 +274,8 @@ def testMoveDirectory(self):
self.assertFalse(self.filesystem.Exists(dst_directory))
self.shutil.move(src_directory, dst_directory)
self.assertTrue(self.filesystem.Exists(dst_directory))
self.assertTrue(self.filesystem.Exists('%s/%s/subfile' % (dst_directory, src_directory)))
self.assertTrue(self.filesystem.Exists('%s/%s/subdir' % (dst_directory, src_directory)))
self.assertTrue(self.filesystem.Exists('%s/subfile' % dst_directory))
self.assertTrue(self.filesystem.Exists('%s/subdir' % dst_directory))
self.assertFalse(self.filesystem.Exists(src_directory))

@unittest.skipIf(sys.version_info < (3, 3), 'New in Python 3.3')
Expand All @@ -302,10 +293,11 @@ def testDiskUsage(self):
self.assertEqual((500, 400, 100), disk_usage)


class CopyFileTest(unittest.TestCase):
class CopyFileTest(fake_filesystem_unittest.TestCase):
def setUp(self):
self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/')
self.shutil = fake_filesystem_shutil.FakeShutilModule(self.filesystem)
self.setUpPyfakefs()
self.filesystem = self.fs
self.shutil = shutil

def testCommonCase(self):
src_file = 'xyzzy'
Expand Down Expand Up @@ -366,7 +358,8 @@ def testRaisesIfDestExistsAndIsNotWritable(self):
self.assertTrue(self.filesystem.Exists(dst_file))
self.assertRaises(IOError, self.shutil.copyfile, src_file, dst_file)

def testRaisesIfDestDirIsNotWritable(self):
def testRaisesIfDestDirIsNotWritableUnderPosix(self):
self.filesystem.is_windows_fs = False
src_file = 'xyzzy'
dst_dir = '/tmp/foo'
dst_file = '%s/%s' % (dst_dir, src_file)
Expand Down
8 changes: 7 additions & 1 deletion pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1592,7 +1592,8 @@ def _FollowLink(link_path_components, link):
# For links to absolute paths, we want to throw out everything in the
# path built so far and replace with the link. For relative links, we
# have to append the link to what we have so far,
if not link_path.startswith(sep):
if (not link_path.startswith(sep) and
(alt_sep is None or not link_path.startswith(alt_sep))):
# Relative path. Append remainder of path to what we have processed
# so far, excluding the name of the link itself.
# /a/b => ../c should yield /a/../c (which will normalize to /c)
Expand Down Expand Up @@ -2857,6 +2858,11 @@ def samefile(self, path1, path2):
stat2 = self.filesystem.GetStat(path2)
return stat1.st_ino == stat2.st_ino and stat1.st_dev == stat2.st_dev

if sys.platform.startswith('linux') and sys.version_info >= (3, 3):
def listxattr(path=None, follow_symlinks=True):
"""Dummy implementation that returns an empty list - used by shutil."""
return []

def _joinrealpath(self, path, rest, seen):
"""Join two paths, normalizing and eliminating any symbolic links
encountered in the second path.
Expand Down
Loading

0 comments on commit bf5ce85

Please sign in to comment.