Exemplo n.º 1
0
    def test_remove_dir(self):

        dirname = '/tmp/pykit-ut-k3fs-remove-dir'

        k3fs.makedirs(dirname)
        self.assertTrue(os.path.isdir(dirname))

        for is_dir, file_path in (
                (False, ('normal_file',)),
                (True, ('sub_dir',)),
                (False, ('sub_dir', 'sub_file1')),
                (False, ('sub_dir', 'sub_file2')),
                (True, ('sub_empty_dir',)),
                (True, ('sub_dir', 'sub_sub_dir')),
                (False, ('sub_dir', 'sub_sub_dir', 'sub_sub_file')),
        ):

            path = os.path.join(dirname, *file_path)

            if is_dir:
                k3fs.makedirs(path)
                self.assertTrue(os.path.isdir(path))
            else:
                k3fs.fwrite(path, '')
                self.assertTrue(os.path.isfile(path))

        k3fs.remove(dirname)
        self.assertFalse(os.path.exists(dirname))
Exemplo n.º 2
0
    def test_remove_link_file(self):

        src_fn = '/tmp/pykit-ut-k3fs-remove-file-normal'
        force_remove(src_fn)

        k3fs.fwrite(src_fn, '', atomic=True)
        self.assertTrue(os.path.isfile(src_fn))

        link_fn = '/tmp/pykit-ut-k3fs-remove-file-link'
        force_remove(link_fn)

        os.link(src_fn, link_fn)
        self.assertTrue(os.path.isfile(link_fn))

        k3fs.remove(link_fn)
        self.assertFalse(os.path.exists(link_fn))

        symlink_fn = '/tmp/pykit-ut-k3fs-remove-file-symlink'
        force_remove(symlink_fn)

        os.symlink(src_fn, symlink_fn)
        self.assertTrue(os.path.islink(symlink_fn))

        k3fs.remove(symlink_fn)
        self.assertFalse(os.path.exists(symlink_fn))

        force_remove(src_fn)
Exemplo n.º 3
0
    def test_remove_dir_with_link(self):

        dirname = '/tmp/pykit-ut-k3fs-remove-dir'

        k3fs.makedirs(dirname)
        self.assertTrue(os.path.isdir(dirname))

        normal_file = 'normal_file'
        normal_path = os.path.join(dirname, normal_file)

        k3fs.fwrite(normal_path, '')
        self.assertTrue(os.path.isfile(normal_path))

        hard_link = 'hard_link'
        hard_path = os.path.join(dirname, hard_link)

        os.link(normal_path, hard_path)
        self.assertTrue(os.path.isfile(hard_path))

        symbolic_link = 'symbolic_link'
        symbolic_path = os.path.join(dirname, symbolic_link)

        os.symlink(hard_path, symbolic_path)
        self.assertTrue(os.path.islink(symbolic_path))

        k3fs.remove(dirname)
        self.assertFalse(os.path.exists(dirname))
Exemplo n.º 4
0
        def _write_wait(cont_write, cont_read, start_after, atomic):

            time.sleep(start_after)

            k3fs.fwrite(fn, cont_write, atomic=atomic)

            if cont_read != k3fs.fread(fn):
                assert_ok['ok'] = False
Exemplo n.º 5
0
    def test_blob_new(self):
        fwrite(pjoin(superp, "newblob"), "newblob!!!")
        # TODO
        g = Git(GitOpt(), cwd=superp)
        blobhash = g.blob_new("newblob")

        content = cmd0(origit, "cat-file", "-p", blobhash, cwd=superp)
        self.assertEqual("newblob!!!", content)
Exemplo n.º 6
0
    def setUp(self):
        self.maxDiff = None

        _clean_case()

        # .git can not be track in a git repo.
        # need to manually create it.
        fwrite(pjoin(this_base, "testdata", "super", ".git"),
               "gitdir: ../supergit")
Exemplo n.º 7
0
    def _add_commit_to_bar_from_other_clone(self):
        cmdx(origit, "clone", bargitp, barp)

        fwrite(pjoin(barp, "for_fetch"), "for_fetch")
        cmdx(origit, "add", "for_fetch", cwd=barp)
        cmdx(origit, *ident_args, "commit", "-m", "add for_fetch", cwd=barp)
        cmdx(origit, "push", "origin", "master", cwd=barp)

        headhash = cmd0(origit, "rev-parse", "HEAD", cwd=barp)
        return headhash
Exemplo n.º 8
0
    def test_branch_default_upstream(self):
        fwrite(branch_test_worktree_p, ".git", "gitdir: ../branch_test_git")

        g = Git(GitOpt(), cwd=branch_test_worktree_p)
        cases = [
            ('master', 'origin/master'),
            ('dev', 'upstream/master'),
            ('not_a_branch', None),
        ]

        for branch, remote in cases:
            got = g.branch_default_upstream(branch)
            self.assertEqual(remote, got)
Exemplo n.º 9
0
    def test_ls_dirs(self):
        k3fs.makedirs('test_dir/sub_dir1/foo')
        k3fs.makedirs('test_dir/sub_dir2')
        k3fs.fwrite('test_dir/test_file', 'foo')

        sub_dirs = k3fs.ls_dirs('test_dir')
        self.assertListEqual(['sub_dir1', 'sub_dir2'], sub_dirs)

        # test multi path segment
        sub_dirs = k3fs.ls_dirs('test_dir', 'sub_dir1')
        self.assertListEqual(['foo'], sub_dirs)

        k3fs.remove('test_dir')
Exemplo n.º 10
0
    def test_read_write_file(self):

        fn = '/tmp/pykit-ut-rw-file'
        force_remove(fn)

        dd('write/read file')
        k3fs.fwrite(fn, 'bar')
        self.assertEqual('bar', k3fs.fread(fn))

        # write with multi path segment
        k3fs.fwrite('/tmp', 'pykit-ut-rw-file', '123')
        self.assertEqual('123', k3fs.fread(fn))

        # read with multi path segment
        self.assertEqual('123', k3fs.fread('/tmp', 'pykit-ut-rw-file'))

        self.assertEqual(b'123', k3fs.fread(fn, mode='b'))

        dd('write/read 3MB file')
        cont = '123' * (1024**2)

        k3fs.fwrite(fn, cont)
        self.assertEqual(cont, k3fs.fread(fn))

        dd('write file with uid/gid')
        k3fs.fwrite(fn, '1', uid=1, gid=1)
        stat = os.stat(fn)
        self.assertEqual(1, stat.st_uid)
        self.assertEqual(1, stat.st_gid)

        force_remove(fn)
Exemplo n.º 11
0
    def test_worktree_is_clean(self):
        # branch_test_git_p is a git-dir with one commit::
        # * 1d5ae3d (HEAD, origin/master, master) A  a

        # write a ".git" file to specify the git-dir for the containing
        # git-work-tree.
        fwrite(branch_test_worktree_p, ".git", "gitdir: ../branch_test_git")

        g = Git(GitOpt(), cwd=branch_test_worktree_p)

        self.assertTrue(g.worktree_is_clean())

        fwrite(branch_test_worktree_p, "a", "foobarfoobar")
        self.assertFalse(g.worktree_is_clean())
Exemplo n.º 12
0
    def test_branch_common_base(self):
        fwrite(branch_test_worktree_p, ".git", "gitdir: ../branch_test_git")

        g = Git(GitOpt(), cwd=branch_test_worktree_p)
        cases = [
            #  (['b2'], (['1315e30ec849dbbe67df3282139c0e0d3fdca606'], ['d1ec6549cffc507a2d41d5e363dcbd23754377c7'])),
            #  (['b2', 'base'], (['1315e30ec849dbbe67df3282139c0e0d3fdca606'], ['d1ec6549cffc507a2d41d5e363dcbd23754377c7'])),
            #  (['b2', 'master'], (['1315e30ec849dbbe67df3282139c0e0d3fdca606'], [])),
            (['b2', 'base'], '3d7f4245f05db036309e9f74430d5479263637ad'),
            (['b2', 'master'], '3d7f4245f05db036309e9f74430d5479263637ad'),
        ]

        for args, want in cases:
            got = g.branch_common_base(*args)
            self.assertEqual(want, got)
Exemplo n.º 13
0
    def test_reset_to_commit(self):
        #  * 1315e30 (b2) add b2
        #  | * d1ec654 (base) add base
        #  |/
        #  * 3d7f424 (HEAD -> master, upstream/master, origin/master, dev) a

        fwrite(branch_test_worktree_p, ".git", "gitdir: ../branch_test_git")

        g = Git(GitOpt(), cwd=branch_test_worktree_p)

        g.cmdf("checkout", 'b2')

        # build index
        fwrite(branch_test_worktree_p, "x", "x")
        g.cmdf('add', 'x')
        fwrite(branch_test_worktree_p, "y", "y")
        g.cmdf('add', 'y')

        # dirty worktree
        fwrite(branch_test_worktree_p, "x", "xx")

        # soft default to HEAD, nothing changed

        g.reset_to_commit('soft')

        out = g.cmdf('diff', '--name-only', '--relative', flag='xo')
        self.assertEqual([
            'x',
        ], out, "dirty worktree")

        out = g.cmdf('diff', '--name-only', '--relative', 'HEAD', flag='xo')
        self.assertEqual(['x', 'y'], out, "compare with HEAD")

        # soft to master

        g.reset_to_commit('soft', 'master')

        out = g.cmdf('diff', '--name-only', '--relative', flag='xo')
        self.assertEqual([
            'x',
        ], out, "dirty worktree")

        out = g.cmdf('diff', '--name-only', '--relative', 'HEAD', flag='xo')
        self.assertEqual([
            'b2',
            'x',
            'y',
        ], out, "compare with HEAD")

        # hard to master

        g.reset_to_commit('hard', 'master')

        out = g.cmdf('diff', '--name-only', '--relative', 'HEAD', flag='xo')
        self.assertEqual([], out, "compare with HEAD")
Exemplo n.º 14
0
    def test_branch_default_remote(self):
        # branch_test_git_p is a git-dir with one commit::
        # * 1d5ae3d (HEAD, origin/master, master) A  a

        # write a ".git" file to specify the git-dir for the containing
        # git-work-tree.
        fwrite(branch_test_worktree_p, ".git", "gitdir: ../branch_test_git")

        g = Git(GitOpt(), cwd=branch_test_worktree_p)
        cases = [
            ('master', 'origin'),
            ('dev', 'upstream'),
            ('not_a_branch', None),
        ]

        for branch, remote in cases:
            got = g.branch_default_remote(branch)
            self.assertEqual(remote, got)
Exemplo n.º 15
0
    def test_remove_normal_file(self):

        f = 'pykit-ut-k3fs-remove-file-normal'
        fn = '/tmp/' + f
        force_remove(fn)

        k3fs.fwrite(fn, '', atomic=True)
        self.assertTrue(os.path.isfile(fn))

        k3fs.remove(fn)
        self.assertFalse(os.path.exists(fn))

        k3fs.fwrite('/tmp', f, '', atomic=True)
        self.assertTrue(os.path.isfile(fn))

        # remove with multi path segments
        k3fs.remove('/tmp', f)
        self.assertFalse(os.path.exists(fn))
Exemplo n.º 16
0
    def test_head_branch(self):
        # branch_test_git_p is a git-dir with one commit::
        # * 1d5ae3d (HEAD, origin/master, master) A  a

        # write a ".git" file to specify the git-dir for the containing
        # git-work-tree.
        fwrite(branch_test_worktree_p, ".git", "gitdir: ../branch_test_git")

        g = Git(GitOpt(), cwd=branch_test_worktree_p)
        got = g.head_branch()
        self.assertEqual('master', got)

        # checkout to a commit pointing to no branch
        # It should return None
        g.checkout('origin/master')
        got = g.head_branch()
        self.assertIsNone(got)

        g.checkout('master')
Exemplo n.º 17
0
    def test_ls_files(self):

        k3fs.makedirs('test_dir/foo_dir')

        k3fs.fwrite('test_dir/foo1', 'foo1')
        k3fs.fwrite('test_dir/foo2', 'foo2')
        k3fs.fwrite('test_dir/foo21', 'foo21')
        k3fs.fwrite('test_dir/foo_dir/foo', 'foo')
        k3fs.fwrite('test_dir/foo_dir/bar', 'bar')

        self.assertEqual(['foo1', 'foo2', 'foo21'], k3fs.ls_files('test_dir'))
        self.assertEqual(['foo2'], k3fs.ls_files('test_dir', pattern='2$'))

        self.assertEqual(['bar', 'foo'], k3fs.ls_files('test_dir/foo_dir'))
        self.assertEqual(['bar'], k3fs.ls_files('test_dir/foo_dir', pattern='^b'))

        # test multi path segments
        self.assertEqual(['bar', 'foo'], k3fs.ls_files('test_dir', 'foo_dir'))

        k3fs.remove('test_dir')
Exemplo n.º 18
0
import os
import sys

import k3fs

fn = sys.argv[1]

k3fs.fwrite(fn, 'boo')
stat = os.stat(fn)
os.write(
    1, '{uid},{gid}'.format(uid=stat.st_uid, gid=stat.st_gid).encode('utf-8'))
Exemplo n.º 19
0
 def _add_file_to_subbar(self):
     fwrite(pjoin(subbarp, "newbar"), "newbar")
     cmdx(giftp, "add", "newbar", cwd=subbarp)
     cmdx(giftp, *ident_args, "commit", "-m", "add newbar", cwd=subbarp)