4
# Copyright (C) 2011-2018 Jelmer Vernooij <jelmer@jelmer.uk>
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.
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.
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
20
"""Tests for the git remote helper."""
22
from __future__ import absolute_import
24
from cStringIO import StringIO
27
from dulwich.repo import Repo
29
from ....tests import (
30
TestCaseWithTransport,
34
from ..object_store import get_object_store
35
from ..git_remote_helper import (
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)
49
class OpenLocalDirTests(TestCaseWithTransport):
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'))
56
def test_from_dir(self):
57
self.make_branch_and_tree('.', format='git')
61
class FetchTests(TestCaseWithTransport):
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'
70
def fetch(self, wants):
72
fetch(outf, wants, self.shortname, self.remote_dir, self.local_dir)
73
return outf.getvalue()
75
def test_no_wants(self):
77
self.assertEquals("\n", r)
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")
87
self.assertTrue(git_sha1 in r.object_store)
92
class RemoteHelperTests(TestCaseWithTransport):
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)
102
def test_capabilities(self):
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)
109
def test_option(self):
111
self.helper.cmd_option(f, [])
112
self.assertEquals("unsupported\n", f.getvalue())
114
def test_list_basic(self):
116
self.helper.cmd_list(f, [])
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>')
129
self.helper.cmd_import(f, ["import", "refs/heads/master"])
131
'commit refs/heads/master\n'
133
'committer Somebody <jrandom@example.com> 1330445983 +0000\n'
136
'M 644 inline afile\n'