1
# Copyright (C) 2020 Jelmer Vernooij <jelmer@jelmer.uk>
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
18
"""Tests for breezy.git.tests."""
20
from __future__ import absolute_import
23
from unittest import TestCase
25
from dulwich.objects import Tree, Blob
27
from breezy.delta import TreeDelta
28
from breezy.tree import TreeChange
29
from breezy.git.tree import (
30
changes_from_git_changes,
31
tree_delta_from_git_changes,
34
from breezy.git.mapping import default_mapping
37
class ChangesFromGitChangesTests(TestCase):
40
super(ChangesFromGitChangesTests, self).setUp()
42
self.mapping = default_mapping
45
self, changes, specific_files=None, include_unchanged=False,
46
source_extras=None, target_extras=None):
47
return list(changes_from_git_changes(
48
changes, self.mapping, specific_files=specific_files,
49
include_unchanged=include_unchanged, source_extras=source_extras,
50
target_extras=target_extras))
53
self.assertEqual([], self.transform([]))
55
def test_modified(self):
56
a = Blob.from_string(b'a')
57
b = Blob.from_string(b'b')
60
b'git:a', ('a', 'a'), True, (True, True),
61
(b'TREE_ROOT', b'TREE_ROOT'), ('a', 'a'), ('file', 'file'),
62
(False, False), False)
65
(b'a', stat.S_IFREG|0o644, a), (b'a', stat.S_IFREG|0o644, b))]))
67
def test_kind_changed(self):
68
a = Blob.from_string(b'a')
69
b = Blob.from_string(b'target')
72
b'git:a', ('a', 'a'), True, (True, True),
73
(b'TREE_ROOT', b'TREE_ROOT'), ('a', 'a'), ('file', 'symlink'),
74
(False, False), False)
77
(b'a', stat.S_IFREG|0o644, a), (b'a', stat.S_IFLNK, b))]))
79
def test_rename_no_changes(self):
80
a = Blob.from_string(b'a')
83
b'git:old', ('old', 'a'), False, (True, True),
84
(b'TREE_ROOT', b'TREE_ROOT'), ('old', 'a'),
85
('file', 'file'), (False, False), False)
88
(b'old', stat.S_IFREG|0o644, a), (b'a', stat.S_IFREG|0o644, a))]))
90
def test_rename_and_modify(self):
91
a = Blob.from_string(b'a')
92
b = Blob.from_string(b'b')
95
b'git:a', ('a', 'b'), True, (True, True),
96
(b'TREE_ROOT', b'TREE_ROOT'), ('a', 'b'),
97
('file', 'file'), (False, False), False)
100
(b'a', stat.S_IFREG|0o644, a), (b'b', stat.S_IFREG|0o644, b))]))
102
def test_copy_no_changes(self):
103
a = Blob.from_string(b'a')
106
b'git:a', ('old', 'a'), False, (True, True),
107
(b'TREE_ROOT', b'TREE_ROOT'), ('old', 'a'),
108
('file', 'file'), (False, False), True)
111
(b'old', stat.S_IFREG|0o644, a), (b'a', stat.S_IFREG|0o644, a))]))
113
def test_copy_and_modify(self):
114
a = Blob.from_string(b'a')
115
b = Blob.from_string(b'b')
118
b'git:b', ('a', 'b'), True, (True, True),
119
(b'TREE_ROOT', b'TREE_ROOT'), ('a', 'b'),
120
('file', 'file'), (False, False), True)
123
(b'a', stat.S_IFREG|0o644, a), (b'b', stat.S_IFREG|0o644, b))]))
126
b = Blob.from_string(b'b')
129
b'git:a', (None, 'a'), True, (False, True), (None, b'TREE_ROOT'),
130
(None, 'a'), (None, 'file'), (None, False), False)
133
(None, None, None), (b'a', stat.S_IFREG|0o644, b))]))
135
def test_delete(self):
136
b = Blob.from_string(b'b')
139
b'git:a', ('a', None), True, (True, False), (b'TREE_ROOT', None),
140
('a', None), ('file', None), (False, None), False)
143
(b'a', stat.S_IFREG|0o644, b), (None, None, None))]))
145
def test_unchanged(self):
146
b = Blob.from_string(b'b')
149
b'git:a', ('a', 'a'), False, (True, True),
150
(b'TREE_ROOT', b'TREE_ROOT'), ('a', 'a'), ('file', 'file'),
151
(False, False), False)
154
(b'a', stat.S_IFREG|0o644, b), (b'a', stat.S_IFREG|0o644, b))],
155
include_unchanged=True))
156
self.assertEqual([], self.transform([
158
(b'a', stat.S_IFREG|0o644, b), (b'a', stat.S_IFREG|0o644, b))],
159
include_unchanged=False))
161
def test_unversioned(self):
162
b = Blob.from_string(b'b')
165
None, (None, 'a'), True, (False, False),
166
(None, b'TREE_ROOT'), (None, 'a'), (None, 'file'),
167
(None, False), False)
170
(None, None, None), (b'a', stat.S_IFREG|0o644, b))],
171
target_extras=set([b'a'])))
174
None, ('a', 'a'), False, (False, False),
175
(b'TREE_ROOT', b'TREE_ROOT'), ('a', 'a'), ('file', 'file'),
176
(False, False), False)
179
(b'a', stat.S_IFREG|0o644, b), (b'a', stat.S_IFREG|0o644, b))],
180
source_extras=set([b'a']),
181
target_extras=set([b'a'])))
184
class DeltaFromGitChangesTests(TestCase):
187
super(DeltaFromGitChangesTests, self).setUp()
189
self.mapping = default_mapping
192
self, changes, specific_files=None,
193
require_versioned=False, include_root=False,
194
source_extras=None, target_extras=None):
195
return tree_delta_from_git_changes(
196
changes, (self.mapping, self.mapping),
197
specific_files=specific_files,
198
require_versioned=require_versioned, include_root=include_root,
199
source_extras=source_extras, target_extras=target_extras)
201
def test_empty(self):
202
self.assertEqual(TreeDelta(), self.transform([]))
204
def test_modified(self):
205
a = Blob.from_string(b'a')
206
b = Blob.from_string(b'b')
207
delta = self.transform([
209
(b'a', stat.S_IFREG|0o644, a), (b'a', stat.S_IFREG|0o644, b))])
210
expected_delta = TreeDelta()
211
expected_delta.modified.append(TreeChange(
212
b'git:a', ('a', 'a'), True, (True, True),
213
(b'TREE_ROOT', b'TREE_ROOT'), ('a', 'a'), ('file', 'file'),
214
(False, False), False))
215
self.assertEqual(expected_delta, delta)
217
def test_rename_no_changes(self):
218
a = Blob.from_string(b'a')
219
delta = self.transform([
221
(b'old', stat.S_IFREG|0o644, a), (b'a', stat.S_IFREG|0o644, a))])
222
expected_delta = TreeDelta()
223
expected_delta.renamed.append(
225
b'git:old', ('old', 'a'), False, (True, True),
226
(b'TREE_ROOT', b'TREE_ROOT'), ('old', 'a'),
227
('file', 'file'), (False, False), False))
228
self.assertEqual(expected_delta, delta)
230
def test_rename_and_modify(self):
231
a = Blob.from_string(b'a')
232
b = Blob.from_string(b'b')
233
delta = self.transform([
235
(b'a', stat.S_IFREG|0o644, a), (b'b', stat.S_IFREG|0o644, b))])
236
expected_delta = TreeDelta()
237
expected_delta.renamed.append(
239
b'git:a', ('a', 'b'), True, (True, True),
240
(b'TREE_ROOT', b'TREE_ROOT'), ('a', 'b'),
241
('file', 'file'), (False, False), False))
242
self.assertEqual(delta, expected_delta)
244
def test_copy_no_changes(self):
245
a = Blob.from_string(b'a')
246
delta = self.transform([
248
(b'old', stat.S_IFREG|0o644, a), (b'a', stat.S_IFREG|0o644, a))])
249
expected_delta = TreeDelta()
250
expected_delta.copied.append(TreeChange(
251
b'git:a', ('old', 'a'), False, (True, True),
252
(b'TREE_ROOT', b'TREE_ROOT'), ('old', 'a'),
253
('file', 'file'), (False, False), True))
254
self.assertEqual(expected_delta, delta)
256
def test_copy_and_modify(self):
257
a = Blob.from_string(b'a')
258
b = Blob.from_string(b'b')
259
delta = self.transform([
261
(b'a', stat.S_IFREG|0o644, a), (b'b', stat.S_IFREG|0o644, b))])
262
expected_delta = TreeDelta()
263
expected_delta.copied.append(TreeChange(
264
b'git:b', ('a', 'b'), True, (True, True),
265
(b'TREE_ROOT', b'TREE_ROOT'), ('a', 'b'),
266
('file', 'file'), (False, False), True))
267
self.assertEqual(expected_delta, delta)
270
b = Blob.from_string(b'b')
271
delta = self.transform([
273
(None, None, None), (b'a', stat.S_IFREG|0o644, b))])
274
expected_delta = TreeDelta()
275
expected_delta.added.append(TreeChange(
276
b'git:a', (None, 'a'), True, (False, True), (None, b'TREE_ROOT'),
277
(None, 'a'), (None, 'file'), (None, False), False))
278
self.assertEqual(delta, expected_delta)
280
def test_delete(self):
281
b = Blob.from_string(b'b')
282
delta = self.transform([
284
(b'a', stat.S_IFREG|0o644, b), (None, None, None))])
285
expected_delta = TreeDelta()
286
expected_delta.removed.append(TreeChange(
287
b'git:a', ('a', None), True, (True, False), (b'TREE_ROOT', None),
288
('a', None), ('file', None), (False, None), False))
289
self.assertEqual(delta, expected_delta)
291
def test_unchanged(self):
292
b = Blob.from_string(b'b')
293
delta = self.transform([
295
(b'a', stat.S_IFREG|0o644, b), (b'a', stat.S_IFREG|0o644, b))])
296
expected_delta = TreeDelta()
297
expected_delta.unchanged.append(TreeChange(
298
b'git:a', ('a', 'a'), False, (True, True),
299
(b'TREE_ROOT', b'TREE_ROOT'), ('a', 'a'), ('file', 'file'),
300
(False, False), False))
302
def test_unversioned(self):
303
b = Blob.from_string(b'b')
304
delta = self.transform([
306
(None, None, None), (b'a', stat.S_IFREG|0o644, b))],
307
target_extras=set([b'a']))
308
expected_delta = TreeDelta()
309
expected_delta.unversioned.append(
311
None, (None, 'a'), True, (False, False),
312
(None, b'TREE_ROOT'), (None, 'a'), (None, 'file'),
313
(None, False), False))
314
self.assertEqual(delta, expected_delta)
315
delta = self.transform([
317
(b'a', stat.S_IFREG|0o644, b), (b'a', stat.S_IFREG|0o644, b))],
318
source_extras=set([b'a']),
319
target_extras=set([b'a']))
320
expected_delta = TreeDelta()
321
expected_delta.unversioned.append(TreeChange(
322
None, ('a', 'a'), False, (False, False),
323
(b'TREE_ROOT', b'TREE_ROOT'), ('a', 'a'), ('file', 'file'),
324
(False, False), False))
325
self.assertEqual(delta, expected_delta)
327
def test_kind_change(self):
328
a = Blob.from_string(b'a')
329
b = Blob.from_string(b'target')
330
delta = self.transform([
332
(b'a', stat.S_IFREG|0o644, a), (b'a', stat.S_IFLNK, b))])
333
expected_delta = TreeDelta()
334
expected_delta.kind_changed.append(TreeChange(
335
b'git:a', ('a', 'a'), True, (True, True),
336
(b'TREE_ROOT', b'TREE_ROOT'), ('a', 'a'), ('file', 'symlink'),
337
(False, False), False))
338
self.assertEqual(expected_delta, delta)