/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-05-24 00:39:50 UTC
  • mto: This revision was merged to the branch mainline in revision 7504.
  • Revision ID: jelmer@jelmer.uk-20200524003950-bbc545r76vc5yajg
Add github action.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
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
16
16
 
17
17
"""Test our ability to build up test repositories"""
18
18
 
19
 
from cStringIO import StringIO
20
 
 
21
 
from bzrlib.plugins.git import tests
 
19
from io import BytesIO
 
20
 
 
21
from dulwich.repo import Repo as GitRepo
 
22
 
 
23
from .. import tests
22
24
 
23
25
 
24
26
class TestGitBranchBuilder(tests.TestCase):
25
27
 
26
28
    def test__create_blob(self):
27
 
        stream = StringIO()
 
29
        stream = BytesIO()
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',
31
33
                             stream.getvalue())
32
34
 
33
35
    def test_set_file(self):
34
 
        stream = StringIO()
 
36
        stream = BytesIO()
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',
38
40
                             stream.getvalue())
39
 
        self.assertEqual(['M 100644 :1 foobar\n'], builder.commit_info)
 
41
        self.assertEqual([b'M 100644 :1 foobar\n'], builder.commit_info)
40
42
 
41
43
    def test_set_file_unicode(self):
42
 
        stream = StringIO()
 
44
        stream = BytesIO()
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',
46
48
                             stream.getvalue())
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)
48
50
 
49
51
    def test_set_file_newline(self):
50
 
        stream = StringIO()
 
52
        stream = BytesIO()
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',
54
56
                             stream.getvalue())
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)
56
58
 
57
59
    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)
 
60
        stream = BytesIO()
 
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',
 
64
                             stream.getvalue())
 
65
        self.assertEqual([b'M 100755 :1 f\xc2\xb5/bar\n'], builder.commit_info)
 
66
 
 
67
    def test_set_symlink(self):
 
68
        stream = BytesIO()
 
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',
 
72
                             stream.getvalue())
 
73
        self.assertEqual([b'M 120000 :1 f\xc2\xb5/bar\n'], builder.commit_info)
 
74
 
 
75
    def test_set_symlink_newline(self):
 
76
        stream = BytesIO()
 
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',
 
80
                             stream.getvalue())
 
81
        self.assertEqual([b'M 120000 :1 "foo\\nbar"\n'], builder.commit_info)
80
82
 
81
83
    def test_delete_entry(self):
82
 
        stream = StringIO()
 
84
        stream = BytesIO()
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)
86
88
 
87
89
    def test_delete_entry_newline(self):
88
 
        stream = StringIO()
 
90
        stream = BytesIO()
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)
92
94
 
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
98
100
        # newline.
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
102
104
        # escaped.
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"')
107
109
 
108
110
    def test_add_and_commit(self):
109
 
        stream = StringIO()
 
111
        stream = BytesIO()
110
112
        builder = tests.GitBranchBuilder(stream)
111
113
 
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',
 
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,
 
118
                                              timezone=b'+0100'))
 
119
        self.assertEqualDiff(b'blob\nmark :1\ndata 13\ncontents\nbar\n\n'
 
120
                             b'commit refs/heads/master\n'
 
121
                             b'mark :2\n'
 
122
                             b'committer Joe Foo <joe@foo.com> 1194586400 +0100\n'
 
123
                             b'data 18\n'
 
124
                             b'committing f\xc2\xb5/bar'
 
125
                             b'\n'
 
126
                             b'M 100644 :1 f\xc2\xb5/bar\n'
 
127
                             b'\n',
126
128
                             stream.getvalue())
127
129
 
128
130
    def test_commit_base(self):
129
 
        stream = StringIO()
 
131
        stream = BytesIO()
130
132
        builder = tests.GitBranchBuilder(stream)
131
133
 
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,
139
141
                            base=r1)
140
142
 
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())
 
143
        self.assertEqualDiff(b'blob\nmark :1\ndata 13\ncontents\nfoo\n\n'
 
144
                             b'commit refs/heads/master\n'
 
145
                             b'mark :2\n'
 
146
                             b'committer Joe Foo <joe@foo.com> 1194586400 +0000\n'
 
147
                             b'data 5\n'
 
148
                             b'first'
 
149
                             b'\n'
 
150
                             b'M 100644 :1 foo\n'
 
151
                             b'\n'
 
152
                             b'commit refs/heads/master\n'
 
153
                             b'mark :3\n'
 
154
                             b'committer Joe Foo <joe@foo.com> 1194586405 +0000\n'
 
155
                             b'data 6\n'
 
156
                             b'second'
 
157
                             b'\n'
 
158
                             b'\n'
 
159
                             b'commit refs/heads/master\n'
 
160
                             b'mark :4\n'
 
161
                             b'committer Joe Foo <joe@foo.com> 1194586410 +0000\n'
 
162
                             b'data 5\n'
 
163
                             b'third'
 
164
                             b'\n'
 
165
                             b'from :2\n'
 
166
                             b'\n', stream.getvalue())
165
167
 
166
168
    def test_commit_merge(self):
167
 
        stream = StringIO()
 
169
        stream = BytesIO()
168
170
        builder = tests.GitBranchBuilder(stream)
169
171
 
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,
177
179
                            base=r1)
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,
180
182
                            merge=[r2])
181
183
 
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())
 
184
        self.assertEqualDiff(b'blob\nmark :1\ndata 13\ncontents\nfoo\n\n'
 
185
                             b'commit refs/heads/master\n'
 
186
                             b'mark :2\n'
 
187
                             b'committer Joe Foo <joe@foo.com> 1194586400 +0000\n'
 
188
                             b'data 5\n'
 
189
                             b'first'
 
190
                             b'\n'
 
191
                             b'M 100644 :1 foo\n'
 
192
                             b'\n'
 
193
                             b'commit refs/heads/master\n'
 
194
                             b'mark :3\n'
 
195
                             b'committer Joe Foo <joe@foo.com> 1194586405 +0000\n'
 
196
                             b'data 6\n'
 
197
                             b'second'
 
198
                             b'\n'
 
199
                             b'\n'
 
200
                             b'commit refs/heads/master\n'
 
201
                             b'mark :4\n'
 
202
                             b'committer Joe Foo <joe@foo.com> 1194586410 +0000\n'
 
203
                             b'data 5\n'
 
204
                             b'third'
 
205
                             b'\n'
 
206
                             b'from :2\n'
 
207
                             b'\n'
 
208
                             b'commit refs/heads/master\n'
 
209
                             b'mark :5\n'
 
210
                             b'committer Joe Foo <joe@foo.com> 1194586415 +0000\n'
 
211
                             b'data 5\n'
 
212
                             b'Merge'
 
213
                             b'\n'
 
214
                             b'merge :3\n'
 
215
                             b'\n', stream.getvalue())
214
216
 
215
217
    def test_auto_timestamp(self):
216
 
        stream = StringIO()
 
218
        stream = BytesIO()
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')
221
223
 
222
224
    def test_reset(self):
223
 
        stream = StringIO()
 
225
        stream = BytesIO()
224
226
        builder = tests.GitBranchBuilder(stream)
225
227
        builder.reset()
226
 
        self.assertEqualDiff('reset refs/heads/master\n\n', stream.getvalue())
 
228
        self.assertEqualDiff(b'reset refs/heads/master\n\n', stream.getvalue())
227
229
 
228
230
    def test_reset_named_ref(self):
229
 
        stream = StringIO()
 
231
        stream = BytesIO()
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())
233
235
 
234
236
    def test_reset_revision(self):
235
 
        stream = StringIO()
 
237
        stream = BytesIO()
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'
240
 
            'from :123\n'
241
 
            '\n', stream.getvalue())
 
241
            b'reset refs/heads/master\n'
 
242
            b'from :123\n'
 
243
            b'\n', stream.getvalue())
242
244
 
243
245
 
244
246
class TestGitBranchBuilderReal(tests.TestCaseInTempDir):
245
247
 
246
248
    def test_create_real_branch(self):
247
 
        tests.run_git('init')
 
249
        GitRepo.init(".")
248
250
 
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
 
                         }, mapping)
 
256
        self.assertEqual({b'1': b'44411e8e9202177dd19b6599d7a7991059fa3cb4',
 
257
                          b'2': b'b0b62e674f67306fddcf72fa888c3b56df100d64',
 
258
                          }, mapping)