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_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_get_os_microsoft(self): """Make sure that OS name Microsoft gets handled correctly.""" def uname(): return "Microsoft", "host", "Windows", "6.1.6000", "", "" self._set_module_attr("platform", "uname", uname) self.assertEqual(util.get_os(), (Os_Windows, "6.1.6000"))
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))
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_get_os(self): self.assertEqual(util.get_os(), (get_os_name(), util.get_os_version()))