74
73
% (revision_spec, exp_revision_id, rev_info.rev_id))
76
def assertInvalid(self, revision_spec, extra=''):
75
def assertInvalid(self, revision_spec, extra='',
76
invalid_as_revision_id=True):
78
78
self.get_in_history(revision_spec)
79
79
except errors.InvalidRevisionSpec, e:
80
80
self.assertEqual(revision_spec, e.spec)
81
81
self.assertEqual(extra, e.extra)
83
self.fail('Expected InvalidRevisionSpec to be raised for %s'
83
self.fail('Expected InvalidRevisionSpec to be raised for'
84
' %r.in_history' % (revision_spec,))
85
if invalid_as_revision_id:
87
spec = RevisionSpec.from_string(revision_spec)
88
spec.as_revision_id(self.tree.branch)
89
except errors.InvalidRevisionSpec, e:
90
self.assertEqual(revision_spec, e.spec)
91
self.assertEqual(extra, e.extra)
93
self.fail('Expected InvalidRevisionSpec to be raised for'
94
' %r.as_revision_id' % (revision_spec,))
96
def assertAsRevisionId(self, revision_id, revision_spec):
97
"""Calling as_revision_id() should return the specified id."""
98
spec = RevisionSpec.from_string(revision_spec)
99
self.assertEqual(revision_id,
100
spec.as_revision_id(self.tree.branch))
102
def get_as_tree(self, revision_spec, tree=None):
105
spec = RevisionSpec.from_string(revision_spec)
106
return spec.as_tree(tree.branch)
109
class RevisionSpecMatchOnTrap(RevisionSpec):
111
def _match_on(self, branch, revs):
112
self.last_call = (branch, revs)
113
return super(RevisionSpecMatchOnTrap, self)._match_on(branch, revs)
116
class TestRevisionSpecBase(TestRevisionSpec):
118
def test_wants_revision_history(self):
119
# If wants_revision_history = True, then _match_on should get the
120
# branch revision history
121
spec = RevisionSpecMatchOnTrap('foo', _internal=True)
122
spec.wants_revision_history = True
123
self.callDeprecated(['RevisionSpec.wants_revision_history was '
124
'deprecated in 2.5 (RevisionSpecMatchOnTrap).'],
125
spec.in_history, self.tree.branch)
127
self.assertEqual((self.tree.branch, ['r1' ,'r2']),
130
def test_wants_no_revision_history(self):
131
# If wants_revision_history = False, then _match_on should get None for
132
# the branch revision history
133
spec = RevisionSpecMatchOnTrap('foo', _internal=True)
134
spec.in_history(self.tree.branch)
136
self.assertEqual((self.tree.branch, None), spec.last_call)
87
140
class TestOddRevisionSpec(TestRevisionSpec):
88
141
"""Test things that aren't normally thought of as revision specs"""
90
143
def test_none(self):
91
self.assertInHistoryIs(0, None, None)
144
self.assertInHistoryIs(None, None, None)
93
146
def test_object(self):
94
147
self.assertRaises(TypeError, RevisionSpec.from_string, object())
96
def test_unregistered_spec(self):
97
self.assertRaises(errors.NoSuchRevisionSpec,
98
RevisionSpec.from_string, 'foo')
99
self.assertRaises(errors.NoSuchRevisionSpec,
100
RevisionSpec.from_string, '123a')
104
class TestRevnoFromString(TestCase):
106
def test_from_string_dotted_decimal(self):
107
self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '-1.1')
108
self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '.1')
109
self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '1..1')
110
self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '1.2..1')
111
self.assertRaises(errors.NoSuchRevisionSpec, RevisionSpec.from_string, '1.')
112
self.assertIsInstance(RevisionSpec.from_string('1.1'), RevisionSpec_revno)
113
self.assertIsInstance(RevisionSpec.from_string('1.1.3'), RevisionSpec_revno)
150
class RevisionSpec_bork(RevisionSpec):
152
prefix = 'irrelevant:'
154
def _match_on(self, branch, revs):
155
if self.spec == "bork":
156
return RevisionInfo.from_revision_id(branch, "r1")
158
raise errors.InvalidRevisionSpec(self.spec, branch)
161
class TestRevisionSpec_dwim(TestRevisionSpec):
163
# Don't need to test revno's explicitly since TRS_revno already
164
# covers that well for us
165
def test_dwim_spec_revno(self):
166
self.assertInHistoryIs(2, 'r2', '2')
167
self.assertAsRevisionId('alt_r2', '1.1.1')
169
def test_dwim_spec_revid(self):
170
self.assertInHistoryIs(2, 'r2', 'r2')
172
def test_dwim_spec_tag(self):
173
self.tree.branch.tags.set_tag('footag', 'r1')
174
self.assertAsRevisionId('r1', 'footag')
175
self.tree.branch.tags.delete_tag('footag')
176
self.assertRaises(errors.InvalidRevisionSpec,
177
self.get_in_history, 'footag')
179
def test_dwim_spec_tag_that_looks_like_revno(self):
180
# Test that we slip past revno with things that look like revnos,
181
# but aren't. Tags are convenient for testing this since we can
182
# make them look however we want.
183
self.tree.branch.tags.set_tag('3', 'r2')
184
self.assertAsRevisionId('r2', '3')
185
self.build_tree(['tree/b'])
187
self.tree.commit('b', rev_id='r3')
188
self.assertAsRevisionId('r3', '3')
190
def test_dwim_spec_date(self):
191
self.assertAsRevisionId('r1', 'today')
193
def test_dwim_spec_branch(self):
194
self.assertInHistoryIs(None, 'alt_r2', 'tree2')
196
def test_dwim_spec_nonexistent(self):
197
self.assertInvalid('somethingrandom', invalid_as_revision_id=False)
198
self.assertInvalid('-1.1', invalid_as_revision_id=False)
199
self.assertInvalid('.1', invalid_as_revision_id=False)
200
self.assertInvalid('1..1', invalid_as_revision_id=False)
201
self.assertInvalid('1.2..1', invalid_as_revision_id=False)
202
self.assertInvalid('1.', invalid_as_revision_id=False)
204
def test_append_dwim_revspec(self):
205
original_dwim_revspecs = list(RevisionSpec_dwim._possible_revspecs)
206
def reset_dwim_revspecs():
207
RevisionSpec_dwim._possible_revspecs = original_dwim_revspecs
208
self.addCleanup(reset_dwim_revspecs)
209
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_bork)
210
self.assertAsRevisionId('r1', 'bork')
212
def test_append_lazy_dwim_revspec(self):
213
original_dwim_revspecs = list(RevisionSpec_dwim._possible_revspecs)
214
def reset_dwim_revspecs():
215
RevisionSpec_dwim._possible_revspecs = original_dwim_revspecs
216
self.addCleanup(reset_dwim_revspecs)
217
RevisionSpec_dwim.append_possible_lazy_revspec(
218
"bzrlib.tests.test_revisionspec", "RevisionSpec_bork")
219
self.assertAsRevisionId('r1', 'bork')
116
222
class TestRevisionSpec_revno(TestRevisionSpec):
118
224
def test_positive_int(self):
119
self.assertInHistoryIs(0, None, '0')
225
self.assertInHistoryIs(0, 'null:', '0')
120
226
self.assertInHistoryIs(1, 'r1', '1')
121
227
self.assertInHistoryIs(2, 'r2', '2')
122
228
self.assertInvalid('3')
124
230
def test_dotted_decimal(self):
125
231
self.assertInHistoryIs(None, 'alt_r2', '1.1.1')
232
self.assertInvalid('1.1.123')
127
234
def test_negative_int(self):
128
235
self.assertInHistoryIs(2, 'r2', '-1')
246
353
self.assertEqual((2, 'b@r-0-2'),
247
354
spec_in_history('revno:2:b/', None))
356
def test_as_revision_id(self):
357
self.assertAsRevisionId('null:', '0')
358
self.assertAsRevisionId('r1', '1')
359
self.assertAsRevisionId('r2', '2')
360
self.assertAsRevisionId('r1', '-2')
361
self.assertAsRevisionId('r2', '-1')
362
self.assertAsRevisionId('alt_r2', '1.1.1')
364
def test_as_tree(self):
365
tree = self.get_as_tree('0')
366
self.assertEquals(_mod_revision.NULL_REVISION, tree.get_revision_id())
367
tree = self.get_as_tree('1')
368
self.assertEquals('r1', tree.get_revision_id())
369
tree = self.get_as_tree('2')
370
self.assertEquals('r2', tree.get_revision_id())
371
tree = self.get_as_tree('-2')
372
self.assertEquals('r1', tree.get_revision_id())
373
tree = self.get_as_tree('-1')
374
self.assertEquals('r2', tree.get_revision_id())
375
tree = self.get_as_tree('1.1.1')
376
self.assertEquals('alt_r2', tree.get_revision_id())
251
379
class TestRevisionSpec_revid(TestRevisionSpec):
253
381
def test_in_history(self):
254
382
# We should be able to access revisions that are directly
255
383
# in the history.
256
384
self.assertInHistoryIs(1, 'r1', 'revid:r1')
257
385
self.assertInHistoryIs(2, 'r2', 'revid:r2')
259
387
def test_missing(self):
260
self.assertInvalid('revid:r3')
388
self.assertInvalid('revid:r3', invalid_as_revision_id=False)
262
390
def test_merged(self):
263
391
"""We can reach revisions in the ancestry"""
499
674
# submit branch overrides parent branch
500
675
self.tree.branch.set_submit_branch('tree2')
501
676
self.assertInHistoryIs(None, 'alt_r2', 'submit:')
678
def test_as_revision_id(self):
679
self.tree.branch.set_submit_branch('tree2')
680
self.assertAsRevisionId('alt_r2', 'branch:tree2')
683
class TestRevisionSpec_mainline(TestRevisionSpec):
685
def test_as_revision_id(self):
686
self.assertAsRevisionId('r1', 'mainline:1')
687
self.assertAsRevisionId('r2', 'mainline:1.1.1')
688
self.assertAsRevisionId('r2', 'mainline:revid:alt_r2')
689
spec = RevisionSpec.from_string('mainline:revid:alt_r22')
690
e = self.assertRaises(errors.InvalidRevisionSpec,
691
spec.as_revision_id, self.tree.branch)
692
self.assertContainsRe(str(e),
693
"Requested revision: 'mainline:revid:alt_r22' does not exist in"
696
def test_in_history(self):
697
self.assertInHistoryIs(2, 'r2', 'mainline:revid:alt_r2')
700
class TestRevisionSpec_annotate(TestRevisionSpec):
703
super(TestRevisionSpec_annotate, self).setUp()
704
self.tree = self.make_branch_and_tree('annotate-tree')
705
self.build_tree_contents([('annotate-tree/file1', '1\n')])
706
self.tree.add('file1')
707
self.tree.commit('r1', rev_id='r1')
708
self.build_tree_contents([('annotate-tree/file1', '2\n1\n')])
709
self.tree.commit('r2', rev_id='r2')
710
self.build_tree_contents([('annotate-tree/file1', '2\n1\n3\n')])
712
def test_as_revision_id_r1(self):
713
self.assertAsRevisionId('r1', 'annotate:annotate-tree/file1:2')
715
def test_as_revision_id_r2(self):
716
self.assertAsRevisionId('r2', 'annotate:annotate-tree/file1:1')
718
def test_as_revision_id_uncommitted(self):
719
spec = RevisionSpec.from_string('annotate:annotate-tree/file1:3')
720
e = self.assertRaises(errors.InvalidRevisionSpec,
721
spec.as_revision_id, self.tree.branch)
722
self.assertContainsRe(str(e),
723
r"Requested revision: \'annotate:annotate-tree/file1:3\' does not"
724
" exist in branch: .*\nLine 3 has not been committed.")
726
def test_non_existent_line(self):
727
spec = RevisionSpec.from_string('annotate:annotate-tree/file1:4')
728
e = self.assertRaises(errors.InvalidRevisionSpec,
729
spec.as_revision_id, self.tree.branch)
730
self.assertContainsRe(str(e),
731
r"Requested revision: \'annotate:annotate-tree/file1:4\' does not"
732
" exist in branch: .*\nNo such line: 4")
734
def test_invalid_line(self):
735
spec = RevisionSpec.from_string('annotate:annotate-tree/file1:q')
736
e = self.assertRaises(errors.InvalidRevisionSpec,
737
spec.as_revision_id, self.tree.branch)
738
self.assertContainsRe(str(e),
739
r"Requested revision: \'annotate:annotate-tree/file1:q\' does not"
740
" exist in branch: .*\nNo such line: q")
742
def test_no_such_file(self):
743
spec = RevisionSpec.from_string('annotate:annotate-tree/file2:1')
744
e = self.assertRaises(errors.InvalidRevisionSpec,
745
spec.as_revision_id, self.tree.branch)
746
self.assertContainsRe(str(e),
747
r"Requested revision: \'annotate:annotate-tree/file2:1\' does not"
748
" exist in branch: .*\nFile 'file2' is not versioned")
750
def test_no_such_file_with_colon(self):
751
spec = RevisionSpec.from_string('annotate:annotate-tree/fi:le2:1')
752
e = self.assertRaises(errors.InvalidRevisionSpec,
753
spec.as_revision_id, self.tree.branch)
754
self.assertContainsRe(str(e),
755
r"Requested revision: \'annotate:annotate-tree/fi:le2:1\' does not"
756
" exist in branch: .*\nFile 'fi:le2' is not versioned")