13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Test our ability to build up test repositories"""
19
from cStringIO import StringIO
21
from bzrlib.plugins.git import tests
19
from io import BytesIO
21
from dulwich.repo import Repo as GitRepo
24
26
class TestGitBranchBuilder(tests.TestCase):
26
28
def test__create_blob(self):
28
30
builder = tests.GitBranchBuilder(stream)
29
self.assertEqual(1, builder._create_blob('foo\nbar\n'))
30
self.assertEqualDiff('blob\nmark :1\ndata 8\nfoo\nbar\n\n',
31
self.assertEqual(1, builder._create_blob(b'foo\nbar\n'))
32
self.assertEqualDiff(b'blob\nmark :1\ndata 8\nfoo\nbar\n\n',
33
35
def test_set_file(self):
35
37
builder = tests.GitBranchBuilder(stream)
36
builder.set_file('foobar', 'foo\nbar\n', False)
37
self.assertEqualDiff('blob\nmark :1\ndata 8\nfoo\nbar\n\n',
38
builder.set_file('foobar', b'foo\nbar\n', False)
39
self.assertEqualDiff(b'blob\nmark :1\ndata 8\nfoo\nbar\n\n',
39
self.assertEqual(['M 100644 :1 foobar\n'], builder.commit_info)
41
self.assertEqual([b'M 100644 :1 foobar\n'], builder.commit_info)
41
43
def test_set_file_unicode(self):
43
45
builder = tests.GitBranchBuilder(stream)
44
builder.set_file(u'f\xb5/bar', 'contents\nbar\n', False)
45
self.assertEqualDiff('blob\nmark :1\ndata 13\ncontents\nbar\n\n',
46
builder.set_file(u'f\xb5/bar', b'contents\nbar\n', False)
47
self.assertEqualDiff(b'blob\nmark :1\ndata 13\ncontents\nbar\n\n',
47
self.assertEqual(['M 100644 :1 f\xc2\xb5/bar\n'], builder.commit_info)
49
self.assertEqual([b'M 100644 :1 f\xc2\xb5/bar\n'], builder.commit_info)
49
51
def test_set_file_newline(self):
51
53
builder = tests.GitBranchBuilder(stream)
52
builder.set_file(u'foo\nbar', 'contents\nbar\n', False)
53
self.assertEqualDiff('blob\nmark :1\ndata 13\ncontents\nbar\n\n',
54
builder.set_file(u'foo\nbar', b'contents\nbar\n', False)
55
self.assertEqualDiff(b'blob\nmark :1\ndata 13\ncontents\nbar\n\n',
55
self.assertEqual(['M 100644 :1 "foo\\nbar"\n'], builder.commit_info)
57
self.assertEqual([b'M 100644 :1 "foo\\nbar"\n'], builder.commit_info)
57
59
def test_set_file_executable(self):
59
builder = tests.GitBranchBuilder(stream)
60
builder.set_file(u'f\xb5/bar', 'contents\nbar\n', True)
61
self.assertEqualDiff('blob\nmark :1\ndata 13\ncontents\nbar\n\n',
63
self.assertEqual(['M 100755 :1 f\xc2\xb5/bar\n'], builder.commit_info)
65
def test_set_link(self):
67
builder = tests.GitBranchBuilder(stream)
68
builder.set_link(u'f\xb5/bar', 'link/contents')
69
self.assertEqualDiff('blob\nmark :1\ndata 13\nlink/contents\n',
71
self.assertEqual(['M 120000 :1 f\xc2\xb5/bar\n'], builder.commit_info)
73
def test_set_link_newline(self):
75
builder = tests.GitBranchBuilder(stream)
76
builder.set_link(u'foo\nbar', 'link/contents')
77
self.assertEqualDiff('blob\nmark :1\ndata 13\nlink/contents\n',
79
self.assertEqual(['M 120000 :1 "foo\\nbar"\n'], builder.commit_info)
61
builder = tests.GitBranchBuilder(stream)
62
builder.set_file(u'f\xb5/bar', b'contents\nbar\n', True)
63
self.assertEqualDiff(b'blob\nmark :1\ndata 13\ncontents\nbar\n\n',
65
self.assertEqual([b'M 100755 :1 f\xc2\xb5/bar\n'], builder.commit_info)
67
def test_set_symlink(self):
69
builder = tests.GitBranchBuilder(stream)
70
builder.set_symlink(u'f\xb5/bar', b'link/contents')
71
self.assertEqualDiff(b'blob\nmark :1\ndata 13\nlink/contents\n',
73
self.assertEqual([b'M 120000 :1 f\xc2\xb5/bar\n'], builder.commit_info)
75
def test_set_symlink_newline(self):
77
builder = tests.GitBranchBuilder(stream)
78
builder.set_symlink(u'foo\nbar', 'link/contents')
79
self.assertEqualDiff(b'blob\nmark :1\ndata 13\nlink/contents\n',
81
self.assertEqual([b'M 120000 :1 "foo\\nbar"\n'], builder.commit_info)
81
83
def test_delete_entry(self):
83
85
builder = tests.GitBranchBuilder(stream)
84
86
builder.delete_entry(u'path/to/f\xb5')
85
self.assertEqual(['D path/to/f\xc2\xb5\n'], builder.commit_info)
87
self.assertEqual([b'D path/to/f\xc2\xb5\n'], builder.commit_info)
87
89
def test_delete_entry_newline(self):
89
91
builder = tests.GitBranchBuilder(stream)
90
92
builder.delete_entry(u'path/to/foo\nbar')
91
self.assertEqual(['D "path/to/foo\\nbar"\n'], builder.commit_info)
93
self.assertEqual([b'D "path/to/foo\\nbar"\n'], builder.commit_info)
93
95
def test_encode_path(self):
94
96
encode = tests.GitBranchBuilder._encode_path
95
97
# Unicode is encoded to utf-8
96
self.assertEqual(encode(u'f\xb5'), 'f\xc2\xb5')
98
self.assertEqual(encode(u'f\xb5'), b'f\xc2\xb5')
97
99
# The name must be quoted if it starts by a double quote or contains a
99
self.assertEqual(encode(u'"foo'), '"\\"foo"')
100
self.assertEqual(encode(u'fo\no'), '"fo\\no"')
101
self.assertEqual(encode(u'"foo'), b'"\\"foo"')
102
self.assertEqual(encode(u'fo\no'), b'"fo\\no"')
101
103
# When the name is quoted, all backslash and quote chars must be
103
self.assertEqual(encode(u'fo\\o\nbar'), '"fo\\\\o\\nbar"')
104
self.assertEqual(encode(u'fo"o"\nbar'), '"fo\\"o\\"\\nbar"')
105
self.assertEqual(encode(u'fo\\o\nbar'), b'"fo\\\\o\\nbar"')
106
self.assertEqual(encode(u'fo"o"\nbar'), b'"fo\\"o\\"\\nbar"')
105
107
# Other control chars, such as \r, need not be escaped.
106
self.assertEqual(encode(u'foo\r\nbar'), '"foo\r\\nbar"')
108
self.assertEqual(encode(u'foo\r\nbar'), b'"foo\r\\nbar"')
108
110
def test_add_and_commit(self):
110
112
builder = tests.GitBranchBuilder(stream)
112
builder.set_file(u'f\xb5/bar', 'contents\nbar\n', False)
113
self.assertEqual(2, builder.commit('Joe Foo <joe@foo.com>',
114
u'committing f\xb5/bar',
115
timestamp=1194586400,
117
self.assertEqualDiff('blob\nmark :1\ndata 13\ncontents\nbar\n\n'
118
'commit refs/heads/master\n'
120
'committer Joe Foo <joe@foo.com> 1194586400 +0100\n'
122
'committing f\xc2\xb5/bar'
124
'M 100644 :1 f\xc2\xb5/bar\n'
114
builder.set_file(u'f\xb5/bar', b'contents\nbar\n', False)
115
self.assertEqual(b'2', builder.commit(b'Joe Foo <joe@foo.com>',
116
u'committing f\xb5/bar',
117
timestamp=1194586400,
119
self.assertEqualDiff(b'blob\nmark :1\ndata 13\ncontents\nbar\n\n'
120
b'commit refs/heads/master\n'
122
b'committer Joe Foo <joe@foo.com> 1194586400 +0100\n'
124
b'committing f\xc2\xb5/bar'
126
b'M 100644 :1 f\xc2\xb5/bar\n'
126
128
stream.getvalue())
128
130
def test_commit_base(self):
130
132
builder = tests.GitBranchBuilder(stream)
132
builder.set_file(u'foo', 'contents\nfoo\n', False)
133
r1 = builder.commit('Joe Foo <joe@foo.com>', u'first',
134
builder.set_file(u'foo', b'contents\nfoo\n', False)
135
r1 = builder.commit(b'Joe Foo <joe@foo.com>', u'first',
134
136
timestamp=1194586400)
135
r2 = builder.commit('Joe Foo <joe@foo.com>', u'second',
137
r2 = builder.commit(b'Joe Foo <joe@foo.com>', u'second',
136
138
timestamp=1194586405)
137
r3 = builder.commit('Joe Foo <joe@foo.com>', u'third',
139
r3 = builder.commit(b'Joe Foo <joe@foo.com>', u'third',
138
140
timestamp=1194586410,
141
self.assertEqualDiff('blob\nmark :1\ndata 13\ncontents\nfoo\n\n'
142
'commit refs/heads/master\n'
144
'committer Joe Foo <joe@foo.com> 1194586400 +0000\n'
150
'commit refs/heads/master\n'
152
'committer Joe Foo <joe@foo.com> 1194586405 +0000\n'
157
'commit refs/heads/master\n'
159
'committer Joe Foo <joe@foo.com> 1194586410 +0000\n'
164
'\n', stream.getvalue())
143
self.assertEqualDiff(b'blob\nmark :1\ndata 13\ncontents\nfoo\n\n'
144
b'commit refs/heads/master\n'
146
b'committer Joe Foo <joe@foo.com> 1194586400 +0000\n'
152
b'commit refs/heads/master\n'
154
b'committer Joe Foo <joe@foo.com> 1194586405 +0000\n'
159
b'commit refs/heads/master\n'
161
b'committer Joe Foo <joe@foo.com> 1194586410 +0000\n'
166
b'\n', stream.getvalue())
166
168
def test_commit_merge(self):
168
170
builder = tests.GitBranchBuilder(stream)
170
builder.set_file(u'foo', 'contents\nfoo\n', False)
171
r1 = builder.commit('Joe Foo <joe@foo.com>', u'first',
172
builder.set_file(u'foo', b'contents\nfoo\n', False)
173
r1 = builder.commit(b'Joe Foo <joe@foo.com>', u'first',
172
174
timestamp=1194586400)
173
r2 = builder.commit('Joe Foo <joe@foo.com>', u'second',
175
r2 = builder.commit(b'Joe Foo <joe@foo.com>', u'second',
174
176
timestamp=1194586405)
175
r3 = builder.commit('Joe Foo <joe@foo.com>', u'third',
177
r3 = builder.commit(b'Joe Foo <joe@foo.com>', u'third',
176
178
timestamp=1194586410,
178
r4 = builder.commit('Joe Foo <joe@foo.com>', u'Merge',
180
r4 = builder.commit(b'Joe Foo <joe@foo.com>', u'Merge',
179
181
timestamp=1194586415,
182
self.assertEqualDiff('blob\nmark :1\ndata 13\ncontents\nfoo\n\n'
183
'commit refs/heads/master\n'
185
'committer Joe Foo <joe@foo.com> 1194586400 +0000\n'
191
'commit refs/heads/master\n'
193
'committer Joe Foo <joe@foo.com> 1194586405 +0000\n'
198
'commit refs/heads/master\n'
200
'committer Joe Foo <joe@foo.com> 1194586410 +0000\n'
206
'commit refs/heads/master\n'
208
'committer Joe Foo <joe@foo.com> 1194586415 +0000\n'
213
'\n', stream.getvalue())
184
self.assertEqualDiff(b'blob\nmark :1\ndata 13\ncontents\nfoo\n\n'
185
b'commit refs/heads/master\n'
187
b'committer Joe Foo <joe@foo.com> 1194586400 +0000\n'
193
b'commit refs/heads/master\n'
195
b'committer Joe Foo <joe@foo.com> 1194586405 +0000\n'
200
b'commit refs/heads/master\n'
202
b'committer Joe Foo <joe@foo.com> 1194586410 +0000\n'
208
b'commit refs/heads/master\n'
210
b'committer Joe Foo <joe@foo.com> 1194586415 +0000\n'
215
b'\n', stream.getvalue())
215
217
def test_auto_timestamp(self):
217
219
builder = tests.GitBranchBuilder(stream)
218
builder.commit('Joe Foo <joe@foo.com>', u'message')
220
builder.commit(b'Joe Foo <joe@foo.com>', u'message')
219
221
self.assertContainsRe(stream.getvalue(),
220
r'committer Joe Foo <joe@foo\.com> \d+ \+0000')
222
br'committer Joe Foo <joe@foo\.com> \d+ \+0000')
222
224
def test_reset(self):
224
226
builder = tests.GitBranchBuilder(stream)
226
self.assertEqualDiff('reset refs/heads/master\n\n', stream.getvalue())
228
self.assertEqualDiff(b'reset refs/heads/master\n\n', stream.getvalue())
228
230
def test_reset_named_ref(self):
230
232
builder = tests.GitBranchBuilder(stream)
231
builder.reset('refs/heads/branch')
232
self.assertEqualDiff('reset refs/heads/branch\n\n', stream.getvalue())
233
builder.reset(b'refs/heads/branch')
234
self.assertEqualDiff(b'reset refs/heads/branch\n\n', stream.getvalue())
234
236
def test_reset_revision(self):
236
238
builder = tests.GitBranchBuilder(stream)
237
builder.reset(mark=123)
239
builder.reset(mark=b'123')
238
240
self.assertEqualDiff(
239
'reset refs/heads/master\n'
241
'\n', stream.getvalue())
241
b'reset refs/heads/master\n'
243
b'\n', stream.getvalue())
244
246
class TestGitBranchBuilderReal(tests.TestCaseInTempDir):
246
248
def test_create_real_branch(self):
247
tests.run_git('init')
249
251
builder = tests.GitBranchBuilder()
250
builder.set_file(u'foo', 'contents\nfoo\n', False)
251
r1 = builder.commit('Joe Foo <joe@foo.com>', u'first',
252
builder.set_file(u'foo', b'contents\nfoo\n', False)
253
r1 = builder.commit(b'Joe Foo <joe@foo.com>', u'first',
252
254
timestamp=1194586400)
253
255
mapping = builder.finish()
254
self.assertEqual({1:'44411e8e9202177dd19b6599d7a7991059fa3cb4',
255
2: 'b0b62e674f67306fddcf72fa888c3b56df100d64',
256
self.assertEqual({b'1': b'44411e8e9202177dd19b6599d7a7991059fa3cb4',
257
b'2': b'b0b62e674f67306fddcf72fa888c3b56df100d64',