def test_remove_dir_force(self): """Test removing directory with read-only contents.""" dpath = self.__create_dir() # Make the directory and its contents read-only util.chmod(dpath, 0, recursive=True) util.remove_dir(dpath, force=True) self.assertNot(os.path.exists(dpath))
def test_remove_dir_force_ro_parent(self): """Test attempting to forcefully remove directory with read-only parent.""" dpath = self.__create_dir() # Make read-only util.chmod(dpath, 0555) self.assertRaises(util.PermissionsError, util.remove_dir, os.path.join(dpath, "testdir"), force=True)
def test_remove_file_force(self): """ Test removing a read-only file forcefully. """ dpath = self.__create_dir() fpath = os.path.join(dpath, "test") # Make the file read-only util.chmod(fpath, stat.S_IEXEC | stat.S_IREAD, recursive=True) util.remove_file(fpath, force=True) self.assertNot(os.path.exists(fpath))
def test_compare_dirs_subdirs(self): """ Test compare_dirs with differing sub-directories. """ dpath0, dpath1 = self._get_tempdir(), self._get_tempdir() subdir0, subdir1 = os.path.join(dpath0, "subdir"), os.path.join(dpath1, "subdir") os.mkdir(subdir0) os.mkdir(subdir1) util.chmod(subdir0, 0) self.assertEqual(util.compare_dirs(dpath0, dpath1), (["subdir"], []))
def test_copy_dir_noperm(self): """ Test copying a directory with missing permissions. """ dpath0, dpath1 = self.__create_dir(), self._get_tempdir() if util.get_os()[0] != Os_Windows: # Can't remove read permission on Windows util.chmod(dpath0, 0) self.assertRaises(util.PermissionsError, util.copy_dir, dpath0, dpath1, mode=util.CopyDir_Delete) util.chmod(dpath0, 0700) # Test directory permissions util.chmod(os.path.join(dpath0, "testdir"), 0000) self.assertRaises(util.PermissionsError, util.copy_dir, dpath0, dpath1, mode=util.CopyDir_Delete) util.chmod(os.path.join(dpath0, "testdir"), 0700) # Test file permissions util.chmod(os.path.join(dpath0, "test"), 0000) self.assertRaises(util.PermissionsError, util.copy_dir, dpath0, dpath1, mode=util.CopyDir_Delete)
def test_remove_file(self): dpath = self._get_tempdir() fpath = util.create_file(os.path.join(dpath, "test")) if util.get_os()[0] == Os_Windows: util.chmod(fpath, 0) else: util.chmod(dpath, 0) self.assertRaises(util.PermissionsError, util.remove_file, fpath) if util.get_os()[0] == Os_Windows: util.chmod(fpath, 0700) else: util.chmod(dpath, 0700) util.remove_file(fpath) self.assertNot(os.path.exists(fpath))
def test_chmod_recursive(self): """ Test chmod in recursive mode. """ dpath = self._get_tempdir() fpath = util.create_file(os.path.join(dpath, "file")) os.mkdir(os.path.join(dpath, "subdir")) # Set executable so the directory can be traversed mode = stat.S_IREAD | stat.S_IEXEC util.chmod(dpath, mode, True) if get_os_name() == Os_Windows: # Some permissions can't be turned off on Windows .. dirmode = mode | (stat.S_IROTH | stat.S_IXOTH | stat.S_IRGRP | stat.S_IXGRP) fmode = stat.S_IREAD | stat.S_IROTH | stat.S_IRGRP else: dirmode = fmode = mode self.assertEqual(util.get_file_permissions(dpath), dirmode) for e in os.listdir(dpath): if os.path.isfile(os.path.join(dpath, e)): mode = fmode else: mode = dirmode self.assertEqual(util.get_file_permissions(os.path.join(dpath, e)), 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_chmod(self): dpath = self.__create_dir() util.chmod(dpath, 0) mode = stat.S_IMODE(os.stat(dpath).st_mode) if util.get_os()[0] == Os_Windows: # Impossible to remove rx permissions on Windows self.assertNot(mode & stat.S_IWRITE) else: self.assertEqual(mode, 0) self.assertRaises(util.PermissionsError, util.chmod, dpath, 0, recursive=True) util.chmod(dpath, 0700) util.chmod(dpath, 0000, recursive=True) util.chmod(dpath, 0700) filemode = stat.S_IMODE(os.stat(os.path.join(dpath, "test")).st_mode) dirmode = stat.S_IMODE(os.stat(os.path.join(dpath, "testdir")).st_mode) if util.get_os()[0] == Os_Windows: self.assertNot(filemode & (stat.S_IWRITE | stat.S_IEXEC)) self.assertNot(dirmode & stat.S_IWRITE) else: self.assertEqual(filemode, 0) self.assertEqual(dirmode, 0)
def test_movefile(self): # There might be a problem moving a file onto another, we are testing # this case as well since the destination already exists dstDir = self._get_tempdir() src, dst = self._get_tempfile(), util.create_file(os.path.join(dstDir, "test")) try: src.write("Test") finally: src.close() if util.get_os()[0] == Os_Windows: util.chmod(dst, 0) else: util.chmod(dstDir, 0) self.assertRaises(util.PermissionsError, util.move_file, src.name, dst) if util.get_os()[0] == Os_Windows: util.chmod(dst, 0700) else: util.chmod(dstDir, 0700) util.move_file(src.name, dst) f = file(dst) try: txt = f.read() finally: f.close() self.assertEqual(txt, "Test") self.assertNot(os.path.exists(src.name))