/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to tests/test_git_remote_helper.py

Support reading .git files.

Merged from https://code.launchpad.net/~jelmer/brz-git/read-gitfile/+merge/342256

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# vim: expandtab
 
3
 
 
4
# Copyright (C) 2011-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
5
 
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
19
 
 
20
"""Tests for the git remote helper."""
 
21
 
 
22
from __future__ import absolute_import
 
23
 
 
24
from cStringIO import StringIO
 
25
import os
 
26
 
 
27
from dulwich.repo import Repo
 
28
 
 
29
from ....tests import (
 
30
    TestCaseWithTransport,
 
31
    TestSkipped,
 
32
    )
 
33
 
 
34
from ..object_store import get_object_store
 
35
from ..git_remote_helper import (
 
36
    RemoteHelper,
 
37
    open_local_dir,
 
38
    fastexporter,
 
39
    fetch,
 
40
    )
 
41
 
 
42
 
 
43
def map_to_git_sha1(dir, bzr_revid):
 
44
    object_store = get_object_store(dir.open_repository())
 
45
    with object_store.lock_read():
 
46
        return object_store._lookup_revision_sha1(bzr_revid)
 
47
 
 
48
 
 
49
class OpenLocalDirTests(TestCaseWithTransport):
 
50
 
 
51
    def test_from_env_dir(self):
 
52
        self.make_branch_and_tree('bla', format='git')
 
53
        self.overrideEnv('GIT_DIR', os.path.join(self.test_dir, 'bla', '.git'))
 
54
        open_local_dir()
 
55
 
 
56
    def test_from_dir(self):
 
57
        self.make_branch_and_tree('.', format='git')
 
58
        open_local_dir()
 
59
 
 
60
 
 
61
class FetchTests(TestCaseWithTransport):
 
62
 
 
63
    def setUp(self):
 
64
        super(FetchTests, self).setUp()
 
65
        self.local_dir = self.make_branch_and_tree('local', format='git').controldir
 
66
        self.remote_tree = self.make_branch_and_tree('remote')
 
67
        self.remote_dir = self.remote_tree.controldir
 
68
        self.shortname = 'bzr'
 
69
 
 
70
    def fetch(self, wants):
 
71
        outf = StringIO()
 
72
        fetch(outf, wants, self.shortname, self.remote_dir, self.local_dir)
 
73
        return outf.getvalue()
 
74
 
 
75
    def test_no_wants(self):
 
76
        r = self.fetch([])
 
77
        self.assertEquals("\n", r)
 
78
 
 
79
    def test_simple(self):
 
80
        self.build_tree(['remote/foo'])
 
81
        self.remote_tree.add("foo")
 
82
        revid = self.remote_tree.commit("msg")
 
83
        git_sha1 = map_to_git_sha1(self.remote_dir, revid)
 
84
        out = self.fetch([(git_sha1, 'HEAD')])
 
85
        self.assertEquals(out, "\n")
 
86
        r = Repo('local')
 
87
        self.assertTrue(git_sha1 in r.object_store)
 
88
        self.assertEquals({
 
89
            }, r.get_refs())
 
90
 
 
91
 
 
92
class RemoteHelperTests(TestCaseWithTransport):
 
93
 
 
94
    def setUp(self):
 
95
        super(RemoteHelperTests, self).setUp()
 
96
        self.local_dir = self.make_branch_and_tree('local', format='git').controldir
 
97
        self.remote_tree = self.make_branch_and_tree('remote')
 
98
        self.remote_dir = self.remote_tree.controldir
 
99
        self.shortname = 'bzr'
 
100
        self.helper = RemoteHelper(self.local_dir, self.shortname, self.remote_dir)
 
101
 
 
102
    def test_capabilities(self):
 
103
        f = StringIO()
 
104
        self.helper.cmd_capabilities(f, [])
 
105
        capabs = f.getvalue()
 
106
        base = "fetch\noption\npush\n"
 
107
        self.assertTrue(capabs in (base+"\n", base+"import\n\n"), capabs)
 
108
 
 
109
    def test_option(self):
 
110
        f = StringIO()
 
111
        self.helper.cmd_option(f, [])
 
112
        self.assertEquals("unsupported\n", f.getvalue())
 
113
 
 
114
    def test_list_basic(self):
 
115
        f = StringIO()
 
116
        self.helper.cmd_list(f, [])
 
117
        self.assertEquals(
 
118
            '\n',
 
119
            f.getvalue())
 
120
 
 
121
    def test_import(self):
 
122
        if fastexporter is None:
 
123
            raise TestSkipped("bzr-fastimport not available")
 
124
        self.build_tree_contents([("remote/afile", "somecontent")])
 
125
        self.remote_tree.add(["afile"])
 
126
        self.remote_tree.commit("A commit message", timestamp=1330445983,
 
127
            timezone=0, committer='Somebody <jrandom@example.com>')
 
128
        f = StringIO()
 
129
        self.helper.cmd_import(f, ["import", "refs/heads/master"])
 
130
        self.assertEquals(
 
131
            'commit refs/heads/master\n'
 
132
            'mark :1\n'
 
133
            'committer Somebody <jrandom@example.com> 1330445983 +0000\n'
 
134
            'data 16\n'
 
135
            'A commit message\n'
 
136
            'M 644 inline afile\n'
 
137
            'data 11\n'
 
138
            'somecontent\n',
 
139
            f.getvalue())