Skip to content

Commit

Permalink
Fix shutil.move implementation
Browse files Browse the repository at this point in the history
If both the source and destination argument of shutil.move are
directories the source directory is copied under the target directory.
pyfakefs implementation copied the source directory to the target
directory.

The real shutil.move call shutil.copy('/path/to/src', '/path/to/dest')
moves /path/to/src to /path/to/dest/src but the pyfakefs implementation
moved the src directory to /path/to/dest
  • Loading branch information
janneronkko committed Dec 22, 2016
1 parent ee9cb06 commit e94bb0f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions fake_filesystem_shutil_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,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/subfile' % dst_directory))
self.assertTrue(self.filesystem.Exists('%s/subdir' % 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.assertFalse(self.filesystem.Exists(src_directory))

@unittest.skipIf(sys.version_info < (3, 3), 'New in Python 3.3')
Expand Down
6 changes: 4 additions & 2 deletions pyfakefs/fake_filesystem_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,10 @@ def _destinsrc(src, dst):
return dst

source_is_dir = stat.S_ISDIR(self.filesystem.GetObject(src).st_mode)
if source_is_dir and self.filesystem.Exists(dst):
raise shutil.Error("Destination path '%s' already exists" % dst)
if source_is_dir:
dst = self.filesystem.JoinPaths(dst, os.path.basename(src))
if self.filesystem.Exists(dst):
raise shutil.Error("Destination path '%s' already exists" % dst)

try:
self.filesystem.RenameObject(src, dst)
Expand Down

0 comments on commit e94bb0f

Please sign in to comment.