~bzr-pqm/bzr/bzr.dev

2220.2.3 by Martin Pool
Add tag: revision namespace.
1
# Copyright (C) 2004, 2005, 2006, 2007 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
897 by Martin Pool
- merge john's revision-naming code
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
897 by Martin Pool
- merge john's revision-naming code
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
897 by Martin Pool
- merge john's revision-naming code
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
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
17
import datetime
974.1.52 by aaron.bentley at utoronto
Merged mpool's latest changes (~0.0.7)
18
import os
1185.1.39 by Robert Collins
Robey Pointers before: namespace to clear up usage of dates in revision parameters
19
import time
1432 by Robert Collins
branch: namespace
20
1948.4.1 by John Arbash Meinel
Update number parsers to raise InvalidRevisionSpec. Update revno: itself so it supports negative numbers
21
from bzrlib import (
2220.2.11 by mbp at sourcefrog
Get tag tests working again, stored in the Branch
22
    branch,
2220.2.3 by Martin Pool
Add tag: revision namespace.
23
    bzrdir,
1948.4.1 by John Arbash Meinel
Update number parsers to raise InvalidRevisionSpec. Update revno: itself so it supports negative numbers
24
    errors,
2220.2.3 by Martin Pool
Add tag: revision namespace.
25
    repository,
1948.4.1 by John Arbash Meinel
Update number parsers to raise InvalidRevisionSpec. Update revno: itself so it supports negative numbers
26
    )
1988.4.5 by Robert Collins
revisions can now be specified using dotted-decimal revision numbers.
27
from bzrlib.tests import TestCase, TestCaseWithTransport
2220.2.3 by Martin Pool
Add tag: revision namespace.
28
from bzrlib.revisionspec import (
29
    RevisionSpec,
30
    RevisionSpec_revno,
31
    RevisionSpec_tag,
32
    )
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
33
34
35
def spec_in_history(spec, branch):
36
    """A simple helper to change a revision spec into a branch search"""
37
    return RevisionSpec.from_string(spec).in_history(branch)
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
38
39
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
40
# Basic class, which just creates a really basic set of revisions
41
class TestRevisionSpec(TestCaseWithTransport):
42
43
    def setUp(self):
44
        super(TestRevisionSpec, self).setUp()
1988.4.5 by Robert Collins
revisions can now be specified using dotted-decimal revision numbers.
45
        # this sets up a revision graph:
46
        # r1: []             1
47
        # alt_r2: [r1]       1.1.1
48
        # r2: [r1, alt_r2]   2
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
49
50
        self.tree = self.make_branch_and_tree('tree')
51
        self.build_tree(['tree/a'])
3298.2.3 by John Arbash Meinel
Add tests that all RevisionSpecs implement in_branch(b, needs_revno)
52
        self.tree.lock_write()
3298.2.11 by Aaron Bentley
Update tests for null:, clea up slightly
53
        self.addCleanup(self.tree.unlock)
54
        self.tree.add(['a'])
55
        self.tree.commit('a', rev_id='r1')
56
57
        self.tree2 = self.tree.bzrdir.sprout('tree2').open_workingtree()
58
        self.tree2.commit('alt', rev_id='alt_r2')
59
60
        self.tree.merge_from_branch(self.tree2.branch)
61
        self.tree.commit('second', rev_id='r2')
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
62
63
    def get_in_history(self, revision_spec):
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
64
        return spec_in_history(revision_spec, self.tree.branch)
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
65
66
    def assertInHistoryIs(self, exp_revno, exp_revision_id, revision_spec):
67
        rev_info = self.get_in_history(revision_spec)
68
        self.assertEqual(exp_revno, rev_info.revno,
2325.2.1 by Marien Zwart
Make the test suite failure reporting a bit more robust.
69
                         'Revision spec: %r returned wrong revno: %r != %r'
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
70
                         % (revision_spec, exp_revno, rev_info.revno))
71
        self.assertEqual(exp_revision_id, rev_info.rev_id,
2325.2.1 by Marien Zwart
Make the test suite failure reporting a bit more robust.
72
                         'Revision spec: %r returned wrong revision id:'
73
                         ' %r != %r'
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
74
                         % (revision_spec, exp_revision_id, rev_info.rev_id))
75
3495.1.1 by John Arbash Meinel
Fix bug #239933, use the right exception for -c0
76
    def assertInvalid(self, revision_spec, extra='',
77
                      invalid_as_revision_id=True):
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
78
        try:
79
            self.get_in_history(revision_spec)
80
        except errors.InvalidRevisionSpec, e:
1948.4.23 by John Arbash Meinel
Change the handling of negative numbers, to be trapped at revno 1
81
            self.assertEqual(revision_spec, e.spec)
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
82
            self.assertEqual(extra, e.extra)
1948.4.5 by John Arbash Meinel
Fix tests for negative entries, and add tests for revno:
83
        else:
3495.1.1 by John Arbash Meinel
Fix bug #239933, use the right exception for -c0
84
            self.fail('Expected InvalidRevisionSpec to be raised for'
85
                      ' %r.in_history' % (revision_spec,))
86
        if invalid_as_revision_id:
87
            try:
88
                spec = RevisionSpec.from_string(revision_spec)
89
                spec.as_revision_id(self.tree.branch)
90
            except errors.InvalidRevisionSpec, e:
91
                self.assertEqual(revision_spec, e.spec)
92
                self.assertEqual(extra, e.extra)
93
            else:
94
                self.fail('Expected InvalidRevisionSpec to be raised for'
3495.1.2 by John Arbash Meinel
tweak
95
                          ' %r.as_revision_id' % (revision_spec,))
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
96
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
97
    def assertAsRevisionId(self, revision_id, revision_spec):
98
        """Calling as_revision_id() should return the specified id."""
99
        spec = RevisionSpec.from_string(revision_spec)
100
        self.assertEqual(revision_id,
101
                         spec.as_revision_id(self.tree.branch))
102
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
103
3460.1.2 by John Arbash Meinel
Add a test for wants_revision_history
104
class RevisionSpecMatchOnTrap(RevisionSpec):
105
106
    def _match_on(self, branch, revs):
107
        self.last_call = (branch, revs)
108
        return super(RevisionSpecMatchOnTrap, self)._match_on(branch, revs)
109
110
111
class TestRevisionSpecBase(TestRevisionSpec):
112
113
    def test_wants_revision_history(self):
114
        # If wants_revision_history = True, then _match_on should get the
115
        # branch revision history
116
        spec = RevisionSpecMatchOnTrap('foo', _internal=True)
117
        spec.in_history(self.tree.branch)
118
119
        self.assertEqual((self.tree.branch, ['r1' ,'r2']),
120
                         spec.last_call)
121
122
    def test_wants_no_revision_history(self):
123
        # If wants_revision_history = False, then _match_on should get None for
124
        # the branch revision history
125
        spec = RevisionSpecMatchOnTrap('foo', _internal=True)
126
        spec.wants_revision_history = False
127
        spec.in_history(self.tree.branch)
128
129
        self.assertEqual((self.tree.branch, None), spec.last_call)
130
131
132
1948.4.19 by John Arbash Meinel
All old tests are covered elsewhere
133
class TestOddRevisionSpec(TestRevisionSpec):
134
    """Test things that aren't normally thought of as revision specs"""
135
136
    def test_none(self):
3298.2.11 by Aaron Bentley
Update tests for null:, clea up slightly
137
        self.assertInHistoryIs(None, None, None)
1948.4.19 by John Arbash Meinel
All old tests are covered elsewhere
138
139
    def test_object(self):
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
140
        self.assertRaises(TypeError, RevisionSpec.from_string, object())
1948.4.19 by John Arbash Meinel
All old tests are covered elsewhere
141
1948.4.25 by John Arbash Meinel
Check that invalid specs are properly handled
142
    def test_unregistered_spec(self):
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
143
        self.assertRaises(errors.NoSuchRevisionSpec,
144
                          RevisionSpec.from_string, 'foo')
145
        self.assertRaises(errors.NoSuchRevisionSpec,
146
                          RevisionSpec.from_string, '123a')
1948.4.32 by John Arbash Meinel
Clean up __repr__, as well as add tests that we can handle -r12:branch/
147
148
1988.4.5 by Robert Collins
revisions can now be specified using dotted-decimal revision numbers.
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)
160
161
1948.4.32 by John Arbash Meinel
Clean up __repr__, as well as add tests that we can handle -r12:branch/
162
class TestRevisionSpec_revno(TestRevisionSpec):
163
164
    def test_positive_int(self):
2598.5.10 by Aaron Bentley
Return NULL_REVISION instead of None for the null revision
165
        self.assertInHistoryIs(0, 'null:', '0')
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
166
        self.assertInHistoryIs(1, 'r1', '1')
167
        self.assertInHistoryIs(2, 'r2', '2')
1948.4.23 by John Arbash Meinel
Change the handling of negative numbers, to be trapped at revno 1
168
        self.assertInvalid('3')
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
169
1988.4.5 by Robert Collins
revisions can now be specified using dotted-decimal revision numbers.
170
    def test_dotted_decimal(self):
171
        self.assertInHistoryIs(None, 'alt_r2', '1.1.1')
172
1948.4.32 by John Arbash Meinel
Clean up __repr__, as well as add tests that we can handle -r12:branch/
173
    def test_negative_int(self):
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
174
        self.assertInHistoryIs(2, 'r2', '-1')
175
        self.assertInHistoryIs(1, 'r1', '-2')
176
1948.4.23 by John Arbash Meinel
Change the handling of negative numbers, to be trapped at revno 1
177
        self.assertInHistoryIs(1, 'r1', '-3')
178
        self.assertInHistoryIs(1, 'r1', '-4')
179
        self.assertInHistoryIs(1, 'r1', '-100')
1948.4.5 by John Arbash Meinel
Fix tests for negative entries, and add tests for revno:
180
181
    def test_positive(self):
2598.5.10 by Aaron Bentley
Return NULL_REVISION instead of None for the null revision
182
        self.assertInHistoryIs(0, 'null:', 'revno:0')
1948.4.5 by John Arbash Meinel
Fix tests for negative entries, and add tests for revno:
183
        self.assertInHistoryIs(1, 'r1', 'revno:1')
184
        self.assertInHistoryIs(2, 'r2', 'revno:2')
185
186
        self.assertInvalid('revno:3')
187
188
    def test_negative(self):
189
        self.assertInHistoryIs(2, 'r2', 'revno:-1')
190
        self.assertInHistoryIs(1, 'r1', 'revno:-2')
191
1948.4.23 by John Arbash Meinel
Change the handling of negative numbers, to be trapped at revno 1
192
        self.assertInHistoryIs(1, 'r1', 'revno:-3')
193
        self.assertInHistoryIs(1, 'r1', 'revno:-4')
1948.4.6 by John Arbash Meinel
A small bugfix, and more tests for revno:
194
195
    def test_invalid_number(self):
196
        # Get the right exception text
197
        try:
198
            int('X')
199
        except ValueError, e:
200
            pass
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
201
        self.assertInvalid('revno:X', extra='\n' + str(e))
1948.4.6 by John Arbash Meinel
A small bugfix, and more tests for revno:
202
203
    def test_missing_number_and_branch(self):
204
        self.assertInvalid('revno::',
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
205
                           extra='\ncannot have an empty revno and no branch')
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
206
207
    def test_invalid_number_with_branch(self):
208
        try:
209
            int('X')
210
        except ValueError, e:
211
            pass
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
212
        self.assertInvalid('revno:X:tree2', extra='\n' + str(e))
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
213
1948.4.16 by John Arbash Meinel
Move the tests into the associated tester, remove redundant tests, some small PEP8 changes
214
    def test_non_exact_branch(self):
215
        # It seems better to require an exact path to the branch
216
        # Branch.open() rather than using Branch.open_containing()
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
217
        spec = RevisionSpec.from_string('revno:2:tree2/a')
1948.4.16 by John Arbash Meinel
Move the tests into the associated tester, remove redundant tests, some small PEP8 changes
218
        self.assertRaises(errors.NotBranchError,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
219
                          spec.in_history, self.tree.branch)
1948.4.16 by John Arbash Meinel
Move the tests into the associated tester, remove redundant tests, some small PEP8 changes
220
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
221
    def test_with_branch(self):
222
        # Passing a URL overrides the supplied branch path
223
        revinfo = self.get_in_history('revno:2:tree2')
224
        self.assertNotEqual(self.tree.branch.base, revinfo.branch.base)
225
        self.assertEqual(self.tree2.branch.base, revinfo.branch.base)
226
        self.assertEqual(2, revinfo.revno)
227
        self.assertEqual('alt_r2', revinfo.rev_id)
228
1948.4.32 by John Arbash Meinel
Clean up __repr__, as well as add tests that we can handle -r12:branch/
229
    def test_int_with_branch(self):
230
        revinfo = self.get_in_history('2:tree2')
231
        self.assertNotEqual(self.tree.branch.base, revinfo.branch.base)
232
        self.assertEqual(self.tree2.branch.base, revinfo.branch.base)
233
        self.assertEqual(2, revinfo.revno)
234
        self.assertEqual('alt_r2', revinfo.rev_id)
235
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
236
    def test_with_url(self):
237
        url = self.get_url() + '/tree2'
238
        revinfo = self.get_in_history('revno:2:%s' % (url,))
239
        self.assertNotEqual(self.tree.branch.base, revinfo.branch.base)
240
        self.assertEqual(self.tree2.branch.base, revinfo.branch.base)
241
        self.assertEqual(2, revinfo.revno)
242
        self.assertEqual('alt_r2', revinfo.rev_id)
243
244
    def test_negative_with_url(self):
245
        url = self.get_url() + '/tree2'
246
        revinfo = self.get_in_history('revno:-1:%s' % (url,))
247
        self.assertNotEqual(self.tree.branch.base, revinfo.branch.base)
248
        self.assertEqual(self.tree2.branch.base, revinfo.branch.base)
249
        self.assertEqual(2, revinfo.revno)
250
        self.assertEqual('alt_r2', revinfo.rev_id)
251
1948.4.22 by John Arbash Meinel
Refactor common code from integer revno handlers
252
    def test_different_history_lengths(self):
253
        # Make sure we use the revisions and offsets in the supplied branch
254
        # not the ones in the original branch.
255
        self.tree2.commit('three', rev_id='r3')
256
        self.assertInHistoryIs(3, 'r3', 'revno:3:tree2')
257
        self.assertInHistoryIs(3, 'r3', 'revno:-1:tree2')
258
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
259
    def test_invalid_branch(self):
260
        self.assertRaises(errors.NotBranchError,
261
                          self.get_in_history, 'revno:-1:tree3')
262
263
    def test_invalid_revno_in_branch(self):
264
        self.tree.commit('three', rev_id='r3')
265
        self.assertInvalid('revno:3:tree2')
1948.4.8 by John Arbash Meinel
Testing the revid: spec
266
1948.4.16 by John Arbash Meinel
Move the tests into the associated tester, remove redundant tests, some small PEP8 changes
267
    def test_revno_n_path(self):
268
        """Old revno:N:path tests"""
269
        wta = self.make_branch_and_tree('a')
270
        ba = wta.branch
271
        
272
        wta.commit('Commit one', rev_id='a@r-0-1')
273
        wta.commit('Commit two', rev_id='a@r-0-2')
274
        wta.commit('Commit three', rev_id='a@r-0-3')
275
276
        wtb = self.make_branch_and_tree('b')
277
        bb = wtb.branch
278
279
        wtb.commit('Commit one', rev_id='b@r-0-1')
280
        wtb.commit('Commit two', rev_id='b@r-0-2')
281
        wtb.commit('Commit three', rev_id='b@r-0-3')
282
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
283
284
        self.assertEqual((1, 'a@r-0-1'),
285
                         spec_in_history('revno:1:a/', ba))
1948.4.16 by John Arbash Meinel
Move the tests into the associated tester, remove redundant tests, some small PEP8 changes
286
        # The argument of in_history should be ignored since it is
287
        # redundant with the path in the spec.
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
288
        self.assertEqual((1, 'a@r-0-1'),
289
                         spec_in_history('revno:1:a/', None))
290
        self.assertEqual((1, 'a@r-0-1'),
291
                         spec_in_history('revno:1:a/', bb))
292
        self.assertEqual((2, 'b@r-0-2'),
293
                         spec_in_history('revno:2:b/', None))
1948.4.16 by John Arbash Meinel
Move the tests into the associated tester, remove redundant tests, some small PEP8 changes
294
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
295
    def test_as_revision_id(self):
3298.2.11 by Aaron Bentley
Update tests for null:, clea up slightly
296
        self.assertAsRevisionId('null:', '0')
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
297
        self.assertAsRevisionId('r1', '1')
298
        self.assertAsRevisionId('r2', '2')
299
        self.assertAsRevisionId('r1', '-2')
300
        self.assertAsRevisionId('r2', '-1')
301
        self.assertAsRevisionId('alt_r2', '1.1.1')
302
1948.4.8 by John Arbash Meinel
Testing the revid: spec
303
304
class TestRevisionSpec_revid(TestRevisionSpec):
305
    
306
    def test_in_history(self):
307
        # We should be able to access revisions that are directly
308
        # in the history.
309
        self.assertInHistoryIs(1, 'r1', 'revid:r1')
310
        self.assertInHistoryIs(2, 'r2', 'revid:r2')
311
        
312
    def test_missing(self):
3495.1.1 by John Arbash Meinel
Fix bug #239933, use the right exception for -c0
313
        self.assertInvalid('revid:r3', invalid_as_revision_id=False)
1948.4.8 by John Arbash Meinel
Testing the revid: spec
314
315
    def test_merged(self):
316
        """We can reach revisions in the ancestry"""
317
        self.assertInHistoryIs(None, 'alt_r2', 'revid:alt_r2')
318
319
    def test_not_here(self):
320
        self.tree2.commit('alt third', rev_id='alt_r3')
321
        # It exists in tree2, but not in tree
3495.1.1 by John Arbash Meinel
Fix bug #239933, use the right exception for -c0
322
        self.assertInvalid('revid:alt_r3', invalid_as_revision_id=False)
1948.4.8 by John Arbash Meinel
Testing the revid: spec
323
324
    def test_in_repository(self):
325
        """We can get any revision id in the repository"""
326
        # XXX: This may change in the future, but for now, it is true
327
        self.tree2.commit('alt third', rev_id='alt_r3')
328
        self.tree.branch.repository.fetch(self.tree2.branch.repository,
329
                                          revision_id='alt_r3')
330
        self.assertInHistoryIs(None, 'alt_r3', 'revid:alt_r3')
1948.4.9 by John Arbash Meinel
Cleanup and test last:
331
2325.2.2 by Marien Zwart
Make the revid RevisionSpec always return a str object, not a unicode object.
332
    def test_unicode(self):
333
        """We correctly convert a unicode ui string to an encoded revid."""
2325.2.4 by Marien Zwart
Rename rev_id to revision_id because HACKING says so.
334
        revision_id = u'\N{SNOWMAN}'.encode('utf-8')
335
        self.tree.commit('unicode', rev_id=revision_id)
336
        self.assertInHistoryIs(3, revision_id, u'revid:\N{SNOWMAN}')
337
        self.assertInHistoryIs(3, revision_id, 'revid:' + revision_id)
2325.2.2 by Marien Zwart
Make the revid RevisionSpec always return a str object, not a unicode object.
338
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
339
    def test_as_revision_id(self):
340
        self.assertAsRevisionId('r1', 'revid:r1')
341
        self.assertAsRevisionId('r2', 'revid:r2')
342
        self.assertAsRevisionId('alt_r2', 'revid:alt_r2')
343
1948.4.9 by John Arbash Meinel
Cleanup and test last:
344
345
class TestRevisionSpec_last(TestRevisionSpec):
346
347
    def test_positive(self):
348
        self.assertInHistoryIs(2, 'r2', 'last:1')
349
        self.assertInHistoryIs(1, 'r1', 'last:2')
2598.5.10 by Aaron Bentley
Return NULL_REVISION instead of None for the null revision
350
        self.assertInHistoryIs(0, 'null:', 'last:3')
1948.4.9 by John Arbash Meinel
Cleanup and test last:
351
352
    def test_empty(self):
353
        self.assertInHistoryIs(2, 'r2', 'last:')
354
355
    def test_negative(self):
356
        self.assertInvalid('last:-1',
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
357
                           extra='\nyou must supply a positive value')
1948.4.9 by John Arbash Meinel
Cleanup and test last:
358
359
    def test_missing(self):
360
        self.assertInvalid('last:4')
361
362
    def test_no_history(self):
363
        tree = self.make_branch_and_tree('tree3')
364
365
        self.assertRaises(errors.NoCommits,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
366
                          spec_in_history, 'last:', tree.branch)
1948.4.9 by John Arbash Meinel
Cleanup and test last:
367
368
    def test_not_a_number(self):
369
        try:
370
            int('Y')
371
        except ValueError, e:
372
            pass
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
373
        self.assertInvalid('last:Y', extra='\n' + str(e))
1948.4.10 by John Arbash Meinel
test the before: spec, currently asserting what seems to be buggy behavior
374
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
375
    def test_as_revision_id(self):
376
        self.assertAsRevisionId('r2', 'last:1')
377
        self.assertAsRevisionId('r1', 'last:2')
378
1948.4.10 by John Arbash Meinel
test the before: spec, currently asserting what seems to be buggy behavior
379
380
class TestRevisionSpec_before(TestRevisionSpec):
381
382
    def test_int(self):
383
        self.assertInHistoryIs(1, 'r1', 'before:2')
384
        self.assertInHistoryIs(1, 'r1', 'before:-1')
385
386
    def test_before_one(self):
2598.5.10 by Aaron Bentley
Return NULL_REVISION instead of None for the null revision
387
        self.assertInHistoryIs(0, 'null:', 'before:1')
1948.4.10 by John Arbash Meinel
test the before: spec, currently asserting what seems to be buggy behavior
388
389
    def test_before_none(self):
1948.4.13 by John Arbash Meinel
Going before:0 is an error, and if you are on another history, use the leftmost parent
390
        self.assertInvalid('before:0',
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
391
                           extra='\ncannot go before the null: revision')
1948.4.10 by John Arbash Meinel
test the before: spec, currently asserting what seems to be buggy behavior
392
393
    def test_revid(self):
394
        self.assertInHistoryIs(1, 'r1', 'before:revid:r2')
395
396
    def test_last(self):
397
        self.assertInHistoryIs(1, 'r1', 'before:last:1')
398
399
    def test_alt_revid(self):
1948.4.13 by John Arbash Meinel
Going before:0 is an error, and if you are on another history, use the leftmost parent
400
        # This will grab the left-most ancestor for alternate histories
401
        self.assertInHistoryIs(1, 'r1', 'before:revid:alt_r2')
1948.4.11 by John Arbash Meinel
Update and test the tag: spec
402
1948.4.14 by John Arbash Meinel
Test the code path for a no-parent alternate history
403
    def test_alt_no_parents(self):
404
        new_tree = self.make_branch_and_tree('new_tree')
405
        new_tree.commit('first', rev_id='new_r1')
406
        self.tree.branch.repository.fetch(new_tree.branch.repository,
407
                                          revision_id='new_r1')
2598.5.10 by Aaron Bentley
Return NULL_REVISION instead of None for the null revision
408
        self.assertInHistoryIs(0, 'null:', 'before:revid:new_r1')
1948.4.14 by John Arbash Meinel
Test the code path for a no-parent alternate history
409
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
410
    def test_as_revision_id(self):
411
        self.assertAsRevisionId('r1', 'before:revid:r2')
412
        self.assertAsRevisionId('r1', 'before:2')
413
        self.assertAsRevisionId('r1', 'before:1.1.1')
414
        self.assertAsRevisionId('r1', 'before:revid:alt_r2')
415
1948.4.11 by John Arbash Meinel
Update and test the tag: spec
416
417
class TestRevisionSpec_tag(TestRevisionSpec):
418
    
2220.2.3 by Martin Pool
Add tag: revision namespace.
419
    def make_branch_and_tree(self, relpath):
420
        # override format as the default one may not support tags
3031.3.1 by Robert Collins
Remove the unneeded ExperimentalBranch class.
421
        return TestRevisionSpec.make_branch_and_tree(
422
            self, relpath, format='dirstate-tags')
2220.2.3 by Martin Pool
Add tag: revision namespace.
423
424
    def test_from_string_tag(self):
425
        spec = RevisionSpec.from_string('tag:bzr-0.14')
426
        self.assertIsInstance(spec, RevisionSpec_tag)
427
        self.assertEqual(spec.spec, 'bzr-0.14')
428
429
    def test_lookup_tag(self):
2220.2.20 by Martin Pool
Tag methods now available through Branch.tags.add_tag, etc
430
        self.tree.branch.tags.set_tag('bzr-0.14', 'r1')
2220.2.3 by Martin Pool
Add tag: revision namespace.
431
        self.assertInHistoryIs(1, 'r1', 'tag:bzr-0.14')
3298.2.11 by Aaron Bentley
Update tests for null:, clea up slightly
432
        self.tree.branch.tags.set_tag('null_rev', 'null:')
433
        self.assertInHistoryIs(0, 'null:', 'tag:null_rev')
2220.2.3 by Martin Pool
Add tag: revision namespace.
434
435
    def test_failed_lookup(self):
436
        # tags that don't exist give a specific message: arguably we should
437
        # just give InvalidRevisionSpec but I think this is more helpful
438
        self.assertRaises(errors.NoSuchTag,
439
            self.get_in_history,
440
            'tag:some-random-tag')
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
441
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
442
    def test_as_revision_id(self):
443
        self.tree.branch.tags.set_tag('my-tag', 'r2')
3298.2.11 by Aaron Bentley
Update tests for null:, clea up slightly
444
        self.tree.branch.tags.set_tag('null_rev', 'null:')
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
445
        self.assertAsRevisionId('r2', 'tag:my-tag')
3298.2.11 by Aaron Bentley
Update tests for null:, clea up slightly
446
        self.assertAsRevisionId('null:', 'tag:null_rev')
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
447
        self.assertAsRevisionId('r1', 'before:tag:my-tag')
448
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
449
450
class TestRevisionSpec_date(TestRevisionSpec):
451
452
    def setUp(self):
453
        super(TestRevisionSpec, self).setUp()
454
455
        new_tree = self.make_branch_and_tree('new_tree')
456
        new_tree.commit('Commit one', rev_id='new_r1',
457
                        timestamp=time.time() - 60*60*24)
458
        new_tree.commit('Commit two', rev_id='new_r2')
459
        new_tree.commit('Commit three', rev_id='new_r3')
460
461
        self.tree = new_tree
462
463
    def test_tomorrow(self):
464
        self.assertInvalid('date:tomorrow')
465
466
    def test_today(self):
467
        self.assertInHistoryIs(2, 'new_r2', 'date:today')
468
        self.assertInHistoryIs(1, 'new_r1', 'before:date:today')
469
470
    def test_yesterday(self):
471
        self.assertInHistoryIs(1, 'new_r1', 'date:yesterday')
472
473
    def test_invalid(self):
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
474
        self.assertInvalid('date:foobar', extra='\ninvalid date')
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
475
        # You must have '-' between year/month/day
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
476
        self.assertInvalid('date:20040404', extra='\ninvalid date')
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
477
        # Need 2 digits for each date piece
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
478
        self.assertInvalid('date:2004-4-4', extra='\ninvalid date')
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
479
480
    def test_day(self):
481
        now = datetime.datetime.now()
482
        self.assertInHistoryIs(2, 'new_r2',
483
            'date:%04d-%02d-%02d' % (now.year, now.month, now.day))
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
484
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
485
    def test_as_revision_id(self):
486
        self.assertAsRevisionId('new_r2', 'date:today')
487
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
488
489
class TestRevisionSpec_ancestor(TestRevisionSpec):
490
    
491
    def test_non_exact_branch(self):
492
        # It seems better to require an exact path to the branch
493
        # Branch.open() rather than using Branch.open_containing()
494
        self.assertRaises(errors.NotBranchError,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
495
                          self.get_in_history, 'ancestor:tree2/a')
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
496
497
    def test_simple(self):
498
        # Common ancestor of trees is 'alt_r2'
499
        self.assertInHistoryIs(None, 'alt_r2', 'ancestor:tree2')
500
501
        # Going the other way, we get a valid revno
502
        tmp = self.tree
503
        self.tree = self.tree2
504
        self.tree2 = tmp
505
        self.assertInHistoryIs(2, 'alt_r2', 'ancestor:tree')
506
507
    def test_self(self):
508
        self.assertInHistoryIs(2, 'r2', 'ancestor:tree')
509
510
    def test_unrelated(self):
511
        new_tree = self.make_branch_and_tree('new_tree')
512
513
        new_tree.commit('Commit one', rev_id='new_r1')
514
        new_tree.commit('Commit two', rev_id='new_r2')
515
        new_tree.commit('Commit three', rev_id='new_r3')
516
517
        # With no common ancestor, we should raise another user error
518
        self.assertRaises(errors.NoCommonAncestor,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
519
                          self.get_in_history, 'ancestor:new_tree')
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
520
521
    def test_no_commits(self):
522
        new_tree = self.make_branch_and_tree('new_tree')
523
        self.assertRaises(errors.NoCommits,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
524
                          spec_in_history, 'ancestor:new_tree',
525
                                           self.tree.branch)
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
526
                        
527
        self.assertRaises(errors.NoCommits,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
528
                          spec_in_history, 'ancestor:tree',
529
                                           new_tree.branch)
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
530
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
531
    def test_as_revision_id(self):
532
        self.assertAsRevisionId('alt_r2', 'ancestor:tree2')
533
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
534
535
class TestRevisionSpec_branch(TestRevisionSpec):
536
    
537
    def test_non_exact_branch(self):
538
        # It seems better to require an exact path to the branch
539
        # Branch.open() rather than using Branch.open_containing()
540
        self.assertRaises(errors.NotBranchError,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
541
                          self.get_in_history, 'branch:tree2/a')
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
542
543
    def test_simple(self):
544
        self.assertInHistoryIs(None, 'alt_r2', 'branch:tree2')
545
546
    def test_self(self):
547
        self.assertInHistoryIs(2, 'r2', 'branch:tree')
548
549
    def test_unrelated(self):
550
        new_tree = self.make_branch_and_tree('new_tree')
551
552
        new_tree.commit('Commit one', rev_id='new_r1')
553
        new_tree.commit('Commit two', rev_id='new_r2')
554
        new_tree.commit('Commit three', rev_id='new_r3')
555
556
        self.assertInHistoryIs(None, 'new_r3', 'branch:new_tree')
557
558
        # XXX: Right now, we use fetch() to make sure the remote revisions
559
        # have been pulled into the local branch. We may change that
560
        # behavior in the future.
561
        self.failUnless(self.tree.branch.repository.has_revision('new_r3'))
562
563
    def test_no_commits(self):
564
        new_tree = self.make_branch_and_tree('new_tree')
565
        self.assertRaises(errors.NoCommits,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
566
                          self.get_in_history, 'branch:new_tree')
1551.10.32 by Aaron Bentley
Add submit: specifier, for merge-directive-like diffs
567
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
568
    def test_as_revision_id(self):
569
        self.assertAsRevisionId('alt_r2', 'branch:tree2')
570
1551.10.32 by Aaron Bentley
Add submit: specifier, for merge-directive-like diffs
571
572
class TestRevisionSpec_submit(TestRevisionSpec):
573
574
    def test_submit_branch(self):
575
        # Common ancestor of trees is 'alt_r2'
576
        self.assertRaises(errors.NoSubmitBranch, self.get_in_history,
577
                          'submit:')
578
        self.tree.branch.set_parent('../tree2')
579
        self.assertInHistoryIs(None, 'alt_r2', 'submit:')
580
        self.tree.branch.set_parent('bogus')
581
        self.assertRaises(errors.NotBranchError, self.get_in_history,
582
            'submit:')
583
        # submit branch overrides parent branch
584
        self.tree.branch.set_submit_branch('tree2')
585
        self.assertInHistoryIs(None, 'alt_r2', 'submit:')
3298.2.3 by John Arbash Meinel
Add tests that all RevisionSpecs implement in_branch(b, needs_revno)
586
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
587
    def test_as_revision_id(self):
588
        self.tree.branch.set_submit_branch('tree2')
589
        self.assertAsRevisionId('alt_r2', 'branch:tree2')