/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 breezy/git/tests/test_builder.py

  • Committer: Jelmer Vernooij
  • Date: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""Test our ability to build up test repositories"""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
from io import BytesIO
 
22
 
 
23
from dulwich.repo import Repo as GitRepo
 
24
 
 
25
from .. import tests
 
26
 
 
27
 
 
28
class TestGitBranchBuilder(tests.TestCase):
 
29
 
 
30
    def test__create_blob(self):
 
31
        stream = BytesIO()
 
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',
 
35
                             stream.getvalue())
 
36
 
 
37
    def test_set_file(self):
 
38
        stream = BytesIO()
 
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',
 
42
                             stream.getvalue())
 
43
        self.assertEqual([b'M 100644 :1 foobar\n'], builder.commit_info)
 
44
 
 
45
    def test_set_file_unicode(self):
 
46
        stream = BytesIO()
 
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',
 
50
                             stream.getvalue())
 
51
        self.assertEqual([b'M 100644 :1 f\xc2\xb5/bar\n'], builder.commit_info)
 
52
 
 
53
    def test_set_file_newline(self):
 
54
        stream = BytesIO()
 
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',
 
58
                             stream.getvalue())
 
59
        self.assertEqual([b'M 100644 :1 "foo\\nbar"\n'], builder.commit_info)
 
60
 
 
61
    def test_set_file_executable(self):
 
62
        stream = BytesIO()
 
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',
 
66
                             stream.getvalue())
 
67
        self.assertEqual([b'M 100755 :1 f\xc2\xb5/bar\n'], builder.commit_info)
 
68
 
 
69
    def test_set_symlink(self):
 
70
        stream = BytesIO()
 
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',
 
74
                             stream.getvalue())
 
75
        self.assertEqual([b'M 120000 :1 f\xc2\xb5/bar\n'], builder.commit_info)
 
76
 
 
77
    def test_set_symlink_newline(self):
 
78
        stream = BytesIO()
 
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',
 
82
                             stream.getvalue())
 
83
        self.assertEqual([b'M 120000 :1 "foo\\nbar"\n'], builder.commit_info)
 
84
 
 
85
    def test_delete_entry(self):
 
86
        stream = BytesIO()
 
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)
 
90
 
 
91
    def test_delete_entry_newline(self):
 
92
        stream = BytesIO()
 
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)
 
96
 
 
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
 
102
        # newline.
 
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
 
106
        # escaped.
 
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"')
 
111
 
 
112
    def test_add_and_commit(self):
 
113
        stream = BytesIO()
 
114
        builder = tests.GitBranchBuilder(stream)
 
115
 
 
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,
 
120
                                              timezone=b'+0100'))
 
121
        self.assertEqualDiff(b'blob\nmark :1\ndata 13\ncontents\nbar\n\n'
 
122
                             b'commit refs/heads/master\n'
 
123
                             b'mark :2\n'
 
124
                             b'committer Joe Foo <joe@foo.com> 1194586400 +0100\n'
 
125
                             b'data 18\n'
 
126
                             b'committing f\xc2\xb5/bar'
 
127
                             b'\n'
 
128
                             b'M 100644 :1 f\xc2\xb5/bar\n'
 
129
                             b'\n',
 
130
                             stream.getvalue())
 
131
 
 
132
    def test_commit_base(self):
 
133
        stream = BytesIO()
 
134
        builder = tests.GitBranchBuilder(stream)
 
135
 
 
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,
 
143
                            base=r1)
 
144
 
 
145
        self.assertEqualDiff(b'blob\nmark :1\ndata 13\ncontents\nfoo\n\n'
 
146
                             b'commit refs/heads/master\n'
 
147
                             b'mark :2\n'
 
148
                             b'committer Joe Foo <joe@foo.com> 1194586400 +0000\n'
 
149
                             b'data 5\n'
 
150
                             b'first'
 
151
                             b'\n'
 
152
                             b'M 100644 :1 foo\n'
 
153
                             b'\n'
 
154
                             b'commit refs/heads/master\n'
 
155
                             b'mark :3\n'
 
156
                             b'committer Joe Foo <joe@foo.com> 1194586405 +0000\n'
 
157
                             b'data 6\n'
 
158
                             b'second'
 
159
                             b'\n'
 
160
                             b'\n'
 
161
                             b'commit refs/heads/master\n'
 
162
                             b'mark :4\n'
 
163
                             b'committer Joe Foo <joe@foo.com> 1194586410 +0000\n'
 
164
                             b'data 5\n'
 
165
                             b'third'
 
166
                             b'\n'
 
167
                             b'from :2\n'
 
168
                             b'\n', stream.getvalue())
 
169
 
 
170
    def test_commit_merge(self):
 
171
        stream = BytesIO()
 
172
        builder = tests.GitBranchBuilder(stream)
 
173
 
 
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,
 
181
                            base=r1)
 
182
        r4 = builder.commit(b'Joe Foo <joe@foo.com>', u'Merge',
 
183
                            timestamp=1194586415,
 
184
                            merge=[r2])
 
185
 
 
186
        self.assertEqualDiff(b'blob\nmark :1\ndata 13\ncontents\nfoo\n\n'
 
187
                             b'commit refs/heads/master\n'
 
188
                             b'mark :2\n'
 
189
                             b'committer Joe Foo <joe@foo.com> 1194586400 +0000\n'
 
190
                             b'data 5\n'
 
191
                             b'first'
 
192
                             b'\n'
 
193
                             b'M 100644 :1 foo\n'
 
194
                             b'\n'
 
195
                             b'commit refs/heads/master\n'
 
196
                             b'mark :3\n'
 
197
                             b'committer Joe Foo <joe@foo.com> 1194586405 +0000\n'
 
198
                             b'data 6\n'
 
199
                             b'second'
 
200
                             b'\n'
 
201
                             b'\n'
 
202
                             b'commit refs/heads/master\n'
 
203
                             b'mark :4\n'
 
204
                             b'committer Joe Foo <joe@foo.com> 1194586410 +0000\n'
 
205
                             b'data 5\n'
 
206
                             b'third'
 
207
                             b'\n'
 
208
                             b'from :2\n'
 
209
                             b'\n'
 
210
                             b'commit refs/heads/master\n'
 
211
                             b'mark :5\n'
 
212
                             b'committer Joe Foo <joe@foo.com> 1194586415 +0000\n'
 
213
                             b'data 5\n'
 
214
                             b'Merge'
 
215
                             b'\n'
 
216
                             b'merge :3\n'
 
217
                             b'\n', stream.getvalue())
 
218
 
 
219
    def test_auto_timestamp(self):
 
220
        stream = BytesIO()
 
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')
 
225
 
 
226
    def test_reset(self):
 
227
        stream = BytesIO()
 
228
        builder = tests.GitBranchBuilder(stream)
 
229
        builder.reset()
 
230
        self.assertEqualDiff(b'reset refs/heads/master\n\n', stream.getvalue())
 
231
 
 
232
    def test_reset_named_ref(self):
 
233
        stream = BytesIO()
 
234
        builder = tests.GitBranchBuilder(stream)
 
235
        builder.reset(b'refs/heads/branch')
 
236
        self.assertEqualDiff(b'reset refs/heads/branch\n\n', stream.getvalue())
 
237
 
 
238
    def test_reset_revision(self):
 
239
        stream = BytesIO()
 
240
        builder = tests.GitBranchBuilder(stream)
 
241
        builder.reset(mark=b'123')
 
242
        self.assertEqualDiff(
 
243
            b'reset refs/heads/master\n'
 
244
            b'from :123\n'
 
245
            b'\n', stream.getvalue())
 
246
 
 
247
 
 
248
class TestGitBranchBuilderReal(tests.TestCaseInTempDir):
 
249
 
 
250
    def test_create_real_branch(self):
 
251
        GitRepo.init(".")
 
252
 
 
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',
 
260
                          }, mapping)