Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / dulwich / tests / test_patch.py @ 959

History | View | Annotate | Download (18.7 KB)

1
# test_patch.py -- tests for patch.py
2
# Copyright (C) 2010 Jelmer Vernooij <jelmer@samba.org>
3
#
4
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
5
# General Public License as public by the Free Software Foundation; version 2.0
6
# or (at your option) any later version. You can redistribute it and/or
7
# modify it under the terms of either of these two licenses.
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14
#
15
# You should have received a copy of the licenses; if not, see
16
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
17
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
18
# License, Version 2.0.
19
#
20

    
21
"""Tests for patch.py."""
22

    
23
from io import BytesIO, StringIO
24

    
25
from dulwich.objects import (
26
    Blob,
27
    Commit,
28
    S_IFGITLINK,
29
    Tree,
30
    )
31
from dulwich.object_store import (
32
    MemoryObjectStore,
33
    )
34
from dulwich.patch import (
35
    git_am_patch_split,
36
    write_blob_diff,
37
    write_commit_patch,
38
    write_object_diff,
39
    write_tree_diff,
40
    )
41
from dulwich.tests import (
42
    SkipTest,
43
    TestCase,
44
    )
45

    
46

    
47
class WriteCommitPatchTests(TestCase):
48

    
49
    def test_simple_bytesio(self):
50
        f = BytesIO()
51
        c = Commit()
52
        c.committer = c.author = b"Jelmer <jelmer@samba.org>"
53
        c.commit_time = c.author_time = 1271350201
54
        c.commit_timezone = c.author_timezone = 0
55
        c.message = b"This is the first line\nAnd this is the second line.\n"
56
        c.tree = Tree().id
57
        write_commit_patch(f, c, b"CONTENTS", (1, 1), version="custom")
58
        f.seek(0)
59
        lines = f.readlines()
60
        self.assertTrue(lines[0].startswith(b"From 0b0d34d1b5b596c928adc9a727a4b9e03d025298"))
61
        self.assertEqual(lines[1], b"From: Jelmer <jelmer@samba.org>\n")
62
        self.assertTrue(lines[2].startswith(b"Date: "))
63
        self.assertEqual([
64
            b"Subject: [PATCH 1/1] This is the first line\n",
65
            b"And this is the second line.\n",
66
            b"\n",
67
            b"\n",
68
            b"---\n"], lines[3:8])
69
        self.assertEqual([
70
            b"CONTENTS-- \n",
71
            b"custom\n"], lines[-2:])
72
        if len(lines) >= 12:
73
            # diffstat may not be present
74
            self.assertEqual(lines[8], b" 0 files changed\n")
75

    
76

    
77
class ReadGitAmPatch(TestCase):
78

    
79
    def test_extract_string(self):
80
        text = b"""From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
81
From: Jelmer Vernooij <jelmer@samba.org>
82
Date: Thu, 15 Apr 2010 15:40:28 +0200
83
Subject: [PATCH 1/2] Remove executable bit from prey.ico (triggers a lintian warning).
84

85
---
86
 pixmaps/prey.ico |  Bin 9662 -> 9662 bytes
87
 1 files changed, 0 insertions(+), 0 deletions(-)
88
 mode change 100755 => 100644 pixmaps/prey.ico
89

90
-- 
91
1.7.0.4
92
"""
93
        c, diff, version = git_am_patch_split(StringIO(text.decode("utf-8")), "utf-8")
94
        self.assertEqual(b"Jelmer Vernooij <jelmer@samba.org>", c.committer)
95
        self.assertEqual(b"Jelmer Vernooij <jelmer@samba.org>", c.author)
96
        self.assertEqual(b"Remove executable bit from prey.ico "
97
            b"(triggers a lintian warning).\n", c.message)
98
        self.assertEqual(b""" pixmaps/prey.ico |  Bin 9662 -> 9662 bytes
99
 1 files changed, 0 insertions(+), 0 deletions(-)
100
 mode change 100755 => 100644 pixmaps/prey.ico
101

102
""", diff)
103
        self.assertEqual(b"1.7.0.4", version)
104

    
105
    def test_extract_bytes(self):
106
        text = b"""From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
107
From: Jelmer Vernooij <jelmer@samba.org>
108
Date: Thu, 15 Apr 2010 15:40:28 +0200
109
Subject: [PATCH 1/2] Remove executable bit from prey.ico (triggers a lintian warning).
110

111
---
112
 pixmaps/prey.ico |  Bin 9662 -> 9662 bytes
113
 1 files changed, 0 insertions(+), 0 deletions(-)
114
 mode change 100755 => 100644 pixmaps/prey.ico
115

116
-- 
117
1.7.0.4
118
"""
119
        c, diff, version = git_am_patch_split(BytesIO(text))
120
        self.assertEqual(b"Jelmer Vernooij <jelmer@samba.org>", c.committer)
121
        self.assertEqual(b"Jelmer Vernooij <jelmer@samba.org>", c.author)
122
        self.assertEqual(b"Remove executable bit from prey.ico "
123
            b"(triggers a lintian warning).\n", c.message)
124
        self.assertEqual(b""" pixmaps/prey.ico |  Bin 9662 -> 9662 bytes
125
 1 files changed, 0 insertions(+), 0 deletions(-)
126
 mode change 100755 => 100644 pixmaps/prey.ico
127

128
""", diff)
129
        self.assertEqual(b"1.7.0.4", version)
130

    
131
    def test_extract_spaces(self):
132
        text = b"""From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
133
From: Jelmer Vernooij <jelmer@samba.org>
134
Date: Thu, 15 Apr 2010 15:40:28 +0200
135
Subject:  [Dulwich-users] [PATCH] Added unit tests for
136
 dulwich.object_store.tree_lookup_path.
137

138
* dulwich/tests/test_object_store.py
139
  (TreeLookupPathTests): This test case contains a few tests that ensure the
140
   tree_lookup_path function works as expected.
141
---
142
 pixmaps/prey.ico |  Bin 9662 -> 9662 bytes
143
 1 files changed, 0 insertions(+), 0 deletions(-)
144
 mode change 100755 => 100644 pixmaps/prey.ico
145

146
-- 
147
1.7.0.4
148
"""
149
        c, diff, version = git_am_patch_split(BytesIO(text), "utf-8")
150
        self.assertEqual(b'Added unit tests for dulwich.object_store.tree_lookup_path.\n\n* dulwich/tests/test_object_store.py\n  (TreeLookupPathTests): This test case contains a few tests that ensure the\n   tree_lookup_path function works as expected.\n', c.message)
151

    
152
    def test_extract_pseudo_from_header(self):
153
        text = b"""From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
154
From: Jelmer Vernooij <jelmer@samba.org>
155
Date: Thu, 15 Apr 2010 15:40:28 +0200
156
Subject:  [Dulwich-users] [PATCH] Added unit tests for
157
 dulwich.object_store.tree_lookup_path.
158

159
From: Jelmer Vernooy <jelmer@debian.org>
160

161
* dulwich/tests/test_object_store.py
162
  (TreeLookupPathTests): This test case contains a few tests that ensure the
163
   tree_lookup_path function works as expected.
164
---
165
 pixmaps/prey.ico |  Bin 9662 -> 9662 bytes
166
 1 files changed, 0 insertions(+), 0 deletions(-)
167
 mode change 100755 => 100644 pixmaps/prey.ico
168

169
-- 
170
1.7.0.4
171
"""
172
        c, diff, version = git_am_patch_split(BytesIO(text), "utf-8")
173
        self.assertEqual(b"Jelmer Vernooy <jelmer@debian.org>", c.author)
174
        self.assertEqual(b'Added unit tests for dulwich.object_store.tree_lookup_path.\n\n* dulwich/tests/test_object_store.py\n  (TreeLookupPathTests): This test case contains a few tests that ensure the\n   tree_lookup_path function works as expected.\n', c.message)
175

    
176
    def test_extract_no_version_tail(self):
177
        text = b"""From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
178
From: Jelmer Vernooij <jelmer@samba.org>
179
Date: Thu, 15 Apr 2010 15:40:28 +0200
180
Subject:  [Dulwich-users] [PATCH] Added unit tests for
181
 dulwich.object_store.tree_lookup_path.
182

183
From: Jelmer Vernooy <jelmer@debian.org>
184

185
---
186
 pixmaps/prey.ico |  Bin 9662 -> 9662 bytes
187
 1 files changed, 0 insertions(+), 0 deletions(-)
188
 mode change 100755 => 100644 pixmaps/prey.ico
189

190
"""
191
        c, diff, version = git_am_patch_split(BytesIO(text), "utf-8")
192
        self.assertEqual(None, version)
193

    
194
    def test_extract_mercurial(self):
195
        raise SkipTest("git_am_patch_split doesn't handle Mercurial patches properly yet")
196
        expected_diff = """diff --git a/dulwich/tests/test_patch.py b/dulwich/tests/test_patch.py
197
--- a/dulwich/tests/test_patch.py
198
+++ b/dulwich/tests/test_patch.py
199
@@ -158,7 +158,7 @@
200
 
201
 '''
202
         c, diff, version = git_am_patch_split(BytesIO(text))
203
-        self.assertIs(None, version)
204
+        self.assertEqual(None, version)
205
 
206
 
207
 class DiffTests(TestCase):
208
"""
209
        text = """From dulwich-users-bounces+jelmer=samba.org@lists.launchpad.net Mon Nov 29 00:58:18 2010
210
Date: Sun, 28 Nov 2010 17:57:27 -0600
211
From: Augie Fackler <durin42@gmail.com>
212
To: dulwich-users <dulwich-users@lists.launchpad.net>
213
Subject: [Dulwich-users] [PATCH] test_patch: fix tests on Python 2.6
214
Content-Transfer-Encoding: 8bit
215

216
Change-Id: I5e51313d4ae3a65c3f00c665002a7489121bb0d6
217

218
%s
219

220
_______________________________________________
221
Mailing list: https://launchpad.net/~dulwich-users
222
Post to     : dulwich-users@lists.launchpad.net
223
Unsubscribe : https://launchpad.net/~dulwich-users
224
More help   : https://help.launchpad.net/ListHelp
225

226
""" % expected_diff
227
        c, diff, version = git_am_patch_split(BytesIO(text))
228
        self.assertEqual(expected_diff, diff)
229
        self.assertEqual(None, version)
230

    
231

    
232
class DiffTests(TestCase):
233
    """Tests for write_blob_diff and write_tree_diff."""
234

    
235
    def test_blob_diff(self):
236
        f = BytesIO()
237
        write_blob_diff(f, (b"foo.txt", 0o644, Blob.from_string(b"old\nsame\n")),
238
                           (b"bar.txt", 0o644, Blob.from_string(b"new\nsame\n")))
239
        self.assertEqual([
240
            b"diff --git a/foo.txt b/bar.txt",
241
            b"index 3b0f961..a116b51 644",
242
            b"--- a/foo.txt",
243
            b"+++ b/bar.txt",
244
            b"@@ -1,2 +1,2 @@",
245
            b"-old",
246
            b"+new",
247
            b" same"
248
            ], f.getvalue().splitlines())
249

    
250
    def test_blob_add(self):
251
        f = BytesIO()
252
        write_blob_diff(f, (None, None, None),
253
                           (b"bar.txt", 0o644, Blob.from_string(b"new\nsame\n")))
254
        self.assertEqual([
255
             b'diff --git /dev/null b/bar.txt',
256
             b'new mode 644',
257
             b'index 0000000..a116b51 644',
258
             b'--- /dev/null',
259
             b'+++ b/bar.txt',
260
             b'@@ -1,0 +1,2 @@',
261
             b'+new',
262
             b'+same'
263
            ], f.getvalue().splitlines())
264

    
265
    def test_blob_remove(self):
266
        f = BytesIO()
267
        write_blob_diff(f, (b"bar.txt", 0o644, Blob.from_string(b"new\nsame\n")),
268
                           (None, None, None))
269
        self.assertEqual([
270
            b'diff --git a/bar.txt /dev/null',
271
            b'deleted mode 644',
272
            b'index a116b51..0000000',
273
            b'--- a/bar.txt',
274
            b'+++ /dev/null',
275
            b'@@ -1,2 +1,0 @@',
276
            b'-new',
277
            b'-same'
278
            ], f.getvalue().splitlines())
279

    
280
    def test_tree_diff(self):
281
        f = BytesIO()
282
        store = MemoryObjectStore()
283
        added = Blob.from_string(b"add\n")
284
        removed = Blob.from_string(b"removed\n")
285
        changed1 = Blob.from_string(b"unchanged\nremoved\n")
286
        changed2 = Blob.from_string(b"unchanged\nadded\n")
287
        unchanged = Blob.from_string(b"unchanged\n")
288
        tree1 = Tree()
289
        tree1.add(b"removed.txt", 0o644, removed.id)
290
        tree1.add(b"changed.txt", 0o644, changed1.id)
291
        tree1.add(b"unchanged.txt", 0o644, changed1.id)
292
        tree2 = Tree()
293
        tree2.add(b"added.txt", 0o644, added.id)
294
        tree2.add(b"changed.txt", 0o644, changed2.id)
295
        tree2.add(b"unchanged.txt", 0o644, changed1.id)
296
        store.add_objects([(o, None) for o in [
297
            tree1, tree2, added, removed, changed1, changed2, unchanged]])
298
        write_tree_diff(f, store, tree1.id, tree2.id)
299
        self.assertEqual([
300
            b'diff --git /dev/null b/added.txt',
301
            b'new mode 644',
302
            b'index 0000000..76d4bb8 644',
303
            b'--- /dev/null',
304
            b'+++ b/added.txt',
305
            b'@@ -1,0 +1,1 @@',
306
            b'+add',
307
            b'diff --git a/changed.txt b/changed.txt',
308
            b'index bf84e48..1be2436 644',
309
            b'--- a/changed.txt',
310
            b'+++ b/changed.txt',
311
            b'@@ -1,2 +1,2 @@',
312
            b' unchanged',
313
            b'-removed',
314
            b'+added',
315
            b'diff --git a/removed.txt /dev/null',
316
            b'deleted mode 644',
317
            b'index 2c3f0b3..0000000',
318
            b'--- a/removed.txt',
319
            b'+++ /dev/null',
320
            b'@@ -1,1 +1,0 @@',
321
            b'-removed',
322
            ], f.getvalue().splitlines())
323

    
324
    def test_tree_diff_submodule(self):
325
        f = BytesIO()
326
        store = MemoryObjectStore()
327
        tree1 = Tree()
328
        tree1.add(b"asubmodule", S_IFGITLINK,
329
            b"06d0bdd9e2e20377b3180e4986b14c8549b393e4")
330
        tree2 = Tree()
331
        tree2.add(b"asubmodule", S_IFGITLINK,
332
            b"cc975646af69f279396d4d5e1379ac6af80ee637")
333
        store.add_objects([(o, None) for o in [tree1, tree2]])
334
        write_tree_diff(f, store, tree1.id, tree2.id)
335
        self.assertEqual([
336
            b'diff --git a/asubmodule b/asubmodule',
337
            b'index 06d0bdd..cc97564 160000',
338
            b'--- a/asubmodule',
339
            b'+++ b/asubmodule',
340
            b'@@ -1,1 +1,1 @@',
341
            b'-Submodule commit 06d0bdd9e2e20377b3180e4986b14c8549b393e4',
342
            b'+Submodule commit cc975646af69f279396d4d5e1379ac6af80ee637',
343
            ], f.getvalue().splitlines())
344

    
345
    def test_object_diff_blob(self):
346
        f = BytesIO()
347
        b1 = Blob.from_string(b"old\nsame\n")
348
        b2 = Blob.from_string(b"new\nsame\n")
349
        store = MemoryObjectStore()
350
        store.add_objects([(b1, None), (b2, None)])
351
        write_object_diff(f, store, (b"foo.txt", 0o644, b1.id),
352
                                    (b"bar.txt", 0o644, b2.id))
353
        self.assertEqual([
354
            b"diff --git a/foo.txt b/bar.txt",
355
            b"index 3b0f961..a116b51 644",
356
            b"--- a/foo.txt",
357
            b"+++ b/bar.txt",
358
            b"@@ -1,2 +1,2 @@",
359
            b"-old",
360
            b"+new",
361
            b" same"
362
            ], f.getvalue().splitlines())
363

    
364
    def test_object_diff_add_blob(self):
365
        f = BytesIO()
366
        store = MemoryObjectStore()
367
        b2 = Blob.from_string(b"new\nsame\n")
368
        store.add_object(b2)
369
        write_object_diff(f, store, (None, None, None),
370
                                    (b"bar.txt", 0o644, b2.id))
371
        self.assertEqual([
372
             b'diff --git /dev/null b/bar.txt',
373
             b'new mode 644',
374
             b'index 0000000..a116b51 644',
375
             b'--- /dev/null',
376
             b'+++ b/bar.txt',
377
             b'@@ -1,0 +1,2 @@',
378
             b'+new',
379
             b'+same'
380
            ], f.getvalue().splitlines())
381

    
382
    def test_object_diff_remove_blob(self):
383
        f = BytesIO()
384
        b1 = Blob.from_string(b"new\nsame\n")
385
        store = MemoryObjectStore()
386
        store.add_object(b1)
387
        write_object_diff(f, store, (b"bar.txt", 0o644, b1.id),
388
                                    (None, None, None))
389
        self.assertEqual([
390
            b'diff --git a/bar.txt /dev/null',
391
            b'deleted mode 644',
392
            b'index a116b51..0000000',
393
            b'--- a/bar.txt',
394
            b'+++ /dev/null',
395
            b'@@ -1,2 +1,0 @@',
396
            b'-new',
397
            b'-same'
398
            ], f.getvalue().splitlines())
399

    
400
    def test_object_diff_bin_blob_force(self):
401
        f = BytesIO()
402
        # Prepare two slightly different PNG headers
403
        b1 = Blob.from_string(
404
            b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
405
            b"\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x04\x00\x00\x00\x05\x04\x8b")
406
        b2 = Blob.from_string(
407
            b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
408
            b"\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x03\x00\x00\x00\x98\xd3\xb3")
409
        store = MemoryObjectStore()
410
        store.add_objects([(b1, None), (b2, None)])
411
        write_object_diff(f, store, (b'foo.png', 0o644, b1.id),
412
                                    (b'bar.png', 0o644, b2.id), diff_binary=True)
413
        self.assertEqual([
414
            b'diff --git a/foo.png b/bar.png',
415
            b'index f73e47d..06364b7 644',
416
            b'--- a/foo.png',
417
            b'+++ b/bar.png',
418
            b'@@ -1,4 +1,4 @@',
419
            b' \x89PNG',
420
            b' \x1a',
421
            b' \x00\x00\x00',
422
            b'-IHDR\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x04\x00\x00\x00\x05\x04\x8b',
423
            b'\\ No newline at end of file',
424
            b'+IHDR\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x03\x00\x00\x00\x98\xd3\xb3',
425
            b'\\ No newline at end of file'
426
            ], f.getvalue().splitlines())
427

    
428
    def test_object_diff_bin_blob(self):
429
        f = BytesIO()
430
        # Prepare two slightly different PNG headers
431
        b1 = Blob.from_string(
432
            b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
433
            b"\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x04\x00\x00\x00\x05\x04\x8b")
434
        b2 = Blob.from_string(
435
            b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
436
            b"\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x03\x00\x00\x00\x98\xd3\xb3")
437
        store = MemoryObjectStore()
438
        store.add_objects([(b1, None), (b2, None)])
439
        write_object_diff(f, store, (b'foo.png', 0o644, b1.id),
440
                                    (b'bar.png', 0o644, b2.id))
441
        self.assertEqual([
442
            b'diff --git a/foo.png b/bar.png',
443
            b'index f73e47d..06364b7 644',
444
            b'Binary files a/foo.png and b/bar.png differ'
445
            ], f.getvalue().splitlines())
446

    
447
    def test_object_diff_add_bin_blob(self):
448
        f = BytesIO()
449
        b2 = Blob.from_string(
450
            b'\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52'
451
            b'\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x03\x00\x00\x00\x98\xd3\xb3')
452
        store = MemoryObjectStore()
453
        store.add_object(b2)
454
        write_object_diff(f, store, (None, None, None),
455
                                    (b'bar.png', 0o644, b2.id))
456
        self.assertEqual([
457
            b'diff --git /dev/null b/bar.png',
458
            b'new mode 644',
459
            b'index 0000000..06364b7 644',
460
            b'Binary files /dev/null and b/bar.png differ'
461
            ], f.getvalue().splitlines())
462

    
463
    def test_object_diff_remove_bin_blob(self):
464
        f = BytesIO()
465
        b1 = Blob.from_string(
466
            b'\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52'
467
            b'\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x04\x00\x00\x00\x05\x04\x8b')
468
        store = MemoryObjectStore()
469
        store.add_object(b1)
470
        write_object_diff(f, store, (b'foo.png', 0o644, b1.id),
471
                                    (None, None, None))
472
        self.assertEqual([
473
            b'diff --git a/foo.png /dev/null',
474
            b'deleted mode 644',
475
            b'index f73e47d..0000000',
476
            b'Binary files a/foo.png and /dev/null differ'
477
            ], f.getvalue().splitlines())
478

    
479
    def test_object_diff_kind_change(self):
480
        f = BytesIO()
481
        b1 = Blob.from_string(b"new\nsame\n")
482
        store = MemoryObjectStore()
483
        store.add_object(b1)
484
        write_object_diff(f, store, (b"bar.txt", 0o644, b1.id),
485
            (b"bar.txt", 0o160000, b"06d0bdd9e2e20377b3180e4986b14c8549b393e4"))
486
        self.assertEqual([
487
            b'diff --git a/bar.txt b/bar.txt',
488
            b'old mode 644',
489
            b'new mode 160000',
490
            b'index a116b51..06d0bdd 160000',
491
            b'--- a/bar.txt',
492
            b'+++ b/bar.txt',
493
            b'@@ -1,2 +1,1 @@',
494
            b'-new',
495
            b'-same',
496
            b'+Submodule commit 06d0bdd9e2e20377b3180e4986b14c8549b393e4',
497
            ], f.getvalue().splitlines())