65
85
booga""".splitlines(True)
68
class TestMergeDirective(tests.TestCase):
89
I was thinking today about creating a merge directive.
95
(I've pasted it in the body of this message)
99
# Bazaar merge directive format 2 (Bazaar 0.18)\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:
109
booga""".splitlines(True)
112
class TestMergeDirective(object):
70
114
def test_merge_source(self):
72
116
timezone = 5 * 3600
73
self.assertRaises(errors.NoMergeSource, merge_directive.MergeDirective,
117
self.assertRaises(errors.NoMergeSource, self.make_merge_directive,
74
118
'example:', 'sha', time, timezone, 'http://example.com')
75
self.assertRaises(errors.NoMergeSource, merge_directive.MergeDirective,
119
self.assertRaises(errors.NoMergeSource, self.make_merge_directive,
76
120
'example:', 'sha', time, timezone, 'http://example.com',
77
121
patch_type='diff')
78
merge_directive.MergeDirective('example:', 'sha', time, timezone,
122
self.make_merge_directive('example:', 'sha', time, timezone,
79
123
'http://example.com', source_branch='http://example.org')
80
md = merge_directive.MergeDirective('null:', 'sha', time, timezone,
124
md = self.make_merge_directive('null:', 'sha', time, timezone,
81
125
'http://example.com', patch='blah', patch_type='bundle')
82
126
self.assertIs(None, md.source_branch)
83
md2 = merge_directive.MergeDirective('null:', 'sha', time, timezone,
127
md2 = self.make_merge_directive('null:', 'sha', time, timezone,
84
128
'http://example.com', patch='blah', patch_type='bundle',
85
129
source_branch='bar')
86
130
self.assertEqual('bar', md2.source_branch)
88
def test_require_patch(self):
91
self.assertRaises(errors.PatchMissing, merge_directive.MergeDirective,
92
'example:', 'sha', time, timezone, 'http://example.com',
94
md = merge_directive.MergeDirective('example:', 'sha1', time, timezone,
95
'http://example.com', source_branch="http://example.org",
96
patch='', patch_type='diff')
97
self.assertEqual(md.patch, '')
99
132
def test_serialization(self):
102
md = merge_directive.MergeDirective('example:', 'sha', time, timezone,
135
md = self.make_merge_directive('example:', 'sha', time, timezone,
103
136
'http://example.com', patch='booga', patch_type='bundle')
104
self.assertEqualDiff(OUTPUT1, ''.join(md.to_lines()))
105
md = merge_directive.MergeDirective('example:', 'sha', time, timezone,
137
self.assertEqualDiff(self.OUTPUT1, ''.join(md.to_lines()))
138
md = self.make_merge_directive('example:', 'sha', time, timezone,
106
139
'http://example.com', source_branch="http://example.org",
107
140
patch='booga', patch_type='diff', message="Hi mom!")
108
self.assertEqualDiff(OUTPUT2, ''.join(md.to_lines()))
141
self.assertEqualDiff(self.OUTPUT2, ''.join(md.to_lines()))
110
143
def test_deserialize_junk(self):
145
178
self.assertEqual('diff', md2.patch_type)
146
179
self.assertEqual('booga', md2.patch)
147
180
self.assertEqual(None, md2.message)
148
md.patch = "# Bazaar revision bundle v0.9\n#\n"
181
self.set_bundle(md, "# Bazaar revision bundle v0.9\n#\n")
149
182
md.message = "Hi mom!"
150
md3 = merge_directive.MergeDirective.from_lines(md.to_lines())
151
self.assertEqual("# Bazaar revision bundle v0.9\n#\n", md3.patch)
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)
152
186
self.assertEqual("bundle", md3.patch_type)
153
187
self.assertContainsRe(md3.to_lines()[0],
154
188
'^# Bazaar merge directive format ')
155
189
self.assertEqual("Hi mom!", md3.message)
156
md3.patch_type = None
191
self.assertIs(None, md3.get_raw_bundle())
158
192
md4 = merge_directive.MergeDirective.from_lines(md3.to_lines())
159
193
self.assertIs(None, md4.patch_type)
196
class TestMergeDirective1(tests.TestCase, TestMergeDirective):
197
"""Test merge directive format 1"""
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)
213
def set_bundle(md, value):
216
def test_require_patch(self):
219
self.assertRaises(errors.PatchMissing, merge_directive.MergeDirective,
220
'example:', 'sha', time, timezone, 'http://example.com',
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, '')
228
class TestMergeDirective2(tests.TestCase, TestMergeDirective):
229
"""Test merge directive format 2"""
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':
245
return merge_directive.MergeDirective2(revision_id, testament_sha1,
246
time, timezone, target_branch, patch, source_branch, message,
247
bundle, base_revision_id)
250
def set_bundle(md, value):
162
254
EMAIL1 = """To: pqm@example.com
163
255
From: J. Random Hacker <jrandom@example.com>
164
256
Subject: Commit of rev2a
198
315
tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
199
316
branch_c = tree_a.bzrdir.sprout('branch_c').open_branch()
200
317
tree_b.commit('message', rev_id='rev2b')
201
self.build_tree_contents([('tree_a/file', 'content_a\ncontent_c\n')])
318
self.build_tree_contents([('tree_a/file', 'content_a\ncontent_c \n')])
202
319
tree_a.commit('Commit of rev2a', rev_id='rev2a')
203
320
return tree_a, tree_b, branch_c
205
322
def test_empty_target(self):
206
323
tree_a, tree_b, branch_c = self.make_trees()
207
324
tree_d = self.make_branch_and_tree('tree_d')
208
md2 = merge_directive.MergeDirective.from_objects(
209
tree_a.branch.repository, 'rev2a', 500, 120, tree_d.branch.base,
210
patch_type='diff', public_branch=tree_a.branch.base)
325
md2 = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 120,
326
tree_d.branch.base, patch_type='diff',
327
public_branch=tree_a.branch.base)
212
329
def test_generate_patch(self):
213
330
tree_a, tree_b, branch_c = self.make_trees()
214
md2 = merge_directive.MergeDirective.from_objects(
215
tree_a.branch.repository, 'rev2a', 500, 120, tree_b.branch.base,
216
patch_type='diff', public_branch=tree_a.branch.base)
331
md2 = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 120,
332
tree_b.branch.base, patch_type='diff',
333
public_branch=tree_a.branch.base)
217
334
self.assertNotContainsRe(md2.patch, 'Bazaar revision bundle')
218
335
self.assertContainsRe(md2.patch, '\\+content_c')
219
336
self.assertNotContainsRe(md2.patch, '\\+\\+\\+ b/')
222
339
def test_public_branch(self):
223
340
tree_a, tree_b, branch_c = self.make_trees()
224
341
self.assertRaises(errors.PublicBranchOutOfDate,
225
merge_directive.MergeDirective.from_objects,
226
tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
227
public_branch=branch_c.base, patch_type='diff')
342
self.from_objects, tree_a.branch.repository, 'rev2a', 500, 144,
343
tree_b.branch.base, public_branch=branch_c.base, patch_type='diff')
344
self.assertRaises(errors.PublicBranchOutOfDate,
345
self.from_objects, tree_a.branch.repository, 'rev2a', 500, 144,
346
tree_b.branch.base, public_branch=branch_c.base, patch_type=None)
228
347
# public branch is not checked if patch format is bundle.
229
md1 = merge_directive.MergeDirective.from_objects(
230
tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
231
public_branch=branch_c.base)
348
md1 = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 144,
349
tree_b.branch.base, public_branch=branch_c.base)
232
350
# public branch is provided with a bundle, despite possibly being out
233
351
# of date, because it's not required if a bundle is present.
234
352
self.assertEqual(md1.source_branch, branch_c.base)
235
353
# Once we update the public branch, we can generate a diff.
236
354
branch_c.pull(tree_a.branch)
237
md3 = merge_directive.MergeDirective.from_objects(
238
tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
239
patch_type=None, public_branch=branch_c.base)
355
md3 = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 144,
356
tree_b.branch.base, patch_type=None, public_branch=branch_c.base)
241
358
def test_use_public_submit_branch(self):
242
359
tree_a, tree_b, branch_c = self.make_trees()
243
360
branch_c.pull(tree_a.branch)
244
md = merge_directive.MergeDirective.from_objects(
245
tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
246
patch_type=None, public_branch=branch_c.base)
361
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 144,
362
tree_b.branch.base, patch_type=None, public_branch=branch_c.base)
247
363
self.assertEqual(md.target_branch, tree_b.branch.base)
248
364
tree_b.branch.set_public_branch('http://example.com')
249
md2 = merge_directive.MergeDirective.from_objects(
365
md2 = self.from_objects(
250
366
tree_a.branch.repository, 'rev2a', 500, 144, tree_b.branch.base,
251
367
patch_type=None, public_branch=branch_c.base)
252
368
self.assertEqual(md2.target_branch, 'http://example.com')
254
370
def test_message(self):
255
371
tree_a, tree_b, branch_c = self.make_trees()
256
md3 = merge_directive.MergeDirective.from_objects(
257
tree_a.branch.repository, 'rev2a', 500, 120, tree_b.branch.base,
258
patch_type=None, public_branch=branch_c.base,
372
md3 = self.from_objects(tree_a.branch.repository, 'rev1', 500, 120,
373
tree_b.branch.base, patch_type=None, public_branch=branch_c.base,
259
374
message='Merge message')
261
376
self.assertIs(None, md3.patch)
306
420
def test_email(self):
307
421
tree_a, tree_b, branch_c = self.make_trees()
308
md = merge_directive.MergeDirective.from_objects(
309
tree_a.branch.repository, 'rev2a', 476, 60, tree_b.branch.base,
310
patch_type=None, public_branch=tree_a.branch.base)
422
md = self.from_objects(tree_a.branch.repository, 'rev2a', 476, 60,
423
tree_b.branch.base, patch_type=None,
424
public_branch=tree_a.branch.base)
311
425
message = md.to_email('pqm@example.com', tree_a.branch)
312
self.assertContainsRe(message.as_string(), EMAIL1)
426
self.assertContainsRe(message.as_string(), self.EMAIL1)
313
427
md.message = 'Commit of rev2a with special message'
314
428
message = md.to_email('pqm@example.com', tree_a.branch)
315
self.assertContainsRe(message.as_string(), EMAIL2)
429
self.assertContainsRe(message.as_string(), self.EMAIL2)
317
431
def test_install_revisions_branch(self):
318
432
tree_a, tree_b, branch_c = self.make_trees()
319
md = merge_directive.MergeDirective.from_objects(
320
tree_a.branch.repository, 'rev2a', 500, 36, tree_b.branch.base,
321
patch_type=None, public_branch=tree_a.branch.base)
433
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
434
tree_b.branch.base, patch_type=None,
435
public_branch=tree_a.branch.base)
322
436
self.assertFalse(tree_b.branch.repository.has_revision('rev2a'))
323
437
revision = md.install_revisions(tree_b.branch.repository)
324
438
self.assertEqual('rev2a', revision)
325
439
self.assertTrue(tree_b.branch.repository.has_revision('rev2a'))
441
def test_get_merge_request(self):
442
tree_a, tree_b, branch_c = self.make_trees()
443
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
444
tree_b.branch.base, patch_type='bundle',
445
public_branch=tree_a.branch.base)
446
self.assertFalse(tree_b.branch.repository.has_revision('rev2a'))
447
md.install_revisions(tree_b.branch.repository)
448
base, revision, verified = md.get_merge_request(
449
tree_b.branch.repository)
450
if isinstance(md, merge_directive.MergeDirective):
451
self.assertIs(None, base)
452
self.assertEqual('inapplicable', verified)
454
self.assertEqual('rev1', base)
455
self.assertEqual('verified', verified)
456
self.assertEqual('rev2a', revision)
457
self.assertTrue(tree_b.branch.repository.has_revision('rev2a'))
458
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
459
tree_b.branch.base, patch_type=None,
460
public_branch=tree_a.branch.base)
461
base, revision, verified = md.get_merge_request(
462
tree_b.branch.repository)
463
if isinstance(md, merge_directive.MergeDirective):
464
self.assertIs(None, base)
465
self.assertEqual('inapplicable', verified)
467
self.assertEqual('rev1', base)
468
self.assertEqual('inapplicable', verified)
469
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
470
tree_b.branch.base, patch_type='diff',
471
public_branch=tree_a.branch.base)
472
base, revision, verified = md.get_merge_request(
473
tree_b.branch.repository)
474
if isinstance(md, merge_directive.MergeDirective):
475
self.assertIs(None, base)
476
self.assertEqual('inapplicable', verified)
478
self.assertEqual('rev1', base)
479
self.assertEqual('verified', verified)
481
base, revision, verified = md.get_merge_request(
482
tree_b.branch.repository)
483
if isinstance(md, merge_directive.MergeDirective):
484
self.assertIs(None, base)
485
self.assertEqual('inapplicable', verified)
487
self.assertEqual('rev1', base)
488
self.assertEqual('failed', verified)
327
490
def test_install_revisions_bundle(self):
328
491
tree_a, tree_b, branch_c = self.make_trees()
329
md = merge_directive.MergeDirective.from_objects(
330
tree_a.branch.repository, 'rev2a', 500, 36, tree_b.branch.base,
331
patch_type='bundle', public_branch=tree_a.branch.base)
492
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 36,
493
tree_b.branch.base, patch_type='bundle',
494
public_branch=tree_a.branch.base)
332
495
self.assertFalse(tree_b.branch.repository.has_revision('rev2a'))
333
496
revision = md.install_revisions(tree_b.branch.repository)
334
497
self.assertEqual('rev2a', revision)
337
500
def test_get_target_revision_nofetch(self):
338
501
tree_a, tree_b, branch_c = self.make_trees()
339
502
tree_b.branch.fetch(tree_a.branch)
340
md = merge_directive.MergeDirective.from_objects(
341
tree_a.branch.repository, 'rev2a', 500, 36, tree_b.branch.base,
342
patch_type=None, public_branch=tree_a.branch.base)
503
md = self.from_objects( tree_a.branch.repository, 'rev2a', 500, 36,
504
tree_b.branch.base, patch_type=None,
505
public_branch=tree_a.branch.base)
343
506
md.source_branch = '/dev/null'
344
507
revision = md.install_revisions(tree_b.branch.repository)
345
508
self.assertEqual('rev2a', revision)
511
class TestMergeDirective1Branch(tests.TestCaseWithTransport,
512
TestMergeDirectiveBranch):
513
"""Test merge directive format 1 with a branch"""
519
def from_objects(self, repository, revision_id, time, timezone,
520
target_branch, patch_type='bundle', local_target_branch=None,
521
public_branch=None, message=None):
522
return merge_directive.MergeDirective.from_objects(
523
repository, revision_id, time, timezone, target_branch,
524
patch_type, local_target_branch, public_branch, message)
526
def make_merge_directive(self, revision_id, testament_sha1, time, timezone,
527
target_branch, patch=None, patch_type=None,
528
source_branch=None, message=None):
529
return merge_directive.MergeDirective(revision_id, testament_sha1,
530
time, timezone, target_branch, patch, patch_type,
531
source_branch, message)
534
class TestMergeDirective2Branch(tests.TestCaseWithTransport,
535
TestMergeDirectiveBranch):
536
"""Test merge directive format 2 with a branch"""
542
def from_objects(self, repository, revision_id, time, timezone,
543
target_branch, patch_type='bundle', local_target_branch=None,
544
public_branch=None, message=None, base_revision_id=None):
545
include_patch = (patch_type in ('bundle', 'diff'))
546
include_bundle = (patch_type == 'bundle')
547
assert patch_type in ('bundle', 'diff', None)
548
return merge_directive.MergeDirective2.from_objects(
549
repository, revision_id, time, timezone, target_branch,
550
include_patch, include_bundle, local_target_branch, public_branch,
551
message, base_revision_id)
553
def make_merge_directive(self, revision_id, testament_sha1, time, timezone,
554
target_branch, patch=None, patch_type=None,
555
source_branch=None, message=None, base_revision_id='null:'):
556
if patch_type == 'bundle':
561
return merge_directive.MergeDirective2(revision_id, testament_sha1,
562
time, timezone, target_branch, patch, source_branch, message,
563
bundle, base_revision_id)
565
def test_base_revision(self):
566
tree_a, tree_b, branch_c = self.make_trees()
567
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 60,
568
tree_b.branch.base, patch_type='bundle',
569
public_branch=tree_a.branch.base, base_revision_id=None)
570
self.assertEqual('rev1', md.base_revision_id)
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='null:')
574
self.assertEqual('null:', md.base_revision_id)
575
lines = md.to_lines()
576
md2 = merge_directive.MergeDirective.from_lines(lines)
577
self.assertEqual(md2.base_revision_id, md.base_revision_id)
579
def test_patch_verification(self):
580
tree_a, tree_b, branch_c = self.make_trees()
581
md = self.from_objects(tree_a.branch.repository, 'rev2a', 500, 60,
582
tree_b.branch.base, patch_type='bundle',
583
public_branch=tree_a.branch.base)
584
lines = md.to_lines()
585
md2 = merge_directive.MergeDirective.from_lines(lines)
586
md2._verify_patch(tree_a.branch.repository)
587
# Stript trailing whitespace
588
md2.patch = md2.patch.replace(' \n', '\n')
589
md2._verify_patch(tree_a.branch.repository)
590
# Convert to Mac line-endings
591
md2.patch = md2.patch.replace('\n', '\r')
592
self.assertTrue(md2._verify_patch(tree_a.branch.repository))
593
# Convert to DOS line-endings
594
md2.patch = md2.patch.replace('\r', '\r\n')
595
self.assertTrue(md2._verify_patch(tree_a.branch.repository))
596
md2.patch = md2.patch.replace('content_c', 'content_d')
597
self.assertFalse(md2._verify_patch(tree_a.branch.repository))