~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_revisionspec.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-04-07 04:44:56 UTC
  • mfrom: (3297.1.3 test-progress)
  • Revision ID: pqm@pqm.ubuntu.com-20080407044456-s1a9orh0kssphdh9
(mbp) more compact test progress counter

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 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
18
import os
23
23
    bzrdir,
24
24
    errors,
25
25
    repository,
26
 
    revision as _mod_revision,
27
26
    )
28
27
from bzrlib.tests import TestCase, TestCaseWithTransport
29
28
from bzrlib.revisionspec import (
74
73
                         ' %r != %r'
75
74
                         % (revision_spec, exp_revision_id, rev_info.rev_id))
76
75
 
77
 
    def assertInvalid(self, revision_spec, extra='',
78
 
                      invalid_as_revision_id=True):
 
76
    def assertInvalid(self, revision_spec, extra=''):
79
77
        try:
80
78
            self.get_in_history(revision_spec)
81
79
        except errors.InvalidRevisionSpec, e:
82
80
            self.assertEqual(revision_spec, e.spec)
83
81
            self.assertEqual(extra, e.extra)
84
82
        else:
85
 
            self.fail('Expected InvalidRevisionSpec to be raised for'
86
 
                      ' %r.in_history' % (revision_spec,))
87
 
        if invalid_as_revision_id:
88
 
            try:
89
 
                spec = RevisionSpec.from_string(revision_spec)
90
 
                spec.as_revision_id(self.tree.branch)
91
 
            except errors.InvalidRevisionSpec, e:
92
 
                self.assertEqual(revision_spec, e.spec)
93
 
                self.assertEqual(extra, e.extra)
94
 
            else:
95
 
                self.fail('Expected InvalidRevisionSpec to be raised for'
96
 
                          ' %r.as_revision_id' % (revision_spec,))
 
83
            self.fail('Expected InvalidRevisionSpec to be raised for %s'
 
84
                      % (revision_spec,))
97
85
 
98
86
    def assertAsRevisionId(self, revision_id, revision_spec):
99
87
        """Calling as_revision_id() should return the specified id."""
101
89
        self.assertEqual(revision_id,
102
90
                         spec.as_revision_id(self.tree.branch))
103
91
 
104
 
    def get_as_tree(self, revision_spec, tree=None):
105
 
        if tree is None:
106
 
            tree = self.tree
107
 
        spec = RevisionSpec.from_string(revision_spec)
108
 
        return spec.as_tree(tree.branch)
109
 
 
110
 
 
111
 
class RevisionSpecMatchOnTrap(RevisionSpec):
112
 
 
113
 
    def _match_on(self, branch, revs):
114
 
        self.last_call = (branch, revs)
115
 
        return super(RevisionSpecMatchOnTrap, self)._match_on(branch, revs)
116
 
 
117
 
 
118
 
class TestRevisionSpecBase(TestRevisionSpec):
119
 
 
120
 
    def test_wants_revision_history(self):
121
 
        # If wants_revision_history = True, then _match_on should get the
122
 
        # branch revision history
123
 
        spec = RevisionSpecMatchOnTrap('foo', _internal=True)
124
 
        spec.in_history(self.tree.branch)
125
 
 
126
 
        self.assertEqual((self.tree.branch, ['r1' ,'r2']),
127
 
                         spec.last_call)
128
 
 
129
 
    def test_wants_no_revision_history(self):
130
 
        # If wants_revision_history = False, then _match_on should get None for
131
 
        # the branch revision history
132
 
        spec = RevisionSpecMatchOnTrap('foo', _internal=True)
133
 
        spec.wants_revision_history = False
134
 
        spec.in_history(self.tree.branch)
135
 
 
136
 
        self.assertEqual((self.tree.branch, None), spec.last_call)
137
 
 
138
 
 
139
92
 
140
93
class TestOddRevisionSpec(TestRevisionSpec):
141
94
    """Test things that aren't normally thought of as revision specs"""
146
99
    def test_object(self):
147
100
        self.assertRaises(TypeError, RevisionSpec.from_string, object())
148
101
 
149
 
 
150
 
class TestRevisionSpec_dwim(TestRevisionSpec):
151
 
 
152
 
    # Don't need to test revno's explicitly since TRS_revno already
153
 
    # covers that well for us
154
 
    def test_dwim_spec_revno(self):
155
 
        self.assertInHistoryIs(2, 'r2', '2')
156
 
        self.assertAsRevisionId('alt_r2', '1.1.1')
157
 
 
158
 
    def test_dwim_spec_revid(self):
159
 
        self.assertInHistoryIs(2, 'r2', 'r2')
160
 
 
161
 
    def test_dwim_spec_tag(self):
162
 
        self.tree.branch.tags.set_tag('footag', 'r1')
163
 
        self.assertAsRevisionId('r1', 'footag')
164
 
        self.tree.branch.tags.delete_tag('footag')
165
 
        self.assertRaises(errors.InvalidRevisionSpec,
166
 
                          self.get_in_history, 'footag')
167
 
 
168
 
    def test_dwim_spec_tag_that_looks_like_revno(self):
169
 
        # Test that we slip past revno with things that look like revnos,
170
 
        # but aren't.  Tags are convenient for testing this since we can
171
 
        # make them look however we want.
172
 
        self.tree.branch.tags.set_tag('3', 'r2')
173
 
        self.assertAsRevisionId('r2', '3')
174
 
        self.build_tree(['tree/b'])
175
 
        self.tree.add(['b'])
176
 
        self.tree.commit('b', rev_id='r3')
177
 
        self.assertAsRevisionId('r3', '3')
178
 
 
179
 
    def test_dwim_spec_date(self):
180
 
        self.assertAsRevisionId('r1', 'today')
181
 
 
182
 
    def test_dwim_spec_branch(self):
183
 
        self.assertInHistoryIs(None, 'alt_r2', 'tree2')
184
 
 
185
 
    def test_dwim_spec_nonexistent(self):
186
 
        self.assertInvalid('somethingrandom', invalid_as_revision_id=False)
187
 
        self.assertInvalid('-1.1', invalid_as_revision_id=False)
188
 
        self.assertInvalid('.1', invalid_as_revision_id=False)
189
 
        self.assertInvalid('1..1', invalid_as_revision_id=False)
190
 
        self.assertInvalid('1.2..1', invalid_as_revision_id=False)
191
 
        self.assertInvalid('1.', invalid_as_revision_id=False)
 
102
    def test_unregistered_spec(self):
 
103
        self.assertRaises(errors.NoSuchRevisionSpec,
 
104
                          RevisionSpec.from_string, 'foo')
 
105
        self.assertRaises(errors.NoSuchRevisionSpec,
 
106
                          RevisionSpec.from_string, '123a')
 
107
 
 
108
 
 
109
 
 
110
class TestRevnoFromString(TestCase):
 
111
 
 
112
    def test_from_string_dotted_decimal(self):
 
113
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '-1.1')
 
114
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '.1')
 
115
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '1..1')
 
116
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '1.2..1')
 
117
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '1.')
 
118
        self.assertIsInstance(RevisionSpec.from_string('1.1'), RevisionSpec_revno)
 
119
        self.assertIsInstance(RevisionSpec.from_string('1.1.3'), RevisionSpec_revno)
192
120
 
193
121
 
194
122
class TestRevisionSpec_revno(TestRevisionSpec):
201
129
 
202
130
    def test_dotted_decimal(self):
203
131
        self.assertInHistoryIs(None, 'alt_r2', '1.1.1')
204
 
        self.assertInvalid('1.1.123')
205
132
 
206
133
    def test_negative_int(self):
207
134
        self.assertInHistoryIs(2, 'r2', '-1')
301
228
        """Old revno:N:path tests"""
302
229
        wta = self.make_branch_and_tree('a')
303
230
        ba = wta.branch
304
 
 
 
231
        
305
232
        wta.commit('Commit one', rev_id='a@r-0-1')
306
233
        wta.commit('Commit two', rev_id='a@r-0-2')
307
234
        wta.commit('Commit three', rev_id='a@r-0-3')
333
260
        self.assertAsRevisionId('r2', '-1')
334
261
        self.assertAsRevisionId('alt_r2', '1.1.1')
335
262
 
336
 
    def test_as_tree(self):
337
 
        tree = self.get_as_tree('0')
338
 
        self.assertEquals(_mod_revision.NULL_REVISION, tree.get_revision_id())
339
 
        tree = self.get_as_tree('1')
340
 
        self.assertEquals('r1', tree.get_revision_id())
341
 
        tree = self.get_as_tree('2')
342
 
        self.assertEquals('r2', tree.get_revision_id())
343
 
        tree = self.get_as_tree('-2')
344
 
        self.assertEquals('r1', tree.get_revision_id())
345
 
        tree = self.get_as_tree('-1')
346
 
        self.assertEquals('r2', tree.get_revision_id())
347
 
        tree = self.get_as_tree('1.1.1')
348
 
        self.assertEquals('alt_r2', tree.get_revision_id())
349
 
 
350
263
 
351
264
class TestRevisionSpec_revid(TestRevisionSpec):
352
 
 
 
265
    
353
266
    def test_in_history(self):
354
267
        # We should be able to access revisions that are directly
355
268
        # in the history.
356
269
        self.assertInHistoryIs(1, 'r1', 'revid:r1')
357
270
        self.assertInHistoryIs(2, 'r2', 'revid:r2')
358
 
 
 
271
        
359
272
    def test_missing(self):
360
 
        self.assertInvalid('revid:r3', invalid_as_revision_id=False)
 
273
        self.assertInvalid('revid:r3')
361
274
 
362
275
    def test_merged(self):
363
276
        """We can reach revisions in the ancestry"""
366
279
    def test_not_here(self):
367
280
        self.tree2.commit('alt third', rev_id='alt_r3')
368
281
        # It exists in tree2, but not in tree
369
 
        self.assertInvalid('revid:alt_r3', invalid_as_revision_id=False)
 
282
        self.assertInvalid('revid:alt_r3')
370
283
 
371
284
    def test_in_repository(self):
372
285
        """We can get any revision id in the repository"""
462
375
 
463
376
 
464
377
class TestRevisionSpec_tag(TestRevisionSpec):
465
 
 
 
378
    
466
379
    def make_branch_and_tree(self, relpath):
467
380
        # override format as the default one may not support tags
468
381
        return TestRevisionSpec.make_branch_and_tree(
534
447
 
535
448
 
536
449
class TestRevisionSpec_ancestor(TestRevisionSpec):
537
 
 
 
450
    
538
451
    def test_non_exact_branch(self):
539
452
        # It seems better to require an exact path to the branch
540
453
        # Branch.open() rather than using Branch.open_containing()
570
483
        self.assertRaises(errors.NoCommits,
571
484
                          spec_in_history, 'ancestor:new_tree',
572
485
                                           self.tree.branch)
573
 
 
 
486
                        
574
487
        self.assertRaises(errors.NoCommits,
575
488
                          spec_in_history, 'ancestor:tree',
576
489
                                           new_tree.branch)
578
491
    def test_as_revision_id(self):
579
492
        self.assertAsRevisionId('alt_r2', 'ancestor:tree2')
580
493
 
581
 
    def test_default(self):
582
 
        # We don't have a parent to default to
583
 
        self.assertRaises(errors.NotBranchError, self.get_in_history,
584
 
                          'ancestor:')
585
 
 
586
 
        # Create a branch with a parent to default to
587
 
        tree3 = self.tree.bzrdir.sprout('tree3').open_workingtree()
588
 
        tree3.commit('foo', rev_id='r3')
589
 
        self.tree = tree3
590
 
        self.assertInHistoryIs(2, 'r2', 'ancestor:')
591
 
 
592
494
 
593
495
class TestRevisionSpec_branch(TestRevisionSpec):
594
 
 
 
496
    
595
497
    def test_non_exact_branch(self):
596
498
        # It seems better to require an exact path to the branch
597
499
        # Branch.open() rather than using Branch.open_containing()
622
524
        new_tree = self.make_branch_and_tree('new_tree')
623
525
        self.assertRaises(errors.NoCommits,
624
526
                          self.get_in_history, 'branch:new_tree')
625
 
        self.assertRaises(errors.NoCommits,
626
 
                          self.get_as_tree, 'branch:new_tree')
627
527
 
628
528
    def test_as_revision_id(self):
629
529
        self.assertAsRevisionId('alt_r2', 'branch:tree2')
630
530
 
631
 
    def test_as_tree(self):
632
 
        tree = self.get_as_tree('branch:tree', self.tree2)
633
 
        self.assertEquals('r2', tree.get_revision_id())
634
 
        self.assertFalse(self.tree2.branch.repository.has_revision('r2'))
635
 
 
636
531
 
637
532
class TestRevisionSpec_submit(TestRevisionSpec):
638
533