/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2005-2012, 2016 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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
897 by Martin Pool
- merge john's revision-naming code
16
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
17
import datetime
1185.1.39 by Robert Collins
Robey Pointers before: namespace to clear up usage of dates in revision parameters
18
import time
1432 by Robert Collins
branch: namespace
19
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
20
from breezy import (
1948.4.1 by John Arbash Meinel
Update number parsers to raise InvalidRevisionSpec. Update revno: itself so it supports negative numbers
21
    errors,
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
22
    revision as _mod_revision,
1948.4.1 by John Arbash Meinel
Update number parsers to raise InvalidRevisionSpec. Update revno: itself so it supports negative numbers
23
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
24
from breezy.tests import TestCaseWithTransport
25
from breezy.revisionspec import (
5671.5.1 by Jelmer Vernooij
Allow lazily registering possible DWIM revspecs.
26
    RevisionInfo,
2220.2.3 by Martin Pool
Add tag: revision namespace.
27
    RevisionSpec,
5671.5.1 by Jelmer Vernooij
Allow lazily registering possible DWIM revspecs.
28
    RevisionSpec_dwim,
2220.2.3 by Martin Pool
Add tag: revision namespace.
29
    RevisionSpec_tag,
30
    )
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
31
32
33
def spec_in_history(spec, branch):
34
    """A simple helper to change a revision spec into a branch search"""
35
    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.
36
37
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
38
# Basic class, which just creates a really basic set of revisions
39
class TestRevisionSpec(TestCaseWithTransport):
40
41
    def setUp(self):
42
        super(TestRevisionSpec, self).setUp()
1988.4.5 by Robert Collins
revisions can now be specified using dotted-decimal revision numbers.
43
        # this sets up a revision graph:
44
        # r1: []             1
45
        # alt_r2: [r1]       1.1.1
46
        # r2: [r1, alt_r2]   2
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
47
48
        self.tree = self.make_branch_and_tree('tree')
49
        self.build_tree(['tree/a'])
3298.2.3 by John Arbash Meinel
Add tests that all RevisionSpecs implement in_branch(b, needs_revno)
50
        self.tree.lock_write()
3298.2.11 by Aaron Bentley
Update tests for null:, clea up slightly
51
        self.addCleanup(self.tree.unlock)
52
        self.tree.add(['a'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
53
        self.tree.commit('a', rev_id=b'r1')
3298.2.11 by Aaron Bentley
Update tests for null:, clea up slightly
54
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
55
        self.tree2 = self.tree.controldir.sprout('tree2').open_workingtree()
6855.4.1 by Jelmer Vernooij
Yet more bees.
56
        self.tree2.commit('alt', rev_id=b'alt_r2')
3298.2.11 by Aaron Bentley
Update tests for null:, clea up slightly
57
58
        self.tree.merge_from_branch(self.tree2.branch)
6855.4.1 by Jelmer Vernooij
Yet more bees.
59
        self.tree.commit('second', rev_id=b'r2')
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
60
61
    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)
62
        return spec_in_history(revision_spec, self.tree.branch)
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
63
64
    def assertInHistoryIs(self, exp_revno, exp_revision_id, revision_spec):
65
        rev_info = self.get_in_history(revision_spec)
66
        self.assertEqual(exp_revno, rev_info.revno,
2325.2.1 by Marien Zwart
Make the test suite failure reporting a bit more robust.
67
                         'Revision spec: %r returned wrong revno: %r != %r'
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
68
                         % (revision_spec, exp_revno, rev_info.revno))
69
        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.
70
                         'Revision spec: %r returned wrong revision id:'
71
                         ' %r != %r'
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
72
                         % (revision_spec, exp_revision_id, rev_info.rev_id))
73
3495.1.1 by John Arbash Meinel
Fix bug #239933, use the right exception for -c0
74
    def assertInvalid(self, revision_spec, extra='',
75
                      invalid_as_revision_id=True):
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
76
        try:
77
            self.get_in_history(revision_spec)
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
78
        except errors.InvalidRevisionSpec as e:
1948.4.23 by John Arbash Meinel
Change the handling of negative numbers, to be trapped at revno 1
79
            self.assertEqual(revision_spec, e.spec)
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
80
            self.assertEqual(extra, e.extra)
1948.4.5 by John Arbash Meinel
Fix tests for negative entries, and add tests for revno:
81
        else:
3495.1.1 by John Arbash Meinel
Fix bug #239933, use the right exception for -c0
82
            self.fail('Expected InvalidRevisionSpec to be raised for'
83
                      ' %r.in_history' % (revision_spec,))
84
        if invalid_as_revision_id:
85
            try:
86
                spec = RevisionSpec.from_string(revision_spec)
87
                spec.as_revision_id(self.tree.branch)
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
88
            except errors.InvalidRevisionSpec as e:
3495.1.1 by John Arbash Meinel
Fix bug #239933, use the right exception for -c0
89
                self.assertEqual(revision_spec, e.spec)
90
                self.assertEqual(extra, e.extra)
91
            else:
92
                self.fail('Expected InvalidRevisionSpec to be raised for'
3495.1.2 by John Arbash Meinel
tweak
93
                          ' %r.as_revision_id' % (revision_spec,))
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
94
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
95
    def assertAsRevisionId(self, revision_id, revision_spec):
96
        """Calling as_revision_id() should return the specified id."""
97
        spec = RevisionSpec.from_string(revision_spec)
98
        self.assertEqual(revision_id,
99
                         spec.as_revision_id(self.tree.branch))
100
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
101
    def get_as_tree(self, revision_spec, tree=None):
102
        if tree is None:
103
            tree = self.tree
104
        spec = RevisionSpec.from_string(revision_spec)
105
        return spec.as_tree(tree.branch)
106
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
107
3460.1.2 by John Arbash Meinel
Add a test for wants_revision_history
108
class RevisionSpecMatchOnTrap(RevisionSpec):
109
110
    def _match_on(self, branch, revs):
111
        self.last_call = (branch, revs)
112
        return super(RevisionSpecMatchOnTrap, self)._match_on(branch, revs)
113
114
115
class TestRevisionSpecBase(TestRevisionSpec):
116
117
    def test_wants_no_revision_history(self):
118
        # If wants_revision_history = False, then _match_on should get None for
119
        # the branch revision history
120
        spec = RevisionSpecMatchOnTrap('foo', _internal=True)
121
        spec.in_history(self.tree.branch)
122
123
        self.assertEqual((self.tree.branch, None), spec.last_call)
124
125
126
1948.4.19 by John Arbash Meinel
All old tests are covered elsewhere
127
class TestOddRevisionSpec(TestRevisionSpec):
128
    """Test things that aren't normally thought of as revision specs"""
129
130
    def test_none(self):
3298.2.11 by Aaron Bentley
Update tests for null:, clea up slightly
131
        self.assertInHistoryIs(None, None, None)
1948.4.19 by John Arbash Meinel
All old tests are covered elsewhere
132
133
    def test_object(self):
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
134
        self.assertRaises(TypeError, RevisionSpec.from_string, object())
1948.4.19 by John Arbash Meinel
All old tests are covered elsewhere
135
1948.4.32 by John Arbash Meinel
Clean up __repr__, as well as add tests that we can handle -r12:branch/
136
5671.5.1 by Jelmer Vernooij
Allow lazily registering possible DWIM revspecs.
137
class RevisionSpec_bork(RevisionSpec):
138
139
    prefix = 'irrelevant:'
140
141
    def _match_on(self, branch, revs):
142
        if self.spec == "bork":
6336.1.1 by Jelmer Vernooij
Deprecate ``RevisionSpec.wants_revision_history`` and remove any uses of it.
143
            return RevisionInfo.from_revision_id(branch, "r1")
5671.5.1 by Jelmer Vernooij
Allow lazily registering possible DWIM revspecs.
144
        else:
145
            raise errors.InvalidRevisionSpec(self.spec, branch)
146
147
4569.2.2 by Matthew Fuller
Add tests for DWIM revspecs.
148
class TestRevisionSpec_dwim(TestRevisionSpec):
149
150
    # Don't need to test revno's explicitly since TRS_revno already
151
    # covers that well for us
4569.2.11 by Matthew Fuller
Eliminate the TestRevnoFromString() test class by moving all its tests
152
    def test_dwim_spec_revno(self):
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
153
        self.assertInHistoryIs(2, b'r2', '2')
6973.5.2 by Jelmer Vernooij
Add more bees.
154
        self.assertAsRevisionId(b'alt_r2', '1.1.1')
4569.2.2 by Matthew Fuller
Add tests for DWIM revspecs.
155
156
    def test_dwim_spec_revid(self):
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
157
        self.assertInHistoryIs(2, b'r2', 'r2')
4569.2.2 by Matthew Fuller
Add tests for DWIM revspecs.
158
159
    def test_dwim_spec_tag(self):
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
160
        self.tree.branch.tags.set_tag('footag', b'r1')
6973.5.2 by Jelmer Vernooij
Add more bees.
161
        self.assertAsRevisionId(b'r1', 'footag')
4569.2.2 by Matthew Fuller
Add tests for DWIM revspecs.
162
        self.tree.branch.tags.delete_tag('footag')
163
        self.assertRaises(errors.InvalidRevisionSpec,
164
                          self.get_in_history, 'footag')
165
4569.2.16 by Matthew Fuller
Add a test that we slip past the revno-checking stage when we're
166
    def test_dwim_spec_tag_that_looks_like_revno(self):
167
        # Test that we slip past revno with things that look like revnos,
168
        # but aren't.  Tags are convenient for testing this since we can
169
        # make them look however we want.
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
170
        self.tree.branch.tags.set_tag('3', b'r2')
6973.5.2 by Jelmer Vernooij
Add more bees.
171
        self.assertAsRevisionId(b'r2', '3')
4569.2.16 by Matthew Fuller
Add a test that we slip past the revno-checking stage when we're
172
        self.build_tree(['tree/b'])
173
        self.tree.add(['b'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
174
        self.tree.commit('b', rev_id=b'r3')
6973.5.2 by Jelmer Vernooij
Add more bees.
175
        self.assertAsRevisionId(b'r3', '3')
4569.2.16 by Matthew Fuller
Add a test that we slip past the revno-checking stage when we're
176
4569.2.4 by Matthew Fuller
Add date: to the list of things DWIM auto-tries.
177
    def test_dwim_spec_date(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
178
        self.assertAsRevisionId(b'r1', 'today')
4569.2.4 by Matthew Fuller
Add date: to the list of things DWIM auto-tries.
179
4569.2.2 by Matthew Fuller
Add tests for DWIM revspecs.
180
    def test_dwim_spec_branch(self):
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
181
        self.assertInHistoryIs(None, b'alt_r2', 'tree2')
4569.2.2 by Matthew Fuller
Add tests for DWIM revspecs.
182
183
    def test_dwim_spec_nonexistent(self):
4569.2.8 by Matthew Fuller
Use a more precise nonexistent thing in test_dwim_spec_nonexistent().
184
        self.assertInvalid('somethingrandom', invalid_as_revision_id=False)
4569.2.11 by Matthew Fuller
Eliminate the TestRevnoFromString() test class by moving all its tests
185
        self.assertInvalid('-1.1', invalid_as_revision_id=False)
186
        self.assertInvalid('.1', invalid_as_revision_id=False)
187
        self.assertInvalid('1..1', invalid_as_revision_id=False)
188
        self.assertInvalid('1.2..1', invalid_as_revision_id=False)
189
        self.assertInvalid('1.', invalid_as_revision_id=False)
1988.4.5 by Robert Collins
revisions can now be specified using dotted-decimal revision numbers.
190
5671.5.1 by Jelmer Vernooij
Allow lazily registering possible DWIM revspecs.
191
    def test_append_dwim_revspec(self):
192
        original_dwim_revspecs = list(RevisionSpec_dwim._possible_revspecs)
193
        def reset_dwim_revspecs():
194
            RevisionSpec_dwim._possible_revspecs = original_dwim_revspecs
195
        self.addCleanup(reset_dwim_revspecs)
196
        RevisionSpec_dwim.append_possible_revspec(RevisionSpec_bork)
6973.5.2 by Jelmer Vernooij
Add more bees.
197
        self.assertAsRevisionId(b'r1', 'bork')
5671.5.1 by Jelmer Vernooij
Allow lazily registering possible DWIM revspecs.
198
199
    def test_append_lazy_dwim_revspec(self):
200
        original_dwim_revspecs = list(RevisionSpec_dwim._possible_revspecs)
201
        def reset_dwim_revspecs():
202
            RevisionSpec_dwim._possible_revspecs = original_dwim_revspecs
203
        self.addCleanup(reset_dwim_revspecs)
5671.5.3 by Jelmer Vernooij
Fix test.. not sure how I missed this.
204
        RevisionSpec_dwim.append_possible_lazy_revspec(
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
205
            "breezy.tests.test_revisionspec", "RevisionSpec_bork")
6973.5.2 by Jelmer Vernooij
Add more bees.
206
        self.assertAsRevisionId(b'r1', 'bork')
5671.5.1 by Jelmer Vernooij
Allow lazily registering possible DWIM revspecs.
207
1988.4.5 by Robert Collins
revisions can now be specified using dotted-decimal revision numbers.
208
1948.4.32 by John Arbash Meinel
Clean up __repr__, as well as add tests that we can handle -r12:branch/
209
class TestRevisionSpec_revno(TestRevisionSpec):
210
211
    def test_positive_int(self):
6973.14.6 by Jelmer Vernooij
Fix some more tests.
212
        self.assertInHistoryIs(0, b'null:', '0')
213
        self.assertInHistoryIs(1, b'r1', '1')
214
        self.assertInHistoryIs(2, b'r2', '2')
1948.4.23 by John Arbash Meinel
Change the handling of negative numbers, to be trapped at revno 1
215
        self.assertInvalid('3')
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
216
1988.4.5 by Robert Collins
revisions can now be specified using dotted-decimal revision numbers.
217
    def test_dotted_decimal(self):
6973.14.6 by Jelmer Vernooij
Fix some more tests.
218
        self.assertInHistoryIs(None, b'alt_r2', '1.1.1')
3878.3.1 by Marius Kruger
Test invalid dotted revion number directly in TestRevisionSpec_revno
219
        self.assertInvalid('1.1.123')
1988.4.5 by Robert Collins
revisions can now be specified using dotted-decimal revision numbers.
220
1948.4.32 by John Arbash Meinel
Clean up __repr__, as well as add tests that we can handle -r12:branch/
221
    def test_negative_int(self):
6973.14.6 by Jelmer Vernooij
Fix some more tests.
222
        self.assertInHistoryIs(2, b'r2', '-1')
223
        self.assertInHistoryIs(1, b'r1', '-2')
1948.4.3 by John Arbash Meinel
Create direct tests for RevisionSpec_int
224
6973.14.6 by Jelmer Vernooij
Fix some more tests.
225
        self.assertInHistoryIs(1, b'r1', '-3')
226
        self.assertInHistoryIs(1, b'r1', '-4')
227
        self.assertInHistoryIs(1, b'r1', '-100')
1948.4.5 by John Arbash Meinel
Fix tests for negative entries, and add tests for revno:
228
229
    def test_positive(self):
6973.14.6 by Jelmer Vernooij
Fix some more tests.
230
        self.assertInHistoryIs(0, b'null:', 'revno:0')
231
        self.assertInHistoryIs(1, b'r1', 'revno:1')
232
        self.assertInHistoryIs(2, b'r2', 'revno:2')
1948.4.5 by John Arbash Meinel
Fix tests for negative entries, and add tests for revno:
233
234
        self.assertInvalid('revno:3')
235
236
    def test_negative(self):
6973.14.6 by Jelmer Vernooij
Fix some more tests.
237
        self.assertInHistoryIs(2, b'r2', 'revno:-1')
238
        self.assertInHistoryIs(1, b'r1', 'revno:-2')
1948.4.5 by John Arbash Meinel
Fix tests for negative entries, and add tests for revno:
239
6973.14.6 by Jelmer Vernooij
Fix some more tests.
240
        self.assertInHistoryIs(1, b'r1', 'revno:-3')
241
        self.assertInHistoryIs(1, b'r1', 'revno:-4')
1948.4.6 by John Arbash Meinel
A small bugfix, and more tests for revno:
242
243
    def test_invalid_number(self):
244
        # Get the right exception text
245
        try:
246
            int('X')
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
247
        except ValueError as e:
1948.4.6 by John Arbash Meinel
A small bugfix, and more tests for revno:
248
            pass
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
249
        self.assertInvalid('revno:X', extra='\n' + str(e))
1948.4.6 by John Arbash Meinel
A small bugfix, and more tests for revno:
250
251
    def test_missing_number_and_branch(self):
252
        self.assertInvalid('revno::',
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
253
                           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
254
255
    def test_invalid_number_with_branch(self):
256
        try:
257
            int('X')
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
258
        except ValueError as e:
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
259
            pass
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
260
        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
261
1948.4.16 by John Arbash Meinel
Move the tests into the associated tester, remove redundant tests, some small PEP8 changes
262
    def test_non_exact_branch(self):
263
        # It seems better to require an exact path to the branch
264
        # 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)
265
        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
266
        self.assertRaises(errors.NotBranchError,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
267
                          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
268
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
269
    def test_with_branch(self):
270
        # Passing a URL overrides the supplied branch path
271
        revinfo = self.get_in_history('revno:2:tree2')
272
        self.assertNotEqual(self.tree.branch.base, revinfo.branch.base)
273
        self.assertEqual(self.tree2.branch.base, revinfo.branch.base)
274
        self.assertEqual(2, revinfo.revno)
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
275
        self.assertEqual(b'alt_r2', revinfo.rev_id)
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
276
1948.4.32 by John Arbash Meinel
Clean up __repr__, as well as add tests that we can handle -r12:branch/
277
    def test_int_with_branch(self):
278
        revinfo = self.get_in_history('2:tree2')
279
        self.assertNotEqual(self.tree.branch.base, revinfo.branch.base)
280
        self.assertEqual(self.tree2.branch.base, revinfo.branch.base)
281
        self.assertEqual(2, revinfo.revno)
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
282
        self.assertEqual(b'alt_r2', revinfo.rev_id)
1948.4.32 by John Arbash Meinel
Clean up __repr__, as well as add tests that we can handle -r12:branch/
283
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
284
    def test_with_url(self):
285
        url = self.get_url() + '/tree2'
286
        revinfo = self.get_in_history('revno:2:%s' % (url,))
287
        self.assertNotEqual(self.tree.branch.base, revinfo.branch.base)
288
        self.assertEqual(self.tree2.branch.base, revinfo.branch.base)
289
        self.assertEqual(2, revinfo.revno)
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
290
        self.assertEqual(b'alt_r2', revinfo.rev_id)
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
291
292
    def test_negative_with_url(self):
293
        url = self.get_url() + '/tree2'
294
        revinfo = self.get_in_history('revno:-1:%s' % (url,))
295
        self.assertNotEqual(self.tree.branch.base, revinfo.branch.base)
296
        self.assertEqual(self.tree2.branch.base, revinfo.branch.base)
297
        self.assertEqual(2, revinfo.revno)
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
298
        self.assertEqual(b'alt_r2', revinfo.rev_id)
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
299
1948.4.22 by John Arbash Meinel
Refactor common code from integer revno handlers
300
    def test_different_history_lengths(self):
301
        # Make sure we use the revisions and offsets in the supplied branch
302
        # not the ones in the original branch.
6855.4.1 by Jelmer Vernooij
Yet more bees.
303
        self.tree2.commit('three', rev_id=b'r3')
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
304
        self.assertInHistoryIs(3, b'r3', 'revno:3:tree2')
305
        self.assertInHistoryIs(3, b'r3', 'revno:-1:tree2')
1948.4.22 by John Arbash Meinel
Refactor common code from integer revno handlers
306
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
307
    def test_invalid_branch(self):
308
        self.assertRaises(errors.NotBranchError,
309
                          self.get_in_history, 'revno:-1:tree3')
310
311
    def test_invalid_revno_in_branch(self):
6855.4.1 by Jelmer Vernooij
Yet more bees.
312
        self.tree.commit('three', rev_id=b'r3')
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
313
        self.assertInvalid('revno:3:tree2')
1948.4.8 by John Arbash Meinel
Testing the revid: spec
314
1948.4.16 by John Arbash Meinel
Move the tests into the associated tester, remove redundant tests, some small PEP8 changes
315
    def test_revno_n_path(self):
316
        """Old revno:N:path tests"""
317
        wta = self.make_branch_and_tree('a')
318
        ba = wta.branch
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
319
6855.4.1 by Jelmer Vernooij
Yet more bees.
320
        wta.commit('Commit one', rev_id=b'a@r-0-1')
321
        wta.commit('Commit two', rev_id=b'a@r-0-2')
322
        wta.commit('Commit three', rev_id=b'a@r-0-3')
1948.4.16 by John Arbash Meinel
Move the tests into the associated tester, remove redundant tests, some small PEP8 changes
323
324
        wtb = self.make_branch_and_tree('b')
325
        bb = wtb.branch
326
6855.4.1 by Jelmer Vernooij
Yet more bees.
327
        wtb.commit('Commit one', rev_id=b'b@r-0-1')
328
        wtb.commit('Commit two', rev_id=b'b@r-0-2')
329
        wtb.commit('Commit three', rev_id=b'b@r-0-3')
1948.4.16 by John Arbash Meinel
Move the tests into the associated tester, remove redundant tests, some small PEP8 changes
330
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
331
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
332
        self.assertEqual((1, b'a@r-0-1'),
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
333
                         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
334
        # The argument of in_history should be ignored since it is
335
        # redundant with the path in the spec.
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
336
        self.assertEqual((1, b'a@r-0-1'),
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
337
                         spec_in_history('revno:1:a/', None))
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
338
        self.assertEqual((1, b'a@r-0-1'),
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
339
                         spec_in_history('revno:1:a/', bb))
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
340
        self.assertEqual((2, b'b@r-0-2'),
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
341
                         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
342
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
343
    def test_as_revision_id(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
344
        self.assertAsRevisionId(b'null:', '0')
345
        self.assertAsRevisionId(b'r1', '1')
346
        self.assertAsRevisionId(b'r2', '2')
347
        self.assertAsRevisionId(b'r1', '-2')
348
        self.assertAsRevisionId(b'r2', '-1')
349
        self.assertAsRevisionId(b'alt_r2', '1.1.1')
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
350
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
351
    def test_as_tree(self):
352
        tree = self.get_as_tree('0')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
353
        self.assertEqual(_mod_revision.NULL_REVISION, tree.get_revision_id())
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
354
        tree = self.get_as_tree('1')
6973.5.2 by Jelmer Vernooij
Add more bees.
355
        self.assertEqual(b'r1', tree.get_revision_id())
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
356
        tree = self.get_as_tree('2')
6973.5.2 by Jelmer Vernooij
Add more bees.
357
        self.assertEqual(b'r2', tree.get_revision_id())
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
358
        tree = self.get_as_tree('-2')
6973.5.2 by Jelmer Vernooij
Add more bees.
359
        self.assertEqual(b'r1', tree.get_revision_id())
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
360
        tree = self.get_as_tree('-1')
6973.5.2 by Jelmer Vernooij
Add more bees.
361
        self.assertEqual(b'r2', tree.get_revision_id())
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
362
        tree = self.get_as_tree('1.1.1')
6973.5.2 by Jelmer Vernooij
Add more bees.
363
        self.assertEqual(b'alt_r2', tree.get_revision_id())
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
364
1948.4.8 by John Arbash Meinel
Testing the revid: spec
365
366
class TestRevisionSpec_revid(TestRevisionSpec):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
367
1948.4.8 by John Arbash Meinel
Testing the revid: spec
368
    def test_in_history(self):
369
        # We should be able to access revisions that are directly
370
        # in the history.
6973.5.2 by Jelmer Vernooij
Add more bees.
371
        self.assertInHistoryIs(1, b'r1', 'revid:r1')
372
        self.assertInHistoryIs(2, b'r2', 'revid:r2')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
373
1948.4.8 by John Arbash Meinel
Testing the revid: spec
374
    def test_missing(self):
3495.1.1 by John Arbash Meinel
Fix bug #239933, use the right exception for -c0
375
        self.assertInvalid('revid:r3', invalid_as_revision_id=False)
1948.4.8 by John Arbash Meinel
Testing the revid: spec
376
377
    def test_merged(self):
378
        """We can reach revisions in the ancestry"""
6973.5.2 by Jelmer Vernooij
Add more bees.
379
        self.assertInHistoryIs(None, b'alt_r2', 'revid:alt_r2')
1948.4.8 by John Arbash Meinel
Testing the revid: spec
380
381
    def test_not_here(self):
6855.4.1 by Jelmer Vernooij
Yet more bees.
382
        self.tree2.commit('alt third', rev_id=b'alt_r3')
1948.4.8 by John Arbash Meinel
Testing the revid: spec
383
        # It exists in tree2, but not in tree
3495.1.1 by John Arbash Meinel
Fix bug #239933, use the right exception for -c0
384
        self.assertInvalid('revid:alt_r3', invalid_as_revision_id=False)
1948.4.8 by John Arbash Meinel
Testing the revid: spec
385
386
    def test_in_repository(self):
387
        """We can get any revision id in the repository"""
388
        # XXX: This may change in the future, but for now, it is true
6855.4.1 by Jelmer Vernooij
Yet more bees.
389
        self.tree2.commit('alt third', rev_id=b'alt_r3')
6973.5.2 by Jelmer Vernooij
Add more bees.
390
        self.tree.branch.fetch(self.tree2.branch, b'alt_r3')
391
        self.assertInHistoryIs(None, b'alt_r3', 'revid:alt_r3')
1948.4.9 by John Arbash Meinel
Cleanup and test last:
392
2325.2.2 by Marien Zwart
Make the revid RevisionSpec always return a str object, not a unicode object.
393
    def test_unicode(self):
394
        """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.
395
        revision_id = u'\N{SNOWMAN}'.encode('utf-8')
396
        self.tree.commit('unicode', rev_id=revision_id)
397
        self.assertInHistoryIs(3, revision_id, u'revid:\N{SNOWMAN}')
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
398
        self.assertInHistoryIs(3, revision_id, 'revid:' + revision_id.decode('utf-8'))
2325.2.2 by Marien Zwart
Make the revid RevisionSpec always return a str object, not a unicode object.
399
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
400
    def test_as_revision_id(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
401
        self.assertAsRevisionId(b'r1', 'revid:r1')
402
        self.assertAsRevisionId(b'r2', 'revid:r2')
403
        self.assertAsRevisionId(b'alt_r2', 'revid:alt_r2')
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
404
1948.4.9 by John Arbash Meinel
Cleanup and test last:
405
406
class TestRevisionSpec_last(TestRevisionSpec):
407
408
    def test_positive(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
409
        self.assertInHistoryIs(2, b'r2', 'last:1')
410
        self.assertInHistoryIs(1, b'r1', 'last:2')
411
        self.assertInHistoryIs(0, b'null:', 'last:3')
1948.4.9 by John Arbash Meinel
Cleanup and test last:
412
413
    def test_empty(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
414
        self.assertInHistoryIs(2, b'r2', 'last:')
1948.4.9 by John Arbash Meinel
Cleanup and test last:
415
416
    def test_negative(self):
417
        self.assertInvalid('last:-1',
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
418
                           extra='\nyou must supply a positive value')
1948.4.9 by John Arbash Meinel
Cleanup and test last:
419
420
    def test_missing(self):
421
        self.assertInvalid('last:4')
422
423
    def test_no_history(self):
424
        tree = self.make_branch_and_tree('tree3')
425
426
        self.assertRaises(errors.NoCommits,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
427
                          spec_in_history, 'last:', tree.branch)
1948.4.9 by John Arbash Meinel
Cleanup and test last:
428
429
    def test_not_a_number(self):
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
430
        last_e = None
1948.4.9 by John Arbash Meinel
Cleanup and test last:
431
        try:
432
            int('Y')
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
433
        except ValueError as e:
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
434
            last_e = e
435
        self.assertInvalid('last:Y', extra='\n' + str(last_e))
1948.4.10 by John Arbash Meinel
test the before: spec, currently asserting what seems to be buggy behavior
436
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
437
    def test_as_revision_id(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
438
        self.assertAsRevisionId(b'r2', 'last:1')
439
        self.assertAsRevisionId(b'r1', 'last:2')
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
440
1948.4.10 by John Arbash Meinel
test the before: spec, currently asserting what seems to be buggy behavior
441
442
class TestRevisionSpec_before(TestRevisionSpec):
443
444
    def test_int(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
445
        self.assertInHistoryIs(1, b'r1', 'before:2')
446
        self.assertInHistoryIs(1, b'r1', 'before:-1')
1948.4.10 by John Arbash Meinel
test the before: spec, currently asserting what seems to be buggy behavior
447
448
    def test_before_one(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
449
        self.assertInHistoryIs(0, b'null:', 'before:1')
1948.4.10 by John Arbash Meinel
test the before: spec, currently asserting what seems to be buggy behavior
450
451
    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
452
        self.assertInvalid('before:0',
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
453
                           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
454
455
    def test_revid(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
456
        self.assertInHistoryIs(1, b'r1', 'before:revid:r2')
1948.4.10 by John Arbash Meinel
test the before: spec, currently asserting what seems to be buggy behavior
457
458
    def test_last(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
459
        self.assertInHistoryIs(1, b'r1', 'before:last:1')
1948.4.10 by John Arbash Meinel
test the before: spec, currently asserting what seems to be buggy behavior
460
461
    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
462
        # This will grab the left-most ancestor for alternate histories
6973.5.2 by Jelmer Vernooij
Add more bees.
463
        self.assertInHistoryIs(1, b'r1', 'before:revid:alt_r2')
1948.4.11 by John Arbash Meinel
Update and test the tag: spec
464
1948.4.14 by John Arbash Meinel
Test the code path for a no-parent alternate history
465
    def test_alt_no_parents(self):
466
        new_tree = self.make_branch_and_tree('new_tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
467
        new_tree.commit('first', rev_id=b'new_r1')
5741.1.5 by Jelmer Vernooij
Have Branch.fetch() take a fetch_tags argument rather than a fetch_spec argument.
468
        self.tree.branch.fetch(new_tree.branch, 'new_r1')
6973.5.2 by Jelmer Vernooij
Add more bees.
469
        self.assertInHistoryIs(0, b'null:', 'before:revid:new_r1')
1948.4.14 by John Arbash Meinel
Test the code path for a no-parent alternate history
470
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
471
    def test_as_revision_id(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
472
        self.assertAsRevisionId(b'r1', 'before:revid:r2')
473
        self.assertAsRevisionId(b'r1', 'before:2')
474
        self.assertAsRevisionId(b'r1', 'before:1.1.1')
475
        self.assertAsRevisionId(b'r1', 'before:revid:alt_r2')
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
476
1948.4.11 by John Arbash Meinel
Update and test the tag: spec
477
478
class TestRevisionSpec_tag(TestRevisionSpec):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
479
2220.2.3 by Martin Pool
Add tag: revision namespace.
480
    def make_branch_and_tree(self, relpath):
481
        # override format as the default one may not support tags
3031.3.1 by Robert Collins
Remove the unneeded ExperimentalBranch class.
482
        return TestRevisionSpec.make_branch_and_tree(
483
            self, relpath, format='dirstate-tags')
2220.2.3 by Martin Pool
Add tag: revision namespace.
484
485
    def test_from_string_tag(self):
486
        spec = RevisionSpec.from_string('tag:bzr-0.14')
487
        self.assertIsInstance(spec, RevisionSpec_tag)
488
        self.assertEqual(spec.spec, 'bzr-0.14')
489
490
    def test_lookup_tag(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
491
        self.tree.branch.tags.set_tag('bzr-0.14', b'r1')
492
        self.assertInHistoryIs(1, b'r1', 'tag:bzr-0.14')
493
        self.tree.branch.tags.set_tag('null_rev', b'null:')
494
        self.assertInHistoryIs(0, b'null:', 'tag:null_rev')
2220.2.3 by Martin Pool
Add tag: revision namespace.
495
496
    def test_failed_lookup(self):
497
        # tags that don't exist give a specific message: arguably we should
498
        # just give InvalidRevisionSpec but I think this is more helpful
499
        self.assertRaises(errors.NoSuchTag,
500
            self.get_in_history,
501
            'tag:some-random-tag')
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
502
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
503
    def test_as_revision_id(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
504
        self.tree.branch.tags.set_tag('my-tag', b'r2')
505
        self.tree.branch.tags.set_tag('null_rev', b'null:')
506
        self.assertAsRevisionId(b'r2', 'tag:my-tag')
507
        self.assertAsRevisionId(b'null:', 'tag:null_rev')
508
        self.assertAsRevisionId(b'r1', 'before:tag:my-tag')
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
509
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
510
511
class TestRevisionSpec_date(TestRevisionSpec):
512
513
    def setUp(self):
514
        super(TestRevisionSpec, self).setUp()
515
516
        new_tree = self.make_branch_and_tree('new_tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
517
        new_tree.commit('Commit one', rev_id=b'new_r1',
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
518
                        timestamp=time.time() - 60*60*24)
6855.4.1 by Jelmer Vernooij
Yet more bees.
519
        new_tree.commit('Commit two', rev_id=b'new_r2')
520
        new_tree.commit('Commit three', rev_id=b'new_r3')
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
521
522
        self.tree = new_tree
523
524
    def test_tomorrow(self):
525
        self.assertInvalid('date:tomorrow')
526
527
    def test_today(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
528
        self.assertInHistoryIs(2, b'new_r2', 'date:today')
529
        self.assertInHistoryIs(1, b'new_r1', 'before:date:today')
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
530
531
    def test_yesterday(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
532
        self.assertInHistoryIs(1, b'new_r1', 'date:yesterday')
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
533
534
    def test_invalid(self):
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
535
        self.assertInvalid('date:foobar', extra='\ninvalid date')
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
536
        # You must have '-' between year/month/day
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
537
        self.assertInvalid('date:20040404', extra='\ninvalid date')
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
538
        # Need 2 digits for each date piece
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
539
        self.assertInvalid('date:2004-4-4', extra='\ninvalid date')
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
540
541
    def test_day(self):
542
        now = datetime.datetime.now()
6973.5.2 by Jelmer Vernooij
Add more bees.
543
        self.assertInHistoryIs(2, b'new_r2',
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
544
            'date:%04d-%02d-%02d' % (now.year, now.month, now.day))
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
545
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
546
    def test_as_revision_id(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
547
        self.assertAsRevisionId(b'new_r2', 'date:today')
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
548
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
549
550
class TestRevisionSpec_ancestor(TestRevisionSpec):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
551
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
552
    def test_non_exact_branch(self):
553
        # It seems better to require an exact path to the branch
554
        # Branch.open() rather than using Branch.open_containing()
555
        self.assertRaises(errors.NotBranchError,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
556
                          self.get_in_history, 'ancestor:tree2/a')
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
557
558
    def test_simple(self):
559
        # Common ancestor of trees is 'alt_r2'
6973.5.2 by Jelmer Vernooij
Add more bees.
560
        self.assertInHistoryIs(None, b'alt_r2', 'ancestor:tree2')
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
561
562
        # Going the other way, we get a valid revno
563
        tmp = self.tree
564
        self.tree = self.tree2
565
        self.tree2 = tmp
6973.5.2 by Jelmer Vernooij
Add more bees.
566
        self.assertInHistoryIs(2, b'alt_r2', 'ancestor:tree')
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
567
568
    def test_self(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
569
        self.assertInHistoryIs(2, b'r2', 'ancestor:tree')
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
570
571
    def test_unrelated(self):
572
        new_tree = self.make_branch_and_tree('new_tree')
573
6855.4.1 by Jelmer Vernooij
Yet more bees.
574
        new_tree.commit('Commit one', rev_id=b'new_r1')
575
        new_tree.commit('Commit two', rev_id=b'new_r2')
576
        new_tree.commit('Commit three', rev_id=b'new_r3')
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
577
578
        # With no common ancestor, we should raise another user error
579
        self.assertRaises(errors.NoCommonAncestor,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
580
                          self.get_in_history, 'ancestor:new_tree')
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
581
582
    def test_no_commits(self):
583
        new_tree = self.make_branch_and_tree('new_tree')
584
        self.assertRaises(errors.NoCommits,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
585
                          spec_in_history, 'ancestor:new_tree',
586
                                           self.tree.branch)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
587
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
588
        self.assertRaises(errors.NoCommits,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
589
                          spec_in_history, 'ancestor:tree',
590
                                           new_tree.branch)
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
591
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
592
    def test_as_revision_id(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
593
        self.assertAsRevisionId(b'alt_r2', 'ancestor:tree2')
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
594
3984.1.1 by Daniel Watkins
Added test.
595
    def test_default(self):
596
        # We don't have a parent to default to
597
        self.assertRaises(errors.NotBranchError, self.get_in_history,
598
                          'ancestor:')
599
600
        # Create a branch with a parent to default to
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
601
        tree3 = self.tree.controldir.sprout('tree3').open_workingtree()
6855.4.1 by Jelmer Vernooij
Yet more bees.
602
        tree3.commit('foo', rev_id=b'r3')
3984.1.1 by Daniel Watkins
Added test.
603
        self.tree = tree3
6973.5.2 by Jelmer Vernooij
Add more bees.
604
        self.assertInHistoryIs(2, b'r2', 'ancestor:')
3984.1.1 by Daniel Watkins
Added test.
605
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
606
607
class TestRevisionSpec_branch(TestRevisionSpec):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
608
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
609
    def test_non_exact_branch(self):
610
        # It seems better to require an exact path to the branch
611
        # Branch.open() rather than using Branch.open_containing()
612
        self.assertRaises(errors.NotBranchError,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
613
                          self.get_in_history, 'branch:tree2/a')
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
614
615
    def test_simple(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
616
        self.assertInHistoryIs(None, b'alt_r2', 'branch:tree2')
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
617
618
    def test_self(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
619
        self.assertInHistoryIs(2, b'r2', 'branch:tree')
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
620
621
    def test_unrelated(self):
622
        new_tree = self.make_branch_and_tree('new_tree')
623
6855.4.1 by Jelmer Vernooij
Yet more bees.
624
        new_tree.commit('Commit one', rev_id=b'new_r1')
625
        new_tree.commit('Commit two', rev_id=b'new_r2')
626
        new_tree.commit('Commit three', rev_id=b'new_r3')
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
627
6973.5.2 by Jelmer Vernooij
Add more bees.
628
        self.assertInHistoryIs(None, b'new_r3', 'branch:new_tree')
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
629
630
        # XXX: Right now, we use fetch() to make sure the remote revisions
631
        # have been pulled into the local branch. We may change that
632
        # behavior in the future.
6973.5.2 by Jelmer Vernooij
Add more bees.
633
        self.assertTrue(self.tree.branch.repository.has_revision(b'new_r3'))
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
634
635
    def test_no_commits(self):
636
        new_tree = self.make_branch_and_tree('new_tree')
637
        self.assertRaises(errors.NoCommits,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
638
                          self.get_in_history, 'branch:new_tree')
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
639
        self.assertRaises(errors.NoCommits,
640
                          self.get_as_tree, 'branch:new_tree')
1551.10.32 by Aaron Bentley
Add submit: specifier, for merge-directive-like diffs
641
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
642
    def test_as_revision_id(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
643
        self.assertAsRevisionId(b'alt_r2', 'branch:tree2')
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
644
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
645
    def test_as_tree(self):
646
        tree = self.get_as_tree('branch:tree', self.tree2)
6973.5.2 by Jelmer Vernooij
Add more bees.
647
        self.assertEqual(b'r2', tree.get_revision_id())
648
        self.assertFalse(self.tree2.branch.repository.has_revision(b'r2'))
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
649
1551.10.32 by Aaron Bentley
Add submit: specifier, for merge-directive-like diffs
650
651
class TestRevisionSpec_submit(TestRevisionSpec):
652
653
    def test_submit_branch(self):
654
        # Common ancestor of trees is 'alt_r2'
655
        self.assertRaises(errors.NoSubmitBranch, self.get_in_history,
656
                          'submit:')
657
        self.tree.branch.set_parent('../tree2')
6973.5.2 by Jelmer Vernooij
Add more bees.
658
        self.assertInHistoryIs(None, b'alt_r2', 'submit:')
1551.10.32 by Aaron Bentley
Add submit: specifier, for merge-directive-like diffs
659
        self.tree.branch.set_parent('bogus')
660
        self.assertRaises(errors.NotBranchError, self.get_in_history,
661
            'submit:')
662
        # submit branch overrides parent branch
663
        self.tree.branch.set_submit_branch('tree2')
6973.5.2 by Jelmer Vernooij
Add more bees.
664
        self.assertInHistoryIs(None, b'alt_r2', 'submit:')
3298.2.3 by John Arbash Meinel
Add tests that all RevisionSpecs implement in_branch(b, needs_revno)
665
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
666
    def test_as_revision_id(self):
667
        self.tree.branch.set_submit_branch('tree2')
6973.5.2 by Jelmer Vernooij
Add more bees.
668
        self.assertAsRevisionId(b'alt_r2', 'branch:tree2')
5365.6.4 by Aaron Bentley
Implement mainline revision spec.
669
670
671
class TestRevisionSpec_mainline(TestRevisionSpec):
672
673
    def test_as_revision_id(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
674
        self.assertAsRevisionId(b'r1', 'mainline:1')
675
        self.assertAsRevisionId(b'r2', 'mainline:1.1.1')
676
        self.assertAsRevisionId(b'r2', 'mainline:revid:alt_r2')
5365.6.4 by Aaron Bentley
Implement mainline revision spec.
677
        spec = RevisionSpec.from_string('mainline:revid:alt_r22')
678
        e = self.assertRaises(errors.InvalidRevisionSpec,
679
                              spec.as_revision_id, self.tree.branch)
680
        self.assertContainsRe(str(e),
681
            "Requested revision: 'mainline:revid:alt_r22' does not exist in"
682
            " branch: ")
683
684
    def test_in_history(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
685
        self.assertInHistoryIs(2, b'r2', 'mainline:revid:alt_r2')
5365.6.6 by Aaron Bentley
Implement 'annotate' revision-id.
686
687
688
class TestRevisionSpec_annotate(TestRevisionSpec):
689
690
    def setUp(self):
6552.1.4 by Vincent Ladeuil
Remaining tests matching setup(self) that can be rewritten with super().
691
        super(TestRevisionSpec_annotate, self).setUp()
5365.6.6 by Aaron Bentley
Implement 'annotate' revision-id.
692
        self.tree = self.make_branch_and_tree('annotate-tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
693
        self.build_tree_contents([('annotate-tree/file1', b'1\n')])
5365.6.6 by Aaron Bentley
Implement 'annotate' revision-id.
694
        self.tree.add('file1')
6855.4.1 by Jelmer Vernooij
Yet more bees.
695
        self.tree.commit('r1', rev_id=b'r1')
696
        self.build_tree_contents([('annotate-tree/file1', b'2\n1\n')])
697
        self.tree.commit('r2', rev_id=b'r2')
698
        self.build_tree_contents([('annotate-tree/file1', b'2\n1\n3\n')])
5365.6.6 by Aaron Bentley
Implement 'annotate' revision-id.
699
700
    def test_as_revision_id_r1(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
701
        self.assertAsRevisionId(b'r1', 'annotate:annotate-tree/file1:2')
5365.6.6 by Aaron Bentley
Implement 'annotate' revision-id.
702
703
    def test_as_revision_id_r2(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
704
        self.assertAsRevisionId(b'r2', 'annotate:annotate-tree/file1:1')
5365.6.6 by Aaron Bentley
Implement 'annotate' revision-id.
705
706
    def test_as_revision_id_uncommitted(self):
707
        spec = RevisionSpec.from_string('annotate:annotate-tree/file1:3')
708
        e = self.assertRaises(errors.InvalidRevisionSpec,
709
                              spec.as_revision_id, self.tree.branch)
710
        self.assertContainsRe(str(e),
711
            r"Requested revision: \'annotate:annotate-tree/file1:3\' does not"
712
            " exist in branch: .*\nLine 3 has not been committed.")
713
5365.6.10 by Andrew Bennetts
Fix typo in test method name.
714
    def test_non_existent_line(self):
5365.6.6 by Aaron Bentley
Implement 'annotate' revision-id.
715
        spec = RevisionSpec.from_string('annotate:annotate-tree/file1:4')
716
        e = self.assertRaises(errors.InvalidRevisionSpec,
717
                              spec.as_revision_id, self.tree.branch)
718
        self.assertContainsRe(str(e),
719
            r"Requested revision: \'annotate:annotate-tree/file1:4\' does not"
720
            " exist in branch: .*\nNo such line: 4")
721
722
    def test_invalid_line(self):
723
        spec = RevisionSpec.from_string('annotate:annotate-tree/file1:q')
724
        e = self.assertRaises(errors.InvalidRevisionSpec,
725
                              spec.as_revision_id, self.tree.branch)
726
        self.assertContainsRe(str(e),
727
            r"Requested revision: \'annotate:annotate-tree/file1:q\' does not"
728
            " exist in branch: .*\nNo such line: q")
729
730
    def test_no_such_file(self):
731
        spec = RevisionSpec.from_string('annotate:annotate-tree/file2:1')
732
        e = self.assertRaises(errors.InvalidRevisionSpec,
733
                              spec.as_revision_id, self.tree.branch)
734
        self.assertContainsRe(str(e),
735
            r"Requested revision: \'annotate:annotate-tree/file2:1\' does not"
736
            " exist in branch: .*\nFile 'file2' is not versioned")
737
738
    def test_no_such_file_with_colon(self):
739
        spec = RevisionSpec.from_string('annotate:annotate-tree/fi:le2:1')
740
        e = self.assertRaises(errors.InvalidRevisionSpec,
741
                              spec.as_revision_id, self.tree.branch)
742
        self.assertContainsRe(str(e),
743
            r"Requested revision: \'annotate:annotate-tree/fi:le2:1\' does not"
744
            " exist in branch: .*\nFile 'fi:le2' is not versioned")