~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_revisionnamespaces.py

  • Committer: Andrew Bennetts
  • Date: 2007-07-04 03:12:37 UTC
  • mfrom: (2466.12.1 log_range)
  • mto: This revision was merged to the branch mainline in revision 2580.
  • Revision ID: andrew.bennetts@canonical.com-20070704031237-faouhtp7ckc4m69t
Merge Kent Gibson's fix for bug #4663, resolving conflicts.

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
    )
 
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)
55
53
        self.tree.add(['a'])
56
54
        self.tree.commit('a', rev_id='r1')
57
55
 
58
56
        self.tree2 = self.tree.bzrdir.sprout('tree2').open_workingtree()
59
57
        self.tree2.commit('alt', rev_id='alt_r2')
60
58
 
61
 
        self.tree.merge_from_branch(self.tree2.branch)
 
59
        self.tree.branch.repository.fetch(self.tree2.branch.repository,
 
60
                                          revision_id='alt_r2')
 
61
        self.tree.set_pending_merges(['alt_r2'])
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='',
78
 
                      invalid_as_revision_id=True):
 
77
    def assertInvalid(self, revision_spec, extra=''):
79
78
        try:
80
79
            self.get_in_history(revision_spec)
81
80
        except errors.InvalidRevisionSpec, e:
82
81
            self.assertEqual(revision_spec, e.spec)
83
82
            self.assertEqual(extra, e.extra)
84
83
        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,))
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
 
 
 
84
            self.fail('Expected InvalidRevisionSpec to be raised for %s'
 
85
                      % (revision_spec,))
138
86
 
139
87
 
140
88
class TestOddRevisionSpec(TestRevisionSpec):
141
89
    """Test things that aren't normally thought of as revision specs"""
142
90
 
143
91
    def test_none(self):
144
 
        self.assertInHistoryIs(None, None, None)
 
92
        self.assertInHistoryIs(0, None, None)
145
93
 
146
94
    def test_object(self):
147
95
        self.assertRaises(TypeError, RevisionSpec.from_string, object())
148
96
 
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)
 
97
    def test_unregistered_spec(self):
 
98
        self.assertRaises(errors.NoSuchRevisionSpec,
 
99
                          RevisionSpec.from_string, 'foo')
 
100
        self.assertRaises(errors.NoSuchRevisionSpec,
 
101
                          RevisionSpec.from_string, '123a')
 
102
 
 
103
 
 
104
 
 
105
class TestRevnoFromString(TestCase):
 
106
 
 
107
    def test_from_string_dotted_decimal(self):
 
108
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '-1.1')
 
109
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '.1')
 
110
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '1..1')
 
111
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '1.2..1')
 
112
        self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '1.')
 
113
        self.assertIsInstance(RevisionSpec.from_string('1.1'), RevisionSpec_revno)
 
114
        self.assertIsInstance(RevisionSpec.from_string('1.1.3'), RevisionSpec_revno)
192
115
 
193
116
 
194
117
class TestRevisionSpec_revno(TestRevisionSpec):
195
118
 
196
119
    def test_positive_int(self):
197
 
        self.assertInHistoryIs(0, 'null:', '0')
 
120
        self.assertInHistoryIs(0, None, '0')
198
121
        self.assertInHistoryIs(1, 'r1', '1')
199
122
        self.assertInHistoryIs(2, 'r2', '2')
200
123
        self.assertInvalid('3')
201
124
 
202
125
    def test_dotted_decimal(self):
203
126
        self.assertInHistoryIs(None, 'alt_r2', '1.1.1')
204
 
        self.assertInvalid('1.1.123')
205
127
 
206
128
    def test_negative_int(self):
207
129
        self.assertInHistoryIs(2, 'r2', '-1')
212
134
        self.assertInHistoryIs(1, 'r1', '-100')
213
135
 
214
136
    def test_positive(self):
215
 
        self.assertInHistoryIs(0, 'null:', 'revno:0')
 
137
        self.assertInHistoryIs(0, None, 'revno:0')
216
138
        self.assertInHistoryIs(1, 'r1', 'revno:1')
217
139
        self.assertInHistoryIs(2, 'r2', 'revno:2')
218
140
 
301
223
        """Old revno:N:path tests"""
302
224
        wta = self.make_branch_and_tree('a')
303
225
        ba = wta.branch
304
 
 
 
226
        
305
227
        wta.commit('Commit one', rev_id='a@r-0-1')
306
228
        wta.commit('Commit two', rev_id='a@r-0-2')
307
229
        wta.commit('Commit three', rev_id='a@r-0-3')
325
247
        self.assertEqual((2, 'b@r-0-2'),
326
248
                         spec_in_history('revno:2:b/', None))
327
249
 
328
 
    def test_as_revision_id(self):
329
 
        self.assertAsRevisionId('null:', '0')
330
 
        self.assertAsRevisionId('r1', '1')
331
 
        self.assertAsRevisionId('r2', '2')
332
 
        self.assertAsRevisionId('r1', '-2')
333
 
        self.assertAsRevisionId('r2', '-1')
334
 
        self.assertAsRevisionId('alt_r2', '1.1.1')
335
 
 
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
250
 
350
251
 
351
252
class TestRevisionSpec_revid(TestRevisionSpec):
352
 
 
 
253
    
353
254
    def test_in_history(self):
354
255
        # We should be able to access revisions that are directly
355
256
        # in the history.
356
257
        self.assertInHistoryIs(1, 'r1', 'revid:r1')
357
258
        self.assertInHistoryIs(2, 'r2', 'revid:r2')
358
 
 
 
259
        
359
260
    def test_missing(self):
360
 
        self.assertInvalid('revid:r3', invalid_as_revision_id=False)
 
261
        self.assertInvalid('revid:r3')
361
262
 
362
263
    def test_merged(self):
363
264
        """We can reach revisions in the ancestry"""
366
267
    def test_not_here(self):
367
268
        self.tree2.commit('alt third', rev_id='alt_r3')
368
269
        # It exists in tree2, but not in tree
369
 
        self.assertInvalid('revid:alt_r3', invalid_as_revision_id=False)
 
270
        self.assertInvalid('revid:alt_r3')
370
271
 
371
272
    def test_in_repository(self):
372
273
        """We can get any revision id in the repository"""
383
284
        self.assertInHistoryIs(3, revision_id, u'revid:\N{SNOWMAN}')
384
285
        self.assertInHistoryIs(3, revision_id, 'revid:' + revision_id)
385
286
 
386
 
    def test_as_revision_id(self):
387
 
        self.assertAsRevisionId('r1', 'revid:r1')
388
 
        self.assertAsRevisionId('r2', 'revid:r2')
389
 
        self.assertAsRevisionId('alt_r2', 'revid:alt_r2')
390
 
 
391
287
 
392
288
class TestRevisionSpec_last(TestRevisionSpec):
393
289
 
394
290
    def test_positive(self):
395
291
        self.assertInHistoryIs(2, 'r2', 'last:1')
396
292
        self.assertInHistoryIs(1, 'r1', 'last:2')
397
 
        self.assertInHistoryIs(0, 'null:', 'last:3')
 
293
        self.assertInHistoryIs(0, None, 'last:3')
398
294
 
399
295
    def test_empty(self):
400
296
        self.assertInHistoryIs(2, 'r2', 'last:')
419
315
            pass
420
316
        self.assertInvalid('last:Y', extra='\n' + str(e))
421
317
 
422
 
    def test_as_revision_id(self):
423
 
        self.assertAsRevisionId('r2', 'last:1')
424
 
        self.assertAsRevisionId('r1', 'last:2')
425
 
 
426
318
 
427
319
class TestRevisionSpec_before(TestRevisionSpec):
428
320
 
431
323
        self.assertInHistoryIs(1, 'r1', 'before:-1')
432
324
 
433
325
    def test_before_one(self):
434
 
        self.assertInHistoryIs(0, 'null:', 'before:1')
 
326
        self.assertInHistoryIs(0, None, 'before:1')
435
327
 
436
328
    def test_before_none(self):
437
329
        self.assertInvalid('before:0',
452
344
        new_tree.commit('first', rev_id='new_r1')
453
345
        self.tree.branch.repository.fetch(new_tree.branch.repository,
454
346
                                          revision_id='new_r1')
455
 
        self.assertInHistoryIs(0, 'null:', 'before:revid:new_r1')
456
 
 
457
 
    def test_as_revision_id(self):
458
 
        self.assertAsRevisionId('r1', 'before:revid:r2')
459
 
        self.assertAsRevisionId('r1', 'before:2')
460
 
        self.assertAsRevisionId('r1', 'before:1.1.1')
461
 
        self.assertAsRevisionId('r1', 'before:revid:alt_r2')
 
347
        self.assertInHistoryIs(0, None, 'before:revid:new_r1')
462
348
 
463
349
 
464
350
class TestRevisionSpec_tag(TestRevisionSpec):
465
 
 
 
351
    
466
352
    def make_branch_and_tree(self, relpath):
467
353
        # override format as the default one may not support tags
468
 
        return TestRevisionSpec.make_branch_and_tree(
469
 
            self, relpath, format='dirstate-tags')
 
354
        control = bzrdir.BzrDir.create(relpath)
 
355
        control.create_repository()
 
356
        branch.BzrBranchExperimental.initialize(control)
 
357
        return control.create_workingtree()
470
358
 
471
359
    def test_from_string_tag(self):
472
360
        spec = RevisionSpec.from_string('tag:bzr-0.14')
476
364
    def test_lookup_tag(self):
477
365
        self.tree.branch.tags.set_tag('bzr-0.14', 'r1')
478
366
        self.assertInHistoryIs(1, 'r1', 'tag:bzr-0.14')
479
 
        self.tree.branch.tags.set_tag('null_rev', 'null:')
480
 
        self.assertInHistoryIs(0, 'null:', 'tag:null_rev')
481
367
 
482
368
    def test_failed_lookup(self):
483
369
        # tags that don't exist give a specific message: arguably we should
486
372
            self.get_in_history,
487
373
            'tag:some-random-tag')
488
374
 
489
 
    def test_as_revision_id(self):
490
 
        self.tree.branch.tags.set_tag('my-tag', 'r2')
491
 
        self.tree.branch.tags.set_tag('null_rev', 'null:')
492
 
        self.assertAsRevisionId('r2', 'tag:my-tag')
493
 
        self.assertAsRevisionId('null:', 'tag:null_rev')
494
 
        self.assertAsRevisionId('r1', 'before:tag:my-tag')
495
 
 
496
375
 
497
376
class TestRevisionSpec_date(TestRevisionSpec):
498
377
 
529
408
        self.assertInHistoryIs(2, 'new_r2',
530
409
            'date:%04d-%02d-%02d' % (now.year, now.month, now.day))
531
410
 
532
 
    def test_as_revision_id(self):
533
 
        self.assertAsRevisionId('new_r2', 'date:today')
534
 
 
535
411
 
536
412
class TestRevisionSpec_ancestor(TestRevisionSpec):
537
 
 
 
413
    
538
414
    def test_non_exact_branch(self):
539
415
        # It seems better to require an exact path to the branch
540
416
        # Branch.open() rather than using Branch.open_containing()
570
446
        self.assertRaises(errors.NoCommits,
571
447
                          spec_in_history, 'ancestor:new_tree',
572
448
                                           self.tree.branch)
573
 
 
 
449
                        
574
450
        self.assertRaises(errors.NoCommits,
575
451
                          spec_in_history, 'ancestor:tree',
576
452
                                           new_tree.branch)
577
453
 
578
 
    def test_as_revision_id(self):
579
 
        self.assertAsRevisionId('alt_r2', 'ancestor:tree2')
580
 
 
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
454
 
593
455
class TestRevisionSpec_branch(TestRevisionSpec):
594
 
 
 
456
    
595
457
    def test_non_exact_branch(self):
596
458
        # It seems better to require an exact path to the branch
597
459
        # Branch.open() rather than using Branch.open_containing()
622
484
        new_tree = self.make_branch_and_tree('new_tree')
623
485
        self.assertRaises(errors.NoCommits,
624
486
                          self.get_in_history, 'branch:new_tree')
625
 
        self.assertRaises(errors.NoCommits,
626
 
                          self.get_as_tree, 'branch:new_tree')
627
 
 
628
 
    def test_as_revision_id(self):
629
 
        self.assertAsRevisionId('alt_r2', 'branch:tree2')
630
 
 
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
487
 
636
488
 
637
489
class TestRevisionSpec_submit(TestRevisionSpec):
648
500
        # submit branch overrides parent branch
649
501
        self.tree.branch.set_submit_branch('tree2')
650
502
        self.assertInHistoryIs(None, 'alt_r2', 'submit:')
651
 
 
652
 
    def test_as_revision_id(self):
653
 
        self.tree.branch.set_submit_branch('tree2')
654
 
        self.assertAsRevisionId('alt_r2', 'branch:tree2')