75
75
% (revision_spec, exp_revision_id, rev_info.rev_id))
77
def assertInvalid(self, revision_spec, extra=''):
77
def assertInvalid(self, revision_spec, extra='',
78
invalid_as_revision_id=True):
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)
84
self.fail('Expected InvalidRevisionSpec to be raised for %s'
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)
88
140
class TestOddRevisionSpec(TestRevisionSpec):
89
141
"""Test things that aren't normally thought of as revision specs"""
91
143
def test_none(self):
92
self.assertInHistoryIs(0, None, None)
144
self.assertInHistoryIs(None, None, None)
94
146
def test_object(self):
95
147
self.assertRaises(TypeError, RevisionSpec.from_string, object())
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)
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)
117
194
class TestRevisionSpec_revno(TestRevisionSpec):
119
196
def test_positive_int(self):
120
self.assertInHistoryIs(0, None, '0')
197
self.assertInHistoryIs(0, 'null:', '0')
121
198
self.assertInHistoryIs(1, 'r1', '1')
122
199
self.assertInHistoryIs(2, 'r2', '2')
123
200
self.assertInvalid('3')
125
202
def test_dotted_decimal(self):
126
203
self.assertInHistoryIs(None, 'alt_r2', '1.1.1')
204
self.assertInvalid('1.1.123')
128
206
def test_negative_int(self):
129
207
self.assertInHistoryIs(2, 'r2', '-1')
247
325
self.assertEqual((2, 'b@r-0-2'),
248
326
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())
252
351
class TestRevisionSpec_revid(TestRevisionSpec):
254
353
def test_in_history(self):
255
354
# We should be able to access revisions that are directly
256
355
# in the history.
257
356
self.assertInHistoryIs(1, 'r1', 'revid:r1')
258
357
self.assertInHistoryIs(2, 'r2', 'revid:r2')
260
359
def test_missing(self):
261
self.assertInvalid('revid:r3')
360
self.assertInvalid('revid:r3', invalid_as_revision_id=False)
263
362
def test_merged(self):
264
363
"""We can reach revisions in the ancestry"""