75
75
% (revision_spec, exp_revision_id, rev_info.rev_id))
77
def assertInvalid(self, revision_spec, extra='',
78
invalid_as_revision_id=True):
77
def assertInvalid(self, revision_spec, extra=''):
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)
85
self.fail('Expected InvalidRevisionSpec to be raised for'
86
' %r.in_history' % (revision_spec,))
87
if invalid_as_revision_id:
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)
95
self.fail('Expected InvalidRevisionSpec to be raised for'
96
' %r.as_revision_id' % (revision_spec,))
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))
104
def get_as_tree(self, revision_spec, tree=None):
107
spec = RevisionSpec.from_string(revision_spec)
108
return spec.as_tree(tree.branch)
111
class RevisionSpecMatchOnTrap(RevisionSpec):
113
def _match_on(self, branch, revs):
114
self.last_call = (branch, revs)
115
return super(RevisionSpecMatchOnTrap, self)._match_on(branch, revs)
118
class TestRevisionSpecBase(TestRevisionSpec):
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)
126
self.assertEqual((self.tree.branch, ['r1' ,'r2']),
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)
136
self.assertEqual((self.tree.branch, None), spec.last_call)
84
self.fail('Expected InvalidRevisionSpec to be raised for %s'
140
88
class TestOddRevisionSpec(TestRevisionSpec):
141
89
"""Test things that aren't normally thought of as revision specs"""
143
91
def test_none(self):
144
self.assertInHistoryIs(None, None, None)
92
self.assertInHistoryIs(0, None, None)
146
94
def test_object(self):
147
95
self.assertRaises(TypeError, RevisionSpec.from_string, object())
150
class TestRevisionSpec_dwim(TestRevisionSpec):
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')
158
def test_dwim_spec_revid(self):
159
self.assertInHistoryIs(2, 'r2', 'r2')
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')
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'])
176
self.tree.commit('b', rev_id='r3')
177
self.assertAsRevisionId('r3', '3')
179
def test_dwim_spec_date(self):
180
self.assertAsRevisionId('r1', 'today')
182
def test_dwim_spec_branch(self):
183
self.assertInHistoryIs(None, 'alt_r2', 'tree2')
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')
105
class TestRevnoFromString(TestCase):
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)
194
117
class TestRevisionSpec_revno(TestRevisionSpec):
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')
202
125
def test_dotted_decimal(self):
203
126
self.assertInHistoryIs(None, 'alt_r2', '1.1.1')
204
self.assertInvalid('1.1.123')
206
128
def test_negative_int(self):
207
129
self.assertInHistoryIs(2, 'r2', '-1')
325
247
self.assertEqual((2, 'b@r-0-2'),
326
248
spec_in_history('revno:2:b/', None))
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')
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())
351
252
class TestRevisionSpec_revid(TestRevisionSpec):
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')
359
260
def test_missing(self):
360
self.assertInvalid('revid:r3', invalid_as_revision_id=False)
261
self.assertInvalid('revid:r3')
362
263
def test_merged(self):
363
264
"""We can reach revisions in the ancestry"""