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

Try to import nothing other than __init__ when not opening git repositories.

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Test our ability to build up test repositories"""
 
18
 
 
19
from cStringIO import StringIO
 
20
 
 
21
from bzrlib.plugins.git import tests
 
22
 
 
23
 
 
24
class TestGitBranchBuilder(tests.TestCase):
 
25
 
 
26
    def test__create_blob(self):
 
27
        stream = StringIO()
 
28
        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
                             stream.getvalue())
 
32
 
 
33
    def test_set_file(self):
 
34
        stream = StringIO()
 
35
        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
                             stream.getvalue())
 
39
        self.assertEqual(['M 100644 :1 foobar\n'], builder.commit_info)
 
40
 
 
41
    def test_set_file_unicode(self):
 
42
        stream = StringIO()
 
43
        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
                             stream.getvalue())
 
47
        self.assertEqual(['M 100644 :1 f\xc2\xb5/bar\n'], builder.commit_info)
 
48
 
 
49
    def test_set_file_newline(self):
 
50
        stream = StringIO()
 
51
        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
                             stream.getvalue())
 
55
        self.assertEqual(['M 100644 :1 "foo\\nbar"\n'], builder.commit_info)
 
56
 
 
57
    def test_set_file_executable(self):
 
58
        stream = StringIO()
 
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',
 
62
                             stream.getvalue())
 
63
        self.assertEqual(['M 100755 :1 f\xc2\xb5/bar\n'], builder.commit_info)
 
64
 
 
65
    def test_set_link(self):
 
66
        stream = StringIO()
 
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',
 
70
                             stream.getvalue())
 
71
        self.assertEqual(['M 120000 :1 f\xc2\xb5/bar\n'], builder.commit_info)
 
72
 
 
73
    def test_set_link_newline(self):
 
74
        stream = StringIO()
 
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',
 
78
                             stream.getvalue())
 
79
        self.assertEqual(['M 120000 :1 "foo\\nbar"\n'], builder.commit_info)
 
80
 
 
81
    def test_delete_entry(self):
 
82
        stream = StringIO()
 
83
        builder = tests.GitBranchBuilder(stream)
 
84
        builder.delete_entry(u'path/to/f\xb5')
 
85
        self.assertEqual(['D path/to/f\xc2\xb5\n'], builder.commit_info)
 
86
 
 
87
    def test_delete_entry_newline(self):
 
88
        stream = StringIO()
 
89
        builder = tests.GitBranchBuilder(stream)
 
90
        builder.delete_entry(u'path/to/foo\nbar')
 
91
        self.assertEqual(['D "path/to/foo\\nbar"\n'], builder.commit_info)
 
92
 
 
93
    def test_encode_path(self):
 
94
        encode = tests.GitBranchBuilder._encode_path
 
95
        # Unicode is encoded to utf-8
 
96
        self.assertEqual(encode(u'f\xb5'), 'f\xc2\xb5')
 
97
        # The name must be quoted if it starts by a double quote or contains a
 
98
        # newline.
 
99
        self.assertEqual(encode(u'"foo'), '"\\"foo"')
 
100
        self.assertEqual(encode(u'fo\no'), '"fo\\no"')
 
101
        # When the name is quoted, all backslash and quote chars must be
 
102
        # escaped.
 
103
        self.assertEqual(encode(u'fo\\o\nbar'), '"fo\\\\o\\nbar"')
 
104
        self.assertEqual(encode(u'fo"o"\nbar'), '"fo\\"o\\"\\nbar"')
 
105
        # Other control chars, such as \r, need not be escaped.
 
106
        self.assertEqual(encode(u'foo\r\nbar'), '"foo\r\\nbar"')
 
107
 
 
108
    def test_add_and_commit(self):
 
109
        stream = StringIO()
 
110
        builder = tests.GitBranchBuilder(stream)
 
111
 
 
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,
 
116
                                           timezone='+0100'))
 
117
        self.assertEqualDiff('blob\nmark :1\ndata 13\ncontents\nbar\n\n'
 
118
                             'commit refs/heads/master\n'
 
119
                             'mark :2\n'
 
120
                             'committer Joe Foo <joe@foo.com> 1194586400 +0100\n'
 
121
                             'data 18\n'
 
122
                             'committing f\xc2\xb5/bar'
 
123
                             '\n'
 
124
                             'M 100644 :1 f\xc2\xb5/bar\n'
 
125
                             '\n',
 
126
                             stream.getvalue())
 
127
 
 
128
    def test_commit_base(self):
 
129
        stream = StringIO()
 
130
        builder = tests.GitBranchBuilder(stream)
 
131
 
 
132
        builder.set_file(u'foo', 'contents\nfoo\n', False)
 
133
        r1 = builder.commit('Joe Foo <joe@foo.com>', u'first',
 
134
                            timestamp=1194586400)
 
135
        r2 = builder.commit('Joe Foo <joe@foo.com>', u'second',
 
136
                            timestamp=1194586405)
 
137
        r3 = builder.commit('Joe Foo <joe@foo.com>', u'third',
 
138
                            timestamp=1194586410,
 
139
                            base=r1)
 
140
 
 
141
        self.assertEqualDiff('blob\nmark :1\ndata 13\ncontents\nfoo\n\n'
 
142
                             'commit refs/heads/master\n'
 
143
                             'mark :2\n'
 
144
                             'committer Joe Foo <joe@foo.com> 1194586400 +0000\n'
 
145
                             'data 5\n'
 
146
                             'first'
 
147
                             '\n'
 
148
                             'M 100644 :1 foo\n'
 
149
                             '\n'
 
150
                             'commit refs/heads/master\n'
 
151
                             'mark :3\n'
 
152
                             'committer Joe Foo <joe@foo.com> 1194586405 +0000\n'
 
153
                             'data 6\n'
 
154
                             'second'
 
155
                             '\n'
 
156
                             '\n'
 
157
                             'commit refs/heads/master\n'
 
158
                             'mark :4\n'
 
159
                             'committer Joe Foo <joe@foo.com> 1194586410 +0000\n'
 
160
                             'data 5\n'
 
161
                             'third'
 
162
                             '\n'
 
163
                             'from :2\n'
 
164
                             '\n', stream.getvalue())
 
165
 
 
166
    def test_commit_merge(self):
 
167
        stream = StringIO()
 
168
        builder = tests.GitBranchBuilder(stream)
 
169
 
 
170
        builder.set_file(u'foo', 'contents\nfoo\n', False)
 
171
        r1 = builder.commit('Joe Foo <joe@foo.com>', u'first',
 
172
                            timestamp=1194586400)
 
173
        r2 = builder.commit('Joe Foo <joe@foo.com>', u'second',
 
174
                            timestamp=1194586405)
 
175
        r3 = builder.commit('Joe Foo <joe@foo.com>', u'third',
 
176
                            timestamp=1194586410,
 
177
                            base=r1)
 
178
        r4 = builder.commit('Joe Foo <joe@foo.com>', u'Merge',
 
179
                            timestamp=1194586415,
 
180
                            merge=[r2])
 
181
 
 
182
        self.assertEqualDiff('blob\nmark :1\ndata 13\ncontents\nfoo\n\n'
 
183
                             'commit refs/heads/master\n'
 
184
                             'mark :2\n'
 
185
                             'committer Joe Foo <joe@foo.com> 1194586400 +0000\n'
 
186
                             'data 5\n'
 
187
                             'first'
 
188
                             '\n'
 
189
                             'M 100644 :1 foo\n'
 
190
                             '\n'
 
191
                             'commit refs/heads/master\n'
 
192
                             'mark :3\n'
 
193
                             'committer Joe Foo <joe@foo.com> 1194586405 +0000\n'
 
194
                             'data 6\n'
 
195
                             'second'
 
196
                             '\n'
 
197
                             '\n'
 
198
                             'commit refs/heads/master\n'
 
199
                             'mark :4\n'
 
200
                             'committer Joe Foo <joe@foo.com> 1194586410 +0000\n'
 
201
                             'data 5\n'
 
202
                             'third'
 
203
                             '\n'
 
204
                             'from :2\n'
 
205
                             '\n'
 
206
                             'commit refs/heads/master\n'
 
207
                             'mark :5\n'
 
208
                             'committer Joe Foo <joe@foo.com> 1194586415 +0000\n'
 
209
                             'data 5\n'
 
210
                             'Merge'
 
211
                             '\n'
 
212
                             'merge :3\n'
 
213
                             '\n', stream.getvalue())
 
214
 
 
215
    def test_auto_timestamp(self):
 
216
        stream = StringIO()
 
217
        builder = tests.GitBranchBuilder(stream)
 
218
        builder.commit('Joe Foo <joe@foo.com>', u'message')
 
219
        self.assertContainsRe(stream.getvalue(),
 
220
                              r'committer Joe Foo <joe@foo\.com> \d+ \+0000')
 
221
 
 
222
    def test_reset(self):
 
223
        stream = StringIO()
 
224
        builder = tests.GitBranchBuilder(stream)
 
225
        builder.reset()
 
226
        self.assertEqualDiff('reset refs/heads/master\n\n', stream.getvalue())
 
227
 
 
228
    def test_reset_named_ref(self):
 
229
        stream = StringIO()
 
230
        builder = tests.GitBranchBuilder(stream)
 
231
        builder.reset('refs/heads/branch')
 
232
        self.assertEqualDiff('reset refs/heads/branch\n\n', stream.getvalue())
 
233
 
 
234
    def test_reset_revision(self):
 
235
        stream = StringIO()
 
236
        builder = tests.GitBranchBuilder(stream)
 
237
        builder.reset(mark=123)
 
238
        self.assertEqualDiff(
 
239
            'reset refs/heads/master\n'
 
240
            'from :123\n'
 
241
            '\n', stream.getvalue())
 
242
 
 
243
 
 
244
class TestGitBranchBuilderReal(tests.TestCaseInTempDir):
 
245
 
 
246
    def test_create_real_branch(self):
 
247
        tests.run_git('init')
 
248
 
 
249
        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
                            timestamp=1194586400)
 
253
        mapping = builder.finish()
 
254
        self.assertEqual({1:'44411e8e9202177dd19b6599d7a7991059fa3cb4',
 
255
                          2: 'b0b62e674f67306fddcf72fa888c3b56df100d64',
 
256
                         }, mapping)