Exemplo n.º 1
0
 def test_create_file_unicode(self):
     """ Test creating file with unicode content. """
     fpath = self._get_tempfname()
     util.create_file(fpath, content=u"æøå", encoding="utf-8")
     f = codecs.open(fpath, encoding="utf-8")
     try: self.assertEqual(f.read(), u"æøå")
     finally: f.close()
Exemplo n.º 2
0
    def test_get_checksum(self):
        """Test get_checksum."""
        dir0, dir1 = self._get_tempdir(), self._get_tempdir()
        self.assertEqual(util.get_checksum(dir0), util.get_checksum(dir1))
        util.create_file(os.path.join(dir1, "test1"), "Test1")

        chksum = util.get_checksum(dir1)
        self.assertNotEqual(util.get_checksum(dir0), chksum)
        self.assertEqual(chksum, "99ea7bf70f6e69ad71659995677b43f8a8312025")
Exemplo n.º 3
0
 def __create_dir(self):
     """ Create directory with contents. """
     dpath = self._get_tempdir()
     # We put some content inside the created files, since read permissions
     # will not affect empty files (i.e., copying an empty file won't
     # provoke an error)
     util.create_file(os.path.join(dpath, "test"), "Test")
     os.mkdir(os.path.join(dpath, "testdir"))
     util.create_file(os.path.join(dpath, "testdir", "test"), "Test")
     return dpath
Exemplo n.º 4
0
 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")
Exemplo n.º 5
0
    def test_get_checksum_filename_order(self):
        """Verify that get_checksum isn't affected by filename order returned by os.listdir."""
        def fake_listdir(dpath):
            return reversed(orig_listdir(dpath))

        orig_listdir = os.listdir
        self._set_attr(os, "listdir", fake_listdir)
        dir0, dir1 = self._get_tempdir(), self._get_tempdir()
        util.create_file(os.path.join(dir1, "test1"), "Test1")
        util.create_file(os.path.join(dir1, "test2"), "Test2")

        chksum = util.get_checksum(dir1)
        self.assertEqual(chksum, "35bceb434ff8e69fb89b829e461c921a28b423b3")
Exemplo n.º 6
0
    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"])
Exemplo n.º 7
0
    def test_get_checksum_cancel(self):
        """ Test canceling checksum calculation. """
        def callback():
            raise _srlerror.Canceled

        path = util.create_file(self._get_tempfname(), "Test")
        self.assertRaises(_srlerror.Canceled, util.get_checksum, path, callback=
            callback)
Exemplo n.º 8
0
    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)
Exemplo n.º 9
0
    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))
Exemplo n.º 10
0
 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))
Exemplo n.º 11
0
    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)))
Exemplo n.º 12
0
 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)
Exemplo n.º 13
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))
Exemplo n.º 14
0
 def test_create_file_bin(self):
     """ Test creating a file in binary mode. """
     f = util.create_file(self._get_tempfname(), binary=True, close=False)
     try:
         self.assertEqual(f.mode, "wb")
     finally: f.close()
Exemplo n.º 15
0
 def test_get_checksum_carriage_return(self):
     """Test get_checksum on file with carriage return newline character."""
     text = "Test\r\n"
     ref_hash = hashlib.sha1(text).hexdigest()
     path = util.create_file(self._get_tempfname(), text, binary=True)
     self.assertEqual(util.get_checksum(path), ref_hash)
Exemplo n.º 16
0
 def test_compare_dirs(self):
     """ Test dir comparison. """
     dpath0 = self._get_tempdir()
     util.create_file(os.path.join(dpath0, "tmpfile"), "Test")
     self.assertEqual(util.compare_dirs(dpath0, dpath0, False), ([], []))
Exemplo n.º 17
0
 def test_compare_dirs_first_empty(self):
     """ Test against an empty first directory. """
     dpath0, dpath1 = self._get_tempdir(), self._get_tempdir()
     util.create_file(os.path.join(dpath1, "file"))
     self.assertEqual(util.compare_dirs(dpath0, dpath1), ([], ["file"]))