1
# Copyright (C) 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Test our ability to build up test repositories"""
19
from __future__ import absolute_import
21
from io import BytesIO
23
from dulwich.repo import Repo as GitRepo
28
class TestGitBranchBuilder(tests.TestCase):
30
def test__create_blob(self):
32
builder = tests.GitBranchBuilder(stream)
33
self.assertEqual(1, builder._create_blob(b'foo\nbar\n'))
34
self.assertEqualDiff(b'blob\nmark :1\ndata 8\nfoo\nbar\n\n',
37
def test_set_file(self):
39
builder = tests.GitBranchBuilder(stream)
40
builder.set_file('foobar', b'foo\nbar\n', False)
41
self.assertEqualDiff(b'blob\nmark :1\ndata 8\nfoo\nbar\n\n',
43
self.assertEqual([b'M 100644 :1 foobar\n'], builder.commit_info)
45
def test_set_file_unicode(self):
47
builder = tests.GitBranchBuilder(stream)
48
builder.set_file(u'f\xb5/bar', b'contents\nbar\n', False)
49
self.assertEqualDiff(b'blob\nmark :1\ndata 13\ncontents\nbar\n\n',
51
self.assertEqual([b'M 100644 :1 f\xc2\xb5/bar\n'], builder.commit_info)
53
def test_set_file_newline(self):
55
builder = tests.GitBranchBuilder(stream)
56
builder.set_file(u'foo\nbar', b'contents\nbar\n', False)
57
self.assertEqualDiff(b'blob\nmark :1\ndata 13\ncontents\nbar\n\n',
59
self.assertEqual([b'M 100644 :1 "foo\\nbar"\n'], builder.commit_info)
61
def test_set_file_executable(self):
63
builder = tests.GitBranchBuilder(stream)
64
builder.set_file(u'f\xb5/bar', b'contents\nbar\n', True)
65
self.assertEqualDiff(b'blob\nmark :1\ndata 13\ncontents\nbar\n\n',
67
self.assertEqual([b'M 100755 :1 f\xc2\xb5/bar\n'], builder.commit_info)
69
def test_set_symlink(self):
71
builder = tests.GitBranchBuilder(stream)
72
builder.set_symlink(u'f\xb5/bar', b'link/contents')
73
self.assertEqualDiff(b'blob\nmark :1\ndata 13\nlink/contents\n',
75
self.assertEqual([b'M 120000 :1 f\xc2\xb5/bar\n'], builder.commit_info)
77
def test_set_symlink_newline(self):
79
builder = tests.GitBranchBuilder(stream)
80
builder.set_symlink(u'foo\nbar', 'link/contents')
81
self.assertEqualDiff(b'blob\nmark :1\ndata 13\nlink/contents\n',
83
self.assertEqual([b'M 120000 :1 "foo\\nbar"\n'], builder.commit_info)
85
def test_delete_entry(self):
87
builder = tests.GitBranchBuilder(stream)
88
builder.delete_entry(u'path/to/f\xb5')
89
self.assertEqual([b'D path/to/f\xc2\xb5\n'], builder.commit_info)
91
def test_delete_entry_newline(self):
93
builder = tests.GitBranchBuilder(stream)
94
builder.delete_entry(u'path/to/foo\nbar')
95
self.assertEqual([b'D "path/to/foo\\nbar"\n'], builder.commit_info)
97
def test_encode_path(self):
98
encode = tests.GitBranchBuilder._encode_path
99
# Unicode is encoded to utf-8
100
self.assertEqual(encode(u'f\xb5'), b'f\xc2\xb5')
101
# The name must be quoted if it starts by a double quote or contains a
103
self.assertEqual(encode(u'"foo'), b'"\\"foo"')
104
self.assertEqual(encode(u'fo\no'), b'"fo\\no"')
105
# When the name is quoted, all backslash and quote chars must be
107
self.assertEqual(encode(u'fo\\o\nbar'), b'"fo\\\\o\\nbar"')
108
self.assertEqual(encode(u'fo"o"\nbar'), b'"fo\\"o\\"\\nbar"')
109
# Other control chars, such as \r, need not be escaped.
110
self.assertEqual(encode(u'foo\r\nbar'), b'"foo\r\\nbar"')
112
def test_add_and_commit(self):
114
builder = tests.GitBranchBuilder(stream)
116
builder.set_file(u'f\xb5/bar', b'contents\nbar\n', False)
117
self.assertEqual(b'2', builder.commit(b'Joe Foo <joe@foo.com>',
118
u'committing f\xb5/bar',
119
timestamp=1194586400,
121
self.assertEqualDiff(b'blob\nmark :1\ndata 13\ncontents\nbar\n\n'
122
b'commit refs/heads/master\n'
124
b'committer Joe Foo <joe@foo.com> 1194586400 +0100\n'
126
b'committing f\xc2\xb5/bar'
128
b'M 100644 :1 f\xc2\xb5/bar\n'
132
def test_commit_base(self):
134
builder = tests.GitBranchBuilder(stream)
136
builder.set_file(u'foo', b'contents\nfoo\n', False)
137
r1 = builder.commit(b'Joe Foo <joe@foo.com>', u'first',
138
timestamp=1194586400)
139
r2 = builder.commit(b'Joe Foo <joe@foo.com>', u'second',
140
timestamp=1194586405)
141
r3 = builder.commit(b'Joe Foo <joe@foo.com>', u'third',
142
timestamp=1194586410,
145
self.assertEqualDiff(b'blob\nmark :1\ndata 13\ncontents\nfoo\n\n'
146
b'commit refs/heads/master\n'
148
b'committer Joe Foo <joe@foo.com> 1194586400 +0000\n'
154
b'commit refs/heads/master\n'
156
b'committer Joe Foo <joe@foo.com> 1194586405 +0000\n'
161
b'commit refs/heads/master\n'
163
b'committer Joe Foo <joe@foo.com> 1194586410 +0000\n'
168
b'\n', stream.getvalue())
170
def test_commit_merge(self):
172
builder = tests.GitBranchBuilder(stream)
174
builder.set_file(u'foo', b'contents\nfoo\n', False)
175
r1 = builder.commit(b'Joe Foo <joe@foo.com>', u'first',
176
timestamp=1194586400)
177
r2 = builder.commit(b'Joe Foo <joe@foo.com>', u'second',
178
timestamp=1194586405)
179
r3 = builder.commit(b'Joe Foo <joe@foo.com>', u'third',
180
timestamp=1194586410,
182
r4 = builder.commit(b'Joe Foo <joe@foo.com>', u'Merge',
183
timestamp=1194586415,
186
self.assertEqualDiff(b'blob\nmark :1\ndata 13\ncontents\nfoo\n\n'
187
b'commit refs/heads/master\n'
189
b'committer Joe Foo <joe@foo.com> 1194586400 +0000\n'
195
b'commit refs/heads/master\n'
197
b'committer Joe Foo <joe@foo.com> 1194586405 +0000\n'
202
b'commit refs/heads/master\n'
204
b'committer Joe Foo <joe@foo.com> 1194586410 +0000\n'
210
b'commit refs/heads/master\n'
212
b'committer Joe Foo <joe@foo.com> 1194586415 +0000\n'
217
b'\n', stream.getvalue())
219
def test_auto_timestamp(self):
221
builder = tests.GitBranchBuilder(stream)
222
builder.commit(b'Joe Foo <joe@foo.com>', u'message')
223
self.assertContainsRe(stream.getvalue(),
224
br'committer Joe Foo <joe@foo\.com> \d+ \+0000')
226
def test_reset(self):
228
builder = tests.GitBranchBuilder(stream)
230
self.assertEqualDiff(b'reset refs/heads/master\n\n', stream.getvalue())
232
def test_reset_named_ref(self):
234
builder = tests.GitBranchBuilder(stream)
235
builder.reset(b'refs/heads/branch')
236
self.assertEqualDiff(b'reset refs/heads/branch\n\n', stream.getvalue())
238
def test_reset_revision(self):
240
builder = tests.GitBranchBuilder(stream)
241
builder.reset(mark=b'123')
242
self.assertEqualDiff(
243
b'reset refs/heads/master\n'
245
b'\n', stream.getvalue())
248
class TestGitBranchBuilderReal(tests.TestCaseInTempDir):
250
def test_create_real_branch(self):
253
builder = tests.GitBranchBuilder()
254
builder.set_file(u'foo', b'contents\nfoo\n', False)
255
r1 = builder.commit(b'Joe Foo <joe@foo.com>', u'first',
256
timestamp=1194586400)
257
mapping = builder.finish()
258
self.assertEqual({b'1': b'44411e8e9202177dd19b6599d7a7991059fa3cb4',
259
b'2': b'b0b62e674f67306fddcf72fa888c3b56df100d64',