1
# Copyright (C) 2007 Jelmer Vernooij <jelmer@samba.org>
2
# -*- encoding: utf-8 -*-
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from bzrlib.inventory import (
23
from dulwich.objects import (
29
from bzrlib.plugins.git import tests
30
from bzrlib.plugins.git.mapping import (
38
class TestRevidConversionV1(tests.TestCase):
40
def test_simple_git_to_bzr_revision_id(self):
41
self.assertEqual("git-v1:"
42
"c6a4d8f1fa4ac650748e647c4b1b368f589a7356",
43
BzrGitMappingv1().revision_id_foreign_to_bzr(
44
"c6a4d8f1fa4ac650748e647c4b1b368f589a7356"))
46
def test_simple_bzr_to_git_revision_id(self):
47
self.assertEqual(("c6a4d8f1fa4ac650748e647c4b1b368f589a7356",
49
BzrGitMappingv1().revision_id_bzr_to_foreign(
51
"c6a4d8f1fa4ac650748e647c4b1b368f589a7356"))
54
class FileidTests(tests.TestCase):
56
def test_escape_space(self):
57
self.assertEquals("bla_s", escape_file_id("bla "))
59
def test_escape_underscore(self):
60
self.assertEquals("bla__", escape_file_id("bla_"))
62
def test_escape_underscore_space(self):
63
self.assertEquals("bla___s", escape_file_id("bla_ "))
65
def test_unescape_underscore(self):
66
self.assertEquals("bla ", unescape_file_id("bla_s"))
68
def test_unescape_underscore_space(self):
69
self.assertEquals("bla _", unescape_file_id("bla_s__"))
72
class TestImportCommit(tests.TestCase):
74
def test_commit(self):
76
c.tree = "cc9462f7f8263ef5adfbeff2fb936bb36b504cba"
77
c.message = "Some message"
78
c.committer = "Committer"
81
c.commit_timezone = 60 * 5
82
c.author_timezone = 60 * 3
84
rev = BzrGitMappingv1().import_commit(c)
85
self.assertEquals("Some message", rev.message)
86
self.assertEquals("Committer", rev.committer)
87
self.assertEquals("Author", rev.properties['author'])
88
self.assertEquals(300, rev.timezone)
89
self.assertEquals((), rev.parent_ids)
90
self.assertEquals("5", rev.properties['author-timestamp'])
91
self.assertEquals("180", rev.properties['author-timezone'])
92
self.assertEquals("git-v1:" + c.id, rev.revision_id)
94
def test_explicit_encoding(self):
96
c.tree = "cc9462f7f8263ef5adfbeff2fb936bb36b504cba"
97
c.message = "Some message"
98
c.committer = "Committer"
101
c.commit_timezone = 60 * 5
102
c.author_timezone = 60 * 3
103
c.author = u"Authér".encode("iso8859-1")
104
c.encoding = "iso8859-1"
105
rev = BzrGitMappingv1().import_commit(c)
106
self.assertEquals(u"Authér", rev.properties['author'])
107
self.assertEquals("iso8859-1", rev.properties["git-explicit-encoding"])
108
self.assertTrue("git-implicit-encoding" not in rev.properties)
110
def test_implicit_encoding_fallback(self):
112
c.tree = "cc9462f7f8263ef5adfbeff2fb936bb36b504cba"
113
c.message = "Some message"
114
c.committer = "Committer"
117
c.commit_timezone = 60 * 5
118
c.author_timezone = 60 * 3
119
c.author = u"Authér".encode("latin1")
120
rev = BzrGitMappingv1().import_commit(c)
121
self.assertEquals(u"Authér", rev.properties['author'])
122
self.assertEquals("latin1", rev.properties["git-implicit-encoding"])
123
self.assertTrue("git-explicit-encoding" not in rev.properties)
125
def test_implicit_encoding_utf8(self):
127
c.tree = "cc9462f7f8263ef5adfbeff2fb936bb36b504cba"
128
c.message = "Some message"
129
c.committer = "Committer"
132
c.commit_timezone = 60 * 5
133
c.author_timezone = 60 * 3
134
c.author = u"Authér".encode("utf-8")
135
rev = BzrGitMappingv1().import_commit(c)
136
self.assertEquals(u"Authér", rev.properties['author'])
137
self.assertTrue("git-explicit-encoding" not in rev.properties)
138
self.assertTrue("git-implicit-encoding" not in rev.properties)
141
class RoundtripRevisionsFromGit(tests.TestCase):
144
super(RoundtripRevisionsFromGit, self).setUp()
145
self.mapping = BzrGitMappingv1()
147
def assertRoundtripTree(self, tree):
148
raise NotImplementedError(self.assertRoundtripTree)
150
def assertRoundtripBlob(self, blob):
151
raise NotImplementedError(self.assertRoundtripBlob)
153
def assertRoundtripCommit(self, commit1):
154
rev = self.mapping.import_commit(commit1)
155
commit2 = self.mapping.export_commit(rev, "12341212121212", None)
156
self.assertEquals(commit1.committer, commit2.committer)
157
self.assertEquals(commit1.commit_time, commit2.commit_time)
158
self.assertEquals(commit1.commit_timezone, commit2.commit_timezone)
159
self.assertEquals(commit1.author, commit2.author)
160
self.assertEquals(commit1.author_time, commit2.author_time)
161
self.assertEquals(commit1.author_timezone, commit2.author_timezone)
162
self.assertEquals(commit1.message, commit2.message)
163
self.assertEquals(commit1.encoding, commit2.encoding)
165
def test_commit(self):
167
c.tree = "cc9462f7f8263ef5adfbeff2fb936bb36b504cba"
168
c.message = "Some message"
169
c.committer = "Committer <Committer>"
171
c.commit_timezone = -60 * 3
173
c.author_timezone = 60 * 2
174
c.author = "Author <author>"
175
self.assertRoundtripCommit(c)
177
def test_commit_zero_utc_timezone(self):
179
c.tree = "cc9462f7f8263ef5adfbeff2fb936bb36b504cba"
180
c.message = "Some message"
181
c.committer = "Committer <Committer>"
183
c.commit_timezone = 0
184
c._commit_timezone_neg_utc = True
186
c.author_timezone = 60 * 2
187
c.author = "Author <author>"
188
self.assertRoundtripCommit(c)
190
def test_commit_encoding(self):
192
c.tree = "cc9462f7f8263ef5adfbeff2fb936bb36b504cba"
193
c.message = "Some message"
194
c.committer = "Committer <Committer>"
195
c.encoding = 'iso8859-1'
197
c.commit_timezone = -60 * 3
199
c.author_timezone = 60 * 2
200
c.author = "Author <author>"
201
self.assertRoundtripCommit(c)
204
class DirectoryToTreeTests(tests.TestCase):
206
def test_empty(self):
207
ie = InventoryDirectory('foo', 'foo', 'foo')
208
t = directory_to_tree(ie, None, {})
209
self.assertEquals(None, t)
211
def test_empty_dir(self):
212
ie = InventoryDirectory('foo', 'foo', 'foo')
213
child_ie = InventoryDirectory('bar', 'bar', 'bar')
214
ie.children['bar'] = child_ie
215
t = directory_to_tree(ie, lambda x: None, {})
216
self.assertEquals(None, t)
218
def test_empty_root(self):
219
ie = InventoryDirectory('foo', 'foo', None)
220
child_ie = InventoryDirectory('bar', 'bar', 'bar')
221
ie.children['bar'] = child_ie
222
t = directory_to_tree(ie, lambda x: None, {})
223
self.assertEquals(Tree(), t)
225
def test_with_file(self):
226
ie = InventoryDirectory('foo', 'foo', 'foo')
227
child_ie = InventoryFile('bar', 'bar', 'bar')
228
ie.children['bar'] = child_ie
229
b = Blob.from_string("bla")
230
t1 = directory_to_tree(ie, lambda x: b.id, {})
232
t2.add(0100644, "bar", b.id)
233
self.assertEquals(t1, t2)