~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: 2009-06-05 02:46:38 UTC
  • mfrom: (4398.5.20 bencode_serializer)
  • Revision ID: pqm@pqm.ubuntu.com-20090605024638-fe5tn388osc38jvw
(bialix, jelmer, jam) Pyrex bencode and --development7-rich-root

Show diffs side-by-side

added added

removed removed

Lines of Context:
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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,
26
27
    )
27
 
from bzrlib.builtins import merge
28
28
from bzrlib.tests import TestCase, TestCaseWithTransport
29
29
from bzrlib.revisionspec import (
30
30
    RevisionSpec,
50
50
 
51
51
        self.tree = self.make_branch_and_tree('tree')
52
52
        self.build_tree(['tree/a'])
 
53
        self.tree.lock_write()
 
54
        self.addCleanup(self.tree.unlock)
53
55
        self.tree.add(['a'])
54
56
        self.tree.commit('a', rev_id='r1')
55
57
 
56
58
        self.tree2 = self.tree.bzrdir.sprout('tree2').open_workingtree()
57
59
        self.tree2.commit('alt', rev_id='alt_r2')
58
60
 
59
 
        self.tree.branch.repository.fetch(self.tree2.branch.repository,
60
 
                                          revision_id='alt_r2')
61
 
        self.tree.set_pending_merges(['alt_r2'])
 
61
        self.tree.merge_from_branch(self.tree2.branch)
62
62
        self.tree.commit('second', rev_id='r2')
63
63
 
64
64
    def get_in_history(self, revision_spec):
74
74
                         ' %r != %r'
75
75
                         % (revision_spec, exp_revision_id, rev_info.rev_id))
76
76
 
77
 
    def assertInvalid(self, revision_spec, extra=''):
 
77
    def assertInvalid(self, revision_spec, extra='',
 
78
                      invalid_as_revision_id=True):
78
79
        try:
79
80
            self.get_in_history(revision_spec)
80
81
        except errors.InvalidRevisionSpec, e:
81
82
            self.assertEqual(revision_spec, e.spec)
82
83
            self.assertEqual(extra, e.extra)
83
84
        else:
84
 
            self.fail('Expected InvalidRevisionSpec to be raised for %s'
85
 
                      % (revision_spec,))
 
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,))
 
97
 
 
98
    def assertAsRevisionId(self, revision_id, revision_spec):
 
99
        """Calling as_revision_id() should return the specified id."""
 
100
        spec = RevisionSpec.from_string(revision_spec)
 
101
        self.assertEqual(revision_id,
 
102
                         spec.as_revision_id(self.tree.branch))
 
103
 
 
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
 
86
138
 
87
139
 
88
140
class TestOddRevisionSpec(TestRevisionSpec):
89
141
    """Test things that aren't normally thought of as revision specs"""
90
142
 
91
143
    def test_none(self):
92
 
        self.assertInHistoryIs(0, None, None)
 
144
        self.assertInHistoryIs(None, None, None)
93
145
 
94
146
    def test_object(self):
95
147
        self.assertRaises(TypeError, RevisionSpec.from_string, object())
117
169
class TestRevisionSpec_revno(TestRevisionSpec):
118
170
 
119
171
    def test_positive_int(self):
120
 
        self.assertInHistoryIs(0, None, '0')
 
172
        self.assertInHistoryIs(0, 'null:', '0')
121
173
        self.assertInHistoryIs(1, 'r1', '1')
122
174
        self.assertInHistoryIs(2, 'r2', '2')
123
175
        self.assertInvalid('3')
124
176
 
125
177
    def test_dotted_decimal(self):
126
178
        self.assertInHistoryIs(None, 'alt_r2', '1.1.1')
 
179
        self.assertInvalid('1.1.123')
127
180
 
128
181
    def test_negative_int(self):
129
182
        self.assertInHistoryIs(2, 'r2', '-1')
134
187
        self.assertInHistoryIs(1, 'r1', '-100')
135
188
 
136
189
    def test_positive(self):
137
 
        self.assertInHistoryIs(0, None, 'revno:0')
 
190
        self.assertInHistoryIs(0, 'null:', 'revno:0')
138
191
        self.assertInHistoryIs(1, 'r1', 'revno:1')
139
192
        self.assertInHistoryIs(2, 'r2', 'revno:2')
140
193
 
223
276
        """Old revno:N:path tests"""
224
277
        wta = self.make_branch_and_tree('a')
225
278
        ba = wta.branch
226
 
        
 
279
 
227
280
        wta.commit('Commit one', rev_id='a@r-0-1')
228
281
        wta.commit('Commit two', rev_id='a@r-0-2')
229
282
        wta.commit('Commit three', rev_id='a@r-0-3')
247
300
        self.assertEqual((2, 'b@r-0-2'),
248
301
                         spec_in_history('revno:2:b/', None))
249
302
 
 
303
    def test_as_revision_id(self):
 
304
        self.assertAsRevisionId('null:', '0')
 
305
        self.assertAsRevisionId('r1', '1')
 
306
        self.assertAsRevisionId('r2', '2')
 
307
        self.assertAsRevisionId('r1', '-2')
 
308
        self.assertAsRevisionId('r2', '-1')
 
309
        self.assertAsRevisionId('alt_r2', '1.1.1')
 
310
 
 
311
    def test_as_tree(self):
 
312
        tree = self.get_as_tree('0')
 
313
        self.assertEquals(_mod_revision.NULL_REVISION, tree.get_revision_id())
 
314
        tree = self.get_as_tree('1')
 
315
        self.assertEquals('r1', tree.get_revision_id())
 
316
        tree = self.get_as_tree('2')
 
317
        self.assertEquals('r2', tree.get_revision_id())
 
318
        tree = self.get_as_tree('-2')
 
319
        self.assertEquals('r1', tree.get_revision_id())
 
320
        tree = self.get_as_tree('-1')
 
321
        self.assertEquals('r2', tree.get_revision_id())
 
322
        tree = self.get_as_tree('1.1.1')
 
323
        self.assertEquals('alt_r2', tree.get_revision_id())
250
324
 
251
325
 
252
326
class TestRevisionSpec_revid(TestRevisionSpec):
253
 
    
 
327
 
254
328
    def test_in_history(self):
255
329
        # We should be able to access revisions that are directly
256
330
        # in the history.
257
331
        self.assertInHistoryIs(1, 'r1', 'revid:r1')
258
332
        self.assertInHistoryIs(2, 'r2', 'revid:r2')
259
 
        
 
333
 
260
334
    def test_missing(self):
261
 
        self.assertInvalid('revid:r3')
 
335
        self.assertInvalid('revid:r3', invalid_as_revision_id=False)
262
336
 
263
337
    def test_merged(self):
264
338
        """We can reach revisions in the ancestry"""
267
341
    def test_not_here(self):
268
342
        self.tree2.commit('alt third', rev_id='alt_r3')
269
343
        # It exists in tree2, but not in tree
270
 
        self.assertInvalid('revid:alt_r3')
 
344
        self.assertInvalid('revid:alt_r3', invalid_as_revision_id=False)
271
345
 
272
346
    def test_in_repository(self):
273
347
        """We can get any revision id in the repository"""
284
358
        self.assertInHistoryIs(3, revision_id, u'revid:\N{SNOWMAN}')
285
359
        self.assertInHistoryIs(3, revision_id, 'revid:' + revision_id)
286
360
 
 
361
    def test_as_revision_id(self):
 
362
        self.assertAsRevisionId('r1', 'revid:r1')
 
363
        self.assertAsRevisionId('r2', 'revid:r2')
 
364
        self.assertAsRevisionId('alt_r2', 'revid:alt_r2')
 
365
 
287
366
 
288
367
class TestRevisionSpec_last(TestRevisionSpec):
289
368
 
290
369
    def test_positive(self):
291
370
        self.assertInHistoryIs(2, 'r2', 'last:1')
292
371
        self.assertInHistoryIs(1, 'r1', 'last:2')
293
 
        self.assertInHistoryIs(0, None, 'last:3')
 
372
        self.assertInHistoryIs(0, 'null:', 'last:3')
294
373
 
295
374
    def test_empty(self):
296
375
        self.assertInHistoryIs(2, 'r2', 'last:')
315
394
            pass
316
395
        self.assertInvalid('last:Y', extra='\n' + str(e))
317
396
 
 
397
    def test_as_revision_id(self):
 
398
        self.assertAsRevisionId('r2', 'last:1')
 
399
        self.assertAsRevisionId('r1', 'last:2')
 
400
 
318
401
 
319
402
class TestRevisionSpec_before(TestRevisionSpec):
320
403
 
323
406
        self.assertInHistoryIs(1, 'r1', 'before:-1')
324
407
 
325
408
    def test_before_one(self):
326
 
        self.assertInHistoryIs(0, None, 'before:1')
 
409
        self.assertInHistoryIs(0, 'null:', 'before:1')
327
410
 
328
411
    def test_before_none(self):
329
412
        self.assertInvalid('before:0',
344
427
        new_tree.commit('first', rev_id='new_r1')
345
428
        self.tree.branch.repository.fetch(new_tree.branch.repository,
346
429
                                          revision_id='new_r1')
347
 
        self.assertInHistoryIs(0, None, 'before:revid:new_r1')
 
430
        self.assertInHistoryIs(0, 'null:', 'before:revid:new_r1')
 
431
 
 
432
    def test_as_revision_id(self):
 
433
        self.assertAsRevisionId('r1', 'before:revid:r2')
 
434
        self.assertAsRevisionId('r1', 'before:2')
 
435
        self.assertAsRevisionId('r1', 'before:1.1.1')
 
436
        self.assertAsRevisionId('r1', 'before:revid:alt_r2')
348
437
 
349
438
 
350
439
class TestRevisionSpec_tag(TestRevisionSpec):
351
 
    
 
440
 
352
441
    def make_branch_and_tree(self, relpath):
353
442
        # override format as the default one may not support tags
354
 
        control = bzrdir.BzrDir.create(relpath)
355
 
        control.create_repository()
356
 
        branch.BzrBranchExperimental.initialize(control)
357
 
        return control.create_workingtree()
 
443
        return TestRevisionSpec.make_branch_and_tree(
 
444
            self, relpath, format='dirstate-tags')
358
445
 
359
446
    def test_from_string_tag(self):
360
447
        spec = RevisionSpec.from_string('tag:bzr-0.14')
364
451
    def test_lookup_tag(self):
365
452
        self.tree.branch.tags.set_tag('bzr-0.14', 'r1')
366
453
        self.assertInHistoryIs(1, 'r1', 'tag:bzr-0.14')
 
454
        self.tree.branch.tags.set_tag('null_rev', 'null:')
 
455
        self.assertInHistoryIs(0, 'null:', 'tag:null_rev')
367
456
 
368
457
    def test_failed_lookup(self):
369
458
        # tags that don't exist give a specific message: arguably we should
372
461
            self.get_in_history,
373
462
            'tag:some-random-tag')
374
463
 
 
464
    def test_as_revision_id(self):
 
465
        self.tree.branch.tags.set_tag('my-tag', 'r2')
 
466
        self.tree.branch.tags.set_tag('null_rev', 'null:')
 
467
        self.assertAsRevisionId('r2', 'tag:my-tag')
 
468
        self.assertAsRevisionId('null:', 'tag:null_rev')
 
469
        self.assertAsRevisionId('r1', 'before:tag:my-tag')
 
470
 
375
471
 
376
472
class TestRevisionSpec_date(TestRevisionSpec):
377
473
 
408
504
        self.assertInHistoryIs(2, 'new_r2',
409
505
            'date:%04d-%02d-%02d' % (now.year, now.month, now.day))
410
506
 
 
507
    def test_as_revision_id(self):
 
508
        self.assertAsRevisionId('new_r2', 'date:today')
 
509
 
411
510
 
412
511
class TestRevisionSpec_ancestor(TestRevisionSpec):
413
 
    
 
512
 
414
513
    def test_non_exact_branch(self):
415
514
        # It seems better to require an exact path to the branch
416
515
        # Branch.open() rather than using Branch.open_containing()
446
545
        self.assertRaises(errors.NoCommits,
447
546
                          spec_in_history, 'ancestor:new_tree',
448
547
                                           self.tree.branch)
449
 
                        
 
548
 
450
549
        self.assertRaises(errors.NoCommits,
451
550
                          spec_in_history, 'ancestor:tree',
452
551
                                           new_tree.branch)
453
552
 
 
553
    def test_as_revision_id(self):
 
554
        self.assertAsRevisionId('alt_r2', 'ancestor:tree2')
 
555
 
 
556
    def test_default(self):
 
557
        # We don't have a parent to default to
 
558
        self.assertRaises(errors.NotBranchError, self.get_in_history,
 
559
                          'ancestor:')
 
560
 
 
561
        # Create a branch with a parent to default to
 
562
        tree3 = self.tree.bzrdir.sprout('tree3').open_workingtree()
 
563
        tree3.commit('foo', rev_id='r3')
 
564
        self.tree = tree3
 
565
        self.assertInHistoryIs(2, 'r2', 'ancestor:')
 
566
 
454
567
 
455
568
class TestRevisionSpec_branch(TestRevisionSpec):
456
 
    
 
569
 
457
570
    def test_non_exact_branch(self):
458
571
        # It seems better to require an exact path to the branch
459
572
        # Branch.open() rather than using Branch.open_containing()
484
597
        new_tree = self.make_branch_and_tree('new_tree')
485
598
        self.assertRaises(errors.NoCommits,
486
599
                          self.get_in_history, 'branch:new_tree')
 
600
        self.assertRaises(errors.NoCommits,
 
601
                          self.get_as_tree, 'branch:new_tree')
 
602
 
 
603
    def test_as_revision_id(self):
 
604
        self.assertAsRevisionId('alt_r2', 'branch:tree2')
 
605
 
 
606
    def test_as_tree(self):
 
607
        tree = self.get_as_tree('branch:tree', self.tree2)
 
608
        self.assertEquals('r2', tree.get_revision_id())
 
609
        self.assertFalse(self.tree2.branch.repository.has_revision('r2'))
487
610
 
488
611
 
489
612
class TestRevisionSpec_submit(TestRevisionSpec):
500
623
        # submit branch overrides parent branch
501
624
        self.tree.branch.set_submit_branch('tree2')
502
625
        self.assertInHistoryIs(None, 'alt_r2', 'submit:')
 
626
 
 
627
    def test_as_revision_id(self):
 
628
        self.tree.branch.set_submit_branch('tree2')
 
629
        self.assertAsRevisionId('alt_r2', 'branch:tree2')