~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_revisionnamespaces.py

  • Committer: Ian Clatworthy
  • Date: 2007-12-11 02:07:30 UTC
  • mto: (3119.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 3120.
  • Revision ID: ian.clatworthy@internode.on.net-20071211020730-sdj4kj794dw0628e
make help topics more discoverable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2004, 2005, 2006, 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
import datetime
 
18
import os
18
19
import time
19
20
 
20
21
from bzrlib import (
 
22
    branch,
 
23
    bzrdir,
21
24
    errors,
22
 
    revision as _mod_revision,
 
25
    repository,
23
26
    )
24
 
from bzrlib.tests import TestCaseWithTransport
 
27
from bzrlib.tests import TestCase, TestCaseWithTransport
25
28
from bzrlib.revisionspec import (
26
29
    RevisionSpec,
 
30
    RevisionSpec_revno,
27
31
    RevisionSpec_tag,
28
32
    )
29
33
 
45
49
 
46
50
        self.tree = self.make_branch_and_tree('tree')
47
51
        self.build_tree(['tree/a'])
48
 
        self.tree.lock_write()
49
 
        self.addCleanup(self.tree.unlock)
50
52
        self.tree.add(['a'])
51
53
        self.tree.commit('a', rev_id='r1')
52
54
 
53
55
        self.tree2 = self.tree.bzrdir.sprout('tree2').open_workingtree()
54
56
        self.tree2.commit('alt', rev_id='alt_r2')
55
57
 
56
 
        self.tree.merge_from_branch(self.tree2.branch)
 
58
        self.tree.branch.repository.fetch(self.tree2.branch.repository,
 
59
                                          revision_id='alt_r2')
 
60
        self.tree.set_pending_merges(['alt_r2'])
57
61
        self.tree.commit('second', rev_id='r2')
58
62
 
59
63
    def get_in_history(self, revision_spec):
69
73
                         ' %r != %r'
70
74
                         % (revision_spec, exp_revision_id, rev_info.rev_id))
71
75
 
72
 
    def assertInvalid(self, revision_spec, extra='',
73
 
                      invalid_as_revision_id=True):
 
76
    def assertInvalid(self, revision_spec, extra=''):
74
77
        try:
75
78
            self.get_in_history(revision_spec)
76
79
        except errors.InvalidRevisionSpec, e:
77
80
            self.assertEqual(revision_spec, e.spec)
78
81
            self.assertEqual(extra, e.extra)
79
82
        else:
80
 
            self.fail('Expected InvalidRevisionSpec to be raised for'
81
 
                      ' %r.in_history' % (revision_spec,))
82
 
        if invalid_as_revision_id:
83
 
            try:
84
 
                spec = RevisionSpec.from_string(revision_spec)
85
 
                spec.as_revision_id(self.tree.branch)
86
 
            except errors.InvalidRevisionSpec, e:
87
 
                self.assertEqual(revision_spec, e.spec)
88
 
                self.assertEqual(extra, e.extra)
89
 
            else:
90
 
                self.fail('Expected InvalidRevisionSpec to be raised for'
91
 
                          ' %r.as_revision_id' % (revision_spec,))
92
 
 
93
 
    def assertAsRevisionId(self, revision_id, revision_spec):
94
 
        """Calling as_revision_id() should return the specified id."""
95
 
        spec = RevisionSpec.from_string(revision_spec)
96
 
        self.assertEqual(revision_id,
97
 
                         spec.as_revision_id(self.tree.branch))
98
 
 
99
 
    def get_as_tree(self, revision_spec, tree=None):
100
 
        if tree is None:
101
 
            tree = self.tree
102
 
        spec = RevisionSpec.from_string(revision_spec)
103
 
        return spec.as_tree(tree.branch)
104
 
 
105
 
 
106
 
class RevisionSpecMatchOnTrap(RevisionSpec):
107
 
 
108
 
    def _match_on(self, branch, revs):
109
 
        self.last_call = (branch, revs)
110
 
        return super(RevisionSpecMatchOnTrap, self)._match_on(branch, revs)
111
 
 
112
 
 
113
 
class TestRevisionSpecBase(TestRevisionSpec):
114
 
 
115
 
    def test_wants_revision_history(self):
116
 
        # If wants_revision_history = True, then _match_on should get the
117
 
        # branch revision history
118
 
        spec = RevisionSpecMatchOnTrap('foo', _internal=True)
119
 
        spec.in_history(self.tree.branch)
120
 
 
121
 
        self.assertEqual((self.tree.branch, ['r1' ,'r2']),
122
 
                         spec.last_call)
123
 
 
124
 
    def test_wants_no_revision_history(self):
125
 
        # If wants_revision_history = False, then _match_on should get None for
126
 
        # the branch revision history
127
 
        spec = RevisionSpecMatchOnTrap('foo', _internal=True)
128
 
        spec.wants_revision_history = False
129
 
        spec.in_history(self.tree.branch)
130
 
 
131
 
        self.assertEqual((self.tree.branch, None), spec.last_call)
132
 
 
 
83
            self.fail('Expected InvalidRevisionSpec to be raised for %s'
 
84
                      % (revision_spec,))
133
85
 
134
86
 
135
87
class TestOddRevisionSpec(TestRevisionSpec):
136
88
    """Test things that aren't normally thought of as revision specs"""
137
89
 
138
90
    def test_none(self):
139
 
        self.assertInHistoryIs(None, None, None)
 
91
        self.assertInHistoryIs(0, None, None)
140
92
 
141
93
    def test_object(self):
142
94
        self.assertRaises(TypeError, RevisionSpec.from_string, object())
143
95
 
144
 
 
145
 
class TestRevisionSpec_dwim(TestRevisionSpec):
146
 
 
147
 
    # Don't need to test revno's explicitly since TRS_revno already
148
 
    # covers that well for us
149
 
    def test_dwim_spec_revno(self):
150
 
        self.assertInHistoryIs(2, 'r2', '2')
151
 
        self.assertAsRevisionId('alt_r2', '1.1.1')
152
 
 
153
 
    def test_dwim_spec_revid(self):
154
 
        self.assertInHistoryIs(2, 'r2', 'r2')
155
 
 
156
 
    def test_dwim_spec_tag(self):
157
 
        self.tree.branch.tags.set_tag('footag', 'r1')
158
 
        self.assertAsRevisionId('r1', 'footag')
159
 
        self.tree.branch.tags.delete_tag('footag')
160
 
        self.assertRaises(errors.InvalidRevisionSpec,
161
 
                          self.get_in_history, 'footag')
162
 
 
163
 
    def test_dwim_spec_tag_that_looks_like_revno(self):
164
 
        # Test that we slip past revno with things that look like revnos,
165
 
        # but aren't.  Tags are convenient for testing this since we can
166
 
        # make them look however we want.
167
 
        self.tree.branch.tags.set_tag('3', 'r2')
168
 
        self.assertAsRevisionId('r2', '3')
169
 
        self.build_tree(['tree/b'])
170
 
        self.tree.add(['b'])
171
 
        self.tree.commit('b', rev_id='r3')
172
 
        self.assertAsRevisionId('r3', '3')
173
 
 
174
 
    def test_dwim_spec_date(self):
175
 
        self.assertAsRevisionId('r1', 'today')
176
 
 
177
 
    def test_dwim_spec_branch(self):
178
 
        self.assertInHistoryIs(None, 'alt_r2', 'tree2')
179
 
 
180
 
    def test_dwim_spec_nonexistent(self):
181
 
        self.assertInvalid('somethingrandom', invalid_as_revision_id=False)
182
 
        self.assertInvalid('-1.1', invalid_as_revision_id=False)
183
 
        self.assertInvalid('.1', invalid_as_revision_id=False)
184
 
        self.assertInvalid('1..1', invalid_as_revision_id=False)
185
 
        self.assertInvalid('1.2..1', invalid_as_revision_id=False)
186
 
        self.assertInvalid('1.', invalid_as_revision_id=False)
 
96
    def test_unregistered_spec(self):
 
97
        self.assertRaises(errors.NoSuchRevisionSpec,
 
98
                          RevisionSpec.from_string, 'foo')
 
99
        self.assertRaises(errors.NoSuchRevisionSpec,
 
100
                          RevisionSpec.from_string, '123a')
 
101
 
 
102
 
 
103
 
 
104
class TestRevnoFromString(TestCase):
 
105
 
 
106
    def test_from_string_dotted_decimal(self):
 
107
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '-1.1')
 
108
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '.1')
 
109
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '1..1')
 
110
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '1.2..1')
 
111
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '1.')
 
112
        self.assertIsInstance(RevisionSpec.from_string('1.1'), RevisionSpec_revno)
 
113
        self.assertIsInstance(RevisionSpec.from_string('1.1.3'), RevisionSpec_revno)
187
114
 
188
115
 
189
116
class TestRevisionSpec_revno(TestRevisionSpec):
196
123
 
197
124
    def test_dotted_decimal(self):
198
125
        self.assertInHistoryIs(None, 'alt_r2', '1.1.1')
199
 
        self.assertInvalid('1.1.123')
200
126
 
201
127
    def test_negative_int(self):
202
128
        self.assertInHistoryIs(2, 'r2', '-1')
296
222
        """Old revno:N:path tests"""
297
223
        wta = self.make_branch_and_tree('a')
298
224
        ba = wta.branch
299
 
 
 
225
        
300
226
        wta.commit('Commit one', rev_id='a@r-0-1')
301
227
        wta.commit('Commit two', rev_id='a@r-0-2')
302
228
        wta.commit('Commit three', rev_id='a@r-0-3')
320
246
        self.assertEqual((2, 'b@r-0-2'),
321
247
                         spec_in_history('revno:2:b/', None))
322
248
 
323
 
    def test_as_revision_id(self):
324
 
        self.assertAsRevisionId('null:', '0')
325
 
        self.assertAsRevisionId('r1', '1')
326
 
        self.assertAsRevisionId('r2', '2')
327
 
        self.assertAsRevisionId('r1', '-2')
328
 
        self.assertAsRevisionId('r2', '-1')
329
 
        self.assertAsRevisionId('alt_r2', '1.1.1')
330
 
 
331
 
    def test_as_tree(self):
332
 
        tree = self.get_as_tree('0')
333
 
        self.assertEquals(_mod_revision.NULL_REVISION, tree.get_revision_id())
334
 
        tree = self.get_as_tree('1')
335
 
        self.assertEquals('r1', tree.get_revision_id())
336
 
        tree = self.get_as_tree('2')
337
 
        self.assertEquals('r2', tree.get_revision_id())
338
 
        tree = self.get_as_tree('-2')
339
 
        self.assertEquals('r1', tree.get_revision_id())
340
 
        tree = self.get_as_tree('-1')
341
 
        self.assertEquals('r2', tree.get_revision_id())
342
 
        tree = self.get_as_tree('1.1.1')
343
 
        self.assertEquals('alt_r2', tree.get_revision_id())
344
249
 
345
250
 
346
251
class TestRevisionSpec_revid(TestRevisionSpec):
347
 
 
 
252
    
348
253
    def test_in_history(self):
349
254
        # We should be able to access revisions that are directly
350
255
        # in the history.
351
256
        self.assertInHistoryIs(1, 'r1', 'revid:r1')
352
257
        self.assertInHistoryIs(2, 'r2', 'revid:r2')
353
 
 
 
258
        
354
259
    def test_missing(self):
355
 
        self.assertInvalid('revid:r3', invalid_as_revision_id=False)
 
260
        self.assertInvalid('revid:r3')
356
261
 
357
262
    def test_merged(self):
358
263
        """We can reach revisions in the ancestry"""
361
266
    def test_not_here(self):
362
267
        self.tree2.commit('alt third', rev_id='alt_r3')
363
268
        # It exists in tree2, but not in tree
364
 
        self.assertInvalid('revid:alt_r3', invalid_as_revision_id=False)
 
269
        self.assertInvalid('revid:alt_r3')
365
270
 
366
271
    def test_in_repository(self):
367
272
        """We can get any revision id in the repository"""
378
283
        self.assertInHistoryIs(3, revision_id, u'revid:\N{SNOWMAN}')
379
284
        self.assertInHistoryIs(3, revision_id, 'revid:' + revision_id)
380
285
 
381
 
    def test_as_revision_id(self):
382
 
        self.assertAsRevisionId('r1', 'revid:r1')
383
 
        self.assertAsRevisionId('r2', 'revid:r2')
384
 
        self.assertAsRevisionId('alt_r2', 'revid:alt_r2')
385
 
 
386
286
 
387
287
class TestRevisionSpec_last(TestRevisionSpec):
388
288
 
414
314
            pass
415
315
        self.assertInvalid('last:Y', extra='\n' + str(e))
416
316
 
417
 
    def test_as_revision_id(self):
418
 
        self.assertAsRevisionId('r2', 'last:1')
419
 
        self.assertAsRevisionId('r1', 'last:2')
420
 
 
421
317
 
422
318
class TestRevisionSpec_before(TestRevisionSpec):
423
319
 
449
345
                                          revision_id='new_r1')
450
346
        self.assertInHistoryIs(0, 'null:', 'before:revid:new_r1')
451
347
 
452
 
    def test_as_revision_id(self):
453
 
        self.assertAsRevisionId('r1', 'before:revid:r2')
454
 
        self.assertAsRevisionId('r1', 'before:2')
455
 
        self.assertAsRevisionId('r1', 'before:1.1.1')
456
 
        self.assertAsRevisionId('r1', 'before:revid:alt_r2')
457
 
 
458
348
 
459
349
class TestRevisionSpec_tag(TestRevisionSpec):
460
 
 
 
350
    
461
351
    def make_branch_and_tree(self, relpath):
462
352
        # override format as the default one may not support tags
463
 
        return TestRevisionSpec.make_branch_and_tree(
464
 
            self, relpath, format='dirstate-tags')
 
353
        control = bzrdir.BzrDir.create(relpath)
 
354
        control.create_repository()
 
355
        branch.BzrBranchExperimental.initialize(control)
 
356
        return control.create_workingtree()
465
357
 
466
358
    def test_from_string_tag(self):
467
359
        spec = RevisionSpec.from_string('tag:bzr-0.14')
471
363
    def test_lookup_tag(self):
472
364
        self.tree.branch.tags.set_tag('bzr-0.14', 'r1')
473
365
        self.assertInHistoryIs(1, 'r1', 'tag:bzr-0.14')
474
 
        self.tree.branch.tags.set_tag('null_rev', 'null:')
475
 
        self.assertInHistoryIs(0, 'null:', 'tag:null_rev')
476
366
 
477
367
    def test_failed_lookup(self):
478
368
        # tags that don't exist give a specific message: arguably we should
481
371
            self.get_in_history,
482
372
            'tag:some-random-tag')
483
373
 
484
 
    def test_as_revision_id(self):
485
 
        self.tree.branch.tags.set_tag('my-tag', 'r2')
486
 
        self.tree.branch.tags.set_tag('null_rev', 'null:')
487
 
        self.assertAsRevisionId('r2', 'tag:my-tag')
488
 
        self.assertAsRevisionId('null:', 'tag:null_rev')
489
 
        self.assertAsRevisionId('r1', 'before:tag:my-tag')
490
 
 
491
374
 
492
375
class TestRevisionSpec_date(TestRevisionSpec):
493
376
 
524
407
        self.assertInHistoryIs(2, 'new_r2',
525
408
            'date:%04d-%02d-%02d' % (now.year, now.month, now.day))
526
409
 
527
 
    def test_as_revision_id(self):
528
 
        self.assertAsRevisionId('new_r2', 'date:today')
529
 
 
530
410
 
531
411
class TestRevisionSpec_ancestor(TestRevisionSpec):
532
 
 
 
412
    
533
413
    def test_non_exact_branch(self):
534
414
        # It seems better to require an exact path to the branch
535
415
        # Branch.open() rather than using Branch.open_containing()
565
445
        self.assertRaises(errors.NoCommits,
566
446
                          spec_in_history, 'ancestor:new_tree',
567
447
                                           self.tree.branch)
568
 
 
 
448
                        
569
449
        self.assertRaises(errors.NoCommits,
570
450
                          spec_in_history, 'ancestor:tree',
571
451
                                           new_tree.branch)
572
452
 
573
 
    def test_as_revision_id(self):
574
 
        self.assertAsRevisionId('alt_r2', 'ancestor:tree2')
575
 
 
576
 
    def test_default(self):
577
 
        # We don't have a parent to default to
578
 
        self.assertRaises(errors.NotBranchError, self.get_in_history,
579
 
                          'ancestor:')
580
 
 
581
 
        # Create a branch with a parent to default to
582
 
        tree3 = self.tree.bzrdir.sprout('tree3').open_workingtree()
583
 
        tree3.commit('foo', rev_id='r3')
584
 
        self.tree = tree3
585
 
        self.assertInHistoryIs(2, 'r2', 'ancestor:')
586
 
 
587
453
 
588
454
class TestRevisionSpec_branch(TestRevisionSpec):
589
 
 
 
455
    
590
456
    def test_non_exact_branch(self):
591
457
        # It seems better to require an exact path to the branch
592
458
        # Branch.open() rather than using Branch.open_containing()
617
483
        new_tree = self.make_branch_and_tree('new_tree')
618
484
        self.assertRaises(errors.NoCommits,
619
485
                          self.get_in_history, 'branch:new_tree')
620
 
        self.assertRaises(errors.NoCommits,
621
 
                          self.get_as_tree, 'branch:new_tree')
622
 
 
623
 
    def test_as_revision_id(self):
624
 
        self.assertAsRevisionId('alt_r2', 'branch:tree2')
625
 
 
626
 
    def test_as_tree(self):
627
 
        tree = self.get_as_tree('branch:tree', self.tree2)
628
 
        self.assertEquals('r2', tree.get_revision_id())
629
 
        self.assertFalse(self.tree2.branch.repository.has_revision('r2'))
630
486
 
631
487
 
632
488
class TestRevisionSpec_submit(TestRevisionSpec):
643
499
        # submit branch overrides parent branch
644
500
        self.tree.branch.set_submit_branch('tree2')
645
501
        self.assertInHistoryIs(None, 'alt_r2', 'submit:')
646
 
 
647
 
    def test_as_revision_id(self):
648
 
        self.tree.branch.set_submit_branch('tree2')
649
 
        self.assertAsRevisionId('alt_r2', 'branch:tree2')
650
 
 
651
 
 
652
 
class TestRevisionSpec_mainline(TestRevisionSpec):
653
 
 
654
 
    def test_as_revision_id(self):
655
 
        self.assertAsRevisionId('r1', 'mainline:1')
656
 
        self.assertAsRevisionId('r2', 'mainline:1.1.1')
657
 
        self.assertAsRevisionId('r2', 'mainline:revid:alt_r2')
658
 
        spec = RevisionSpec.from_string('mainline:revid:alt_r22')
659
 
        e = self.assertRaises(errors.InvalidRevisionSpec,
660
 
                              spec.as_revision_id, self.tree.branch)
661
 
        self.assertContainsRe(str(e),
662
 
            "Requested revision: 'mainline:revid:alt_r22' does not exist in"
663
 
            " branch: ")
664
 
 
665
 
    def test_in_history(self):
666
 
        self.assertInHistoryIs(2, 'r2', 'mainline:revid:alt_r2')
667
 
 
668
 
 
669
 
class TestRevisionSpec_annotate(TestRevisionSpec):
670
 
 
671
 
    def setUp(self):
672
 
        TestRevisionSpec.setUp(self)
673
 
        self.tree = self.make_branch_and_tree('annotate-tree')
674
 
        self.build_tree_contents([('annotate-tree/file1', '1\n')])
675
 
        self.tree.add('file1')
676
 
        self.tree.commit('r1', rev_id='r1')
677
 
        self.build_tree_contents([('annotate-tree/file1', '2\n1\n')])
678
 
        self.tree.commit('r2', rev_id='r2')
679
 
        self.build_tree_contents([('annotate-tree/file1', '2\n1\n3\n')])
680
 
 
681
 
    def test_as_revision_id_r1(self):
682
 
        self.assertAsRevisionId('r1', 'annotate:annotate-tree/file1:2')
683
 
 
684
 
    def test_as_revision_id_r2(self):
685
 
        self.assertAsRevisionId('r2', 'annotate:annotate-tree/file1:1')
686
 
 
687
 
    def test_as_revision_id_uncommitted(self):
688
 
        spec = RevisionSpec.from_string('annotate:annotate-tree/file1:3')
689
 
        e = self.assertRaises(errors.InvalidRevisionSpec,
690
 
                              spec.as_revision_id, self.tree.branch)
691
 
        self.assertContainsRe(str(e),
692
 
            r"Requested revision: \'annotate:annotate-tree/file1:3\' does not"
693
 
            " exist in branch: .*\nLine 3 has not been committed.")
694
 
 
695
 
    def test_non_existent_line(self):
696
 
        spec = RevisionSpec.from_string('annotate:annotate-tree/file1:4')
697
 
        e = self.assertRaises(errors.InvalidRevisionSpec,
698
 
                              spec.as_revision_id, self.tree.branch)
699
 
        self.assertContainsRe(str(e),
700
 
            r"Requested revision: \'annotate:annotate-tree/file1:4\' does not"
701
 
            " exist in branch: .*\nNo such line: 4")
702
 
 
703
 
    def test_invalid_line(self):
704
 
        spec = RevisionSpec.from_string('annotate:annotate-tree/file1:q')
705
 
        e = self.assertRaises(errors.InvalidRevisionSpec,
706
 
                              spec.as_revision_id, self.tree.branch)
707
 
        self.assertContainsRe(str(e),
708
 
            r"Requested revision: \'annotate:annotate-tree/file1:q\' does not"
709
 
            " exist in branch: .*\nNo such line: q")
710
 
 
711
 
    def test_no_such_file(self):
712
 
        spec = RevisionSpec.from_string('annotate:annotate-tree/file2:1')
713
 
        e = self.assertRaises(errors.InvalidRevisionSpec,
714
 
                              spec.as_revision_id, self.tree.branch)
715
 
        self.assertContainsRe(str(e),
716
 
            r"Requested revision: \'annotate:annotate-tree/file2:1\' does not"
717
 
            " exist in branch: .*\nFile 'file2' is not versioned")
718
 
 
719
 
    def test_no_such_file_with_colon(self):
720
 
        spec = RevisionSpec.from_string('annotate:annotate-tree/fi:le2:1')
721
 
        e = self.assertRaises(errors.InvalidRevisionSpec,
722
 
                              spec.as_revision_id, self.tree.branch)
723
 
        self.assertContainsRe(str(e),
724
 
            r"Requested revision: \'annotate:annotate-tree/fi:le2:1\' does not"
725
 
            " exist in branch: .*\nFile 'fi:le2' is not versioned")