~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_revisionspec.py

  • Committer: Lukáš Lalinský
  • Date: 2008-08-23 13:06:24 UTC
  • mto: (3650.1.1 bzr.ab.integration)
  • mto: This revision was merged to the branch mainline in revision 3651.
  • Revision ID: lalinsky@gmail.com-20080823130624-rvbasutb8urxkamy
Add a test

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2012, 2016 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,
23
 
    symbol_versioning,
 
25
    repository,
24
26
    )
25
 
from bzrlib.tests import TestCaseWithTransport
 
27
from bzrlib.tests import TestCase, TestCaseWithTransport
26
28
from bzrlib.revisionspec import (
27
 
    RevisionInfo,
28
29
    RevisionSpec,
29
 
    RevisionSpec_dwim,
 
30
    RevisionSpec_revno,
30
31
    RevisionSpec_tag,
31
32
    )
32
33
 
99
100
        self.assertEqual(revision_id,
100
101
                         spec.as_revision_id(self.tree.branch))
101
102
 
102
 
    def get_as_tree(self, revision_spec, tree=None):
103
 
        if tree is None:
104
 
            tree = self.tree
105
 
        spec = RevisionSpec.from_string(revision_spec)
106
 
        return spec.as_tree(tree.branch)
107
 
 
108
103
 
109
104
class RevisionSpecMatchOnTrap(RevisionSpec):
110
105
 
119
114
        # If wants_revision_history = True, then _match_on should get the
120
115
        # branch revision history
121
116
        spec = RevisionSpecMatchOnTrap('foo', _internal=True)
122
 
        spec.wants_revision_history = True
123
 
        self.callDeprecated(['RevisionSpec.wants_revision_history was '
124
 
            'deprecated in 2.5 (RevisionSpecMatchOnTrap).'],
125
 
            spec.in_history, self.tree.branch)
 
117
        spec.in_history(self.tree.branch)
126
118
 
127
119
        self.assertEqual((self.tree.branch, ['r1' ,'r2']),
128
120
                         spec.last_call)
131
123
        # If wants_revision_history = False, then _match_on should get None for
132
124
        # the branch revision history
133
125
        spec = RevisionSpecMatchOnTrap('foo', _internal=True)
 
126
        spec.wants_revision_history = False
134
127
        spec.in_history(self.tree.branch)
135
128
 
136
129
        self.assertEqual((self.tree.branch, None), spec.last_call)
146
139
    def test_object(self):
147
140
        self.assertRaises(TypeError, RevisionSpec.from_string, object())
148
141
 
149
 
 
150
 
class RevisionSpec_bork(RevisionSpec):
151
 
 
152
 
    prefix = 'irrelevant:'
153
 
 
154
 
    def _match_on(self, branch, revs):
155
 
        if self.spec == "bork":
156
 
            return RevisionInfo.from_revision_id(branch, "r1")
157
 
        else:
158
 
            raise errors.InvalidRevisionSpec(self.spec, branch)
159
 
 
160
 
 
161
 
class TestRevisionSpec_dwim(TestRevisionSpec):
162
 
 
163
 
    # Don't need to test revno's explicitly since TRS_revno already
164
 
    # covers that well for us
165
 
    def test_dwim_spec_revno(self):
166
 
        self.assertInHistoryIs(2, 'r2', '2')
167
 
        self.assertAsRevisionId('alt_r2', '1.1.1')
168
 
 
169
 
    def test_dwim_spec_revid(self):
170
 
        self.assertInHistoryIs(2, 'r2', 'r2')
171
 
 
172
 
    def test_dwim_spec_tag(self):
173
 
        self.tree.branch.tags.set_tag('footag', 'r1')
174
 
        self.assertAsRevisionId('r1', 'footag')
175
 
        self.tree.branch.tags.delete_tag('footag')
176
 
        self.assertRaises(errors.InvalidRevisionSpec,
177
 
                          self.get_in_history, 'footag')
178
 
 
179
 
    def test_dwim_spec_tag_that_looks_like_revno(self):
180
 
        # Test that we slip past revno with things that look like revnos,
181
 
        # but aren't.  Tags are convenient for testing this since we can
182
 
        # make them look however we want.
183
 
        self.tree.branch.tags.set_tag('3', 'r2')
184
 
        self.assertAsRevisionId('r2', '3')
185
 
        self.build_tree(['tree/b'])
186
 
        self.tree.add(['b'])
187
 
        self.tree.commit('b', rev_id='r3')
188
 
        self.assertAsRevisionId('r3', '3')
189
 
 
190
 
    def test_dwim_spec_date(self):
191
 
        self.assertAsRevisionId('r1', 'today')
192
 
 
193
 
    def test_dwim_spec_branch(self):
194
 
        self.assertInHistoryIs(None, 'alt_r2', 'tree2')
195
 
 
196
 
    def test_dwim_spec_nonexistent(self):
197
 
        self.assertInvalid('somethingrandom', invalid_as_revision_id=False)
198
 
        self.assertInvalid('-1.1', invalid_as_revision_id=False)
199
 
        self.assertInvalid('.1', invalid_as_revision_id=False)
200
 
        self.assertInvalid('1..1', invalid_as_revision_id=False)
201
 
        self.assertInvalid('1.2..1', invalid_as_revision_id=False)
202
 
        self.assertInvalid('1.', invalid_as_revision_id=False)
203
 
 
204
 
    def test_append_dwim_revspec(self):
205
 
        original_dwim_revspecs = list(RevisionSpec_dwim._possible_revspecs)
206
 
        def reset_dwim_revspecs():
207
 
            RevisionSpec_dwim._possible_revspecs = original_dwim_revspecs
208
 
        self.addCleanup(reset_dwim_revspecs)
209
 
        RevisionSpec_dwim.append_possible_revspec(RevisionSpec_bork)
210
 
        self.assertAsRevisionId('r1', 'bork')
211
 
 
212
 
    def test_append_lazy_dwim_revspec(self):
213
 
        original_dwim_revspecs = list(RevisionSpec_dwim._possible_revspecs)
214
 
        def reset_dwim_revspecs():
215
 
            RevisionSpec_dwim._possible_revspecs = original_dwim_revspecs
216
 
        self.addCleanup(reset_dwim_revspecs)
217
 
        RevisionSpec_dwim.append_possible_lazy_revspec(
218
 
            "bzrlib.tests.test_revisionspec", "RevisionSpec_bork")
219
 
        self.assertAsRevisionId('r1', 'bork')
 
142
    def test_unregistered_spec(self):
 
143
        self.assertRaises(errors.NoSuchRevisionSpec,
 
144
                          RevisionSpec.from_string, 'foo')
 
145
        self.assertRaises(errors.NoSuchRevisionSpec,
 
146
                          RevisionSpec.from_string, '123a')
 
147
 
 
148
 
 
149
 
 
150
class TestRevnoFromString(TestCase):
 
151
 
 
152
    def test_from_string_dotted_decimal(self):
 
153
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '-1.1')
 
154
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '.1')
 
155
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '1..1')
 
156
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '1.2..1')
 
157
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '1.')
 
158
        self.assertIsInstance(RevisionSpec.from_string('1.1'), RevisionSpec_revno)
 
159
        self.assertIsInstance(RevisionSpec.from_string('1.1.3'), RevisionSpec_revno)
220
160
 
221
161
 
222
162
class TestRevisionSpec_revno(TestRevisionSpec):
229
169
 
230
170
    def test_dotted_decimal(self):
231
171
        self.assertInHistoryIs(None, 'alt_r2', '1.1.1')
232
 
        self.assertInvalid('1.1.123')
233
172
 
234
173
    def test_negative_int(self):
235
174
        self.assertInHistoryIs(2, 'r2', '-1')
329
268
        """Old revno:N:path tests"""
330
269
        wta = self.make_branch_and_tree('a')
331
270
        ba = wta.branch
332
 
 
 
271
        
333
272
        wta.commit('Commit one', rev_id='a@r-0-1')
334
273
        wta.commit('Commit two', rev_id='a@r-0-2')
335
274
        wta.commit('Commit three', rev_id='a@r-0-3')
361
300
        self.assertAsRevisionId('r2', '-1')
362
301
        self.assertAsRevisionId('alt_r2', '1.1.1')
363
302
 
364
 
    def test_as_tree(self):
365
 
        tree = self.get_as_tree('0')
366
 
        self.assertEqual(_mod_revision.NULL_REVISION, tree.get_revision_id())
367
 
        tree = self.get_as_tree('1')
368
 
        self.assertEqual('r1', tree.get_revision_id())
369
 
        tree = self.get_as_tree('2')
370
 
        self.assertEqual('r2', tree.get_revision_id())
371
 
        tree = self.get_as_tree('-2')
372
 
        self.assertEqual('r1', tree.get_revision_id())
373
 
        tree = self.get_as_tree('-1')
374
 
        self.assertEqual('r2', tree.get_revision_id())
375
 
        tree = self.get_as_tree('1.1.1')
376
 
        self.assertEqual('alt_r2', tree.get_revision_id())
377
 
 
378
303
 
379
304
class TestRevisionSpec_revid(TestRevisionSpec):
380
 
 
 
305
    
381
306
    def test_in_history(self):
382
307
        # We should be able to access revisions that are directly
383
308
        # in the history.
384
309
        self.assertInHistoryIs(1, 'r1', 'revid:r1')
385
310
        self.assertInHistoryIs(2, 'r2', 'revid:r2')
386
 
 
 
311
        
387
312
    def test_missing(self):
388
313
        self.assertInvalid('revid:r3', invalid_as_revision_id=False)
389
314
 
400
325
        """We can get any revision id in the repository"""
401
326
        # XXX: This may change in the future, but for now, it is true
402
327
        self.tree2.commit('alt third', rev_id='alt_r3')
403
 
        self.tree.branch.fetch(self.tree2.branch, 'alt_r3')
 
328
        self.tree.branch.repository.fetch(self.tree2.branch.repository,
 
329
                                          revision_id='alt_r3')
404
330
        self.assertInHistoryIs(None, 'alt_r3', 'revid:alt_r3')
405
331
 
406
332
    def test_unicode(self):
477
403
    def test_alt_no_parents(self):
478
404
        new_tree = self.make_branch_and_tree('new_tree')
479
405
        new_tree.commit('first', rev_id='new_r1')
480
 
        self.tree.branch.fetch(new_tree.branch, 'new_r1')
 
406
        self.tree.branch.repository.fetch(new_tree.branch.repository,
 
407
                                          revision_id='new_r1')
481
408
        self.assertInHistoryIs(0, 'null:', 'before:revid:new_r1')
482
409
 
483
410
    def test_as_revision_id(self):
488
415
 
489
416
 
490
417
class TestRevisionSpec_tag(TestRevisionSpec):
491
 
 
 
418
    
492
419
    def make_branch_and_tree(self, relpath):
493
420
        # override format as the default one may not support tags
494
421
        return TestRevisionSpec.make_branch_and_tree(
560
487
 
561
488
 
562
489
class TestRevisionSpec_ancestor(TestRevisionSpec):
563
 
 
 
490
    
564
491
    def test_non_exact_branch(self):
565
492
        # It seems better to require an exact path to the branch
566
493
        # Branch.open() rather than using Branch.open_containing()
596
523
        self.assertRaises(errors.NoCommits,
597
524
                          spec_in_history, 'ancestor:new_tree',
598
525
                                           self.tree.branch)
599
 
 
 
526
                        
600
527
        self.assertRaises(errors.NoCommits,
601
528
                          spec_in_history, 'ancestor:tree',
602
529
                                           new_tree.branch)
604
531
    def test_as_revision_id(self):
605
532
        self.assertAsRevisionId('alt_r2', 'ancestor:tree2')
606
533
 
607
 
    def test_default(self):
608
 
        # We don't have a parent to default to
609
 
        self.assertRaises(errors.NotBranchError, self.get_in_history,
610
 
                          'ancestor:')
611
 
 
612
 
        # Create a branch with a parent to default to
613
 
        tree3 = self.tree.bzrdir.sprout('tree3').open_workingtree()
614
 
        tree3.commit('foo', rev_id='r3')
615
 
        self.tree = tree3
616
 
        self.assertInHistoryIs(2, 'r2', 'ancestor:')
617
 
 
618
534
 
619
535
class TestRevisionSpec_branch(TestRevisionSpec):
620
 
 
 
536
    
621
537
    def test_non_exact_branch(self):
622
538
        # It seems better to require an exact path to the branch
623
539
        # Branch.open() rather than using Branch.open_containing()
642
558
        # XXX: Right now, we use fetch() to make sure the remote revisions
643
559
        # have been pulled into the local branch. We may change that
644
560
        # behavior in the future.
645
 
        self.assertTrue(self.tree.branch.repository.has_revision('new_r3'))
 
561
        self.failUnless(self.tree.branch.repository.has_revision('new_r3'))
646
562
 
647
563
    def test_no_commits(self):
648
564
        new_tree = self.make_branch_and_tree('new_tree')
649
565
        self.assertRaises(errors.NoCommits,
650
566
                          self.get_in_history, 'branch:new_tree')
651
 
        self.assertRaises(errors.NoCommits,
652
 
                          self.get_as_tree, 'branch:new_tree')
653
567
 
654
568
    def test_as_revision_id(self):
655
569
        self.assertAsRevisionId('alt_r2', 'branch:tree2')
656
570
 
657
 
    def test_as_tree(self):
658
 
        tree = self.get_as_tree('branch:tree', self.tree2)
659
 
        self.assertEqual('r2', tree.get_revision_id())
660
 
        self.assertFalse(self.tree2.branch.repository.has_revision('r2'))
661
 
 
662
571
 
663
572
class TestRevisionSpec_submit(TestRevisionSpec):
664
573
 
678
587
    def test_as_revision_id(self):
679
588
        self.tree.branch.set_submit_branch('tree2')
680
589
        self.assertAsRevisionId('alt_r2', 'branch:tree2')
681
 
 
682
 
 
683
 
class TestRevisionSpec_mainline(TestRevisionSpec):
684
 
 
685
 
    def test_as_revision_id(self):
686
 
        self.assertAsRevisionId('r1', 'mainline:1')
687
 
        self.assertAsRevisionId('r2', 'mainline:1.1.1')
688
 
        self.assertAsRevisionId('r2', 'mainline:revid:alt_r2')
689
 
        spec = RevisionSpec.from_string('mainline:revid:alt_r22')
690
 
        e = self.assertRaises(errors.InvalidRevisionSpec,
691
 
                              spec.as_revision_id, self.tree.branch)
692
 
        self.assertContainsRe(str(e),
693
 
            "Requested revision: 'mainline:revid:alt_r22' does not exist in"
694
 
            " branch: ")
695
 
 
696
 
    def test_in_history(self):
697
 
        self.assertInHistoryIs(2, 'r2', 'mainline:revid:alt_r2')
698
 
 
699
 
 
700
 
class TestRevisionSpec_annotate(TestRevisionSpec):
701
 
 
702
 
    def setUp(self):
703
 
        super(TestRevisionSpec_annotate, self).setUp()
704
 
        self.tree = self.make_branch_and_tree('annotate-tree')
705
 
        self.build_tree_contents([('annotate-tree/file1', '1\n')])
706
 
        self.tree.add('file1')
707
 
        self.tree.commit('r1', rev_id='r1')
708
 
        self.build_tree_contents([('annotate-tree/file1', '2\n1\n')])
709
 
        self.tree.commit('r2', rev_id='r2')
710
 
        self.build_tree_contents([('annotate-tree/file1', '2\n1\n3\n')])
711
 
 
712
 
    def test_as_revision_id_r1(self):
713
 
        self.assertAsRevisionId('r1', 'annotate:annotate-tree/file1:2')
714
 
 
715
 
    def test_as_revision_id_r2(self):
716
 
        self.assertAsRevisionId('r2', 'annotate:annotate-tree/file1:1')
717
 
 
718
 
    def test_as_revision_id_uncommitted(self):
719
 
        spec = RevisionSpec.from_string('annotate:annotate-tree/file1:3')
720
 
        e = self.assertRaises(errors.InvalidRevisionSpec,
721
 
                              spec.as_revision_id, self.tree.branch)
722
 
        self.assertContainsRe(str(e),
723
 
            r"Requested revision: \'annotate:annotate-tree/file1:3\' does not"
724
 
            " exist in branch: .*\nLine 3 has not been committed.")
725
 
 
726
 
    def test_non_existent_line(self):
727
 
        spec = RevisionSpec.from_string('annotate:annotate-tree/file1:4')
728
 
        e = self.assertRaises(errors.InvalidRevisionSpec,
729
 
                              spec.as_revision_id, self.tree.branch)
730
 
        self.assertContainsRe(str(e),
731
 
            r"Requested revision: \'annotate:annotate-tree/file1:4\' does not"
732
 
            " exist in branch: .*\nNo such line: 4")
733
 
 
734
 
    def test_invalid_line(self):
735
 
        spec = RevisionSpec.from_string('annotate:annotate-tree/file1:q')
736
 
        e = self.assertRaises(errors.InvalidRevisionSpec,
737
 
                              spec.as_revision_id, self.tree.branch)
738
 
        self.assertContainsRe(str(e),
739
 
            r"Requested revision: \'annotate:annotate-tree/file1:q\' does not"
740
 
            " exist in branch: .*\nNo such line: q")
741
 
 
742
 
    def test_no_such_file(self):
743
 
        spec = RevisionSpec.from_string('annotate:annotate-tree/file2:1')
744
 
        e = self.assertRaises(errors.InvalidRevisionSpec,
745
 
                              spec.as_revision_id, self.tree.branch)
746
 
        self.assertContainsRe(str(e),
747
 
            r"Requested revision: \'annotate:annotate-tree/file2:1\' does not"
748
 
            " exist in branch: .*\nFile 'file2' is not versioned")
749
 
 
750
 
    def test_no_such_file_with_colon(self):
751
 
        spec = RevisionSpec.from_string('annotate:annotate-tree/fi:le2:1')
752
 
        e = self.assertRaises(errors.InvalidRevisionSpec,
753
 
                              spec.as_revision_id, self.tree.branch)
754
 
        self.assertContainsRe(str(e),
755
 
            r"Requested revision: \'annotate:annotate-tree/fi:le2:1\' does not"
756
 
            " exist in branch: .*\nFile 'fi:le2' is not versioned")