summaryrefslogtreecommitdiff
path: root/bzrlib/tests/per_branch/test_tags.py
blob: 7556dba5f05c53f8755b40fde216752e561d0b8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# Copyright (C) 2007, 2009, 2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Tags stored within a branch

The tags are actually in the Branch.tags namespace, but these are
1:1 with Branch implementations so can be tested from here.
"""

from bzrlib import (
    branch,
    controldir,
    errors,
    tests,
    )
from bzrlib.tests import per_branch


class TestBranchTags(per_branch.TestCaseWithBranch):

    def setUp(self):
        super(TestBranchTags, self).setUp()
        # formats that don't support tags can skip the rest of these
        # tests...
        branch = self.make_branch('probe')
        if not branch._format.supports_tags():
            raise tests.TestSkipped(
                "format %s doesn't support tags" % branch._format)

    def make_branch_with_revisions(self, relpath, revisions):
        builder = self.make_branch_builder(relpath)
        builder.start_series()
        for revid in revisions:
            builder.build_commit(rev_id=revid)
        builder.finish_series()
        return builder.get_branch()

    def test_tags_initially_empty(self):
        b = self.make_branch('b')
        tags = b.tags.get_tag_dict()
        self.assertEqual(tags, {})

    def test_make_and_lookup_tag(self):
        b = self.make_branch_with_revisions('b',
            ['target-revid-1', 'target-revid-2'])
        b.tags.set_tag('tag-name', 'target-revid-1')
        b.tags.set_tag('other-name', 'target-revid-2')
        # then reopen the branch and see they're still there
        b = branch.Branch.open('b')
        self.assertEqual(b.tags.get_tag_dict(),
            {'tag-name': 'target-revid-1',
             'other-name': 'target-revid-2',
            })
        # read one at a time
        result = b.tags.lookup_tag('tag-name')
        self.assertEqual(result, 'target-revid-1')
        # and try has_tag
        self.assertTrue(b.tags.has_tag('tag-name'))
        self.assertFalse(b.tags.has_tag('imaginary'))

    def test_reverse_tag_dict(self):
        b = self.make_branch_with_revisions('b',
            ['target-revid-1', 'target-revid-2'])
        b.tags.set_tag('tag-name', 'target-revid-1')
        b.tags.set_tag('other-name', 'target-revid-2')
        # then reopen the branch and check reverse map id->tags list
        b = branch.Branch.open('b')
        self.assertEqual(dict(b.tags.get_reverse_tag_dict()),
            {'target-revid-1': ['tag-name'],
             'target-revid-2': ['other-name'],
            })

    def test_ghost_tag(self):
        b = self.make_branch('b')
        if not b._format.supports_tags_referencing_ghosts():
            self.assertRaises(errors.GhostTagsNotSupported,
                b.tags.set_tag, "ghost", "idontexist")
        else:
            b.tags.set_tag("ghost", "idontexist")
            self.assertEquals("idontexist", b.tags.lookup_tag("ghost"))

    def test_no_such_tag(self):
        b = self.make_branch('b')
        try:
            b.tags.lookup_tag('bosko')
        except errors.NoSuchTag, e:
            self.assertEquals(e.tag_name, 'bosko')
            self.assertEquals(str(e), 'No such tag: bosko')
        else:
            self.fail("didn't get expected exception")

    def test_merge_tags(self):
        b1 = self.make_branch_with_revisions('b1', ['revid', 'revid-1'])
        b2 = self.make_branch_with_revisions('b2', ['revid', 'revid-2'])
        # if there are tags in the source and not the destination, then they
        # just go across
        b1.tags.set_tag('tagname', 'revid')
        b1.tags.merge_to(b2.tags)
        self.assertEquals(b2.tags.lookup_tag('tagname'), 'revid')
        # if a tag is in the destination and not in the source, it is not
        # removed when we merge them
        b2.tags.set_tag('in-destination', 'revid')
        updates, conflicts = b1.tags.merge_to(b2.tags)
        self.assertEquals(list(conflicts), [])
        self.assertEquals(updates, {})
        self.assertEquals(b2.tags.lookup_tag('in-destination'), 'revid')
        # if there's a conflicting tag, it's reported -- the command line
        # interface will say "these tags couldn't be copied"
        b1.tags.set_tag('conflicts', 'revid-1')
        b2.tags.set_tag('conflicts', 'revid-2')
        updates, conflicts = b1.tags.merge_to(b2.tags)
        self.assertEquals(list(conflicts),
            [('conflicts', 'revid-1', 'revid-2')])
        # and it keeps the same value
        self.assertEquals(updates, {})
        self.assertEquals(b2.tags.lookup_tag('conflicts'), 'revid-2')

    def test_unicode_tag(self):
        tag_name = u'\u3070'
        # in anticipation of the planned change to treating revision ids as
        # just 8bit strings
        revid = ('revid' + tag_name).encode('utf-8')
        b1 = self.make_branch_with_revisions('b', [revid])
        b1.tags.set_tag(tag_name, revid)
        self.assertEquals(b1.tags.lookup_tag(tag_name), revid)

    def test_delete_tag(self):
        tag_name = u'\N{GREEK SMALL LETTER ALPHA}'
        revid = ('revid' + tag_name).encode('utf-8')
        b = self.make_branch_with_revisions('b', [revid])
        b.tags.set_tag(tag_name, revid)
        # now try to delete it
        b.tags.delete_tag(tag_name)
        # now you can't look it up
        self.assertRaises(errors.NoSuchTag,
            b.tags.lookup_tag, tag_name)
        # and it's not in the dictionary
        self.assertEquals(b.tags.get_tag_dict(), {})
        # and you can't remove it a second time
        self.assertRaises(errors.NoSuchTag,
            b.tags.delete_tag, tag_name)
        # or remove a tag that never existed
        self.assertRaises(errors.NoSuchTag,
            b.tags.delete_tag, tag_name + '2')

    def test_merge_empty_tags(self):
        # you can merge tags between two instances, since neither have tags
        b1 = self.make_branch('b1')
        b2 = self.make_branch('b2')
        b1.tags.merge_to(b2.tags)

    def test_read_lock_caches_tags(self):
        """Tags are read from a branch only once during a read-lock."""
        # Open the same branch twice.  Read-lock one, and then mutate the tags
        # in the second.  The read-locked branch never re-reads the tags, so it
        # never observes the changed/new tags.
        b1 = self.make_branch_with_revisions('b',
            ['rev-1', 'rev-1-changed', 'rev-2'])
        b1.tags.set_tag('one', 'rev-1')
        b2 = controldir.ControlDir.open('b').open_branch()
        b1.lock_read()
        self.assertEqual({'one': 'rev-1'}, b1.tags.get_tag_dict())
        # Add a tag and modify a tag in b2.  b1 is read-locked and has already
        # read the tags, so it is unaffected.
        b2.tags.set_tag('one', 'rev-1-changed')
        b2.tags.set_tag('two', 'rev-2')
        self.assertEqual({'one': 'rev-1'}, b1.tags.get_tag_dict())
        b1.unlock()
        # Once unlocked the cached value is forgotten, so now the latest tags
        # will be retrieved.
        self.assertEqual(
            {'one': 'rev-1-changed', 'two': 'rev-2'}, b1.tags.get_tag_dict())

    def test_unlocked_does_not_cache_tags(self):
        """Unlocked branches do not cache tags."""
        # Open the same branch twice.
        b1 = self.make_branch_with_revisions('b',
            ['rev-1', 'rev-1-changed', 'rev-2'])
        b1.tags.set_tag('one', 'rev-1')
        b2 = b1.bzrdir.open_branch()
        self.assertEqual({'one': 'rev-1'}, b1.tags.get_tag_dict())
        # Add a tag and modify a tag in b2.  b1 isn't locked, so it will
        # immediately return the new tags too.
        b2.tags.set_tag('one', 'rev-1-changed')
        b2.tags.set_tag('two', 'rev-2')
        self.assertEqual(
            {'one': 'rev-1-changed', 'two': 'rev-2'}, b1.tags.get_tag_dict())

    def test_cached_tag_dict_not_accidentally_mutable(self):
        """When there's a cached version of the tags, b.tags.get_tag_dict
        returns a copy of the cached data so that callers cannot accidentally
        corrupt the cache.
        """
        b = self.make_branch_with_revisions('b',
            ['rev-1', 'rev-2', 'rev-3'])
        b.tags.set_tag('one', 'rev-1')
        self.addCleanup(b.lock_read().unlock)
        # The first time the data returned will not be in the cache
        tags_dict = b.tags.get_tag_dict()
        tags_dict['two'] = 'rev-2'
        # The second time the data comes from the cache
        tags_dict = b.tags.get_tag_dict()
        tags_dict['three'] = 'rev-3'
        # The get_tag_dict() result should still be unchanged, even though we
        # mutated its earlier return values.
        self.assertEqual({'one': 'rev-1'}, b.tags.get_tag_dict())

    def make_write_locked_branch_with_one_tag(self):
        b = self.make_branch_with_revisions('b',
            ['rev-1', 'rev-1-changed', 'rev-2'])
        b.tags.set_tag('one', 'rev-1')
        self.addCleanup(b.lock_write().unlock)
        # Populate the cache
        b.tags.get_tag_dict()
        return b

    def test_set_tag_invalides_cache(self):
        b = self.make_write_locked_branch_with_one_tag()
        b.tags.set_tag('one', 'rev-1-changed')
        self.assertEqual({'one': 'rev-1-changed'}, b.tags.get_tag_dict())

    def test_delete_tag_invalides_cache(self):
        b = self.make_write_locked_branch_with_one_tag()
        b.tags.delete_tag('one')
        self.assertEqual({}, b.tags.get_tag_dict())

    def test_merge_to_invalides_cache(self):
        b1 = self.make_write_locked_branch_with_one_tag()
        b2 = self.make_branch_with_revisions('b2', ['rev-2', 'rev-1'])
        b2.tags.set_tag('two', 'rev-2')
        b2.tags.merge_to(b1.tags)
        self.assertEqual(
            {'one': 'rev-1', 'two': 'rev-2'}, b1.tags.get_tag_dict())

    def test_rename_revisions_invalides_cache(self):
        b = self.make_write_locked_branch_with_one_tag()
        b.tags.rename_revisions({'rev-1': 'rev-1-changed'})
        self.assertEqual({'one': 'rev-1-changed'}, b.tags.get_tag_dict())


class TestTagsMergeToInCheckouts(per_branch.TestCaseWithBranch):
    """Tests for checkout.branch.tags.merge_to.
    
    In particular this exercises variations in tag conflicts in the master
    branch and/or the checkout (child).  It may seem strange to have different
    tags in the child and master, but 'bzr merge' intentionally updates the
    child and not the master (instead the next 'bzr commit', if the user
    decides to commit, will update the master).  Also, merge_to in bzr < 2.3
    didn't propagate changes to the master, and current bzr versions may find
    themselves operating on checkouts touched by older bzrs
    
    So we need to make sure bzr copes gracefully with differing tags in the
    master versus the child.

    See also <https://bugs.launchpad.net/bzr/+bug/603395>.
    """

    def setUp(self):
        super(TestTagsMergeToInCheckouts, self).setUp()
        branch1 = self.make_branch('tags-probe')
        if not branch1._format.supports_tags():
            raise tests.TestSkipped(
                "format %s doesn't support tags" % branch1._format)
        branch2 = self.make_branch('bind-probe')
        try:
            branch2.bind(branch1)
        except errors.UpgradeRequired:
            raise tests.TestNotApplicable(
                "format %s doesn't support bound branches" % branch2._format)

    def test_merge_to_propagates_tags(self):
        """merge_to(child) also merges tags to the master."""
        master = self.make_branch('master')
        other = self.make_branch('other')
        other.tags.set_tag('foo', 'rev-1')
        child = self.make_branch('child')
        child.bind(master)
        child.update()
        other.tags.merge_to(child.tags)
        self.assertEquals('rev-1', child.tags.lookup_tag('foo'))
        self.assertEquals('rev-1', master.tags.lookup_tag('foo'))

    def test_ignore_master_disables_tag_propagation(self):
        """merge_to(child, ignore_master=True) does not merge tags to the
        master.
        """
        master = self.make_branch('master')
        other = self.make_branch('other')
        other.tags.set_tag('foo', 'rev-1')
        child = self.make_branch('child')
        child.bind(master)
        child.update()
        other.tags.merge_to(child.tags, ignore_master=True)
        self.assertEquals('rev-1', child.tags.lookup_tag('foo'))
        self.assertRaises(errors.NoSuchTag, master.tags.lookup_tag, 'foo')

    def test_merge_to_overwrite_conflict_in_master(self):
        """merge_to(child, overwrite=True) overwrites any conflicting tags in
        the master.
        """
        master = self.make_branch('master')
        other = self.make_branch('other')
        other.tags.set_tag('foo', 'rev-1')
        child = self.make_branch('child')
        child.bind(master)
        child.update()
        master.tags.set_tag('foo', 'rev-2')
        tag_updates, tag_conflicts = other.tags.merge_to(child.tags, overwrite=True)
        self.assertEquals('rev-1', child.tags.lookup_tag('foo'))
        self.assertEquals('rev-1', master.tags.lookup_tag('foo'))
        self.assertEquals({"foo": "rev-1"}, tag_updates)
        self.assertLength(0, tag_conflicts)

    def test_merge_to_overwrite_conflict_in_child_and_master(self):
        """merge_to(child, overwrite=True) overwrites any conflicting tags in
        both the child and the master.
        """
        master = self.make_branch('master')
        master.tags.set_tag('foo', 'rev-2')
        other = self.make_branch('other')
        other.tags.set_tag('foo', 'rev-1')
        child = self.make_branch('child')
        child.bind(master)
        child.update()
        tag_updates, tag_conflicts = other.tags.merge_to(
            child.tags, overwrite=True)
        self.assertEquals('rev-1', child.tags.lookup_tag('foo'))
        self.assertEquals('rev-1', master.tags.lookup_tag('foo'))
        self.assertEquals({u'foo': 'rev-1'}, tag_updates)
        self.assertLength(0, tag_conflicts)

    def test_merge_to_conflict_in_child_only(self):
        """When new_tags.merge_to(child.tags) conflicts with the child but not
        the master, a conflict is reported and the child receives the new tag.
        """
        master = self.make_branch('master')
        master.tags.set_tag('foo', 'rev-2')
        other = self.make_branch('other')
        other.tags.set_tag('foo', 'rev-1')
        child = self.make_branch('child')
        child.bind(master)
        child.update()
        master.tags.delete_tag('foo')
        tag_updates, tag_conflicts = other.tags.merge_to(child.tags)
        # Conflict in child, so it is unchanged.
        self.assertEquals('rev-2', child.tags.lookup_tag('foo'))
        # No conflict in the master, so the 'foo' tag equals other's value here.
        self.assertEquals('rev-1', master.tags.lookup_tag('foo'))
        # The conflict is reported.
        self.assertEquals([(u'foo', 'rev-1', 'rev-2')], list(tag_conflicts))
        self.assertEquals({u'foo': 'rev-1'}, tag_updates)

    def test_merge_to_conflict_in_master_only(self):
        """When new_tags.merge_to(child.tags) conflicts with the master but not
        the child, a conflict is reported and the child receives the new tag.
        """
        master = self.make_branch('master')
        other = self.make_branch('other')
        other.tags.set_tag('foo', 'rev-1')
        child = self.make_branch('child')
        child.bind(master)
        child.update()
        master.tags.set_tag('foo', 'rev-2')
        tag_updates, tag_conflicts = other.tags.merge_to(child.tags)
        # No conflict in the child, so the 'foo' tag equals other's value here.
        self.assertEquals('rev-1', child.tags.lookup_tag('foo'))
        # Conflict in master, so it is unchanged.
        self.assertEquals('rev-2', master.tags.lookup_tag('foo'))
        # The conflict is reported.
        self.assertEquals({u'foo': 'rev-1'}, tag_updates)
        self.assertEquals([(u'foo', 'rev-1', 'rev-2')], list(tag_conflicts))

    def test_merge_to_same_conflict_in_master_and_child(self):
        """When new_tags.merge_to(child.tags) conflicts the same way with the
        master and the child a single conflict is reported.
        """
        master = self.make_branch('master')
        master.tags.set_tag('foo', 'rev-2')
        other = self.make_branch('other')
        other.tags.set_tag('foo', 'rev-1')
        child = self.make_branch('child')
        child.bind(master)
        child.update()
        tag_updates, tag_conflicts = other.tags.merge_to(child.tags)
        # Both master and child conflict, so both stay as rev-2
        self.assertEquals('rev-2', child.tags.lookup_tag('foo'))
        self.assertEquals('rev-2', master.tags.lookup_tag('foo'))
        # The conflict is reported exactly once, even though it occurs in both
        # master and child.
        self.assertEquals({}, tag_updates)
        self.assertEquals([(u'foo', 'rev-1', 'rev-2')], list(tag_conflicts))

    def test_merge_to_different_conflict_in_master_and_child(self):
        """When new_tags.merge_to(child.tags) conflicts differently in the
        master and the child both conflicts are reported.
        """
        master = self.make_branch('master')
        master.tags.set_tag('foo', 'rev-2')
        other = self.make_branch('other')
        other.tags.set_tag('foo', 'rev-1')
        child = self.make_branch('child')
        child.bind(master)
        child.update()
        # We use the private method _set_tag_dict because normally bzr tries to
        # avoid this scenario.
        child.tags._set_tag_dict({'foo': 'rev-3'})
        tag_updates, tag_conflicts = other.tags.merge_to(child.tags)
        # Both master and child conflict, so both stay as they were.
        self.assertEquals('rev-3', child.tags.lookup_tag('foo'))
        self.assertEquals('rev-2', master.tags.lookup_tag('foo'))
        # Both conflicts are reported.
        self.assertEquals({}, tag_updates)
        self.assertEquals(
            [(u'foo', 'rev-1', 'rev-2'), (u'foo', 'rev-1', 'rev-3')],
            sorted(tag_conflicts))


class TestUnsupportedTags(per_branch.TestCaseWithBranch):
    """Formats that don't support tags should give reasonable errors."""

    def setUp(self):
        super(TestUnsupportedTags, self).setUp()
        branch = self.make_branch('probe')
        if branch._format.supports_tags():
            raise tests.TestSkipped("Format %s declares that tags are supported"
                                    % branch._format)
            # it's covered by TestBranchTags

    def test_tag_methods_raise(self):
        b = self.make_branch('b')
        self.assertRaises(errors.TagsNotSupported,
            b.tags.set_tag, 'foo', 'bar')
        self.assertRaises(errors.TagsNotSupported,
            b.tags.lookup_tag, 'foo')
        self.assertRaises(errors.TagsNotSupported,
            b.tags.set_tag, 'foo', 'bar')
        self.assertRaises(errors.TagsNotSupported,
            b.tags.delete_tag, 'foo')

    def test_merge_empty_tags(self):
        # you can merge tags between two instances, since neither have tags
        b1 = self.make_branch('b1')
        b2 = self.make_branch('b2')
        b1.tags.merge_to(b2.tags)


class AutomaticTagNameTests(per_branch.TestCaseWithBranch):

    def setUp(self):
        super(AutomaticTagNameTests, self).setUp()
        if isinstance(self.branch_format, branch.BranchReferenceFormat):
            # This test could in principle apply to BranchReferenceFormat, but
            # make_branch_builder doesn't support it.
            raise tests.TestSkipped(
                "BranchBuilder can't make reference branches.")
        self.builder = self.make_branch_builder('.')
        self.builder.build_snapshot('foo', None,
            [('add', ('', None, 'directory', None))],
            message='foo')
        self.branch = self.builder.get_branch()
        if not self.branch._format.supports_tags():
            raise tests.TestSkipped(
                "format %s doesn't support tags" % self.branch._format)

    def test_no_functions(self):
        rev = self.branch.last_revision()
        self.assertEquals(None, self.branch.automatic_tag_name(rev))

    def test_returns_tag_name(self):
        def get_tag_name(br, revid):
            return "foo"
        branch.Branch.hooks.install_named_hook('automatic_tag_name',
            get_tag_name, 'get tag name foo')
        self.assertEquals("foo", self.branch.automatic_tag_name(
            self.branch.last_revision()))

    def test_uses_first_return(self):
        get_tag_name_1 = lambda br, revid: "foo1"
        get_tag_name_2 = lambda br, revid: "foo2"
        branch.Branch.hooks.install_named_hook('automatic_tag_name',
            get_tag_name_1, 'tagname1')
        branch.Branch.hooks.install_named_hook('automatic_tag_name',
            get_tag_name_2, 'tagname2')
        self.assertEquals("foo1", self.branch.automatic_tag_name(
            self.branch.last_revision()))

    def test_ignores_none(self):
        get_tag_name_1 = lambda br, revid: None
        get_tag_name_2 = lambda br, revid: "foo2"
        branch.Branch.hooks.install_named_hook('automatic_tag_name',
            get_tag_name_1, 'tagname1')
        branch.Branch.hooks.install_named_hook('automatic_tag_name',
            get_tag_name_2, 'tagname2')
        self.assertEquals("foo2", self.branch.automatic_tag_name(
            self.branch.last_revision()))