~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_revisionnamespaces.py

  • Committer: mbp at sourcefrog
  • Date: 2005-03-21 22:29:49 UTC
  • Revision ID: mbp@sourcefrog.net-20050321222949-232c2093a6eadd80
fixup doctest for new module structure

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005, 2006 by Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
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
16
 
 
17
 
import datetime
18
 
import os
19
 
import time
20
 
 
21
 
from bzrlib import (
22
 
    errors,
23
 
    )
24
 
from bzrlib.builtins import merge
25
 
from bzrlib.tests import TestCaseWithTransport
26
 
from bzrlib.revisionspec import RevisionSpec
27
 
 
28
 
 
29
 
def spec_in_history(spec, branch):
30
 
    """A simple helper to change a revision spec into a branch search"""
31
 
    return RevisionSpec.from_string(spec).in_history(branch)
32
 
 
33
 
 
34
 
# Basic class, which just creates a really basic set of revisions
35
 
class TestRevisionSpec(TestCaseWithTransport):
36
 
 
37
 
    def setUp(self):
38
 
        super(TestRevisionSpec, self).setUp()
39
 
 
40
 
        self.tree = self.make_branch_and_tree('tree')
41
 
        self.build_tree(['tree/a'])
42
 
        self.tree.add(['a'])
43
 
        self.tree.commit('a', rev_id='r1')
44
 
 
45
 
        self.tree2 = self.tree.bzrdir.sprout('tree2').open_workingtree()
46
 
        self.tree2.commit('alt', rev_id='alt_r2')
47
 
 
48
 
        self.tree.branch.repository.fetch(self.tree2.branch.repository,
49
 
                                          revision_id='alt_r2')
50
 
        self.tree.set_pending_merges(['alt_r2'])
51
 
        self.tree.commit('second', rev_id='r2')
52
 
 
53
 
    def get_in_history(self, revision_spec):
54
 
        return spec_in_history(revision_spec, self.tree.branch)
55
 
 
56
 
    def assertInHistoryIs(self, exp_revno, exp_revision_id, revision_spec):
57
 
        rev_info = self.get_in_history(revision_spec)
58
 
        self.assertEqual(exp_revno, rev_info.revno,
59
 
                         'Revision spec: %s returned wrong revno: %s != %s'
60
 
                         % (revision_spec, exp_revno, rev_info.revno))
61
 
        self.assertEqual(exp_revision_id, rev_info.rev_id,
62
 
                         'Revision spec: %s returned wrong revision id:'
63
 
                         ' %s != %s'
64
 
                         % (revision_spec, exp_revision_id, rev_info.rev_id))
65
 
 
66
 
    def assertInvalid(self, revision_spec, extra=''):
67
 
        try:
68
 
            self.get_in_history(revision_spec)
69
 
        except errors.InvalidRevisionSpec, e:
70
 
            self.assertEqual(revision_spec, e.spec)
71
 
            self.assertEqual(extra, e.extra)
72
 
        else:
73
 
            self.fail('Expected InvalidRevisionSpec to be raised for %s'
74
 
                      % (revision_spec,))
75
 
 
76
 
 
77
 
class TestOddRevisionSpec(TestRevisionSpec):
78
 
    """Test things that aren't normally thought of as revision specs"""
79
 
 
80
 
    def test_none(self):
81
 
        self.assertInHistoryIs(0, None, None)
82
 
 
83
 
    def test_object(self):
84
 
        self.assertRaises(TypeError, RevisionSpec.from_string, object())
85
 
 
86
 
    def test_unregistered_spec(self):
87
 
        self.assertRaises(errors.NoSuchRevisionSpec,
88
 
                          RevisionSpec.from_string, 'foo')
89
 
        self.assertRaises(errors.NoSuchRevisionSpec,
90
 
                          RevisionSpec.from_string, '123a')
91
 
 
92
 
 
93
 
class TestRevisionSpec_revno(TestRevisionSpec):
94
 
 
95
 
    def test_positive_int(self):
96
 
        self.assertInHistoryIs(0, None, '0')
97
 
        self.assertInHistoryIs(1, 'r1', '1')
98
 
        self.assertInHistoryIs(2, 'r2', '2')
99
 
 
100
 
        self.assertInvalid('3')
101
 
 
102
 
    def test_negative_int(self):
103
 
        self.assertInHistoryIs(2, 'r2', '-1')
104
 
        self.assertInHistoryIs(1, 'r1', '-2')
105
 
 
106
 
        self.assertInHistoryIs(1, 'r1', '-3')
107
 
        self.assertInHistoryIs(1, 'r1', '-4')
108
 
        self.assertInHistoryIs(1, 'r1', '-100')
109
 
 
110
 
    def test_positive(self):
111
 
        self.assertInHistoryIs(0, None, 'revno:0')
112
 
        self.assertInHistoryIs(1, 'r1', 'revno:1')
113
 
        self.assertInHistoryIs(2, 'r2', 'revno:2')
114
 
 
115
 
        self.assertInvalid('revno:3')
116
 
 
117
 
    def test_negative(self):
118
 
        self.assertInHistoryIs(2, 'r2', 'revno:-1')
119
 
        self.assertInHistoryIs(1, 'r1', 'revno:-2')
120
 
 
121
 
        self.assertInHistoryIs(1, 'r1', 'revno:-3')
122
 
        self.assertInHistoryIs(1, 'r1', 'revno:-4')
123
 
 
124
 
    def test_invalid_number(self):
125
 
        # Get the right exception text
126
 
        try:
127
 
            int('X')
128
 
        except ValueError, e:
129
 
            pass
130
 
        self.assertInvalid('revno:X', extra='\n' + str(e))
131
 
 
132
 
    def test_missing_number_and_branch(self):
133
 
        self.assertInvalid('revno::',
134
 
                           extra='\ncannot have an empty revno and no branch')
135
 
 
136
 
    def test_invalid_number_with_branch(self):
137
 
        try:
138
 
            int('X')
139
 
        except ValueError, e:
140
 
            pass
141
 
        self.assertInvalid('revno:X:tree2', extra='\n' + str(e))
142
 
 
143
 
    def test_non_exact_branch(self):
144
 
        # It seems better to require an exact path to the branch
145
 
        # Branch.open() rather than using Branch.open_containing()
146
 
        spec = RevisionSpec.from_string('revno:2:tree2/a')
147
 
        self.assertRaises(errors.NotBranchError,
148
 
                          spec.in_history, self.tree.branch)
149
 
 
150
 
    def test_with_branch(self):
151
 
        # Passing a URL overrides the supplied branch path
152
 
        revinfo = self.get_in_history('revno:2:tree2')
153
 
        self.assertNotEqual(self.tree.branch.base, revinfo.branch.base)
154
 
        self.assertEqual(self.tree2.branch.base, revinfo.branch.base)
155
 
        self.assertEqual(2, revinfo.revno)
156
 
        self.assertEqual('alt_r2', revinfo.rev_id)
157
 
 
158
 
    def test_int_with_branch(self):
159
 
        revinfo = self.get_in_history('2:tree2')
160
 
        self.assertNotEqual(self.tree.branch.base, revinfo.branch.base)
161
 
        self.assertEqual(self.tree2.branch.base, revinfo.branch.base)
162
 
        self.assertEqual(2, revinfo.revno)
163
 
        self.assertEqual('alt_r2', revinfo.rev_id)
164
 
 
165
 
    def test_with_url(self):
166
 
        url = self.get_url() + '/tree2'
167
 
        revinfo = self.get_in_history('revno:2:%s' % (url,))
168
 
        self.assertNotEqual(self.tree.branch.base, revinfo.branch.base)
169
 
        self.assertEqual(self.tree2.branch.base, revinfo.branch.base)
170
 
        self.assertEqual(2, revinfo.revno)
171
 
        self.assertEqual('alt_r2', revinfo.rev_id)
172
 
 
173
 
    def test_negative_with_url(self):
174
 
        url = self.get_url() + '/tree2'
175
 
        revinfo = self.get_in_history('revno:-1:%s' % (url,))
176
 
        self.assertNotEqual(self.tree.branch.base, revinfo.branch.base)
177
 
        self.assertEqual(self.tree2.branch.base, revinfo.branch.base)
178
 
        self.assertEqual(2, revinfo.revno)
179
 
        self.assertEqual('alt_r2', revinfo.rev_id)
180
 
 
181
 
    def test_different_history_lengths(self):
182
 
        # Make sure we use the revisions and offsets in the supplied branch
183
 
        # not the ones in the original branch.
184
 
        self.tree2.commit('three', rev_id='r3')
185
 
        self.assertInHistoryIs(3, 'r3', 'revno:3:tree2')
186
 
        self.assertInHistoryIs(3, 'r3', 'revno:-1:tree2')
187
 
 
188
 
    def test_invalid_branch(self):
189
 
        self.assertRaises(errors.NotBranchError,
190
 
                          self.get_in_history, 'revno:-1:tree3')
191
 
 
192
 
    def test_invalid_revno_in_branch(self):
193
 
        self.tree.commit('three', rev_id='r3')
194
 
        self.assertInvalid('revno:3:tree2')
195
 
 
196
 
    def test_revno_n_path(self):
197
 
        """Old revno:N:path tests"""
198
 
        wta = self.make_branch_and_tree('a')
199
 
        ba = wta.branch
200
 
        
201
 
        wta.commit('Commit one', rev_id='a@r-0-1')
202
 
        wta.commit('Commit two', rev_id='a@r-0-2')
203
 
        wta.commit('Commit three', rev_id='a@r-0-3')
204
 
 
205
 
        wtb = self.make_branch_and_tree('b')
206
 
        bb = wtb.branch
207
 
 
208
 
        wtb.commit('Commit one', rev_id='b@r-0-1')
209
 
        wtb.commit('Commit two', rev_id='b@r-0-2')
210
 
        wtb.commit('Commit three', rev_id='b@r-0-3')
211
 
 
212
 
 
213
 
        self.assertEqual((1, 'a@r-0-1'),
214
 
                         spec_in_history('revno:1:a/', ba))
215
 
        # The argument of in_history should be ignored since it is
216
 
        # redundant with the path in the spec.
217
 
        self.assertEqual((1, 'a@r-0-1'),
218
 
                         spec_in_history('revno:1:a/', None))
219
 
        self.assertEqual((1, 'a@r-0-1'),
220
 
                         spec_in_history('revno:1:a/', bb))
221
 
        self.assertEqual((2, 'b@r-0-2'),
222
 
                         spec_in_history('revno:2:b/', None))
223
 
 
224
 
 
225
 
 
226
 
class TestRevisionSpec_revid(TestRevisionSpec):
227
 
    
228
 
    def test_in_history(self):
229
 
        # We should be able to access revisions that are directly
230
 
        # in the history.
231
 
        self.assertInHistoryIs(1, 'r1', 'revid:r1')
232
 
        self.assertInHistoryIs(2, 'r2', 'revid:r2')
233
 
        
234
 
    def test_missing(self):
235
 
        self.assertInvalid('revid:r3')
236
 
 
237
 
    def test_merged(self):
238
 
        """We can reach revisions in the ancestry"""
239
 
        self.assertInHistoryIs(None, 'alt_r2', 'revid:alt_r2')
240
 
 
241
 
    def test_not_here(self):
242
 
        self.tree2.commit('alt third', rev_id='alt_r3')
243
 
        # It exists in tree2, but not in tree
244
 
        self.assertInvalid('revid:alt_r3')
245
 
 
246
 
    def test_in_repository(self):
247
 
        """We can get any revision id in the repository"""
248
 
        # XXX: This may change in the future, but for now, it is true
249
 
        self.tree2.commit('alt third', rev_id='alt_r3')
250
 
        self.tree.branch.repository.fetch(self.tree2.branch.repository,
251
 
                                          revision_id='alt_r3')
252
 
        self.assertInHistoryIs(None, 'alt_r3', 'revid:alt_r3')
253
 
 
254
 
 
255
 
class TestRevisionSpec_last(TestRevisionSpec):
256
 
 
257
 
    def test_positive(self):
258
 
        self.assertInHistoryIs(2, 'r2', 'last:1')
259
 
        self.assertInHistoryIs(1, 'r1', 'last:2')
260
 
        self.assertInHistoryIs(0, None, 'last:3')
261
 
 
262
 
    def test_empty(self):
263
 
        self.assertInHistoryIs(2, 'r2', 'last:')
264
 
 
265
 
    def test_negative(self):
266
 
        self.assertInvalid('last:-1',
267
 
                           extra='\nyou must supply a positive value')
268
 
 
269
 
    def test_missing(self):
270
 
        self.assertInvalid('last:4')
271
 
 
272
 
    def test_no_history(self):
273
 
        tree = self.make_branch_and_tree('tree3')
274
 
 
275
 
        self.assertRaises(errors.NoCommits,
276
 
                          spec_in_history, 'last:', tree.branch)
277
 
 
278
 
    def test_not_a_number(self):
279
 
        try:
280
 
            int('Y')
281
 
        except ValueError, e:
282
 
            pass
283
 
        self.assertInvalid('last:Y', extra='\n' + str(e))
284
 
 
285
 
 
286
 
class TestRevisionSpec_before(TestRevisionSpec):
287
 
 
288
 
    def test_int(self):
289
 
        self.assertInHistoryIs(1, 'r1', 'before:2')
290
 
        self.assertInHistoryIs(1, 'r1', 'before:-1')
291
 
 
292
 
    def test_before_one(self):
293
 
        self.assertInHistoryIs(0, None, 'before:1')
294
 
 
295
 
    def test_before_none(self):
296
 
        self.assertInvalid('before:0',
297
 
                           extra='\ncannot go before the null: revision')
298
 
 
299
 
    def test_revid(self):
300
 
        self.assertInHistoryIs(1, 'r1', 'before:revid:r2')
301
 
 
302
 
    def test_last(self):
303
 
        self.assertInHistoryIs(1, 'r1', 'before:last:1')
304
 
 
305
 
    def test_alt_revid(self):
306
 
        # This will grab the left-most ancestor for alternate histories
307
 
        self.assertInHistoryIs(1, 'r1', 'before:revid:alt_r2')
308
 
 
309
 
    def test_alt_no_parents(self):
310
 
        new_tree = self.make_branch_and_tree('new_tree')
311
 
        new_tree.commit('first', rev_id='new_r1')
312
 
        self.tree.branch.repository.fetch(new_tree.branch.repository,
313
 
                                          revision_id='new_r1')
314
 
        self.assertInHistoryIs(0, None, 'before:revid:new_r1')
315
 
 
316
 
 
317
 
class TestRevisionSpec_tag(TestRevisionSpec):
318
 
    
319
 
    def test_invalid(self):
320
 
        self.assertInvalid('tag:foo', extra='\ntag: namespace registered,'
321
 
                                            ' but not implemented')
322
 
 
323
 
 
324
 
class TestRevisionSpec_date(TestRevisionSpec):
325
 
 
326
 
    def setUp(self):
327
 
        super(TestRevisionSpec, self).setUp()
328
 
 
329
 
        new_tree = self.make_branch_and_tree('new_tree')
330
 
        new_tree.commit('Commit one', rev_id='new_r1',
331
 
                        timestamp=time.time() - 60*60*24)
332
 
        new_tree.commit('Commit two', rev_id='new_r2')
333
 
        new_tree.commit('Commit three', rev_id='new_r3')
334
 
 
335
 
        self.tree = new_tree
336
 
 
337
 
    def test_tomorrow(self):
338
 
        self.assertInvalid('date:tomorrow')
339
 
 
340
 
    def test_today(self):
341
 
        self.assertInHistoryIs(2, 'new_r2', 'date:today')
342
 
        self.assertInHistoryIs(1, 'new_r1', 'before:date:today')
343
 
 
344
 
    def test_yesterday(self):
345
 
        self.assertInHistoryIs(1, 'new_r1', 'date:yesterday')
346
 
 
347
 
    def test_invalid(self):
348
 
        self.assertInvalid('date:foobar', extra='\ninvalid date')
349
 
        # You must have '-' between year/month/day
350
 
        self.assertInvalid('date:20040404', extra='\ninvalid date')
351
 
        # Need 2 digits for each date piece
352
 
        self.assertInvalid('date:2004-4-4', extra='\ninvalid date')
353
 
 
354
 
    def test_day(self):
355
 
        now = datetime.datetime.now()
356
 
        self.assertInHistoryIs(2, 'new_r2',
357
 
            'date:%04d-%02d-%02d' % (now.year, now.month, now.day))
358
 
 
359
 
 
360
 
class TestRevisionSpec_ancestor(TestRevisionSpec):
361
 
    
362
 
    def test_non_exact_branch(self):
363
 
        # It seems better to require an exact path to the branch
364
 
        # Branch.open() rather than using Branch.open_containing()
365
 
        self.assertRaises(errors.NotBranchError,
366
 
                          self.get_in_history, 'ancestor:tree2/a')
367
 
 
368
 
    def test_simple(self):
369
 
        # Common ancestor of trees is 'alt_r2'
370
 
        self.assertInHistoryIs(None, 'alt_r2', 'ancestor:tree2')
371
 
 
372
 
        # Going the other way, we get a valid revno
373
 
        tmp = self.tree
374
 
        self.tree = self.tree2
375
 
        self.tree2 = tmp
376
 
        self.assertInHistoryIs(2, 'alt_r2', 'ancestor:tree')
377
 
 
378
 
    def test_self(self):
379
 
        self.assertInHistoryIs(2, 'r2', 'ancestor:tree')
380
 
 
381
 
    def test_unrelated(self):
382
 
        new_tree = self.make_branch_and_tree('new_tree')
383
 
 
384
 
        new_tree.commit('Commit one', rev_id='new_r1')
385
 
        new_tree.commit('Commit two', rev_id='new_r2')
386
 
        new_tree.commit('Commit three', rev_id='new_r3')
387
 
 
388
 
        # With no common ancestor, we should raise another user error
389
 
        self.assertRaises(errors.NoCommonAncestor,
390
 
                          self.get_in_history, 'ancestor:new_tree')
391
 
 
392
 
    def test_no_commits(self):
393
 
        new_tree = self.make_branch_and_tree('new_tree')
394
 
        self.assertRaises(errors.NoCommits,
395
 
                          spec_in_history, 'ancestor:new_tree',
396
 
                                           self.tree.branch)
397
 
                        
398
 
        self.assertRaises(errors.NoCommits,
399
 
                          spec_in_history, 'ancestor:tree',
400
 
                                           new_tree.branch)
401
 
 
402
 
 
403
 
class TestRevisionSpec_branch(TestRevisionSpec):
404
 
    
405
 
    def test_non_exact_branch(self):
406
 
        # It seems better to require an exact path to the branch
407
 
        # Branch.open() rather than using Branch.open_containing()
408
 
        self.assertRaises(errors.NotBranchError,
409
 
                          self.get_in_history, 'branch:tree2/a')
410
 
 
411
 
    def test_simple(self):
412
 
        self.assertInHistoryIs(None, 'alt_r2', 'branch:tree2')
413
 
 
414
 
    def test_self(self):
415
 
        self.assertInHistoryIs(2, 'r2', 'branch:tree')
416
 
 
417
 
    def test_unrelated(self):
418
 
        new_tree = self.make_branch_and_tree('new_tree')
419
 
 
420
 
        new_tree.commit('Commit one', rev_id='new_r1')
421
 
        new_tree.commit('Commit two', rev_id='new_r2')
422
 
        new_tree.commit('Commit three', rev_id='new_r3')
423
 
 
424
 
        self.assertInHistoryIs(None, 'new_r3', 'branch:new_tree')
425
 
 
426
 
        # XXX: Right now, we use fetch() to make sure the remote revisions
427
 
        # have been pulled into the local branch. We may change that
428
 
        # behavior in the future.
429
 
        self.failUnless(self.tree.branch.repository.has_revision('new_r3'))
430
 
 
431
 
    def test_no_commits(self):
432
 
        new_tree = self.make_branch_and_tree('new_tree')
433
 
        self.assertRaises(errors.NoCommits,
434
 
                          self.get_in_history, 'branch:new_tree')