1
# Copyright (C) 2004, 2005, 2006 Canonical Ltd
1
# Copyright (C) 2004, 2005, 2006, 2007 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
21
21
from bzrlib import (
24
from bzrlib.builtins import merge
25
27
from bzrlib.tests import TestCase, TestCaseWithTransport
26
from bzrlib.revisionspec import RevisionSpec, RevisionSpec_revno
28
from bzrlib.revisionspec import (
29
35
def spec_in_history(spec, branch):
60
66
def assertInHistoryIs(self, exp_revno, exp_revision_id, revision_spec):
61
67
rev_info = self.get_in_history(revision_spec)
62
68
self.assertEqual(exp_revno, rev_info.revno,
63
'Revision spec: %s returned wrong revno: %s != %s'
69
'Revision spec: %r returned wrong revno: %r != %r'
64
70
% (revision_spec, exp_revno, rev_info.revno))
65
71
self.assertEqual(exp_revision_id, rev_info.rev_id,
66
'Revision spec: %s returned wrong revision id:'
72
'Revision spec: %r returned wrong revision id:'
68
74
% (revision_spec, exp_revision_id, rev_info.rev_id))
70
76
def assertInvalid(self, revision_spec, extra=''):
110
116
class TestRevisionSpec_revno(TestRevisionSpec):
112
118
def test_positive_int(self):
113
self.assertInHistoryIs(0, None, '0')
119
self.assertInHistoryIs(0, 'null:', '0')
114
120
self.assertInHistoryIs(1, 'r1', '1')
115
121
self.assertInHistoryIs(2, 'r2', '2')
116
122
self.assertInvalid('3')
127
133
self.assertInHistoryIs(1, 'r1', '-100')
129
135
def test_positive(self):
130
self.assertInHistoryIs(0, None, 'revno:0')
136
self.assertInHistoryIs(0, 'null:', 'revno:0')
131
137
self.assertInHistoryIs(1, 'r1', 'revno:1')
132
138
self.assertInHistoryIs(2, 'r2', 'revno:2')
270
276
revision_id='alt_r3')
271
277
self.assertInHistoryIs(None, 'alt_r3', 'revid:alt_r3')
279
def test_unicode(self):
280
"""We correctly convert a unicode ui string to an encoded revid."""
281
revision_id = u'\N{SNOWMAN}'.encode('utf-8')
282
self.tree.commit('unicode', rev_id=revision_id)
283
self.assertInHistoryIs(3, revision_id, u'revid:\N{SNOWMAN}')
284
self.assertInHistoryIs(3, revision_id, 'revid:' + revision_id)
274
287
class TestRevisionSpec_last(TestRevisionSpec):
276
289
def test_positive(self):
277
290
self.assertInHistoryIs(2, 'r2', 'last:1')
278
291
self.assertInHistoryIs(1, 'r1', 'last:2')
279
self.assertInHistoryIs(0, None, 'last:3')
292
self.assertInHistoryIs(0, 'null:', 'last:3')
281
294
def test_empty(self):
282
295
self.assertInHistoryIs(2, 'r2', 'last:')
309
322
self.assertInHistoryIs(1, 'r1', 'before:-1')
311
324
def test_before_one(self):
312
self.assertInHistoryIs(0, None, 'before:1')
325
self.assertInHistoryIs(0, 'null:', 'before:1')
314
327
def test_before_none(self):
315
328
self.assertInvalid('before:0',
330
343
new_tree.commit('first', rev_id='new_r1')
331
344
self.tree.branch.repository.fetch(new_tree.branch.repository,
332
345
revision_id='new_r1')
333
self.assertInHistoryIs(0, None, 'before:revid:new_r1')
346
self.assertInHistoryIs(0, 'null:', 'before:revid:new_r1')
336
349
class TestRevisionSpec_tag(TestRevisionSpec):
338
def test_invalid(self):
339
self.assertInvalid('tag:foo', extra='\ntag: namespace registered,'
340
' but not implemented')
351
def make_branch_and_tree(self, relpath):
352
# override format as the default one may not support tags
353
return TestRevisionSpec.make_branch_and_tree(
354
self, relpath, format='dirstate-tags')
356
def test_from_string_tag(self):
357
spec = RevisionSpec.from_string('tag:bzr-0.14')
358
self.assertIsInstance(spec, RevisionSpec_tag)
359
self.assertEqual(spec.spec, 'bzr-0.14')
361
def test_lookup_tag(self):
362
self.tree.branch.tags.set_tag('bzr-0.14', 'r1')
363
self.assertInHistoryIs(1, 'r1', 'tag:bzr-0.14')
365
def test_failed_lookup(self):
366
# tags that don't exist give a specific message: arguably we should
367
# just give InvalidRevisionSpec but I think this is more helpful
368
self.assertRaises(errors.NoSuchTag,
370
'tag:some-random-tag')
343
373
class TestRevisionSpec_date(TestRevisionSpec):
451
481
new_tree = self.make_branch_and_tree('new_tree')
452
482
self.assertRaises(errors.NoCommits,
453
483
self.get_in_history, 'branch:new_tree')
486
class TestRevisionSpec_submit(TestRevisionSpec):
488
def test_submit_branch(self):
489
# Common ancestor of trees is 'alt_r2'
490
self.assertRaises(errors.NoSubmitBranch, self.get_in_history,
492
self.tree.branch.set_parent('../tree2')
493
self.assertInHistoryIs(None, 'alt_r2', 'submit:')
494
self.tree.branch.set_parent('bogus')
495
self.assertRaises(errors.NotBranchError, self.get_in_history,
497
# submit branch overrides parent branch
498
self.tree.branch.set_submit_branch('tree2')
499
self.assertInHistoryIs(None, 'alt_r2', 'submit:')