/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":
7045.4.22 by Jelmer Vernooij
Fix another ~100 tests.
143
            return RevisionInfo.from_revision_id(branch, b"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:
7045.4.22 by Jelmer Vernooij
Fix another ~100 tests.
248
            self.assertInvalid('revno:X', extra='\n' + str(e))
249
        else:
250
            self.fail()
1948.4.6 by John Arbash Meinel
A small bugfix, and more tests for revno:
251
252
    def test_missing_number_and_branch(self):
253
        self.assertInvalid('revno::',
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
254
                           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
255
256
    def test_invalid_number_with_branch(self):
257
        try:
258
            int('X')
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
259
        except ValueError as e:
7045.4.22 by Jelmer Vernooij
Fix another ~100 tests.
260
            self.assertInvalid('revno:X:tree2', extra='\n' + str(e))
261
        else:
262
            self.fail()
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
263
1948.4.16 by John Arbash Meinel
Move the tests into the associated tester, remove redundant tests, some small PEP8 changes
264
    def test_non_exact_branch(self):
265
        # It seems better to require an exact path to the branch
266
        # 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)
267
        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
268
        self.assertRaises(errors.NotBranchError,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
269
                          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
270
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
271
    def test_with_branch(self):
272
        # Passing a URL overrides the supplied branch path
273
        revinfo = self.get_in_history('revno:2:tree2')
274
        self.assertNotEqual(self.tree.branch.base, revinfo.branch.base)
275
        self.assertEqual(self.tree2.branch.base, revinfo.branch.base)
276
        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.
277
        self.assertEqual(b'alt_r2', revinfo.rev_id)
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
278
1948.4.32 by John Arbash Meinel
Clean up __repr__, as well as add tests that we can handle -r12:branch/
279
    def test_int_with_branch(self):
280
        revinfo = self.get_in_history('2:tree2')
281
        self.assertNotEqual(self.tree.branch.base, revinfo.branch.base)
282
        self.assertEqual(self.tree2.branch.base, revinfo.branch.base)
283
        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.
284
        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/
285
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
286
    def test_with_url(self):
287
        url = self.get_url() + '/tree2'
288
        revinfo = self.get_in_history('revno:2:%s' % (url,))
289
        self.assertNotEqual(self.tree.branch.base, revinfo.branch.base)
290
        self.assertEqual(self.tree2.branch.base, revinfo.branch.base)
291
        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.
292
        self.assertEqual(b'alt_r2', revinfo.rev_id)
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
293
294
    def test_negative_with_url(self):
295
        url = self.get_url() + '/tree2'
296
        revinfo = self.get_in_history('revno:-1:%s' % (url,))
297
        self.assertNotEqual(self.tree.branch.base, revinfo.branch.base)
298
        self.assertEqual(self.tree2.branch.base, revinfo.branch.base)
299
        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.
300
        self.assertEqual(b'alt_r2', revinfo.rev_id)
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
301
1948.4.22 by John Arbash Meinel
Refactor common code from integer revno handlers
302
    def test_different_history_lengths(self):
303
        # Make sure we use the revisions and offsets in the supplied branch
304
        # not the ones in the original branch.
6855.4.1 by Jelmer Vernooij
Yet more bees.
305
        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.
306
        self.assertInHistoryIs(3, b'r3', 'revno:3:tree2')
307
        self.assertInHistoryIs(3, b'r3', 'revno:-1:tree2')
1948.4.22 by John Arbash Meinel
Refactor common code from integer revno handlers
308
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
309
    def test_invalid_branch(self):
310
        self.assertRaises(errors.NotBranchError,
311
                          self.get_in_history, 'revno:-1:tree3')
312
313
    def test_invalid_revno_in_branch(self):
6855.4.1 by Jelmer Vernooij
Yet more bees.
314
        self.tree.commit('three', rev_id=b'r3')
1948.4.7 by John Arbash Meinel
More revno: tests, now testing the branch/url parameter
315
        self.assertInvalid('revno:3:tree2')
1948.4.8 by John Arbash Meinel
Testing the revid: spec
316
1948.4.16 by John Arbash Meinel
Move the tests into the associated tester, remove redundant tests, some small PEP8 changes
317
    def test_revno_n_path(self):
318
        """Old revno:N:path tests"""
319
        wta = self.make_branch_and_tree('a')
320
        ba = wta.branch
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
321
6855.4.1 by Jelmer Vernooij
Yet more bees.
322
        wta.commit('Commit one', rev_id=b'a@r-0-1')
323
        wta.commit('Commit two', rev_id=b'a@r-0-2')
324
        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
325
326
        wtb = self.make_branch_and_tree('b')
327
        bb = wtb.branch
328
6855.4.1 by Jelmer Vernooij
Yet more bees.
329
        wtb.commit('Commit one', rev_id=b'b@r-0-1')
330
        wtb.commit('Commit two', rev_id=b'b@r-0-2')
331
        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
332
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
333
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
334
        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)
335
                         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
336
        # The argument of in_history should be ignored since it is
337
        # 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.
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/', None))
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
340
        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)
341
                         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.
342
        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)
343
                         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
344
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
345
    def test_as_revision_id(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
346
        self.assertAsRevisionId(b'null:', '0')
347
        self.assertAsRevisionId(b'r1', '1')
348
        self.assertAsRevisionId(b'r2', '2')
349
        self.assertAsRevisionId(b'r1', '-2')
350
        self.assertAsRevisionId(b'r2', '-1')
351
        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)
352
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
353
    def test_as_tree(self):
354
        tree = self.get_as_tree('0')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
355
        self.assertEqual(_mod_revision.NULL_REVISION, tree.get_revision_id())
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
356
        tree = self.get_as_tree('1')
6973.5.2 by Jelmer Vernooij
Add more bees.
357
        self.assertEqual(b'r1', 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'r2', tree.get_revision_id())
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
360
        tree = self.get_as_tree('-2')
6973.5.2 by Jelmer Vernooij
Add more bees.
361
        self.assertEqual(b'r1', tree.get_revision_id())
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
362
        tree = self.get_as_tree('-1')
6973.5.2 by Jelmer Vernooij
Add more bees.
363
        self.assertEqual(b'r2', tree.get_revision_id())
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
364
        tree = self.get_as_tree('1.1.1')
6973.5.2 by Jelmer Vernooij
Add more bees.
365
        self.assertEqual(b'alt_r2', tree.get_revision_id())
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
366
1948.4.8 by John Arbash Meinel
Testing the revid: spec
367
368
class TestRevisionSpec_revid(TestRevisionSpec):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
369
1948.4.8 by John Arbash Meinel
Testing the revid: spec
370
    def test_in_history(self):
371
        # We should be able to access revisions that are directly
372
        # in the history.
6973.5.2 by Jelmer Vernooij
Add more bees.
373
        self.assertInHistoryIs(1, b'r1', 'revid:r1')
374
        self.assertInHistoryIs(2, b'r2', 'revid:r2')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
375
1948.4.8 by John Arbash Meinel
Testing the revid: spec
376
    def test_missing(self):
3495.1.1 by John Arbash Meinel
Fix bug #239933, use the right exception for -c0
377
        self.assertInvalid('revid:r3', invalid_as_revision_id=False)
1948.4.8 by John Arbash Meinel
Testing the revid: spec
378
379
    def test_merged(self):
380
        """We can reach revisions in the ancestry"""
6973.5.2 by Jelmer Vernooij
Add more bees.
381
        self.assertInHistoryIs(None, b'alt_r2', 'revid:alt_r2')
1948.4.8 by John Arbash Meinel
Testing the revid: spec
382
383
    def test_not_here(self):
6855.4.1 by Jelmer Vernooij
Yet more bees.
384
        self.tree2.commit('alt third', rev_id=b'alt_r3')
1948.4.8 by John Arbash Meinel
Testing the revid: spec
385
        # It exists in tree2, but not in tree
3495.1.1 by John Arbash Meinel
Fix bug #239933, use the right exception for -c0
386
        self.assertInvalid('revid:alt_r3', invalid_as_revision_id=False)
1948.4.8 by John Arbash Meinel
Testing the revid: spec
387
388
    def test_in_repository(self):
389
        """We can get any revision id in the repository"""
390
        # XXX: This may change in the future, but for now, it is true
6855.4.1 by Jelmer Vernooij
Yet more bees.
391
        self.tree2.commit('alt third', rev_id=b'alt_r3')
6973.5.2 by Jelmer Vernooij
Add more bees.
392
        self.tree.branch.fetch(self.tree2.branch, b'alt_r3')
393
        self.assertInHistoryIs(None, b'alt_r3', 'revid:alt_r3')
1948.4.9 by John Arbash Meinel
Cleanup and test last:
394
2325.2.2 by Marien Zwart
Make the revid RevisionSpec always return a str object, not a unicode object.
395
    def test_unicode(self):
396
        """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.
397
        revision_id = u'\N{SNOWMAN}'.encode('utf-8')
398
        self.tree.commit('unicode', rev_id=revision_id)
399
        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.
400
        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.
401
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
402
    def test_as_revision_id(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
403
        self.assertAsRevisionId(b'r1', 'revid:r1')
404
        self.assertAsRevisionId(b'r2', 'revid:r2')
405
        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)
406
1948.4.9 by John Arbash Meinel
Cleanup and test last:
407
408
class TestRevisionSpec_last(TestRevisionSpec):
409
410
    def test_positive(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
411
        self.assertInHistoryIs(2, b'r2', 'last:1')
412
        self.assertInHistoryIs(1, b'r1', 'last:2')
413
        self.assertInHistoryIs(0, b'null:', 'last:3')
1948.4.9 by John Arbash Meinel
Cleanup and test last:
414
415
    def test_empty(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
416
        self.assertInHistoryIs(2, b'r2', 'last:')
1948.4.9 by John Arbash Meinel
Cleanup and test last:
417
418
    def test_negative(self):
419
        self.assertInvalid('last:-1',
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
420
                           extra='\nyou must supply a positive value')
1948.4.9 by John Arbash Meinel
Cleanup and test last:
421
422
    def test_missing(self):
423
        self.assertInvalid('last:4')
424
425
    def test_no_history(self):
426
        tree = self.make_branch_and_tree('tree3')
427
428
        self.assertRaises(errors.NoCommits,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
429
                          spec_in_history, 'last:', tree.branch)
1948.4.9 by John Arbash Meinel
Cleanup and test last:
430
431
    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.
432
        last_e = None
1948.4.9 by John Arbash Meinel
Cleanup and test last:
433
        try:
434
            int('Y')
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
435
        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.
436
            last_e = e
437
        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
438
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
439
    def test_as_revision_id(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
440
        self.assertAsRevisionId(b'r2', 'last:1')
441
        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)
442
1948.4.10 by John Arbash Meinel
test the before: spec, currently asserting what seems to be buggy behavior
443
444
class TestRevisionSpec_before(TestRevisionSpec):
445
446
    def test_int(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
447
        self.assertInHistoryIs(1, b'r1', 'before:2')
448
        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
449
450
    def test_before_one(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
451
        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
452
453
    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
454
        self.assertInvalid('before:0',
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
455
                           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
456
457
    def test_revid(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
458
        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
459
460
    def test_last(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
461
        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
462
463
    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
464
        # This will grab the left-most ancestor for alternate histories
6973.5.2 by Jelmer Vernooij
Add more bees.
465
        self.assertInHistoryIs(1, b'r1', 'before:revid:alt_r2')
1948.4.11 by John Arbash Meinel
Update and test the tag: spec
466
1948.4.14 by John Arbash Meinel
Test the code path for a no-parent alternate history
467
    def test_alt_no_parents(self):
468
        new_tree = self.make_branch_and_tree('new_tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
469
        new_tree.commit('first', rev_id=b'new_r1')
7045.4.22 by Jelmer Vernooij
Fix another ~100 tests.
470
        self.tree.branch.fetch(new_tree.branch, b'new_r1')
6973.5.2 by Jelmer Vernooij
Add more bees.
471
        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
472
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
473
    def test_as_revision_id(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
474
        self.assertAsRevisionId(b'r1', 'before:revid:r2')
475
        self.assertAsRevisionId(b'r1', 'before:2')
476
        self.assertAsRevisionId(b'r1', 'before:1.1.1')
477
        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)
478
1948.4.11 by John Arbash Meinel
Update and test the tag: spec
479
480
class TestRevisionSpec_tag(TestRevisionSpec):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
481
2220.2.3 by Martin Pool
Add tag: revision namespace.
482
    def make_branch_and_tree(self, relpath):
483
        # override format as the default one may not support tags
3031.3.1 by Robert Collins
Remove the unneeded ExperimentalBranch class.
484
        return TestRevisionSpec.make_branch_and_tree(
485
            self, relpath, format='dirstate-tags')
2220.2.3 by Martin Pool
Add tag: revision namespace.
486
487
    def test_from_string_tag(self):
488
        spec = RevisionSpec.from_string('tag:bzr-0.14')
489
        self.assertIsInstance(spec, RevisionSpec_tag)
490
        self.assertEqual(spec.spec, 'bzr-0.14')
491
492
    def test_lookup_tag(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
493
        self.tree.branch.tags.set_tag('bzr-0.14', b'r1')
494
        self.assertInHistoryIs(1, b'r1', 'tag:bzr-0.14')
495
        self.tree.branch.tags.set_tag('null_rev', b'null:')
496
        self.assertInHistoryIs(0, b'null:', 'tag:null_rev')
2220.2.3 by Martin Pool
Add tag: revision namespace.
497
498
    def test_failed_lookup(self):
499
        # tags that don't exist give a specific message: arguably we should
500
        # just give InvalidRevisionSpec but I think this is more helpful
501
        self.assertRaises(errors.NoSuchTag,
502
            self.get_in_history,
503
            'tag:some-random-tag')
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
504
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
505
    def test_as_revision_id(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
506
        self.tree.branch.tags.set_tag('my-tag', b'r2')
507
        self.tree.branch.tags.set_tag('null_rev', b'null:')
508
        self.assertAsRevisionId(b'r2', 'tag:my-tag')
509
        self.assertAsRevisionId(b'null:', 'tag:null_rev')
510
        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)
511
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
512
513
class TestRevisionSpec_date(TestRevisionSpec):
514
515
    def setUp(self):
516
        super(TestRevisionSpec, self).setUp()
517
518
        new_tree = self.make_branch_and_tree('new_tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
519
        new_tree.commit('Commit one', rev_id=b'new_r1',
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
520
                        timestamp=time.time() - 60*60*24)
6855.4.1 by Jelmer Vernooij
Yet more bees.
521
        new_tree.commit('Commit two', rev_id=b'new_r2')
522
        new_tree.commit('Commit three', rev_id=b'new_r3')
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
523
524
        self.tree = new_tree
525
526
    def test_tomorrow(self):
527
        self.assertInvalid('date:tomorrow')
528
529
    def test_today(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
530
        self.assertInHistoryIs(2, b'new_r2', 'date:today')
531
        self.assertInHistoryIs(1, b'new_r1', 'before:date:today')
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
532
533
    def test_yesterday(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
534
        self.assertInHistoryIs(1, b'new_r1', 'date:yesterday')
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
535
536
    def test_invalid(self):
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
537
        self.assertInvalid('date:foobar', extra='\ninvalid date')
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
538
        # You must have '-' between year/month/day
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
539
        self.assertInvalid('date:20040404', extra='\ninvalid date')
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
540
        # Need 2 digits for each date piece
1948.4.15 by John Arbash Meinel
Change the InvalidRevisionSpec formatting to be more readable
541
        self.assertInvalid('date:2004-4-4', extra='\ninvalid date')
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
542
543
    def test_day(self):
544
        now = datetime.datetime.now()
6973.5.2 by Jelmer Vernooij
Add more bees.
545
        self.assertInHistoryIs(2, b'new_r2',
1948.4.12 by John Arbash Meinel
Some tests for the date: spec
546
            'date:%04d-%02d-%02d' % (now.year, now.month, now.day))
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
547
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
548
    def test_as_revision_id(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
549
        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)
550
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
551
552
class TestRevisionSpec_ancestor(TestRevisionSpec):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
553
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
554
    def test_non_exact_branch(self):
555
        # It seems better to require an exact path to the branch
556
        # Branch.open() rather than using Branch.open_containing()
557
        self.assertRaises(errors.NotBranchError,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
558
                          self.get_in_history, 'ancestor:tree2/a')
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
559
560
    def test_simple(self):
561
        # Common ancestor of trees is 'alt_r2'
6973.5.2 by Jelmer Vernooij
Add more bees.
562
        self.assertInHistoryIs(None, b'alt_r2', 'ancestor:tree2')
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
563
564
        # Going the other way, we get a valid revno
565
        tmp = self.tree
566
        self.tree = self.tree2
567
        self.tree2 = tmp
6973.5.2 by Jelmer Vernooij
Add more bees.
568
        self.assertInHistoryIs(2, b'alt_r2', 'ancestor:tree')
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
569
570
    def test_self(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
571
        self.assertInHistoryIs(2, b'r2', 'ancestor:tree')
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
572
573
    def test_unrelated(self):
574
        new_tree = self.make_branch_and_tree('new_tree')
575
6855.4.1 by Jelmer Vernooij
Yet more bees.
576
        new_tree.commit('Commit one', rev_id=b'new_r1')
577
        new_tree.commit('Commit two', rev_id=b'new_r2')
578
        new_tree.commit('Commit three', rev_id=b'new_r3')
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
579
580
        # With no common ancestor, we should raise another user error
581
        self.assertRaises(errors.NoCommonAncestor,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
582
                          self.get_in_history, 'ancestor:new_tree')
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
583
584
    def test_no_commits(self):
585
        new_tree = self.make_branch_and_tree('new_tree')
586
        self.assertRaises(errors.NoCommits,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
587
                          spec_in_history, 'ancestor:new_tree',
588
                                           self.tree.branch)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
589
1948.4.17 by John Arbash Meinel
Update tests for ancestor: spec
590
        self.assertRaises(errors.NoCommits,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
591
                          spec_in_history, 'ancestor:tree',
592
                                           new_tree.branch)
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
593
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
594
    def test_as_revision_id(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
595
        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)
596
3984.1.1 by Daniel Watkins
Added test.
597
    def test_default(self):
598
        # We don't have a parent to default to
599
        self.assertRaises(errors.NotBranchError, self.get_in_history,
600
                          'ancestor:')
601
602
        # Create a branch with a parent to default to
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
603
        tree3 = self.tree.controldir.sprout('tree3').open_workingtree()
6855.4.1 by Jelmer Vernooij
Yet more bees.
604
        tree3.commit('foo', rev_id=b'r3')
3984.1.1 by Daniel Watkins
Added test.
605
        self.tree = tree3
6973.5.2 by Jelmer Vernooij
Add more bees.
606
        self.assertInHistoryIs(2, b'r2', 'ancestor:')
3984.1.1 by Daniel Watkins
Added test.
607
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
608
609
class TestRevisionSpec_branch(TestRevisionSpec):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
610
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
611
    def test_non_exact_branch(self):
612
        # It seems better to require an exact path to the branch
613
        # Branch.open() rather than using Branch.open_containing()
614
        self.assertRaises(errors.NotBranchError,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
615
                          self.get_in_history, 'branch:tree2/a')
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
616
617
    def test_simple(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
618
        self.assertInHistoryIs(None, b'alt_r2', 'branch:tree2')
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
619
620
    def test_self(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
621
        self.assertInHistoryIs(2, b'r2', 'branch:tree')
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
622
623
    def test_unrelated(self):
624
        new_tree = self.make_branch_and_tree('new_tree')
625
6855.4.1 by Jelmer Vernooij
Yet more bees.
626
        new_tree.commit('Commit one', rev_id=b'new_r1')
627
        new_tree.commit('Commit two', rev_id=b'new_r2')
628
        new_tree.commit('Commit three', rev_id=b'new_r3')
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
629
6973.5.2 by Jelmer Vernooij
Add more bees.
630
        self.assertInHistoryIs(None, b'new_r3', 'branch:new_tree')
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
631
632
        # XXX: Right now, we use fetch() to make sure the remote revisions
633
        # have been pulled into the local branch. We may change that
634
        # behavior in the future.
6973.5.2 by Jelmer Vernooij
Add more bees.
635
        self.assertTrue(self.tree.branch.repository.has_revision(b'new_r3'))
1948.4.18 by John Arbash Meinel
Update branch: spec and tests
636
637
    def test_no_commits(self):
638
        new_tree = self.make_branch_and_tree('new_tree')
639
        self.assertRaises(errors.NoCommits,
1948.4.33 by John Arbash Meinel
Switch from get_revision_spec() to RevisionSpec.from_string() (as advised by Martin)
640
                          self.get_in_history, 'branch:new_tree')
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
641
        self.assertRaises(errors.NoCommits,
642
                          self.get_as_tree, 'branch:new_tree')
1551.10.32 by Aaron Bentley
Add submit: specifier, for merge-directive-like diffs
643
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
644
    def test_as_revision_id(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
645
        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)
646
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
647
    def test_as_tree(self):
648
        tree = self.get_as_tree('branch:tree', self.tree2)
6973.5.2 by Jelmer Vernooij
Add more bees.
649
        self.assertEqual(b'r2', tree.get_revision_id())
650
        self.assertFalse(self.tree2.branch.repository.has_revision(b'r2'))
3655.3.1 by Lukáš Lalinský
Fix `bzr st -rbranch:PATH_TO_BRANCH`
651
1551.10.32 by Aaron Bentley
Add submit: specifier, for merge-directive-like diffs
652
653
class TestRevisionSpec_submit(TestRevisionSpec):
654
655
    def test_submit_branch(self):
656
        # Common ancestor of trees is 'alt_r2'
657
        self.assertRaises(errors.NoSubmitBranch, self.get_in_history,
658
                          'submit:')
659
        self.tree.branch.set_parent('../tree2')
6973.5.2 by Jelmer Vernooij
Add more bees.
660
        self.assertInHistoryIs(None, b'alt_r2', 'submit:')
1551.10.32 by Aaron Bentley
Add submit: specifier, for merge-directive-like diffs
661
        self.tree.branch.set_parent('bogus')
662
        self.assertRaises(errors.NotBranchError, self.get_in_history,
663
            'submit:')
664
        # submit branch overrides parent branch
665
        self.tree.branch.set_submit_branch('tree2')
6973.5.2 by Jelmer Vernooij
Add more bees.
666
        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)
667
3298.2.4 by John Arbash Meinel
Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
668
    def test_as_revision_id(self):
669
        self.tree.branch.set_submit_branch('tree2')
6973.5.2 by Jelmer Vernooij
Add more bees.
670
        self.assertAsRevisionId(b'alt_r2', 'branch:tree2')
5365.6.4 by Aaron Bentley
Implement mainline revision spec.
671
672
673
class TestRevisionSpec_mainline(TestRevisionSpec):
674
675
    def test_as_revision_id(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
676
        self.assertAsRevisionId(b'r1', 'mainline:1')
677
        self.assertAsRevisionId(b'r2', 'mainline:1.1.1')
678
        self.assertAsRevisionId(b'r2', 'mainline:revid:alt_r2')
5365.6.4 by Aaron Bentley
Implement mainline revision spec.
679
        spec = RevisionSpec.from_string('mainline:revid:alt_r22')
680
        e = self.assertRaises(errors.InvalidRevisionSpec,
681
                              spec.as_revision_id, self.tree.branch)
682
        self.assertContainsRe(str(e),
683
            "Requested revision: 'mainline:revid:alt_r22' does not exist in"
684
            " branch: ")
685
686
    def test_in_history(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
687
        self.assertInHistoryIs(2, b'r2', 'mainline:revid:alt_r2')
5365.6.6 by Aaron Bentley
Implement 'annotate' revision-id.
688
689
690
class TestRevisionSpec_annotate(TestRevisionSpec):
691
692
    def setUp(self):
6552.1.4 by Vincent Ladeuil
Remaining tests matching setup(self) that can be rewritten with super().
693
        super(TestRevisionSpec_annotate, self).setUp()
5365.6.6 by Aaron Bentley
Implement 'annotate' revision-id.
694
        self.tree = self.make_branch_and_tree('annotate-tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
695
        self.build_tree_contents([('annotate-tree/file1', b'1\n')])
5365.6.6 by Aaron Bentley
Implement 'annotate' revision-id.
696
        self.tree.add('file1')
6855.4.1 by Jelmer Vernooij
Yet more bees.
697
        self.tree.commit('r1', rev_id=b'r1')
698
        self.build_tree_contents([('annotate-tree/file1', b'2\n1\n')])
699
        self.tree.commit('r2', rev_id=b'r2')
700
        self.build_tree_contents([('annotate-tree/file1', b'2\n1\n3\n')])
5365.6.6 by Aaron Bentley
Implement 'annotate' revision-id.
701
702
    def test_as_revision_id_r1(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
703
        self.assertAsRevisionId(b'r1', 'annotate:annotate-tree/file1:2')
5365.6.6 by Aaron Bentley
Implement 'annotate' revision-id.
704
705
    def test_as_revision_id_r2(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
706
        self.assertAsRevisionId(b'r2', 'annotate:annotate-tree/file1:1')
5365.6.6 by Aaron Bentley
Implement 'annotate' revision-id.
707
708
    def test_as_revision_id_uncommitted(self):
709
        spec = RevisionSpec.from_string('annotate:annotate-tree/file1:3')
710
        e = self.assertRaises(errors.InvalidRevisionSpec,
711
                              spec.as_revision_id, self.tree.branch)
712
        self.assertContainsRe(str(e),
713
            r"Requested revision: \'annotate:annotate-tree/file1:3\' does not"
714
            " exist in branch: .*\nLine 3 has not been committed.")
715
5365.6.10 by Andrew Bennetts
Fix typo in test method name.
716
    def test_non_existent_line(self):
5365.6.6 by Aaron Bentley
Implement 'annotate' revision-id.
717
        spec = RevisionSpec.from_string('annotate:annotate-tree/file1:4')
718
        e = self.assertRaises(errors.InvalidRevisionSpec,
719
                              spec.as_revision_id, self.tree.branch)
720
        self.assertContainsRe(str(e),
721
            r"Requested revision: \'annotate:annotate-tree/file1:4\' does not"
722
            " exist in branch: .*\nNo such line: 4")
723
724
    def test_invalid_line(self):
725
        spec = RevisionSpec.from_string('annotate:annotate-tree/file1:q')
726
        e = self.assertRaises(errors.InvalidRevisionSpec,
727
                              spec.as_revision_id, self.tree.branch)
728
        self.assertContainsRe(str(e),
729
            r"Requested revision: \'annotate:annotate-tree/file1:q\' does not"
730
            " exist in branch: .*\nNo such line: q")
731
732
    def test_no_such_file(self):
733
        spec = RevisionSpec.from_string('annotate:annotate-tree/file2:1')
734
        e = self.assertRaises(errors.InvalidRevisionSpec,
735
                              spec.as_revision_id, self.tree.branch)
736
        self.assertContainsRe(str(e),
737
            r"Requested revision: \'annotate:annotate-tree/file2:1\' does not"
738
            " exist in branch: .*\nFile 'file2' is not versioned")
739
740
    def test_no_such_file_with_colon(self):
741
        spec = RevisionSpec.from_string('annotate:annotate-tree/fi:le2:1')
742
        e = self.assertRaises(errors.InvalidRevisionSpec,
743
                              spec.as_revision_id, self.tree.branch)
744
        self.assertContainsRe(str(e),
745
            r"Requested revision: \'annotate:annotate-tree/fi:le2:1\' does not"
746
            " exist in branch: .*\nFile 'fi:le2' is not versioned")