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
- FakePathModule: do not implement samefile if not existent
- fake_filesystem.ResolvePath: consider alternative path separator for resolving links
- fixes pytest-dev#194
  • Loading branch information
mrbean-bremen committed Jun 11, 2017
1 parent 6e2c752 commit 059933c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 328 deletions.
51 changes: 16 additions & 35 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,17 +358,6 @@ def testRaisesIfDestExistsAndIsNotWritable(self):
self.assertTrue(self.filesystem.Exists(dst_file))
self.assertRaises(IOError, self.shutil.copyfile, src_file, dst_file)

def testRaisesIfDestDirIsNotWritable(self):
src_file = 'xyzzy'
dst_dir = '/tmp/foo'
dst_file = '%s/%s' % (dst_dir, src_file)
src_contents = 'contents of source file'
self.filesystem.CreateFile(src_file, contents=src_contents)
self.filesystem.CreateDirectory(dst_dir, perm_bits=0o555)
self.assertTrue(self.filesystem.Exists(src_file))
self.assertTrue(self.filesystem.Exists(dst_dir))
self.assertRaises(IOError, self.shutil.copyfile, src_file, dst_file)

def testRaisesIfSrcDoesntExist(self):
src_file = 'xyzzy'
dst_file = 'xyzzy_copy'
Expand Down
38 changes: 21 additions & 17 deletions pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1558,10 +1558,12 @@ def _FollowLink(link_path_components, link):
"""
link_path = link.contents
sep = self._path_separator(link_path)
alt_sep = self._alternative_path_separator(link_path)
# 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 @@ -2792,25 +2794,27 @@ def realpath(self, filename):
path, ok = self._joinrealpath(filename[:0], filename, {})
return self.abspath(path)

def samefile(self, path1, path2):
"""Return whether path1 and path2 point to the same file.
Windows support new in Python 3.2.
New in pyfakefs 3.3.
if sys.platform != 'win32' or sys.version_info >= (3, 2):
def samefile(self, path1, path2):
"""Return whether path1 and path2 point to the same file.
Windows support new in Python 3.2.
New in pyfakefs 3.3.
Args:
path1: first file path or path object (Python >=3.6)
path2: second file path or path object (Python >=3.6)
Raises:
OSError: if one of the paths does not point to an existing file system object.
"""
if self.filesystem.is_windows_fs and sys.version_info < (3, 2):
raise (AttributeError, "'module' object has no attribute 'samefile'")
Args:
path1: first file path or path object (Python >=3.6)
path2: second file path or path object (Python >=3.6)
stat1 = self.filesystem.GetStat(path1)
stat2 = self.filesystem.GetStat(path2)
return stat1.st_ino == stat2.st_ino and stat1.st_dev == stat2.st_dev
Raises:
OSError: if one of the paths does not point to an existing file system object.
"""
stat1 = self.filesystem.GetStat(path1)
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
Expand Down
Loading

0 comments on commit 059933c

Please sign in to comment.