def test_copy_dir_symlink(self): """ Test copying directory with symlink. """ srcdir, dstdir = self._get_tempdir(), self._get_tempdir() util.create_file(os.path.join(srcdir, "test"), "Test") os.symlink("test", os.path.join(srcdir, "sym")) util.copy_dir(srcdir, dstdir, mode=util.CopyDir_Delete) self.assert_(os.path.islink(os.path.join(dstdir, "sym")), "Symlink dereferenced") self.assertEqual(os.readlink(os.path.join(dstdir, "sym")), "test", "Symlink not copied correctly")
def test_copy_dir_writefile(self): """ Test passing a custom writefile function to copy_dir. """ def copyfile(src, dst, callback, **kwds): self.__invoked = True callback(50.0) callback(100.0) def callback(progress): self.__progress.append(progress) srcdir, dstdir = self._get_tempdir(), self._get_tempdir() util.create_file(os.path.join(srcdir, "test1"), "Test") self.__invoked = False self.__progress = [] util.copy_dir(srcdir, dstdir, mode=util.CopyDir_Delete, copyfile= copyfile, callback=callback) self.assert_(self.__invoked, "copyfile was not invoked") self.assertEqual(self.__progress, [0, 50.0, 100.0], "Wrong progress: %r" % self.__progress)
def test_copy_dir_merge(self): """ Test copying directory while merging new contents with old. """ srcdir = self._get_tempdir() dstdir = self._get_tempdir() # Make sure that the merge operation replaces directories in the # destination directory with files in the source directory and vice # versa os.mkdir(os.path.join(srcdir, "testdir")) util.create_file(os.path.join(srcdir, "testfile"), "Test") util.create_file(os.path.join(srcdir, "oldfile"), "Test") util.create_file(os.path.join(dstdir, "newfile"), "Test") util.create_file(os.path.join(dstdir, "testdir"), "Test") os.mkdir(os.path.join(dstdir, "testfile")) util.copy_dir(srcdir, dstdir, mode=util.CopyDir_Merge) self.assertSortedEqual(os.listdir(dstdir), ["testdir", "testfile", "oldfile", "newfile"]) self.assert_(os.path.isdir(os.path.join(dstdir, "testdir"))) for fname in ("testfile", "oldfile", "newfile"): self.assert_(os.path.isfile(os.path.join(dstdir, fname)))
def test_copy_dir_fs_mode(self): """Test copy_dir with specific fs_mode argument.""" srcdir, dstdir = self._get_tempdir(), self._get_tempdir() util.create_file(os.path.join(srcdir, "test"), "Test") if get_os_name() != Os_Windows: fs_mode = 0755 else: # This is a mode that'll work well on Windows for files fs_mode = 0666 util.copy_dir(srcdir, dstdir, mode=util.CopyDir_Delete, fs_mode=fs_mode) pathnames = [os.path.join(dstdir, "test")] if get_os_name() != Os_Windows: # It's very restricted which permissions we can set on Windows directories pathnames.append(dstdir) for pathname in pathnames: got_mode = stat.S_IMODE(os.lstat(pathname).st_mode) self.assertEqual(got_mode, fs_mode, "Mode not correctly set on '%s' (%o)" % (pathname, got_mode))
def test_copy_dir(self): """ Test copying a directory. When a directory is copied, the client callback should be called periodically with progress status. """ def callback(progress): self.__progress.append(progress) dpath = self.__create_dir() dstDir = self._get_tempdir() self.assertRaises(util.DestinationExists, util.copy_dir, dpath, dstDir) # Try forcing deletion of existing directory os.mkdir(os.path.join(dstDir, "testdir")) # Remove only write permission if util.get_os()[0] == Os_Windows: util.chmod(os.path.join(dstDir, "testdir"), 0500) else: util.chmod(dstDir, 0500) # Make sure we can access the directory (requires the correct # permissions on dstDir) assert os.path.exists(os.path.join(dstDir, "testdir")) # This should fail, because the conflicting directory is protected from # deletion self.assertRaises(util.PermissionsError, util.copy_dir, dpath, os.path.join(dstDir, "testdir"), mode=util.CopyDir_Delete) if util.get_os()[0] == Os_Windows: util.chmod(os.path.join(dstDir, "testdir"), 0700) else: util.chmod(dstDir, 0700) util.copy_dir(dpath, dstDir, mode=util.CopyDir_Delete) util.remove_dir(dstDir, recurse=True) self.__progress = [] util.copy_dir(dpath, dstDir, callback=callback) self.assertEqual(compare_dirs(dpath, dstDir), ([], [])) self.assertEqual(self.__progress[0], 0.0) self.assertEqual(self.__progress[-1], 100.0) # Test ignoring certain files dpath = self._get_tempdir() os.mkdir(os.path.join(dpath, ".svn")) util.create_file(os.path.join(dpath, "test")) dstDir = self._get_tempdir() util.copy_dir(dpath, dstDir, ignore=[".*"], mode=util.CopyDir_Delete) self.assertEqual(os.listdir(dstDir), ["test"])
def test_copy_dir_delete(self): """ Test copying directory after first deleting the destination. """ srcdir = self.__create_dir() dstdir = self._get_tempdir() util.copy_dir(srcdir, dstdir, mode=util.CopyDir_Delete) self.assertEqual(os.listdir(dstdir), os.listdir(srcdir))