~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_merge_directive.py

  • Committer: Martin Pool
  • Date: 2005-07-23 13:59:30 UTC
  • Revision ID: mbp@sourcefrog.net-20050723135930-d81530c82c925cb0
- less dodgy is_inside function

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
 
 
18
 
from bzrlib import (
19
 
    errors,
20
 
    gpg,
21
 
    merge_directive,
22
 
    tests,
23
 
    )
24
 
 
25
 
 
26
 
OUTPUT1 = """# Bazaar merge directive format 1
27
 
# revision_id: example:
28
 
# target_branch: http://example.com
29
 
# testament_sha1: sha
30
 
# timestamp: 1970-01-01 00:09:33 +0002
31
 
#\x20
32
 
booga"""
33
 
 
34
 
OUTPUT1_2 = """# Bazaar merge directive format 2 (Bazaar 0.90)
35
 
# revision_id: example:
36
 
# target_branch: http://example.com
37
 
# testament_sha1: sha
38
 
# timestamp: 1970-01-01 00:09:33 +0002
39
 
# base_revision_id: null:
40
 
#\x20
41
 
# Begin bundle
42
 
booga"""
43
 
 
44
 
OUTPUT2 = """# Bazaar merge directive format 1
45
 
# revision_id: example:
46
 
# target_branch: http://example.com
47
 
# testament_sha1: sha
48
 
# timestamp: 1970-01-01 00:09:33 +0002
49
 
# source_branch: http://example.org
50
 
# message: Hi mom!
51
 
#\x20
52
 
booga"""
53
 
 
54
 
OUTPUT2_2 = """# Bazaar merge directive format 2 (Bazaar 0.90)
55
 
# revision_id: example:
56
 
# target_branch: http://example.com
57
 
# testament_sha1: sha
58
 
# timestamp: 1970-01-01 00:09:33 +0002
59
 
# source_branch: http://example.org
60
 
# message: Hi mom!
61
 
# base_revision_id: null:
62
 
#\x20
63
 
# Begin patch
64
 
booga"""
65
 
 
66
 
INPUT1 = """
67
 
I was thinking today about creating a merge directive.
68
 
 
69
 
So I did.
70
 
 
71
 
Here it is.
72
 
 
73
 
(I've pasted it in the body of this message)
74
 
 
75
 
Aaron
76
 
 
77
 
# Bazaar merge directive format 1\r
78
 
# revision_id: example:
79
 
# target_branch: http://example.com
80
 
# testament_sha1: sha
81
 
# timestamp: 1970-01-01 00:09:33 +0002
82
 
# source_branch: http://example.org
83
 
# message: Hi mom!
84
 
#\x20
85
 
booga""".splitlines(True)
86
 
 
87
 
 
88
 
INPUT1_2 = """
89
 
I was thinking today about creating a merge directive.
90
 
 
91
 
So I did.
92
 
 
93
 
Here it is.
94
 
 
95
 
(I've pasted it in the body of this message)
96
 
 
97
 
Aaron
98
 
 
99
 
# Bazaar merge directive format 2 (Bazaar 0.90)\r
100
 
# revision_id: example:
101
 
# target_branch: http://example.com
102
 
# testament_sha1: sha
103
 
# timestamp: 1970-01-01 00:09:33 +0002
104
 
# source_branch: http://example.org
105
 
# base_revision_id: null:
106
 
# message: Hi mom!
107
 
#\x20
108
 
# Begin patch
109
 
booga""".splitlines(True)
110
 
 
111
 
 
112
 
class TestMergeDirective(object):
113
 
 
114
 
    def test_merge_source(self):
115
 
        time = 500000.0
116
 
        timezone = 5 * 3600
117
 
        self.assertRaises(errors.NoMergeSource, self.make_merge_directive,
118
 
            'example:', 'sha', time, timezone, 'http://example.com')
119
 
        self.assertRaises(errors.NoMergeSource, self.make_merge_directive,
120
 
            'example:', 'sha', time, timezone, 'http://example.com',
121
 
            patch_type='diff')
122
 
        self.make_merge_directive('example:', 'sha', time, timezone,
123
 
            'http://example.com', source_branch='http://example.org')
124
 
        md = self.make_merge_directive('null:', 'sha', time, timezone,
125
 
            'http://example.com', patch='blah', patch_type='bundle')
126
 
        self.assertIs(None, md.source_branch)
127
 
        md2 = self.make_merge_directive('null:', 'sha', time, timezone,
128
 
            'http://example.com', patch='blah', patch_type='bundle',
129
 
            source_branch='bar')
130
 
        self.assertEqual('bar', md2.source_branch)
131
 
 
132
 
    def test_serialization(self):
133
 
        time = 453
134
 
        timezone = 120
135
 
        md = self.make_merge_directive('example:', 'sha', time, timezone,
136
 
            'http://example.com', patch='booga', patch_type='bundle')
137
 
        self.assertEqualDiff(self.OUTPUT1, ''.join(md.to_lines()))
138
 
        md = self.make_merge_directive('example:', 'sha', time, timezone,
139
 
            'http://example.com', source_branch="http://example.org",
140
 
            patch='booga', patch_type='diff', message="Hi mom!")
141
 
        self.assertEqualDiff(self.OUTPUT2, ''.join(md.to_lines()))
142
 
 
143
 
    def test_deserialize_junk(self):
144
 
        time = 501
145
 
        self.assertRaises(errors.NotAMergeDirective,
146
 
                          merge_directive.MergeDirective.from_lines, 'lala')
147
 
 
148
 
    def test_deserialize_empty(self):
149
 
        self.assertRaises(errors.NotAMergeDirective,
150
 
                          merge_directive.MergeDirective.from_lines, [])
151
 
 
152
 
    def test_deserialize_leading_junk(self):
153
 
        md = merge_directive.MergeDirective.from_lines(self.INPUT1)
154
 
        self.assertEqual('example:', md.revision_id)
155
 
        self.assertEqual('sha', md.testament_sha1)
156
 
        self.assertEqual('http://example.com', md.target_branch)
157
 
        self.assertEqual('http://example.org', md.source_branch)
158
 
        self.assertEqual(453, md.time)
159
 
        self.assertEqual(120, md.timezone)
160
 
        self.assertEqual('booga', md.patch)
161
 
        self.assertEqual('diff', md.patch_type)
162
 
        self.assertEqual('Hi mom!', md.message)
163
 
 
164
 
    def test_roundtrip(self):
165
 
        time = 500000
166
 
        timezone = 7.5 * 3600
167
 
        md = self.make_merge_directive('example:', 'sha', time, timezone,
168
 
            'http://example.com', source_branch="http://example.org",
169
 
            patch='booga', patch_type='diff')
170
 
        md2 = merge_directive.MergeDirective.from_lines(md.to_lines())
171
 
        self.assertEqual('example:', md2.revision_id)
172
 
        self.assertIsInstance(md2.revision_id, str)
173
 
        self.assertEqual('sha', md2.testament_sha1)
174
 
        self.assertEqual('http://example.com', md2.target_branch)
175
 
        self.assertEqual('http://example.org', md2.source_branch)
176
 
        self.assertEqual(time, md2.time)
177
 
        self.assertEqual(timezone, md2.timezone)
178
 
        self.assertEqual('diff', md2.patch_type)
179
 
        self.assertEqual('booga', md2.patch)
180
 
        self.assertEqual(None, md2.message)
181
 
        self.set_bundle(md, "# Bazaar revision bundle v0.9\n#\n")
182
 
        md.message = "Hi mom!"
183
 
        lines = md.to_lines()
184
 
        md3 = merge_directive.MergeDirective.from_lines(lines)
185
 
        self.assertEqual("# Bazaar revision bundle v0.9\n#\n", md3.bundle)
186
 
        self.assertEqual("bundle", md3.patch_type)
187
 
        self.assertContainsRe(md3.to_lines()[0],
188
 
            '^# Bazaar merge directive format ')
189
 
        self.assertEqual("Hi mom!", md3.message)
190
 
        md3.clear_payload()
191
 
        self.assertIs(None, md3.get_raw_bundle())
192
 
        md4 = merge_directive.MergeDirective.from_lines(md3.to_lines())
193
 
        self.assertIs(None, md4.patch_type)
194
 
 
195
 
 
196
 
class TestMergeDirective1(tests.TestCase, TestMergeDirective):
197
 
    """Test merge directive format 1"""
198
 
 
199
 
    INPUT1 = INPUT1
200
 
 
201
 
    OUTPUT1 = OUTPUT1
202
 
 
203
 
    OUTPUT2 = OUTPUT2
204
 
 
205
 
    def make_merge_directive(self, revision_id, testament_sha1, time, timezone,
206
 
                 target_branch, patch=None, patch_type=None,
207
 
                 source_branch=None, message=None):
208
 
        return merge_directive.MergeDirective(revision_id, testament_sha1,
209
 
                 time, timezone, target_branch, patch, patch_type,
210
 
                 source_branch, message)
211
 
 
212
 
    @staticmethod
213
 
    def set_bundle(md, value):
214
 
        md.patch = value
215
 
 
216
 
    def test_require_patch(self):
217
 
        time = 500.0
218
 
        timezone = 120
219
 
        self.assertRaises(errors.PatchMissing, merge_directive.MergeDirective,
220
 
            'example:', 'sha', time, timezone, 'http://example.com',
221
 
            patch_type='bundle')
222
 
        md = merge_directive.MergeDirective('example:', 'sha1', time, timezone,
223
 
            'http://example.com', source_branch="http://example.org",
224
 
            patch='', patch_type='diff')
225
 
        self.assertEqual(md.patch, '')
226
 
 
227
 
 
228
 
class TestMergeDirective2(tests.TestCase, TestMergeDirective):
229
 
    """Test merge directive format 2"""
230
 
 
231
 
    INPUT1 = INPUT1_2
232
 
 
233
 
    OUTPUT1 = OUTPUT1_2
234
 
 
235
 
    OUTPUT2 = OUTPUT2_2
236
 
 
237
 
    def make_merge_directive(self, revision_id, testament_sha1, time, timezone,
238
 
                 target_branch, patch=None, patch_type=None,
239
 
                 source_branch=None, message=None, base_revision_id='null:'):
240
 
        if patch_type == 'bundle':
241
 
            bundle = patch
242
 
            patch = None
243
 
        else:
244
 
            bundle = None
245
 
        return merge_directive.MergeDirective2(revision_id, testament_sha1,
246
 
            time, timezone, target_branch, patch, source_branch, message,
247
 
            bundle, base_revision_id)
248
 
 
249
 
    @staticmethod
250
 
    def set_bundle(md, value):
251
 
        md.bundle = value
252
 
 
253
 
 
254
 
EMAIL1 = """From: "J. Random Hacker" <jrandom@example.com>
255
 
Subject: Commit of rev2a
256
 
To: pqm@example.com
257
 
User-Agent: Bazaar \(.*\)
258
 
 
259
 
# Bazaar merge directive format 1
260
 
# revision_id: rev2a
261
 
# target_branch: (.|\n)*
262
 
# testament_sha1: .*
263
 
# timestamp: 1970-01-01 00:08:56 \\+0001
264
 
# source_branch: (.|\n)*
265
 
"""
266
 
 
267
 
 
268
 
EMAIL1_2 = """From: "J. Random Hacker" <jrandom@example.com>
269
 
Subject: Commit of rev2a
270
 
To: pqm@example.com
271
 
User-Agent: Bazaar \(.*\)
272
 
 
273
 
# Bazaar merge directive format 2 \\(Bazaar 0.90\\)
274
 
# revision_id: rev2a
275
 
# target_branch: (.|\n)*
276
 
# testament_sha1: .*
277
 
# timestamp: 1970-01-01 00:08:56 \\+0001
278
 
# source_branch: (.|\n)*
279
 
"""
280
 
 
281
 
 
282
 
EMAIL2 = """From: "J. Random Hacker" <jrandom@example.com>
283
 
Subject: Commit of rev2a with special message
284
 
To: pqm@example.com
285
 
User-Agent: Bazaar \(.*\)
286
 
 
287
 
# Bazaar merge directive format 1
288
 
# revision_id: rev2a
289
 
# target_branch: (.|\n)*
290
 
# testament_sha1: .*
291
 
# timestamp: 1970-01-01 00:08:56 \\+0001
292
 
# source_branch: (.|\n)*
293
 
# message: Commit of rev2a with special message
294
 
"""
295
 
 
296
 
EMAIL2_2 = """From: "J. Random Hacker" <jrandom@example.com>
297
 
Subject: Commit of rev2a with special message
298
 
To: pqm@example.com
299
 
User-Agent: Bazaar \(.*\)
300
 
 
301
 
# Bazaar merge directive format 2 \\(Bazaar 0.90\\)
302
 
# revision_id: rev2a
303
 
# target_branch: (.|\n)*
304
 
# testament_sha1: .*
305
 
# timestamp: 1970-01-01 00:08:56 \\+0001
306
 
# source_branch: (.|\n)*
307
 
# message: Commit of rev2a with special message
308
 
"""
309
 
 
310
 
class TestMergeDirectiveBranch(object):
311
 
 
312
 
    def make_trees(self):
313
 
        tree_a = self.make_branch_and_tree('tree_a')
314
 
        tree_a.branch.get_config().set_user_option('email',
315
 
            'J. Random Hacker <jrandom@example.com>')
316
 
        self.build_tree_contents([('tree_a/file', 'content_a\ncontent_b\n')])
317
 
        tree_a.add('file')
318
 
        tree_a.commit('message', rev_id='rev1')
319
 
        tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
320
 
        branch_c = tree_a.bzrdir.sprout('branch_c').open_branch()
321
 
        tree_b.commit('message', rev_id='rev2b')
322
 
        self.build_tree_contents([('tree_a/file', 'content_a\ncontent_c \n')])
323
 
        tree_a.commit('Commit of rev2a', rev_id='rev2a')
324
 
        return tree_a, tree_b, branch_c
325
 
 
326
 
    def test_empty_target(self):
327
 
        tree_a, tree_b, branch_c = self.make_trees()
328
 
        tree_d = self.make_branch_and_tree('tree_d')
329
 
        md2 = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 120,
330
 
            tree_d.branch.base, patch_type='diff',
331
 
            public_branch=tree_a.branch.base)
332
 
 
333
 
    def test_generate_patch(self):
334
 
        tree_a, tree_b, branch_c = self.make_trees()
335
 
        md2 = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 120,
336
 
            tree_b.branch.base, patch_type='diff',
337
 
            public_branch=tree_a.branch.base)
338
 
        self.assertNotContainsRe(md2.patch, 'Bazaar revision bundle')
339
 
        self.assertContainsRe(md2.patch, '\\+content_c')
340
 
        self.assertNotContainsRe(md2.patch, '\\+\\+\\+ b/')
341
 
        self.assertContainsRe(md2.patch, '\\+\\+\\+ file')
342
 
 
343
 
    def test_public_branch(self):
344
 
        tree_a, tree_b, branch_c = self.make_trees()
345
 
        self.assertRaises(errors.PublicBranchOutOfDate,
346
 
            self.from_objects, tree_a.branch.repository, 'rev2a', 500, 144,
347
 
            tree_b.branch.base, public_branch=branch_c.base, patch_type='diff')
348
 
        self.assertRaises(errors.PublicBranchOutOfDate,
349
 
            self.from_objects, tree_a.branch.repository, 'rev2a', 500, 144,
350
 
            tree_b.branch.base, public_branch=branch_c.base, patch_type=None)
351
 
        # public branch is not checked if patch format is bundle.
352
 
        md1 = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 144,
353
 
            tree_b.branch.base, public_branch=branch_c.base)
354
 
        # public branch is provided with a bundle, despite possibly being out
355
 
        # of date, because it's not required if a bundle is present.
356
 
        self.assertEqual(md1.source_branch, branch_c.base)
357
 
        # Once we update the public branch, we can generate a diff.
358
 
        branch_c.pull(tree_a.branch)
359
 
        md3 = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 144,
360
 
            tree_b.branch.base, patch_type=None, public_branch=branch_c.base)
361
 
 
362
 
    def test_use_public_submit_branch(self):
363
 
        tree_a, tree_b, branch_c = self.make_trees()
364
 
        branch_c.pull(tree_a.branch)
365
 
        md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 144,
366
 
            tree_b.branch.base, patch_type=None, public_branch=branch_c.base)
367
 
        self.assertEqual(md.target_branch, tree_b.branch.base)
368
 
        tree_b.branch.set_public_branch('http://example.com')
369
 
        md2 = self.from_objects(
370
 
              tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
371
 
              patch_type=None, public_branch=branch_c.base)
372
 
        self.assertEqual(md2.target_branch, 'http://example.com')
373
 
 
374
 
    def test_message(self):
375
 
        tree_a, tree_b, branch_c = self.make_trees()
376
 
        md3 = self.from_objects(tree_a.branch.repository, 'rev1', 500, 120,
377
 
            tree_b.branch.base, patch_type=None, public_branch=branch_c.base,
378
 
            message='Merge message')
379
 
        md3.to_lines()
380
 
        self.assertIs(None, md3.patch)
381
 
        self.assertEqual('Merge message', md3.message)
382
 
 
383
 
    def test_generate_bundle(self):
384
 
        tree_a, tree_b, branch_c = self.make_trees()
385
 
        md1 = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 120,
386
 
            tree_b.branch.base, public_branch=branch_c.base)
387
 
 
388
 
        self.assertContainsRe(md1.get_raw_bundle(), 'Bazaar revision bundle')
389
 
        self.assertContainsRe(md1.patch, '\\+content_c')
390
 
        self.assertNotContainsRe(md1.patch, '\\+content_a')
391
 
        self.assertContainsRe(md1.patch, '\\+content_c')
392
 
        self.assertNotContainsRe(md1.patch, '\\+content_a')
393
 
 
394
 
    def test_broken_bundle(self):
395
 
        tree_a, tree_b, branch_c = self.make_trees()
396
 
        md1 = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 120,
397
 
            tree_b.branch.base, public_branch=branch_c.base)
398
 
        lines = md1.to_lines()
399
 
        lines = [l.replace('\n', '\r\n') for l in lines]
400
 
        md2 = merge_directive.MergeDirective.from_lines(lines)
401
 
        self.assertEqual('rev2a', md2.revision_id)
402
 
 
403
 
    def test_signing(self):
404
 
        time = 453
405
 
        timezone = 7200
406
 
        class FakeBranch(object):
407
 
            def get_config(self):
408
 
                return self
409
 
            def gpg_signing_command(self):
410
 
                return 'loopback'
411
 
        md = self.make_merge_directive('example:', 'sha', time, timezone,
412
 
            'http://example.com', source_branch="http://example.org",
413
 
            patch='booga', patch_type='diff')
414
 
        old_strategy = gpg.GPGStrategy
415
 
        gpg.GPGStrategy = gpg.LoopbackGPGStrategy
416
 
        try:
417
 
            signed = md.to_signed(FakeBranch())
418
 
        finally:
419
 
            gpg.GPGStrategy = old_strategy
420
 
        self.assertContainsRe(signed, '^-----BEGIN PSEUDO-SIGNED CONTENT')
421
 
        self.assertContainsRe(signed, 'example.org')
422
 
        self.assertContainsRe(signed, 'booga')
423
 
 
424
 
    def test_email(self):
425
 
        tree_a, tree_b, branch_c = self.make_trees()
426
 
        md = self.from_objects(tree_a.branch.repository, 'rev2a', 476, 60,
427
 
            tree_b.branch.base, patch_type=None,
428
 
            public_branch=tree_a.branch.base)
429
 
        message = md.to_email('pqm@example.com', tree_a.branch)
430
 
        self.assertContainsRe(message.as_string(), self.EMAIL1)
431
 
        md.message = 'Commit of rev2a with special message'
432
 
        message = md.to_email('pqm@example.com', tree_a.branch)
433
 
        self.assertContainsRe(message.as_string(), self.EMAIL2)
434
 
 
435
 
    def test_install_revisions_branch(self):
436
 
        tree_a, tree_b, branch_c = self.make_trees()
437
 
        md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
438
 
            tree_b.branch.base, patch_type=None,
439
 
            public_branch=tree_a.branch.base)
440
 
        self.assertFalse(tree_b.branch.repository.has_revision('rev2a'))
441
 
        revision = md.install_revisions(tree_b.branch.repository)
442
 
        self.assertEqual('rev2a', revision)
443
 
        self.assertTrue(tree_b.branch.repository.has_revision('rev2a'))
444
 
 
445
 
    def test_get_merge_request(self):
446
 
        tree_a, tree_b, branch_c = self.make_trees()
447
 
        md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
448
 
            tree_b.branch.base, patch_type='bundle',
449
 
            public_branch=tree_a.branch.base)
450
 
        self.assertFalse(tree_b.branch.repository.has_revision('rev2a'))
451
 
        md.install_revisions(tree_b.branch.repository)
452
 
        base, revision, verified = md.get_merge_request(
453
 
            tree_b.branch.repository)
454
 
        if isinstance(md, merge_directive.MergeDirective):
455
 
            self.assertIs(None, base)
456
 
            self.assertEqual('inapplicable', verified)
457
 
        else:
458
 
            self.assertEqual('rev1', base)
459
 
            self.assertEqual('verified', verified)
460
 
        self.assertEqual('rev2a', revision)
461
 
        self.assertTrue(tree_b.branch.repository.has_revision('rev2a'))
462
 
        md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
463
 
            tree_b.branch.base, patch_type=None,
464
 
            public_branch=tree_a.branch.base)
465
 
        base, revision, verified = md.get_merge_request(
466
 
            tree_b.branch.repository)
467
 
        if isinstance(md, merge_directive.MergeDirective):
468
 
            self.assertIs(None, base)
469
 
            self.assertEqual('inapplicable', verified)
470
 
        else:
471
 
            self.assertEqual('rev1', base)
472
 
            self.assertEqual('inapplicable', verified)
473
 
        md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
474
 
            tree_b.branch.base, patch_type='diff',
475
 
            public_branch=tree_a.branch.base)
476
 
        base, revision, verified = md.get_merge_request(
477
 
            tree_b.branch.repository)
478
 
        if isinstance(md, merge_directive.MergeDirective):
479
 
            self.assertIs(None, base)
480
 
            self.assertEqual('inapplicable', verified)
481
 
        else:
482
 
            self.assertEqual('rev1', base)
483
 
            self.assertEqual('verified', verified)
484
 
        md.patch='asdf'
485
 
        base, revision, verified = md.get_merge_request(
486
 
            tree_b.branch.repository)
487
 
        if isinstance(md, merge_directive.MergeDirective):
488
 
            self.assertIs(None, base)
489
 
            self.assertEqual('inapplicable', verified)
490
 
        else:
491
 
            self.assertEqual('rev1', base)
492
 
            self.assertEqual('failed', verified)
493
 
 
494
 
    def test_install_revisions_bundle(self):
495
 
        tree_a, tree_b, branch_c = self.make_trees()
496
 
        md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
497
 
            tree_b.branch.base, patch_type='bundle',
498
 
            public_branch=tree_a.branch.base)
499
 
        self.assertFalse(tree_b.branch.repository.has_revision('rev2a'))
500
 
        revision = md.install_revisions(tree_b.branch.repository)
501
 
        self.assertEqual('rev2a', revision)
502
 
        self.assertTrue(tree_b.branch.repository.has_revision('rev2a'))
503
 
 
504
 
    def test_get_target_revision_nofetch(self):
505
 
        tree_a, tree_b, branch_c = self.make_trees()
506
 
        tree_b.branch.fetch(tree_a.branch)
507
 
        md = self.from_objects( tree_a.branch.repository, 'rev2a', 500, 36,
508
 
            tree_b.branch.base, patch_type=None,
509
 
            public_branch=tree_a.branch.base)
510
 
        md.source_branch = '/dev/null'
511
 
        revision = md.install_revisions(tree_b.branch.repository)
512
 
        self.assertEqual('rev2a', revision)
513
 
 
514
 
 
515
 
class TestMergeDirective1Branch(tests.TestCaseWithTransport,
516
 
    TestMergeDirectiveBranch):
517
 
    """Test merge directive format 1 with a branch"""
518
 
 
519
 
    EMAIL1 = EMAIL1
520
 
 
521
 
    EMAIL2 = EMAIL2
522
 
 
523
 
    def from_objects(self, repository, revision_id, time, timezone,
524
 
        target_branch, patch_type='bundle', local_target_branch=None,
525
 
        public_branch=None, message=None):
526
 
        return merge_directive.MergeDirective.from_objects(
527
 
            repository, revision_id, time, timezone, target_branch,
528
 
            patch_type, local_target_branch, public_branch, message)
529
 
 
530
 
    def make_merge_directive(self, revision_id, testament_sha1, time, timezone,
531
 
                 target_branch, patch=None, patch_type=None,
532
 
                 source_branch=None, message=None):
533
 
        return merge_directive.MergeDirective(revision_id, testament_sha1,
534
 
                 time, timezone, target_branch, patch, patch_type,
535
 
                 source_branch, message)
536
 
 
537
 
 
538
 
class TestMergeDirective2Branch(tests.TestCaseWithTransport,
539
 
    TestMergeDirectiveBranch):
540
 
    """Test merge directive format 2 with a branch"""
541
 
 
542
 
    EMAIL1 = EMAIL1_2
543
 
 
544
 
    EMAIL2 = EMAIL2_2
545
 
 
546
 
    def from_objects(self, repository, revision_id, time, timezone,
547
 
        target_branch, patch_type='bundle', local_target_branch=None,
548
 
        public_branch=None, message=None, base_revision_id=None):
549
 
        include_patch = (patch_type in ('bundle', 'diff'))
550
 
        include_bundle = (patch_type == 'bundle')
551
 
        assert patch_type in ('bundle', 'diff', None)
552
 
        return merge_directive.MergeDirective2.from_objects(
553
 
            repository, revision_id, time, timezone, target_branch,
554
 
            include_patch, include_bundle, local_target_branch, public_branch,
555
 
            message, base_revision_id)
556
 
 
557
 
    def make_merge_directive(self, revision_id, testament_sha1, time, timezone,
558
 
                 target_branch, patch=None, patch_type=None,
559
 
                 source_branch=None, message=None, base_revision_id='null:'):
560
 
        if patch_type == 'bundle':
561
 
            bundle = patch
562
 
            patch = None
563
 
        else:
564
 
            bundle = None
565
 
        return merge_directive.MergeDirective2(revision_id, testament_sha1,
566
 
            time, timezone, target_branch, patch, source_branch, message,
567
 
            bundle, base_revision_id)
568
 
 
569
 
    def test_base_revision(self):
570
 
        tree_a, tree_b, branch_c = self.make_trees()
571
 
        md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 60,
572
 
            tree_b.branch.base, patch_type='bundle',
573
 
            public_branch=tree_a.branch.base, base_revision_id=None)
574
 
        self.assertEqual('rev1', md.base_revision_id)
575
 
        md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 60,
576
 
            tree_b.branch.base, patch_type='bundle',
577
 
            public_branch=tree_a.branch.base, base_revision_id='null:')
578
 
        self.assertEqual('null:', md.base_revision_id)
579
 
        lines = md.to_lines()
580
 
        md2 = merge_directive.MergeDirective.from_lines(lines)
581
 
        self.assertEqual(md2.base_revision_id, md.base_revision_id)
582
 
 
583
 
    def test_patch_verification(self):
584
 
        tree_a, tree_b, branch_c = self.make_trees()
585
 
        md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 60,
586
 
            tree_b.branch.base, patch_type='bundle',
587
 
            public_branch=tree_a.branch.base)
588
 
        lines = md.to_lines()
589
 
        md2 = merge_directive.MergeDirective.from_lines(lines)
590
 
        md2._verify_patch(tree_a.branch.repository)
591
 
        # Stript trailing whitespace
592
 
        md2.patch = md2.patch.replace(' \n', '\n')
593
 
        md2._verify_patch(tree_a.branch.repository)
594
 
        # Convert to Mac line-endings
595
 
        md2.patch = md2.patch.replace('\n', '\r')
596
 
        self.assertTrue(md2._verify_patch(tree_a.branch.repository))
597
 
        # Convert to DOS line-endings
598
 
        md2.patch = md2.patch.replace('\r', '\r\n')
599
 
        self.assertTrue(md2._verify_patch(tree_a.branch.repository))
600
 
        md2.patch = md2.patch.replace('content_c', 'content_d')
601
 
        self.assertFalse(md2._verify_patch(tree_a.branch.repository))